Move secrets API to extension context

This commit is contained in:
Rachel Macfarlane
2021-01-04 22:03:21 -08:00
parent a48ef56fbf
commit 7db413d4c1
17 changed files with 236 additions and 144 deletions

View File

@@ -10,7 +10,7 @@ import * as vscode from 'vscode';
import { createServer, startServer } from './authServer';
import { v4 as uuid } from 'uuid';
import { keychain } from './keychain';
import { Keychain } from './keychain';
import Logger from './logger';
import { toBase64UrlEncoding } from './utils';
import fetch, { Response } from 'node-fetch';
@@ -100,13 +100,16 @@ export class AzureActiveDirectoryService {
private _codeExchangePromises = new Map<string, Promise<vscode.AuthenticationSession>>();
private _codeVerfifiers = new Map<string, string>();
constructor() {
private _keychain: Keychain;
constructor(private _context: vscode.ExtensionContext) {
this._keychain = new Keychain(_context);
this._uriHandler = new UriEventHandler();
this._disposables.push(vscode.window.registerUriHandler(this._uriHandler));
}
public async initialize(): Promise<void> {
const storedData = await keychain.getToken() || await keychain.tryMigrate();
const storedData = await this._keychain.getToken() || await this._keychain.tryMigrate();
if (storedData) {
try {
const sessions = this.parseStoredData(storedData);
@@ -146,7 +149,7 @@ export class AzureActiveDirectoryService {
}
}
this._disposables.push(vscode.authentication.onDidChangePassword(() => this.checkForUpdates));
this._disposables.push(this._context.secretState.onDidChangePassword(() => this.checkForUpdates));
}
private parseStoredData(data: string): IStoredSession[] {
@@ -163,13 +166,13 @@ export class AzureActiveDirectoryService {
};
});
await keychain.setToken(JSON.stringify(serializedData));
await this._keychain.setToken(JSON.stringify(serializedData));
}
private async checkForUpdates(): Promise<void> {
const addedIds: string[] = [];
let removedIds: string[] = [];
const storedData = await keychain.getToken();
const storedData = await this._keychain.getToken();
if (storedData) {
try {
const sessions = this.parseStoredData(storedData);
@@ -651,7 +654,7 @@ export class AzureActiveDirectoryService {
this.removeInMemorySessionData(sessionId);
if (this._tokens.length === 0) {
await keychain.deleteToken();
await this._keychain.deleteToken();
} else {
this.storeTokenData();
}
@@ -660,7 +663,7 @@ export class AzureActiveDirectoryService {
public async clearSessions() {
Logger.info('Logging out of all sessions');
this._tokens = [];
await keychain.deleteToken();
await this._keychain.deleteToken();
this._refreshTimeouts.forEach(timeout => {
clearTimeout(timeout);

View File

@@ -13,7 +13,7 @@ export async function activate(context: vscode.ExtensionContext) {
const { name, version, aiKey } = require('../package.json') as { name: string, version: string, aiKey: string };
const telemetryReporter = new TelemetryReporter(name, version, aiKey);
const loginService = new AzureActiveDirectoryService();
const loginService = new AzureActiveDirectoryService(context);
context.subscriptions.push(loginService);
await loginService.initialize();

View File

@@ -35,7 +35,7 @@ const ACCOUNT_ID = 'account';
export class Keychain {
private keytar: Keytar;
constructor() {
constructor(private context: vscode.ExtensionContext) {
const keytar = getKeytar();
if (!keytar) {
throw new Error('System keychain unavailable');
@@ -46,8 +46,9 @@ export class Keychain {
async setToken(token: string): Promise<void> {
try {
return await vscode.authentication.setPassword(SERVICE_ID, token);
return await this.context.secretState.set(SERVICE_ID, token);
} catch (e) {
Logger.error(`Setting token failed: ${e}`);
@@ -69,7 +70,7 @@ export class Keychain {
async getToken(): Promise<string | null | undefined> {
try {
return await vscode.authentication.getPassword(SERVICE_ID);
return await this.context.secretState.get(SERVICE_ID);
} catch (e) {
// Ignore
Logger.error(`Getting token failed: ${e}`);
@@ -79,7 +80,7 @@ export class Keychain {
async deleteToken(): Promise<void> {
try {
return await vscode.authentication.deletePassword(SERVICE_ID);
return await this.context.secretState.delete(SERVICE_ID);
} catch (e) {
// Ignore
Logger.error(`Deleting token failed: ${e}`);
@@ -102,5 +103,3 @@ export class Keychain {
}
}
}
export const keychain = new Keychain();