Skip to content

Commit fb328b8

Browse files
Add logging and also re-get the sessions after updating. (microsoft#249683)
1 parent 96d57fd commit fb328b8

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ export class DynamicAuthProvider implements vscode.AuthenticationProvider {
228228
onDidChange: scopedEvent,
229229
set: (tokens) => _proxy.$setSessionsForDynamicAuthProvider(this._serverMetadata.issuer, this.clientId, tokens),
230230
},
231-
initialTokens
231+
initialTokens,
232+
this._logger
232233
));
233234
this._disposable.add(this._tokenStore.onDidChangeSessions(e => this._onDidChangeSessions.fire(e)));
234235
// Will be extended later to support other flows
@@ -275,11 +276,13 @@ export class DynamicAuthProvider implements vscode.AuthenticationProvider {
275276
}
276277
}
277278

278-
async getSessions(scopes: readonly string[] | undefined, options: vscode.AuthenticationProviderSessionOptions): Promise<vscode.AuthenticationSession[]> {
279+
async getSessions(scopes: readonly string[] | undefined, _options: vscode.AuthenticationProviderSessionOptions): Promise<vscode.AuthenticationSession[]> {
280+
this._logger.info(`Getting sessions for scopes: ${scopes?.join(' ') ?? 'all'}`);
279281
if (!scopes) {
280282
return this._tokenStore.sessions;
281283
}
282-
const sessions = this._tokenStore.sessions.filter(session => session.scopes.join(' ') === scopes.join(' ')) || [];
284+
let sessions = this._tokenStore.sessions.filter(session => session.scopes.join(' ') === scopes.join(' '));
285+
this._logger.info(`Found ${sessions.length} sessions for scopes: ${scopes.join(' ')}`);
283286
if (sessions.length) {
284287
const newTokens: IAuthorizationToken[] = [];
285288
const removedTokens: IAuthorizationToken[] = [];
@@ -291,13 +294,16 @@ export class DynamicAuthProvider implements vscode.AuthenticationProvider {
291294
const expiresInMS = token.expires_in * 1000;
292295
// Check if the token is about to expire in 5 minutes or if it is expired
293296
if (now > token.created_at + expiresInMS - (5 * 60 * 1000)) {
297+
this._logger.info(`Token for session ${session.id} is about to expire, refreshing...`);
294298
removedTokens.push(token);
295299
if (!token.refresh_token) {
296300
// No refresh token available, cannot refresh
301+
this._logger.warn(`No refresh token available for scopes ${session.scopes.join(' ')}. Throwing away token.`);
297302
continue;
298303
}
299304
try {
300305
const newToken = await this.exchangeRefreshTokenForToken(token.refresh_token);
306+
this._logger.info(`Successfully created a new token for scopes ${session.scopes.join(' ')}.`);
301307
newTokens.push(newToken);
302308
} catch (err) {
303309
this._logger.error(`Failed to refresh token: ${err}`);
@@ -308,13 +314,18 @@ export class DynamicAuthProvider implements vscode.AuthenticationProvider {
308314
}
309315
if (newTokens.length || removedTokens.length) {
310316
this._tokenStore.update({ added: newTokens, removed: removedTokens });
317+
// Since we updated the tokens, we need to re-filter the sessions
318+
// to get the latest state
319+
sessions = this._tokenStore.sessions.filter(session => session.scopes.join(' ') === scopes.join(' '));
311320
}
321+
this._logger.info(`Found ${sessions.length} sessions for scopes: ${scopes.join(' ')}`);
312322
return sessions;
313323
}
314324
return [];
315325
}
316326

317327
async createSession(scopes: string[], _options: vscode.AuthenticationProviderSessionOptions): Promise<vscode.AuthenticationSession> {
328+
this._logger.info(`Creating session for scopes: ${scopes.join(' ')}`);
318329
let token: IAuthorizationTokenResponse | undefined;
319330
for (const createFlow of this._createFlows) {
320331
try {
@@ -333,10 +344,12 @@ export class DynamicAuthProvider implements vscode.AuthenticationProvider {
333344
// Store session for later retrieval
334345
this._tokenStore.update({ added: [{ ...token, created_at: Date.now() }], removed: [] });
335346
const session = this._tokenStore.sessions.find(t => t.accessToken === token.access_token)!;
347+
this._logger.info(`Created session for scopes: ${scopes.join(' ')}`);
336348
return session;
337349
}
338350

339351
async removeSession(sessionId: string): Promise<void> {
352+
this._logger.info(`Removing session with id: ${sessionId}`);
340353
const session = this._tokenStore.sessions.find(session => session.id === sessionId);
341354
if (!session) {
342355
this._logger.error(`Session with id ${sessionId} not found`);
@@ -348,6 +361,7 @@ export class DynamicAuthProvider implements vscode.AuthenticationProvider {
348361
return;
349362
}
350363
this._tokenStore.update({ added: [], removed: [token] });
364+
this._logger.info(`Removed token for session: ${session.id} with scopes: ${session.scopes.join(' ')}`);
351365
}
352366

353367
dispose(): void {
@@ -519,7 +533,8 @@ class TokenStore implements Disposable {
519533

520534
constructor(
521535
private readonly _persistence: { onDidChange: Event<IAuthorizationToken[]>; set: (tokens: IAuthorizationToken[]) => void },
522-
initialTokens: IAuthorizationToken[]
536+
initialTokens: IAuthorizationToken[],
537+
private readonly _logger: ILogger
523538
) {
524539
this._disposable = new DisposableStore();
525540
this._tokensObservable = observableValue<IAuthorizationToken[]>('tokens', initialTokens);
@@ -544,6 +559,7 @@ class TokenStore implements Disposable {
544559
}
545560

546561
update({ added, removed }: { added: IAuthorizationToken[]; removed: IAuthorizationToken[] }): void {
562+
this._logger.trace(`Updating tokens: added ${added.length}, removed ${removed.length}`);
547563
const currentTokens = [...this._tokensObservable.get()];
548564
for (const token of removed) {
549565
const index = currentTokens.findIndex(t => t.access_token === token.access_token);
@@ -563,18 +579,22 @@ class TokenStore implements Disposable {
563579
this._tokensObservable.set(currentTokens, undefined);
564580
void this._persistence.set(currentTokens);
565581
}
582+
this._logger.trace(`Tokens updated: ${currentTokens.length} tokens stored.`);
566583
}
567584

568585
private _registerChangeEventAutorun(): IDisposable {
569586
let previousSessions: vscode.AuthenticationSession[] = [];
570587
return autorun((reader) => {
588+
this._logger.trace('Checking for session changes...');
571589
const currentSessions = this._sessionsObservable.read(reader);
572590
if (previousSessions === currentSessions) {
591+
this._logger.trace('No session changes detected.');
573592
return;
574593
}
575594

576595
if (!currentSessions || currentSessions.length === 0) {
577596
// If currentSessions is undefined, all previous sessions are considered removed
597+
this._logger.trace('All sessions removed.');
578598
if (previousSessions.length > 0) {
579599
this._onDidChangeSessions.fire({
580600
added: [],
@@ -607,6 +627,7 @@ class TokenStore implements Disposable {
607627

608628
// Fire the event if there are any changes
609629
if (added.length > 0 || removed.length > 0) {
630+
this._logger.trace(`Sessions changed: added ${added.length}, removed ${removed.length}`);
610631
this._onDidChangeSessions.fire({ added, removed, changed: [] });
611632
}
612633

0 commit comments

Comments
 (0)