Skip to content

Commit

Permalink
displayName -> label, use providerIds in favor of getProviderIds, #10…
Browse files Browse the repository at this point in the history
  • Loading branch information
Rachel Macfarlane committed Jul 7, 2020
1 parent 1728c5b commit 348e2b4
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 36 deletions.
9 changes: 6 additions & 3 deletions extensions/github-authentication/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const onDidChangeSessions = new vscode.EventEmitter<vscode.Authentication
interface SessionData {
id: string;
account?: {
displayName: string;
label?: string;
displayName?: string;
id: string;
}
scopes: string[];
Expand Down Expand Up @@ -95,7 +96,9 @@ export class GitHubAuthenticationProvider {
return {
id: session.id,
account: {
displayName: session.account?.displayName ?? userInfo!.accountName,
label: session.account
? session.account.label || session.account.displayName!
: userInfo!.accountName,
id: session.account?.id ?? userInfo!.id
},
scopes: session.scopes,
Expand Down Expand Up @@ -138,7 +141,7 @@ export class GitHubAuthenticationProvider {

private async tokenToSession(token: string, scopes: string[]): Promise<vscode.AuthenticationSession> {
const userInfo = await this._githubServer.getUserInfo(token);
return new vscode.AuthenticationSession(uuid(), token, { displayName: userInfo.accountName, id: userInfo.id }, scopes);
return new vscode.AuthenticationSession(uuid(), token, { label: userInfo.accountName, id: userInfo.id }, scopes);
}

private async setToken(session: vscode.AuthenticationSession): Promise<void> {
Expand Down
9 changes: 5 additions & 4 deletions extensions/microsoft-authentication/src/AADHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface IToken {
refreshToken: string;

account: {
displayName: string;
label: string;
id: string;
};
scope: string;
Expand All @@ -48,7 +48,8 @@ interface IStoredSession {
refreshToken: string;
scope: string; // Scopes are alphabetized and joined with a space
account: {
displayName: string,
label?: string;
displayName?: string,
id: string
}
}
Expand Down Expand Up @@ -101,7 +102,7 @@ export class AzureActiveDirectoryService {
accessToken: undefined,
refreshToken: session.refreshToken,
account: {
displayName: session.account.displayName,
label: session.account.label ?? session.account.displayName!,
id: session.account.id
},
scope: session.scope,
Expand Down Expand Up @@ -437,7 +438,7 @@ export class AzureActiveDirectoryService {
scope,
sessionId: existingId || `${claims.tid}/${(claims.oid || (claims.altsecid || '' + claims.ipd || ''))}/${uuid()}`,
account: {
displayName: claims.email || claims.unique_name || 'user@example.com',
label: claims.email || claims.unique_name || 'user@example.com',
id: `${claims.tid}/${(claims.oid || (claims.altsecid || '' + claims.ipd || ''))}`
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/common/modes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ export interface AuthenticationSession {
id: string;
accessToken: string;
account: {
displayName: string;
label: string;
id: string;
}
scopes: ReadonlyArray<string>;
Expand Down
4 changes: 2 additions & 2 deletions src/vs/vscode.proposed.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ declare module 'vscode' {
/**
* The human-readable name of the account.
*/
readonly displayName: string;
readonly label: string;

/**
* The unique identifier of the account.
Expand Down Expand Up @@ -175,13 +175,13 @@ declare module 'vscode' {
export const onDidChangeAuthenticationProviders: Event<AuthenticationProvidersChangeEvent>;

/**
* @deprecated
* The ids of the currently registered authentication providers.
* @returns An array of the ids of authentication providers that are currently registered.
*/
export function getProviderIds(): Thenable<ReadonlyArray<string>>;

/**
* @deprecated
* An array of the ids of authentication providers that are currently registered.
*/
export const providerIds: ReadonlyArray<string>;
Expand Down
20 changes: 10 additions & 10 deletions src/vs/workbench/api/browser/mainThreadAuthentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,17 @@ export class MainThreadAuthenticationProvider extends Disposable {
}

private registerSession(session: modes.AuthenticationSession) {
this._sessions.set(session.id, session.account.displayName);
this._sessions.set(session.id, session.account.label);

const existingSessionsForAccount = this._accounts.get(session.account.displayName);
const existingSessionsForAccount = this._accounts.get(session.account.label);
if (existingSessionsForAccount) {
this._accounts.set(session.account.displayName, existingSessionsForAccount.concat(session.id));
this._accounts.set(session.account.label, existingSessionsForAccount.concat(session.id));
return;
} else {
this._accounts.set(session.account.displayName, [session.id]);
this._accounts.set(session.account.label, [session.id]);
}

this.storageKeysSyncRegistryService.registerStorageKey({ key: `${this.id}-${session.account.displayName}`, version: 1 });
this.storageKeysSyncRegistryService.registerStorageKey({ key: `${this.id}-${session.account.label}`, version: 1 });
}

async signOut(accountName: string): Promise<void> {
Expand Down Expand Up @@ -273,7 +273,7 @@ export class MainThreadAuthentication extends Disposable implements MainThreadAu
if (sessions.length) {
if (!this.authenticationService.supportsMultipleAccounts(providerId)) {
const session = sessions[0];
const allowed = await this.$getSessionsPrompt(providerId, session.account.displayName, label, extensionId, extensionName);
const allowed = await this.$getSessionsPrompt(providerId, session.account.label, label, extensionId, extensionName);
if (allowed) {
return session;
} else {
Expand All @@ -292,7 +292,7 @@ export class MainThreadAuthentication extends Disposable implements MainThreadAu
}

const session = await this.authenticationService.login(providerId, scopes);
await this.$setTrustedExtension(providerId, session.account.displayName, extensionId, extensionName);
await this.$setTrustedExtension(providerId, session.account.label, extensionId, extensionName);
return session;
} else {
await this.$requestNewSession(providerId, scopes, extensionId, extensionName);
Expand All @@ -313,7 +313,7 @@ export class MainThreadAuthentication extends Disposable implements MainThreadAu
if (existingSessionPreference) {
const matchingSession = potentialSessions.find(session => session.id === existingSessionPreference);
if (matchingSession) {
const allowed = await this.$getSessionsPrompt(providerId, matchingSession.account.displayName, providerName, extensionId, extensionName);
const allowed = await this.$getSessionsPrompt(providerId, matchingSession.account.label, providerName, extensionId, extensionName);
if (allowed) {
return matchingSession;
}
Expand All @@ -326,7 +326,7 @@ export class MainThreadAuthentication extends Disposable implements MainThreadAu
quickPick.ignoreFocusOut = true;
const items: { label: string, session?: modes.AuthenticationSession }[] = potentialSessions.map(session => {
return {
label: session.account.displayName,
label: session.account.label,
session
};
});
Expand All @@ -351,7 +351,7 @@ export class MainThreadAuthentication extends Disposable implements MainThreadAu

const session = selected.session ?? await this.authenticationService.login(providerId, scopes);

const accountName = session.account.displayName;
const accountName = session.account.label;

const allowList = readAllowedExtensions(this.storageService, providerId, accountName);
if (!allowList.find(allowed => allowed.id === extensionId)) {
Expand Down
33 changes: 25 additions & 8 deletions src/vs/workbench/api/common/extHostAuthentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
private _proxy: MainThreadAuthenticationShape;
private _authenticationProviders: Map<string, vscode.AuthenticationProvider> = new Map<string, vscode.AuthenticationProvider>();

private _providerIds: string[] = [];

private _onDidChangeAuthenticationProviders = new Emitter<vscode.AuthenticationProvidersChangeEvent>();
readonly onDidChangeAuthenticationProviders: Event<vscode.AuthenticationProvidersChangeEvent> = this._onDidChangeAuthenticationProviders.event;

Expand All @@ -29,12 +31,7 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
}

get providerIds(): string[] {
const ids: string[] = [];
this._authenticationProviders.forEach(provider => {
ids.push(provider.id);
});

return ids;
return this._providerIds;
}

private async resolveSessions(providerId: string): Promise<ReadonlyArray<modes.AuthenticationSession>> {
Expand Down Expand Up @@ -72,7 +69,7 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
if (sessions.length) {
if (!provider.supportsMultipleAccounts) {
const session = sessions[0];
const allowed = await this._proxy.$getSessionsPrompt(providerId, session.account.displayName, provider.label, extensionId, extensionName);
const allowed = await this._proxy.$getSessionsPrompt(providerId, session.account.label, provider.label, extensionId, extensionName);
if (allowed) {
return session;
} else {
Expand All @@ -91,7 +88,7 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
}

const session = await provider.login(scopes);
await this._proxy.$setTrustedExtension(providerId, session.account.displayName, extensionId, extensionName);
await this._proxy.$setTrustedExtension(providerId, session.account.label, extensionId, extensionName);
return session;
} else {
await this._proxy.$requestNewSession(providerId, scopes, extensionId, extensionName);
Expand All @@ -115,6 +112,9 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
}

this._authenticationProviders.set(provider.id, provider);
if (!this._providerIds.includes(provider.id)) {
this._providerIds.push(provider.id);
}

const listener = provider.onDidChangeSessions(e => {
this._proxy.$sendDidChangeSessions(provider.id, e);
Expand All @@ -125,6 +125,10 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
return new Disposable(() => {
listener.dispose();
this._authenticationProviders.delete(provider.id);
const index = this._providerIds.findIndex(id => id === provider.id);
if (index > -1) {
this._providerIds.splice(index);
}
this._proxy.$unregisterAuthenticationProvider(provider.id);
});
}
Expand Down Expand Up @@ -177,6 +181,19 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
}

$onDidChangeAuthenticationProviders(added: string[], removed: string[]) {
added.forEach(id => {
if (!this._providerIds.includes(id)) {
this._providerIds.push(id);
}
});

removed.forEach(id => {
const index = this._providerIds.findIndex(provider => provider === id);
if (index > -1) {
this._providerIds.splice(index);
}
});

this._onDidChangeAuthenticationProviders.fire({ added, removed });
return Promise.resolve();
}
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/common/extHostTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2770,7 +2770,7 @@ export enum ExtensionMode {

//#region Authentication
export class AuthenticationSession implements vscode.AuthenticationSession {
constructor(public id: string, public accessToken: string, public account: { displayName: string, id: string }, public scopes: string[]) { }
constructor(public id: string, public accessToken: string, public account: { label: string, id: string }, public scopes: string[]) { }
}

//#endregion Authentication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class AccountsActionViewItem extends ActivityActionViewItem {
const providers = this.authenticationService.getProviderIds();
const allSessions = providers.map(async id => {
const sessions = await this.authenticationService.getSessions(id);
const uniqueSessions = distinct(sessions, session => session.account.displayName);
const uniqueSessions = distinct(sessions, session => session.account.label);
return {
providerId: id,
sessions: uniqueSessions
Expand All @@ -153,7 +153,7 @@ export class AccountsActionViewItem extends ActivityActionViewItem {
result.forEach(sessionInfo => {
const providerDisplayName = this.authenticationService.getLabel(sessionInfo.providerId);
sessionInfo.sessions.forEach(session => {
const accountName = session.account.displayName;
const accountName = session.account.label;
const menu = new ContextSubMenu(`${accountName} (${providerDisplayName})`, [
new Action(`configureSessions${accountName}`, nls.localize('manageTrustedExtensions', "Manage Trusted Extensions"), '', true, _ => {
return this.authenticationService.manageTrustedExtensionsForAccount(sessionInfo.providerId, accountName);
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/contrib/url/browser/trustedDomains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export async function readTrustedDomains(accessor: ServicesAccessor) {
const userDomains =
authenticationService.isAuthenticationProviderRegistered('github')
? ((await authenticationService.getSessions('github')) ?? [])
.map(session => session.account.displayName)
.map(session => session.account.label)
.filter((v, i, a) => a.indexOf(v) === i)
.map(username => `https://github.com/${username}/`)
: [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ export class AuthenticationService extends Disposable implements IAuthentication
const session = await authenticationService.login(providerId, scopes);

// Add extension to allow list since user explicitly signed in on behalf of it
const allowList = readAllowedExtensions(storageService, providerId, session.account.displayName);
const allowList = readAllowedExtensions(storageService, providerId, session.account.label);
if (!allowList.find(allowed => allowed.id === extensionId)) {
allowList.push({ id: extensionId, name: extensionName });
storageService.store(`${providerId}-${session.account.displayName}`, JSON.stringify(allowList), StorageScope.GLOBAL);
storageService.store(`${providerId}-${session.account.label}`, JSON.stringify(allowList), StorageScope.GLOBAL);
}

// And also set it as the preferred account for the extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class UserDataSyncAccount implements IUserDataSyncAccount {
constructor(readonly authenticationProviderId: string, private readonly session: AuthenticationSession) { }

get sessionId(): string { return this.session.id; }
get accountName(): string { return this.session.account.displayName; }
get accountName(): string { return this.session.account.label; }
get accountId(): string { return this.session.account.id; }
get token(): string { return this.session.accessToken; }
}
Expand Down Expand Up @@ -305,7 +305,7 @@ export class UserDataSyncWorkbenchService extends Disposable implements IUserDat
if (isAuthenticationProvider(result)) {
const session = await this.authenticationService.login(result.id, result.scopes);
sessionId = session.id;
accountName = session.account.displayName;
accountName = session.account.label;
accountId = session.account.id;
} else {
sessionId = result.sessionId;
Expand Down

0 comments on commit 348e2b4

Please sign in to comment.