Skip to content

Commit 8eb9cda

Browse files
author
Rachel Macfarlane
committed
Fix microsoft#105955, make sure auth provider registration completes when getSession is called
1 parent 355fbca commit 8eb9cda

2 files changed

Lines changed: 35 additions & 13 deletions

File tree

extensions/github-authentication/src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export async function activate(context: vscode.ExtensionContext) {
2222
return loginService.manuallyProvideToken();
2323
}));
2424

25-
vscode.authentication.registerAuthenticationProvider({
25+
context.subscriptions.push(vscode.authentication.registerAuthenticationProvider({
2626
id: 'github',
2727
label: 'GitHub',
2828
supportsMultipleAccounts: false,
@@ -70,7 +70,7 @@ export async function activate(context: vscode.ExtensionContext) {
7070
throw e;
7171
}
7272
}
73-
});
73+
}));
7474

7575
return;
7676
}

src/vs/workbench/services/authentication/browser/authenticationService.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -432,24 +432,46 @@ export class AuthenticationService extends Disposable implements IAuthentication
432432
}
433433
}
434434

435-
async getSessions(id: string): Promise<ReadonlyArray<AuthenticationSession>> {
436-
await this.extensionService.activateByEvent(getAuthenticationProviderActivationEvent(id));
435+
private async tryActivateProvider(providerId: string): Promise<MainThreadAuthenticationProvider> {
436+
await this.extensionService.activateByEvent(getAuthenticationProviderActivationEvent(providerId));
437+
let provider = this._authenticationProviders.get(providerId);
438+
if (provider) {
439+
return provider;
440+
}
437441

438-
const authProvider = this._authenticationProviders.get(id);
439-
if (authProvider) {
442+
// When activate has completed, the extension has made the call to `registerAuthenticationProvider`.
443+
// However, activate cannot block on this, so the renderer may not have gotten the event yet.
444+
const didRegister: Promise<MainThreadAuthenticationProvider> = new Promise((resolve, _) => {
445+
this.onDidRegisterAuthenticationProvider(e => {
446+
if (e.id === providerId) {
447+
resolve(this._authenticationProviders.get(providerId));
448+
}
449+
});
450+
});
451+
452+
const didTimeout: Promise<MainThreadAuthenticationProvider> = new Promise((_, reject) => {
453+
setTimeout(() => {
454+
reject();
455+
}, 2000);
456+
});
457+
458+
return Promise.race([didRegister, didTimeout]);
459+
}
460+
461+
async getSessions(id: string): Promise<ReadonlyArray<AuthenticationSession>> {
462+
try {
463+
const authProvider = this._authenticationProviders.get(id) || await this.tryActivateProvider(id);
440464
return await authProvider.getSessions();
441-
} else {
465+
} catch (_) {
442466
throw new Error(`No authentication provider '${id}' is currently registered.`);
443467
}
444468
}
445469

446470
async login(id: string, scopes: string[]): Promise<AuthenticationSession> {
447-
await this.extensionService.activateByEvent(getAuthenticationProviderActivationEvent(id));
448-
449-
const authProvider = this._authenticationProviders.get(id);
450-
if (authProvider) {
451-
return authProvider.login(scopes);
452-
} else {
471+
try {
472+
const authProvider = this._authenticationProviders.get(id) || await this.tryActivateProvider(id);
473+
return await authProvider.login(scopes);
474+
} catch (_) {
453475
throw new Error(`No authentication provider '${id}' is currently registered.`);
454476
}
455477
}

0 commit comments

Comments
 (0)