Ensure access token comes from correct provider (#150473)

This commit is contained in:
Tomer Chachamu
2022-05-27 22:52:12 +00:00
committed by GitHub
parent ca1055adb2
commit c6350b3800
2 changed files with 66 additions and 5 deletions
@@ -11,6 +11,7 @@ import { IExtensionDescription, ExtensionIdentifier } from 'vs/platform/extensio
interface GetSessionsRequest {
scopes: string;
providerId: string;
result: Promise<vscode.AuthenticationSession | undefined>;
}
@@ -48,13 +49,14 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
const extensionId = ExtensionIdentifier.toKey(requestingExtension.identifier);
const inFlightRequests = this._inFlightRequests.get(extensionId) || [];
const sortedScopes = [...scopes].sort().join(' ');
let inFlightRequest: GetSessionsRequest | undefined = inFlightRequests.find(request => request.scopes === sortedScopes);
let inFlightRequest: GetSessionsRequest | undefined = inFlightRequests.find(request => request.providerId === providerId && request.scopes === sortedScopes);
if (inFlightRequest) {
return inFlightRequest.result;
} else {
const session = this._getSession(requestingExtension, extensionId, providerId, scopes, options);
inFlightRequest = {
providerId,
scopes: sortedScopes,
result: session
};
@@ -65,7 +67,7 @@ export class ExtHostAuthentication implements ExtHostAuthenticationShape {
try {
await session;
} finally {
const requestIndex = inFlightRequests.findIndex(request => request.scopes === sortedScopes);
const requestIndex = inFlightRequests.findIndex(request => request.providerId === providerId && request.scopes === sortedScopes);
if (requestIndex > -1) {
inFlightRequests.splice(requestIndex);
this._inFlightRequests.set(extensionId, inFlightRequests);
@@ -59,6 +59,7 @@ class TestAuthProvider implements AuthenticationProvider {
private id = 1;
private sessions = new Map<string, AuthenticationSession>();
onDidChangeSessions = () => { return { dispose() { } }; };
constructor(private readonly authProviderName: string) { }
async getSessions(scopes?: readonly string[]): Promise<AuthenticationSession[]> {
if (!scopes) {
return [...this.sessions.values()];
@@ -76,7 +77,7 @@ class TestAuthProvider implements AuthenticationProvider {
scopes,
id: `${this.id}`,
account: {
label: `${this.id}`,
label: this.authProviderName,
id: `${this.id}`,
},
accessToken: Math.random() + '',
@@ -118,11 +119,11 @@ suite('ExtHostAuthentication', () => {
setup(async () => {
disposables = new DisposableStore();
disposables.add(extHostAuthentication.registerAuthenticationProvider('test', 'test provider', new TestAuthProvider()));
disposables.add(extHostAuthentication.registerAuthenticationProvider('test', 'test provider', new TestAuthProvider('test')));
disposables.add(extHostAuthentication.registerAuthenticationProvider(
'test-multiple',
'test multiple provider',
new TestAuthProvider(),
new TestAuthProvider('test-multiple'),
{ supportsMultipleAccounts: true }));
});
@@ -441,5 +442,63 @@ suite('ExtHostAuthentication', () => {
assert.strictEqual(session?.scopes[0], 'foo');
});
test('Can get multiple sessions (from different providers) in one extension', async () => {
let session: AuthenticationSession | undefined = await extHostAuthentication.getSession(
extensionDescription,
'test-multiple',
['foo'],
{
createIfNone: true
});
session = await extHostAuthentication.getSession(
extensionDescription,
'test',
['foo'],
{
createIfNone: true
});
assert.strictEqual(session?.id, '1');
assert.strictEqual(session?.scopes[0], 'foo');
assert.strictEqual(session?.account.label, 'test');
const session2 = await extHostAuthentication.getSession(
extensionDescription,
'test-multiple',
['foo'],
{
createIfNone: false
});
assert.strictEqual(session2?.id, '1');
assert.strictEqual(session2?.scopes[0], 'foo');
assert.strictEqual(session2?.account.label, 'test-multiple');
});
test('Can get multiple sessions (from different providers) in one extension at the same time', async () => {
let sessionP: Promise<AuthenticationSession | undefined> = extHostAuthentication.getSession(
extensionDescription,
'test',
['foo'],
{
createIfNone: true
});
let session2P: Promise<AuthenticationSession | undefined> = extHostAuthentication.getSession(
extensionDescription,
'test-multiple',
['foo'],
{
createIfNone: true
});
const session = await sessionP;
assert.strictEqual(session?.id, '1');
assert.strictEqual(session?.scopes[0], 'foo');
assert.strictEqual(session?.account.label, 'test');
const session2 = await session2P;
assert.strictEqual(session2?.id, '1');
assert.strictEqual(session2?.scopes[0], 'foo');
assert.strictEqual(session2?.account.label, 'test-multiple');
});
//#endregion
});