|
5 | 5 | */
|
6 | 6 |
|
7 | 7 | import { MigrationInterface, QueryRunner } from "typeorm";
|
| 8 | +import * as crypto from "crypto"; |
| 9 | +import { v4 as uuidv4 } from 'uuid'; |
8 | 10 |
|
9 | 11 | import { BUILTIN_WORKSPACE_PROBE_USER_ID } from "../../user-db";
|
10 | 12 |
|
| 13 | +// From encryption-key.json |
| 14 | +const encryption_key = "4uGh1q8y2DYryJwrVMHs0kWXJlqvHWWt/KJuNi04edI="; |
| 15 | + |
| 16 | +function encrypt(data: string, key: Buffer) { |
| 17 | + const iv = crypto.randomBytes(16); |
| 18 | + const cipher = crypto.createCipheriv('aes-256-cbc', key, iv); |
| 19 | + const encrypted = cipher.update(new Buffer(data, 'utf8')); |
| 20 | + const finalEncrypted = Buffer.concat([encrypted, cipher.final()]); |
| 21 | + return { |
| 22 | + data: finalEncrypted.toString('base64'), |
| 23 | + keyParams: { |
| 24 | + iv: iv.toString('base64') |
| 25 | + } |
| 26 | + }; |
| 27 | +} |
| 28 | + |
11 | 29 | export class Baseline1592203031938 implements MigrationInterface {
|
12 | 30 |
|
13 | 31 | public async up(queryRunner: QueryRunner): Promise<any> {
|
@@ -87,6 +105,24 @@ export class Baseline1592203031938 implements MigrationInterface {
|
87 | 105 | if (!existsIdentity) {
|
88 | 106 | await queryRunner.query(`INSERT IGNORE INTO d_b_identity (authProviderId, authId, authName, userId, tokens) VALUES ('Public-GitHub', '12345678', 'builtin-workspace-prober', 'builtin-user-workspace-probe-0000000', '[]')`)
|
89 | 107 | }
|
| 108 | + const existsToken = (await queryRunner.query(`SELECT COUNT(1) AS cnt FROM d_b_token_entry WHERE authId = '12345678'`))[0].cnt == 1; |
| 109 | + if (!existsToken) { |
| 110 | + const encriptedData = encrypt( |
| 111 | + JSON.stringify(JSON.stringify({ |
| 112 | + value: '', |
| 113 | + scopes: [] |
| 114 | + })), |
| 115 | + new Buffer(encryption_key, 'base64') |
| 116 | + ); |
| 117 | + const token = JSON.stringify({ |
| 118 | + ...encriptedData, |
| 119 | + keyMetadata: { |
| 120 | + name: "general", |
| 121 | + version: 1 |
| 122 | + } |
| 123 | + }); |
| 124 | + await queryRunner.query(`INSERT IGNORE INTO d_b_token_entry (authProviderId, authId, token, uid) VALUES ('Public-GitHub', '12345678', '${token}', '${uuidv4()}')`) |
| 125 | + } |
90 | 126 | }
|
91 | 127 | }
|
92 | 128 |
|
|
0 commit comments