Updates to authentication provider API

This commit is contained in:
Rachel Macfarlane
2020-01-15 15:45:04 -08:00
parent 2dcd10f914
commit b80e813365
11 changed files with 174 additions and 128 deletions

View File

@@ -23,7 +23,7 @@ interface IToken {
refreshToken: string;
}
export const onDidChangeAccounts = new vscode.EventEmitter<vscode.Account[]>();
export const onDidChangeSessions = new vscode.EventEmitter<void>();
export class AzureActiveDirectoryService {
private _token: IToken | undefined;
@@ -44,20 +44,20 @@ export class AzureActiveDirectoryService {
// Another window has logged in, generate access token for this instance.
if (refreshToken && !this._token) {
await this.refreshToken(refreshToken);
onDidChangeAccounts.fire(this.accounts);
onDidChangeSessions.fire();
}
// Another window has logged out
if (!refreshToken && this._token) {
await this.logout();
onDidChangeAccounts.fire(this.accounts);
onDidChangeSessions.fire();
}
this.pollForChange();
}, 1000 * 30);
}
private tokenToAccount(token: IToken): vscode.Account {
private tokenToAccount(token: IToken): vscode.Session {
return {
id: '',
accessToken: token.accessToken,
@@ -77,7 +77,7 @@ export class AzureActiveDirectoryService {
return displayName;
}
get accounts(): vscode.Account[] {
get sessions(): vscode.Session[] {
return this._token ? [this.tokenToAccount(this._token)] : [];
}
@@ -146,7 +146,7 @@ export class AzureActiveDirectoryService {
} catch (e) {
await this.logout();
} finally {
onDidChangeAccounts.fire(this.accounts);
onDidChangeSessions.fire();
}
}, 1000 * (parseInt(token.expiresIn) - 10));

View File

@@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { AzureActiveDirectoryService, onDidChangeAccounts } from './AADHelper';
import { AzureActiveDirectoryService, onDidChangeSessions } from './AADHelper';
export async function activate(context: vscode.ExtensionContext) {
@@ -15,12 +15,12 @@ export async function activate(context: vscode.ExtensionContext) {
vscode.authentication.registerAuthenticationProvider({
id: 'MSA',
displayName: 'Microsoft Account', // TODO localize
onDidChangeAccounts: onDidChangeAccounts.event,
getAccounts: () => Promise.resolve(loginService.accounts),
onDidChangeSessions: onDidChangeSessions.event,
getSessions: () => Promise.resolve(loginService.sessions),
login: async () => {
try {
await loginService.login();
return loginService.accounts[0]!;
return loginService.sessions[0]!;
} catch (e) {
vscode.window.showErrorMessage(`Logging in failed: ${e}`);
throw e;

View File

@@ -16,25 +16,45 @@
declare module 'vscode' {
export interface Account {
readonly id: string;
readonly accessToken: string;
readonly displayName: string;
export interface Session {
id: string;
accessToken: string;
displayName: string;
}
export interface AuthenticationProvider {
readonly id: string;
readonly displayName: string;
readonly onDidChangeSessions: Event<void>;
getAccounts(): Promise<ReadonlyArray<Account>>;
readonly onDidChangeAccounts: Event<ReadonlyArray<Account>>;
/**
* Returns an array of current sessions.
*/
getSessions(): Promise<ReadonlyArray<Session>>;
login(): Promise<Account>;
logout(accountId: string): Promise<void>;
/**
* Prompts a user to login.
*/
login(): Promise<Session>;
logout(sessionId: string): Promise<void>;
}
export namespace authentication {
export function registerAuthenticationProvider(provider: AuthenticationProvider): Disposable;
/**
* Fires with the provider id that was registered or unregistered.
*/
export const onDidRegisterAuthenticationProvider: Event<string>;
export const onDidUnregisterAuthenticationProvider: Event<string>;
/**
* Fires with the provider id that changed sessions.
*/
export const onDidChangeSessions: Event<string>;
export function login(providerId: string): Promise<Session>;
export function logout(providerId: string, accountId: string): Promise<void>;
export function getSessions(providerId: string): Promise<ReadonlyArray<Session> | undefined>;
}
// #region Ben - extension auth flow (desktop+web)