Verify GitHub tokens on auth provider start, fixes #108680

This commit is contained in:
Rachel Macfarlane
2020-10-26 16:01:33 -07:00
parent 1c7154d00c
commit 928e79f838
2 changed files with 35 additions and 11 deletions

View File

@@ -29,6 +29,7 @@ export class GitHubAuthenticationProvider {
public async initialize(context: vscode.ExtensionContext): Promise<void> {
try {
this._sessions = await this.readSessions();
await this.verifySessions();
} catch (e) {
// Ignore, network request failed
}
@@ -36,6 +37,28 @@ export class GitHubAuthenticationProvider {
context.subscriptions.push(vscode.authentication.onDidChangePassword(() => this.checkForUpdates()));
}
private async verifySessions(): Promise<void> {
const verifiedSessions: vscode.AuthenticationSession[] = [];
const verificationPromises = this._sessions.map(async session => {
try {
await this._githubServer.getUserInfo(session.accessToken);
verifiedSessions.push(session);
} catch (e) {
// Remove sessions that return unauthorized response
if (e.message !== 'Unauthorized') {
verifiedSessions.push(session);
}
}
});
Promise.all(verificationPromises).then(_ => {
if (this._sessions.length !== verifiedSessions.length) {
this._sessions = verifiedSessions;
this.storeSessions();
}
});
}
private async checkForUpdates() {
let storedSessions: vscode.AuthenticationSession[];
try {