Skip to content

Commit 629e1d7

Browse files
author
Rachel Macfarlane
committed
Remove old getSessions and login methods from auth provider API
1 parent fcbc010 commit 629e1d7

9 files changed

Lines changed: 32 additions & 141 deletions

File tree

extensions/github-authentication/src/github.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface SessionData {
2222
}
2323

2424
export class GitHubAuthenticationProvider {
25-
private _sessions: vscode.AuthenticationSession2[] = [];
25+
private _sessions: vscode.AuthenticationSession[] = [];
2626
private _githubServer = new GitHubServer();
2727

2828
public async initialize(): Promise<void> {
@@ -37,7 +37,7 @@ export class GitHubAuthenticationProvider {
3737

3838
private pollForChange() {
3939
setTimeout(async () => {
40-
let storedSessions: vscode.AuthenticationSession2[];
40+
let storedSessions: vscode.AuthenticationSession[];
4141
try {
4242
storedSessions = await this.readSessions();
4343
} catch (e) {
@@ -80,12 +80,12 @@ export class GitHubAuthenticationProvider {
8080
}, 1000 * 30);
8181
}
8282

83-
private async readSessions(): Promise<vscode.AuthenticationSession2[]> {
83+
private async readSessions(): Promise<vscode.AuthenticationSession[]> {
8484
const storedSessions = await keychain.getToken();
8585
if (storedSessions) {
8686
try {
8787
const sessionData: SessionData[] = JSON.parse(storedSessions);
88-
const sessionPromises = sessionData.map(async (session: SessionData): Promise<vscode.AuthenticationSession2> => {
88+
const sessionPromises = sessionData.map(async (session: SessionData): Promise<vscode.AuthenticationSession> => {
8989
const needsUserInfo = !session.account;
9090
let userInfo: { id: string, accountName: string };
9191
if (needsUserInfo) {
@@ -121,11 +121,11 @@ export class GitHubAuthenticationProvider {
121121
await keychain.setToken(JSON.stringify(this._sessions));
122122
}
123123

124-
get sessions(): vscode.AuthenticationSession2[] {
124+
get sessions(): vscode.AuthenticationSession[] {
125125
return this._sessions;
126126
}
127127

128-
public async login(scopes: string): Promise<vscode.AuthenticationSession2> {
128+
public async login(scopes: string): Promise<vscode.AuthenticationSession> {
129129
const token = await this._githubServer.login(scopes);
130130
const session = await this.tokenToSession(token, scopes.split(' '));
131131
await this.setToken(session);
@@ -136,12 +136,12 @@ export class GitHubAuthenticationProvider {
136136
this._githubServer.manuallyProvideToken();
137137
}
138138

139-
private async tokenToSession(token: string, scopes: string[]): Promise<vscode.AuthenticationSession2> {
139+
private async tokenToSession(token: string, scopes: string[]): Promise<vscode.AuthenticationSession> {
140140
const userInfo = await this._githubServer.getUserInfo(token);
141-
return new vscode.AuthenticationSession2(uuid(), token, { displayName: userInfo.accountName, id: userInfo.id }, scopes);
141+
return new vscode.AuthenticationSession(uuid(), token, { displayName: userInfo.accountName, id: userInfo.id }, scopes);
142142
}
143143

144-
private async setToken(session: vscode.AuthenticationSession2): Promise<void> {
144+
private async setToken(session: vscode.AuthenticationSession): Promise<void> {
145145
const sessionIndex = this._sessions.findIndex(s => s.id === session.id);
146146
if (sessionIndex > -1) {
147147
this._sessions.splice(sessionIndex, 1, session);

extensions/github-browser/src/githubfs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'use strict';
77
import {
88
authentication,
9-
AuthenticationSession2,
9+
AuthenticationSession,
1010
CancellationToken,
1111
Disposable,
1212
Event,
@@ -336,7 +336,7 @@ interface SearchQueryResults {
336336
}
337337

338338
class GitHubApi {
339-
constructor(private readonly session: AuthenticationSession2) { }
339+
constructor(private readonly session: AuthenticationSession) { }
340340

341341
private _graphql: typeof graphql | undefined;
342342
private get graphql() {

extensions/github/src/auth.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,15 @@ function getAgent(url: string | undefined = process.env.HTTPS_PROXY): Agent {
2727
const scopes = ['repo', 'workflow'];
2828

2929
export async function getSession(): Promise<AuthenticationSession> {
30-
const authenticationSessions = await authentication.getSessions('github', scopes);
31-
32-
if (authenticationSessions.length) {
33-
return await authenticationSessions[0];
34-
} else {
35-
return await authentication.login('github', scopes);
36-
}
30+
return await authentication.getSession('github', scopes, { createIfNone: true });
3731
}
3832

3933
let _octokit: Promise<Octokit> | undefined;
4034

4135
export function getOctokit(): Promise<Octokit> {
4236
if (!_octokit) {
43-
_octokit = getSession().then(async session => {
44-
const token = await session.getAccessToken();
37+
_octokit = getSession().then(session => {
38+
const token = session.accessToken;
4539
const agent = getAgent();
4640

4741
return new Octokit({

extensions/github/src/credentialProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class GitHubCredentialProvider implements CredentialsProvider {
1717
}
1818

1919
const session = await getSession();
20-
return { username: session.account.id, password: await session.getAccessToken() };
20+
return { username: session.account.id, password: session.accessToken };
2121
}
2222
}
2323

extensions/microsoft-authentication/src/AADHelper.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ export class AzureActiveDirectoryService {
205205
}, 1000 * 30);
206206
}
207207

208-
private async convertToSession(token: IToken): Promise<vscode.AuthenticationSession2> {
208+
private async convertToSession(token: IToken): Promise<vscode.AuthenticationSession> {
209209
const resolvedToken = await this.resolveAccessToken(token);
210-
return new vscode.AuthenticationSession2(token.sessionId, resolvedToken, token.account, token.scope.split(' '));
210+
return new vscode.AuthenticationSession(token.sessionId, resolvedToken, token.account, token.scope.split(' '));
211211
}
212212

213213
private async resolveAccessToken(token: IToken): Promise<string> {
@@ -240,11 +240,11 @@ export class AzureActiveDirectoryService {
240240
}
241241
}
242242

243-
get sessions(): Promise<vscode.AuthenticationSession2[]> {
243+
get sessions(): Promise<vscode.AuthenticationSession[]> {
244244
return Promise.all(this._tokens.map(token => this.convertToSession(token)));
245245
}
246246

247-
public async login(scope: string): Promise<vscode.AuthenticationSession2> {
247+
public async login(scope: string): Promise<vscode.AuthenticationSession> {
248248
Logger.info('Logging in...');
249249
if (!scope.includes('offline_access')) {
250250
Logger.info('Warning: The \'offline_access\' scope was not included, so the generated token will not be able to be refreshed.');
@@ -338,7 +338,7 @@ export class AzureActiveDirectoryService {
338338
}
339339
}
340340

341-
private async loginWithoutLocalServer(scope: string): Promise<vscode.AuthenticationSession2> {
341+
private async loginWithoutLocalServer(scope: string): Promise<vscode.AuthenticationSession> {
342342
const callbackUri = await vscode.env.asExternalUri(vscode.Uri.parse(`${vscode.env.uriScheme}://vscode.microsoft-authentication`));
343343
const nonce = crypto.randomBytes(16).toString('base64');
344344
const port = (callbackUri.authority.match(/:([0-9]*)$/) || [])[1] || (callbackUri.scheme === 'https' ? 443 : 80);
@@ -353,7 +353,7 @@ export class AzureActiveDirectoryService {
353353
});
354354
vscode.env.openExternal(uri);
355355

356-
const timeoutPromise = new Promise((_: (value: vscode.AuthenticationSession2) => void, reject) => {
356+
const timeoutPromise = new Promise((_: (value: vscode.AuthenticationSession) => void, reject) => {
357357
const wait = setTimeout(() => {
358358
clearTimeout(wait);
359359
reject('Login timed out.');
@@ -363,9 +363,9 @@ export class AzureActiveDirectoryService {
363363
return Promise.race([this.handleCodeResponse(state, codeVerifier, scope), timeoutPromise]);
364364
}
365365

366-
private async handleCodeResponse(state: string, codeVerifier: string, scope: string): Promise<vscode.AuthenticationSession2> {
366+
private async handleCodeResponse(state: string, codeVerifier: string, scope: string): Promise<vscode.AuthenticationSession> {
367367
let uriEventListener: vscode.Disposable;
368-
return new Promise((resolve: (value: vscode.AuthenticationSession2) => void, reject) => {
368+
return new Promise((resolve: (value: vscode.AuthenticationSession) => void, reject) => {
369369
uriEventListener = this._uriHandler.event(async (uri: vscode.Uri) => {
370370
try {
371371
const query = parseQuery(uri);

src/vs/vscode.proposed.d.ts

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,7 @@ declare module 'vscode' {
2020

2121
// #region auth provider: https://github.com/microsoft/vscode/issues/88309
2222

23-
export interface AuthenticationSession {
24-
id: string;
25-
getAccessToken(): Thenable<string>;
26-
account: {
27-
displayName: string;
28-
id: string;
29-
};
30-
scopes: string[];
31-
}
32-
33-
export class AuthenticationSession2 {
23+
export class AuthenticationSession {
3424
/**
3525
* The identifier of the authentication session.
3626
*/
@@ -148,12 +138,12 @@ declare module 'vscode' {
148138
/**
149139
* Returns an array of current sessions.
150140
*/
151-
getSessions(): Thenable<ReadonlyArray<AuthenticationSession2>>;
141+
getSessions(): Thenable<ReadonlyArray<AuthenticationSession>>;
152142

153143
/**
154144
* Prompts a user to login.
155145
*/
156-
login(scopes: string[]): Thenable<AuthenticationSession2>;
146+
login(scopes: string[]): Thenable<AuthenticationSession>;
157147

158148
/**
159149
* Removes the session corresponding to session id.
@@ -211,7 +201,7 @@ declare module 'vscode' {
211201
* @param options The [getSessionOptions](#GetSessionOptions) to use
212202
* @returns A thenable that resolves to an authentication session
213203
*/
214-
export function getSession(providerId: string, scopes: string[], options: AuthenticationGetSessionOptions & { createIfNone: true }): Thenable<AuthenticationSession2>;
204+
export function getSession(providerId: string, scopes: string[], options: AuthenticationGetSessionOptions & { createIfNone: true }): Thenable<AuthenticationSession>;
215205

216206
/**
217207
* Get an authentication session matching the desired scopes. Rejects if a provider with providerId is not
@@ -223,29 +213,7 @@ declare module 'vscode' {
223213
* @param options The [getSessionOptions](#GetSessionOptions) to use
224214
* @returns A thenable that resolves to an authentication session if available, or undefined if there are no sessions
225215
*/
226-
export function getSession(providerId: string, scopes: string[], options: AuthenticationGetSessionOptions): Thenable<AuthenticationSession2 | undefined>;
227-
228-
/**
229-
* @deprecated
230-
* Get existing authentication sessions. Rejects if a provider with providerId is not
231-
* registered, or if the user does not consent to sharing authentication information with
232-
* the extension.
233-
* @param providerId The id of the provider to use
234-
* @param scopes A list of scopes representing the permissions requested. These are dependent on the authentication
235-
* provider
236-
*/
237-
export function getSessions(providerId: string, scopes: string[]): Thenable<ReadonlyArray<AuthenticationSession>>;
238-
239-
/**
240-
* @deprecated
241-
* Prompt a user to login to create a new authenticaiton session. Rejects if a provider with
242-
* providerId is not registered, or if the user does not consent to sharing authentication
243-
* information with the extension.
244-
* @param providerId The id of the provider to use
245-
* @param scopes A list of scopes representing the permissions requested. These are dependent on the authentication
246-
* provider
247-
*/
248-
export function login(providerId: string, scopes: string[]): Thenable<AuthenticationSession>;
216+
export function getSession(providerId: string, scopes: string[], options: AuthenticationGetSessionOptions): Thenable<AuthenticationSession | undefined>;
249217

250218
/**
251219
* @deprecated

src/vs/workbench/api/common/extHost.api.impl.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,6 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
206206
getSession(providerId: string, scopes: string[], options: vscode.AuthenticationGetSessionOptions) {
207207
return extHostAuthentication.getSession(extension, providerId, scopes, options as any);
208208
},
209-
getSessions(providerId: string, scopes: string[]): Thenable<readonly vscode.AuthenticationSession[]> {
210-
return extHostAuthentication.getSessions(extension, providerId, scopes);
211-
},
212-
login(providerId: string, scopes: string[]): Thenable<vscode.AuthenticationSession> {
213-
return extHostAuthentication.login(extension, providerId, scopes);
214-
},
215209
logout(providerId: string, sessionId: string): Thenable<void> {
216210
return extHostAuthentication.logout(providerId, sessionId);
217211
},
@@ -1102,7 +1096,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
11021096
CellKind: extHostTypes.CellKind,
11031097
CellOutputKind: extHostTypes.CellOutputKind,
11041098
NotebookCellRunState: extHostTypes.NotebookCellRunState,
1105-
AuthenticationSession2: extHostTypes.AuthenticationSession
1099+
AuthenticationSession: extHostTypes.AuthenticationSession
11061100
};
11071101
};
11081102
}

src/vs/workbench/api/common/extHostAuthentication.ts

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
5656
return !!(sessions.filter(session => session.scopes.sort().join(' ') === orderedScopes).length);
5757
}
5858

59-
async getSession(requestingExtension: IExtensionDescription, providerId: string, scopes: string[], options: vscode.AuthenticationGetSessionOptions & { createIfNone: true }): Promise<vscode.AuthenticationSession2>;
60-
async getSession(requestingExtension: IExtensionDescription, providerId: string, scopes: string[], options: vscode.AuthenticationGetSessionOptions): Promise<vscode.AuthenticationSession2 | undefined> {
59+
async getSession(requestingExtension: IExtensionDescription, providerId: string, scopes: string[], options: vscode.AuthenticationGetSessionOptions & { createIfNone: true }): Promise<vscode.AuthenticationSession>;
60+
async getSession(requestingExtension: IExtensionDescription, providerId: string, scopes: string[], options: vscode.AuthenticationGetSessionOptions): Promise<vscode.AuthenticationSession | undefined> {
6161
const provider = this._authenticationProviders.get(providerId);
6262
const extensionName = requestingExtension.displayName || requestingExtension.name;
6363
const extensionId = ExtensionIdentifier.toKey(requestingExtension.identifier);
@@ -100,71 +100,6 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
100100
}
101101
}
102102

103-
async getSessions(requestingExtension: IExtensionDescription, providerId: string, scopes: string[]): Promise<readonly vscode.AuthenticationSession[]> {
104-
const extensionId = ExtensionIdentifier.toKey(requestingExtension.identifier);
105-
const orderedScopes = scopes.sort().join(' ');
106-
const sessions = await this.resolveSessions(providerId);
107-
return sessions
108-
.filter(session => session.scopes.sort().join(' ') === orderedScopes)
109-
.map(session => {
110-
return {
111-
id: session.id,
112-
account: session.account,
113-
scopes: session.scopes,
114-
getAccessToken: async () => {
115-
const isAllowed = await this._proxy.$getSessionsPrompt(
116-
providerId,
117-
session.account.displayName,
118-
'', // TODO
119-
// provider.displayName,
120-
extensionId,
121-
requestingExtension.displayName || requestingExtension.name);
122-
123-
if (!isAllowed) {
124-
throw new Error('User did not consent to token access.');
125-
}
126-
127-
return session.accessToken;
128-
}
129-
};
130-
});
131-
}
132-
133-
async login(requestingExtension: IExtensionDescription, providerId: string, scopes: string[]): Promise<vscode.AuthenticationSession> {
134-
const provider = this._authenticationProviders.get(providerId);
135-
if (!provider) {
136-
throw new Error(`No authentication provider with id '${providerId}' is currently registered.`);
137-
}
138-
139-
const extensionName = requestingExtension.displayName || requestingExtension.name;
140-
const isAllowed = await this._proxy.$loginPrompt(provider.displayName, extensionName);
141-
if (!isAllowed) {
142-
throw new Error('User did not consent to login.');
143-
}
144-
145-
const session = await provider.login(scopes);
146-
await this._proxy.$setTrustedExtension(provider.id, session.account.displayName, ExtensionIdentifier.toKey(requestingExtension.identifier), extensionName);
147-
return {
148-
id: session.id,
149-
account: session.account,
150-
scopes: session.scopes,
151-
getAccessToken: async () => {
152-
const isAllowed = await this._proxy.$getSessionsPrompt(
153-
provider.id,
154-
session.account.displayName,
155-
provider.displayName,
156-
ExtensionIdentifier.toKey(requestingExtension.identifier),
157-
requestingExtension.displayName || requestingExtension.name);
158-
159-
if (!isAllowed) {
160-
throw new Error('User did not consent to token access.');
161-
}
162-
163-
return session.accessToken;
164-
}
165-
};
166-
}
167-
168103
async logout(providerId: string, sessionId: string): Promise<void> {
169104
const provider = this._authenticationProviders.get(providerId);
170105
if (!provider) {

src/vs/workbench/api/common/extHostTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2769,7 +2769,7 @@ export enum ExtensionMode {
27692769

27702770

27712771
//#region Authentication
2772-
export class AuthenticationSession implements vscode.AuthenticationSession2 {
2772+
export class AuthenticationSession implements vscode.AuthenticationSession {
27732773
constructor(public id: string, public accessToken: string, public account: { displayName: string, id: string }, public scopes: string[]) { }
27742774
}
27752775

0 commit comments

Comments
 (0)