refactor github auth to be a bit simpler. Remove PAT for GitHub auth since Settings Sync doesn't allow for it and add timeout so that GitHub Auth is not stuck.

This commit is contained in:
Tyler Leonhardt
2021-08-16 16:34:13 -07:00
parent 4ea225a1f0
commit 520fa49e68
7 changed files with 295 additions and 186 deletions

View File

@@ -7,8 +7,8 @@
// how we load it
import type * as keytarType from 'keytar';
import * as vscode from 'vscode';
import Logger from './logger';
import * as nls from 'vscode-nls';
import { Log } from './logger';
const localize = nls.loadMessageBundle();
@@ -29,13 +29,18 @@ export type Keytar = {
};
export class Keychain {
constructor(private context: vscode.ExtensionContext, private serviceId: string) { }
constructor(
private readonly context: vscode.ExtensionContext,
private readonly serviceId: string,
private readonly Logger: Log
) { }
async setToken(token: string): Promise<void> {
try {
return await this.context.secrets.store(this.serviceId, token);
} catch (e) {
// Ignore
Logger.error(`Setting token failed: ${e}`);
this.Logger.error(`Setting token failed: ${e}`);
const troubleshooting = localize('troubleshooting', "Troubleshooting Guide");
const result = await vscode.window.showErrorMessage(localize('keychainWriteError', "Writing login information to the keychain failed with error '{0}'.", e.message), troubleshooting);
if (result === troubleshooting) {
@@ -48,12 +53,12 @@ export class Keychain {
try {
const secret = await this.context.secrets.get(this.serviceId);
if (secret && secret !== '[]') {
Logger.trace('Token acquired from secret storage.');
this.Logger.trace('Token acquired from secret storage.');
}
return secret;
} catch (e) {
// Ignore
Logger.error(`Getting token failed: ${e}`);
this.Logger.error(`Getting token failed: ${e}`);
return Promise.resolve(undefined);
}
}
@@ -63,7 +68,7 @@ export class Keychain {
return await this.context.secrets.delete(this.serviceId);
} catch (e) {
// Ignore
Logger.error(`Deleting token failed: ${e}`);
this.Logger.error(`Deleting token failed: ${e}`);
return Promise.resolve(undefined);
}
}
@@ -77,7 +82,7 @@ export class Keychain {
const oldValue = await keytar.getPassword(`${vscode.env.uriScheme}-github.login`, 'account');
if (oldValue) {
Logger.trace('Attempting to migrate from keytar to secret store...');
this.Logger.trace('Attempting to migrate from keytar to secret store...');
await this.setToken(oldValue);
await keytar.deletePassword(`${vscode.env.uriScheme}-github.login`, 'account');
}