1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { DisposableStore } from 'vs/base/common/lifecycle';
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { TestDialogService } from 'vs/platform/dialogs/test/common/testDialogService';
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { TestNotificationService } from 'vs/platform/notification/test/common/testNotificationService';
import { IQuickInputHideEvent, IQuickInputService, IQuickPickDidAcceptEvent } from 'vs/platform/quickinput/common/quickInput';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { NullTelemetryService } from 'vs/platform/telemetry/common/telemetryUtils';
import { MainThreadAuthentication } from 'vs/workbench/api/browser/mainThreadAuthentication';
import { ExtHostContext, MainContext } from 'vs/workbench/api/common/extHost.protocol';
import { ExtHostAuthentication } from 'vs/workbench/api/common/extHostAuthentication';
import { IActivityService } from 'vs/workbench/services/activity/common/activity';
import { AuthenticationService } from 'vs/workbench/services/authentication/browser/authenticationService';
import { IAuthenticationService } from 'vs/workbench/services/authentication/common/authentication';
import { IExtensionService, nullExtensionDescription as extensionDescription } from 'vs/workbench/services/extensions/common/extensions';
import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
import { TestRPCProtocol } from 'vs/workbench/api/test/common/testRPCProtocol';
import { TestQuickInputService, TestRemoteAgentService } from 'vs/workbench/test/browser/workbenchTestServices';
import { TestActivityService, TestExtensionService, TestStorageService } from 'vs/workbench/test/common/workbenchTestServices';
import type { AuthenticationProvider, AuthenticationSession } from 'vscode';
class AuthQuickPick {
private listener: ((e: IQuickPickDidAcceptEvent) => any) | undefined;
public items = [];
public get selectedItems(): string[] {
return this.items;
}
onDidAccept(listener: (e: IQuickPickDidAcceptEvent) => any) {
this.listener = listener;
}
onDidHide(listener: (e: IQuickInputHideEvent) => any) {
}
dispose() {
}
show() {
this.listener!({
inBackground: false
});
}
}
class AuthTestQuickInputService extends TestQuickInputService {
override createQuickPick() {
return <any>new AuthQuickPick();
}
}
class TestAuthProvider implements AuthenticationProvider {
private id = 1;
private sessions = new Map<string, AuthenticationSession>();
onDidChangeSessions = () => { return { dispose() { } }; };
async getSessions(scopes?: readonly string[]): Promise<AuthenticationSession[]> {
if (!scopes) {
return [...this.sessions.values()];
}
if (scopes[0] === 'return multiple') {
return [...this.sessions.values()];
}
const sessions = this.sessions.get(scopes.join(' '));
return sessions ? [sessions] : [];
}
async createSession(scopes: readonly string[]): Promise<AuthenticationSession> {
const scopesStr = scopes.join(' ');
const session = {
scopes,
id: `${this.id}`,
account: {
label: `${this.id}`,
id: `${this.id}`,
},
accessToken: Math.random() + '',
};
this.sessions.set(scopesStr, session);
this.id++;
return session;
}
async removeSession(sessionId: string): Promise<void> {
this.sessions.delete(sessionId);
}
}
suite('ExtHostAuthentication', () => {
let disposables: DisposableStore;
let extHostAuthentication: ExtHostAuthentication;
let instantiationService: TestInstantiationService;
suiteSetup(async () => {
instantiationService = new TestInstantiationService();
instantiationService.stub(IDialogService, new TestDialogService());
instantiationService.stub(IStorageService, new TestStorageService());
instantiationService.stub(IQuickInputService, new AuthTestQuickInputService());
instantiationService.stub(IExtensionService, new TestExtensionService());
instantiationService.stub(IActivityService, new TestActivityService());
instantiationService.stub(IRemoteAgentService, new TestRemoteAgentService());
instantiationService.stub(INotificationService, new TestNotificationService());
instantiationService.stub(ITelemetryService, NullTelemetryService);
const rpcProtocol = new TestRPCProtocol();
instantiationService.stub(IAuthenticationService, instantiationService.createInstance(AuthenticationService));
rpcProtocol.set(MainContext.MainThreadAuthentication, instantiationService.createInstance(MainThreadAuthentication, rpcProtocol));
extHostAuthentication = new ExtHostAuthentication(rpcProtocol);
rpcProtocol.set(ExtHostContext.ExtHostAuthentication, extHostAuthentication);
});
setup(async () => {
disposables = new DisposableStore();
disposables.add(extHostAuthentication.registerAuthenticationProvider('test', 'test provider', new TestAuthProvider()));
disposables.add(extHostAuthentication.registerAuthenticationProvider(
'test-multiple',
'test multiple provider',
new TestAuthProvider(),
{ supportsMultipleAccounts: true }));
});
teardown(() => {
disposables.dispose();
});
test('createIfNone - true', async () => {
const scopes = ['foo'];
const session = await extHostAuthentication.getSession(
extensionDescription,
'test',
scopes,
{
createIfNone: true
});
assert.strictEqual(session?.id, '1');
assert.strictEqual(session?.scopes[0], 'foo');
});
test('createIfNone - false', async () => {
const scopes = ['foo'];
const nosession = await extHostAuthentication.getSession(
extensionDescription,
'test',
scopes,
{});
assert.strictEqual(nosession, undefined);
// Now create the session
const session = await extHostAuthentication.getSession(
extensionDescription,
'test',
scopes,
{
createIfNone: true
});
assert.strictEqual(session?.id, '1');
assert.strictEqual(session?.scopes[0], 'foo');
const session2 = await extHostAuthentication.getSession(
extensionDescription,
'test',
scopes,
{});
assert.strictEqual(session.id, session2?.id);
assert.strictEqual(session.scopes[0], session2?.scopes[0]);
assert.strictEqual(session.accessToken, session2?.accessToken);
});
// should behave the same as createIfNone: false
test('silent - true', async () => {
const scopes = ['foo'];
const nosession = await extHostAuthentication.getSession(
extensionDescription,
'test',
scopes,
{
silent: true
});
assert.strictEqual(nosession, undefined);
// Now create the session
const session = await extHostAuthentication.getSession(
extensionDescription,
'test',
scopes,
{
createIfNone: true
});
assert.strictEqual(session?.id, '1');
assert.strictEqual(session?.scopes[0], 'foo');
const session2 = await extHostAuthentication.getSession(
extensionDescription,
'test',
scopes,
{
silent: true
});
assert.strictEqual(session.id, session2?.id);
assert.strictEqual(session.scopes[0], session2?.scopes[0]);
});
test('forceNewSession - true', async () => {
const scopes = ['foo'];
const session1 = await extHostAuthentication.getSession(
extensionDescription,
'test',
scopes,
{
createIfNone: true
});
// Now create the session
const session2 = await extHostAuthentication.getSession(
extensionDescription,
'test',
scopes,
{
forceNewSession: true
});
assert.strictEqual(session2?.id, '2');
assert.strictEqual(session2?.scopes[0], 'foo');
assert.notStrictEqual(session1.accessToken, session2?.accessToken);
});
test('forceNewSession - detail', async () => {
const scopes = ['foo'];
const session1 = await extHostAuthentication.getSession(
extensionDescription,
'test',
scopes,
{
createIfNone: true
});
// Now create the session
const session2 = await extHostAuthentication.getSession(
extensionDescription,
'test',
scopes,
{
forceNewSession: { detail: 'bar' }
});
assert.strictEqual(session2?.id, '2');
assert.strictEqual(session2?.scopes[0], 'foo');
assert.notStrictEqual(session1.accessToken, session2?.accessToken);
});
//#region Multi-Account AuthProvider
test('clearSessionPreference - true', async () => {
const scopes = ['foo'];
// Now create the session
const session = await extHostAuthentication.getSession(
extensionDescription,
'test-multiple',
scopes,
{
createIfNone: true
});
assert.strictEqual(session?.id, '1');
assert.strictEqual(session?.scopes[0], scopes[0]);
const scopes2 = ['bar'];
const session2 = await extHostAuthentication.getSession(
extensionDescription,
'test-multiple',
scopes2,
{
createIfNone: true
});
assert.strictEqual(session2?.id, '2');
assert.strictEqual(session2?.scopes[0], scopes2[0]);
const session3 = await extHostAuthentication.getSession(
extensionDescription,
'test-multiple',
['return multiple'],
{
clearSessionPreference: true,
createIfNone: true
});
// clearing session preference causes us to get the first session
// because it would normally show a quick pick for the user to choose
assert.strictEqual(session.id, session3?.id);
assert.strictEqual(session.scopes[0], session3?.scopes[0]);
assert.strictEqual(session.accessToken, session3?.accessToken);
});
test('silently getting session should return a session (if any) regardless of preference - fixes #137819', async () => {
const scopes = ['foo'];
// Now create the session
const session = await extHostAuthentication.getSession(
extensionDescription,
'test-multiple',
scopes,
{
createIfNone: true
});
assert.strictEqual(session?.id, '1');
assert.strictEqual(session?.scopes[0], scopes[0]);
const scopes2 = ['bar'];
const session2 = await extHostAuthentication.getSession(
extensionDescription,
'test-multiple',
scopes2,
{
createIfNone: true
});
assert.strictEqual(session2?.id, '2');
assert.strictEqual(session2?.scopes[0], scopes2[0]);
const shouldBeSession1 = await extHostAuthentication.getSession(
extensionDescription,
'test-multiple',
scopes,
{});
assert.strictEqual(session.id, shouldBeSession1?.id);
assert.strictEqual(session.scopes[0], shouldBeSession1?.scopes[0]);
assert.strictEqual(session.accessToken, shouldBeSession1?.accessToken);
const shouldBeSession2 = await extHostAuthentication.getSession(
extensionDescription,
'test-multiple',
scopes2,
{});
assert.strictEqual(session2.id, shouldBeSession2?.id);
assert.strictEqual(session2.scopes[0], shouldBeSession2?.scopes[0]);
assert.strictEqual(session2.accessToken, shouldBeSession2?.accessToken);
});
//#endregion
//#region error cases
test('forceNewSession with no sessions', async () => {
try {
await extHostAuthentication.getSession(
extensionDescription,
'test',
['foo'],
{
forceNewSession: true
});
assert.fail('should have thrown an Error.');
} catch (e) {
assert.strictEqual(e.message, 'No existing sessions found.');
}
});
test('createIfNone and forceNewSession', async () => {
try {
await extHostAuthentication.getSession(
extensionDescription,
'test',
['foo'],
{
createIfNone: true,
forceNewSession: true
});
assert.fail('should have thrown an Error.');
} catch (e) {
assert.ok(e);
}
});
test('forceNewSession and silent', async () => {
try {
await extHostAuthentication.getSession(
extensionDescription,
'test',
['foo'],
{
forceNewSession: true,
silent: true
});
assert.fail('should have thrown an Error.');
} catch (e) {
assert.ok(e);
}
});
test('createIfNone and silent', async () => {
try {
await extHostAuthentication.getSession(
extensionDescription,
'test',
['foo'],
{
createIfNone: true,
silent: true
});
assert.fail('should have thrown an Error.');
} catch (e) {
assert.ok(e);
}
});
test('Can get multiple sessions (with different scopes) in one extension', async () => {
let session: AuthenticationSession | undefined = await extHostAuthentication.getSession(
extensionDescription,
'test-multiple',
['foo'],
{
createIfNone: true
});
session = await extHostAuthentication.getSession(
extensionDescription,
'test-multiple',
['bar'],
{
createIfNone: true
});
assert.strictEqual(session?.id, '2');
assert.strictEqual(session?.scopes[0], 'bar');
session = await extHostAuthentication.getSession(
extensionDescription,
'test-multiple',
['foo'],
{
createIfNone: false
});
assert.strictEqual(session?.id, '1');
assert.strictEqual(session?.scopes[0], 'foo');
});
//#endregion
});
|