@@ -228,7 +228,8 @@ export class DynamicAuthProvider implements vscode.AuthenticationProvider {
228
228
onDidChange : scopedEvent ,
229
229
set : ( tokens ) => _proxy . $setSessionsForDynamicAuthProvider ( this . _serverMetadata . issuer , this . clientId , tokens ) ,
230
230
} ,
231
- initialTokens
231
+ initialTokens ,
232
+ this . _logger
232
233
) ) ;
233
234
this . _disposable . add ( this . _tokenStore . onDidChangeSessions ( e => this . _onDidChangeSessions . fire ( e ) ) ) ;
234
235
// Will be extended later to support other flows
@@ -275,11 +276,13 @@ export class DynamicAuthProvider implements vscode.AuthenticationProvider {
275
276
}
276
277
}
277
278
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' } ` ) ;
279
281
if ( ! scopes ) {
280
282
return this . _tokenStore . sessions ;
281
283
}
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 ( ' ' ) } ` ) ;
283
286
if ( sessions . length ) {
284
287
const newTokens : IAuthorizationToken [ ] = [ ] ;
285
288
const removedTokens : IAuthorizationToken [ ] = [ ] ;
@@ -291,13 +294,16 @@ export class DynamicAuthProvider implements vscode.AuthenticationProvider {
291
294
const expiresInMS = token . expires_in * 1000 ;
292
295
// Check if the token is about to expire in 5 minutes or if it is expired
293
296
if ( now > token . created_at + expiresInMS - ( 5 * 60 * 1000 ) ) {
297
+ this . _logger . info ( `Token for session ${ session . id } is about to expire, refreshing...` ) ;
294
298
removedTokens . push ( token ) ;
295
299
if ( ! token . refresh_token ) {
296
300
// No refresh token available, cannot refresh
301
+ this . _logger . warn ( `No refresh token available for scopes ${ session . scopes . join ( ' ' ) } . Throwing away token.` ) ;
297
302
continue ;
298
303
}
299
304
try {
300
305
const newToken = await this . exchangeRefreshTokenForToken ( token . refresh_token ) ;
306
+ this . _logger . info ( `Successfully created a new token for scopes ${ session . scopes . join ( ' ' ) } .` ) ;
301
307
newTokens . push ( newToken ) ;
302
308
} catch ( err ) {
303
309
this . _logger . error ( `Failed to refresh token: ${ err } ` ) ;
@@ -308,13 +314,18 @@ export class DynamicAuthProvider implements vscode.AuthenticationProvider {
308
314
}
309
315
if ( newTokens . length || removedTokens . length ) {
310
316
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 ( ' ' ) ) ;
311
320
}
321
+ this . _logger . info ( `Found ${ sessions . length } sessions for scopes: ${ scopes . join ( ' ' ) } ` ) ;
312
322
return sessions ;
313
323
}
314
324
return [ ] ;
315
325
}
316
326
317
327
async createSession ( scopes : string [ ] , _options : vscode . AuthenticationProviderSessionOptions ) : Promise < vscode . AuthenticationSession > {
328
+ this . _logger . info ( `Creating session for scopes: ${ scopes . join ( ' ' ) } ` ) ;
318
329
let token : IAuthorizationTokenResponse | undefined ;
319
330
for ( const createFlow of this . _createFlows ) {
320
331
try {
@@ -333,10 +344,12 @@ export class DynamicAuthProvider implements vscode.AuthenticationProvider {
333
344
// Store session for later retrieval
334
345
this . _tokenStore . update ( { added : [ { ...token , created_at : Date . now ( ) } ] , removed : [ ] } ) ;
335
346
const session = this . _tokenStore . sessions . find ( t => t . accessToken === token . access_token ) ! ;
347
+ this . _logger . info ( `Created session for scopes: ${ scopes . join ( ' ' ) } ` ) ;
336
348
return session ;
337
349
}
338
350
339
351
async removeSession ( sessionId : string ) : Promise < void > {
352
+ this . _logger . info ( `Removing session with id: ${ sessionId } ` ) ;
340
353
const session = this . _tokenStore . sessions . find ( session => session . id === sessionId ) ;
341
354
if ( ! session ) {
342
355
this . _logger . error ( `Session with id ${ sessionId } not found` ) ;
@@ -348,6 +361,7 @@ export class DynamicAuthProvider implements vscode.AuthenticationProvider {
348
361
return ;
349
362
}
350
363
this . _tokenStore . update ( { added : [ ] , removed : [ token ] } ) ;
364
+ this . _logger . info ( `Removed token for session: ${ session . id } with scopes: ${ session . scopes . join ( ' ' ) } ` ) ;
351
365
}
352
366
353
367
dispose ( ) : void {
@@ -519,7 +533,8 @@ class TokenStore implements Disposable {
519
533
520
534
constructor (
521
535
private readonly _persistence : { onDidChange : Event < IAuthorizationToken [ ] > ; set : ( tokens : IAuthorizationToken [ ] ) => void } ,
522
- initialTokens : IAuthorizationToken [ ]
536
+ initialTokens : IAuthorizationToken [ ] ,
537
+ private readonly _logger : ILogger
523
538
) {
524
539
this . _disposable = new DisposableStore ( ) ;
525
540
this . _tokensObservable = observableValue < IAuthorizationToken [ ] > ( 'tokens' , initialTokens ) ;
@@ -544,6 +559,7 @@ class TokenStore implements Disposable {
544
559
}
545
560
546
561
update ( { added, removed } : { added : IAuthorizationToken [ ] ; removed : IAuthorizationToken [ ] } ) : void {
562
+ this . _logger . trace ( `Updating tokens: added ${ added . length } , removed ${ removed . length } ` ) ;
547
563
const currentTokens = [ ...this . _tokensObservable . get ( ) ] ;
548
564
for ( const token of removed ) {
549
565
const index = currentTokens . findIndex ( t => t . access_token === token . access_token ) ;
@@ -563,18 +579,22 @@ class TokenStore implements Disposable {
563
579
this . _tokensObservable . set ( currentTokens , undefined ) ;
564
580
void this . _persistence . set ( currentTokens ) ;
565
581
}
582
+ this . _logger . trace ( `Tokens updated: ${ currentTokens . length } tokens stored.` ) ;
566
583
}
567
584
568
585
private _registerChangeEventAutorun ( ) : IDisposable {
569
586
let previousSessions : vscode . AuthenticationSession [ ] = [ ] ;
570
587
return autorun ( ( reader ) => {
588
+ this . _logger . trace ( 'Checking for session changes...' ) ;
571
589
const currentSessions = this . _sessionsObservable . read ( reader ) ;
572
590
if ( previousSessions === currentSessions ) {
591
+ this . _logger . trace ( 'No session changes detected.' ) ;
573
592
return ;
574
593
}
575
594
576
595
if ( ! currentSessions || currentSessions . length === 0 ) {
577
596
// If currentSessions is undefined, all previous sessions are considered removed
597
+ this . _logger . trace ( 'All sessions removed.' ) ;
578
598
if ( previousSessions . length > 0 ) {
579
599
this . _onDidChangeSessions . fire ( {
580
600
added : [ ] ,
@@ -607,6 +627,7 @@ class TokenStore implements Disposable {
607
627
608
628
// Fire the event if there are any changes
609
629
if ( added . length > 0 || removed . length > 0 ) {
630
+ this . _logger . trace ( `Sessions changed: added ${ added . length } , removed ${ removed . length } ` ) ;
610
631
this . _onDidChangeSessions . fire ( { added, removed, changed : [ ] } ) ;
611
632
}
612
633
0 commit comments