Allow users to use file based keychain if they have to (#9952)

This commit is contained in:
Amir Omidi
2020-04-14 14:33:24 -07:00
committed by GitHub
parent 4e69eabf52
commit fac2982d7a

View File

@@ -3,9 +3,10 @@
* Licensed under the Source EULA. See License.txt in the project root for license information. * Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import * as keytarType from 'keytar'; import * as keytarType from 'keytar';
import { join } from 'path'; import { join, parse } from 'path';
import { FileDatabase } from './utils/fileDatabase'; import { FileDatabase } from './utils/fileDatabase';
import * as azdata from 'azdata'; import * as azdata from 'azdata';
import * as crypto from 'crypto';
function getSystemKeytar(): Keytar | undefined | null { function getSystemKeytar(): Keytar | undefined | null {
try { try {
@@ -22,45 +23,43 @@ export type MultipleAccountsResponse = { account: string, password: string }[];
const separator = '§'; const separator = '§';
async function getFileKeytar(filePath: string, credentialService: azdata.CredentialProvider): Promise<Keytar | undefined> { async function getFileKeytar(filePath: string, credentialService: azdata.CredentialProvider): Promise<Keytar | undefined> {
// Comment alias: amomidi, PR: 9743 March 26th 2020 const fileName = parse(filePath).base;
// const fileName = parse(filePath).base; const iv = await credentialService.readCredential(`${fileName}-iv`);
// const iv = await credentialService.readCredential(`${fileName}-iv`); const key = await credentialService.readCredential(`${fileName}-key`);
// const key = await credentialService.readCredential(`${fileName}-key`); let ivBuffer: Buffer;
// let ivBuffer: Buffer; let keyBuffer: Buffer;
// let keyBuffer: Buffer; if (!iv?.password || !key?.password) {
// if (!iv?.password || !key?.password) { ivBuffer = crypto.randomBytes(16);
// ivBuffer = crypto.randomBytes(16); keyBuffer = crypto.randomBytes(32);
// keyBuffer = crypto.randomBytes(32); try {
// try { await credentialService.saveCredential(`${fileName}-iv`, ivBuffer.toString('hex'));
// await credentialService.saveCredential(`${fileName}-iv`, ivBuffer.toString('hex')); await credentialService.saveCredential(`${fileName}-key`, keyBuffer.toString('hex'));
// await credentialService.saveCredential(`${fileName}-key`, keyBuffer.toString('hex')); } catch (ex) {
// } catch (ex) { console.log(ex);
// console.log(ex); }
// } } else {
// } else { ivBuffer = Buffer.from(iv.password, 'hex');
// ivBuffer = Buffer.from(iv.password, 'hex'); keyBuffer = Buffer.from(key.password, 'hex');
// keyBuffer = Buffer.from(key.password, 'hex'); }
// }
// const fileSaver = async (content: string): Promise<string> => { const fileSaver = async (content: string): Promise<string> => {
// const cipherIv = crypto.createCipheriv('aes-256-gcm', keyBuffer, ivBuffer); const cipherIv = crypto.createCipheriv('aes-256-gcm', keyBuffer, ivBuffer);
// return `${cipherIv.update(content, 'utf8', 'hex')}${cipherIv.final('hex')}%${cipherIv.getAuthTag().toString('hex')}`; return `${cipherIv.update(content, 'utf8', 'hex')}${cipherIv.final('hex')}%${cipherIv.getAuthTag().toString('hex')}`;
// }; };
// const fileOpener = async (content: string): Promise<string> => { const fileOpener = async (content: string): Promise<string> => {
// const decipherIv = crypto.createDecipheriv('aes-256-gcm', keyBuffer, ivBuffer); const decipherIv = crypto.createDecipheriv('aes-256-gcm', keyBuffer, ivBuffer);
// const split = content.split('%'); const split = content.split('%');
// if (split.length !== 2) { if (split.length !== 2) {
// throw new Error('File didn\'t contain the auth tag.'); throw new Error('File didn\'t contain the auth tag.');
// } }
// decipherIv.setAuthTag(Buffer.from(split[1], 'hex')); decipherIv.setAuthTag(Buffer.from(split[1], 'hex'));
// return `${decipherIv.update(split[0], 'hex', 'utf8')}${decipherIv.final('utf8')}`; return `${decipherIv.update(split[0], 'hex', 'utf8')}${decipherIv.final('utf8')}`;
// }; };
// const db = new FileDatabase(filePath, fileOpener, fileSaver); const db = new FileDatabase(filePath, fileOpener, fileSaver);
const db = new FileDatabase(filePath);
await db.initialize(); await db.initialize();
const fileKeytar: Keytar = { const fileKeytar: Keytar = {