@@ -8,7 +8,7 @@ import { SecretStorageCachePlugin } from '../common/cachePlugin';
8
8
import { SecretStorage , LogOutputChannel , Disposable , SecretStorageChangeEvent , EventEmitter , Memento , window , ProgressLocation , l10n } from 'vscode' ;
9
9
import { MsalLoggerOptions } from '../common/loggerOptions' ;
10
10
import { ICachedPublicClientApplication , ICachedPublicClientApplicationManager } from '../common/publicClientCache' ;
11
- import { Delayer , raceCancellationAndTimeoutError } from '../common/async' ;
11
+ import { raceCancellationAndTimeoutError } from '../common/async' ;
12
12
13
13
export interface IPublicClientApplicationInfo {
14
14
clientId : string ;
@@ -34,7 +34,7 @@ export class CachedPublicClientApplicationManager implements ICachedPublicClient
34
34
}
35
35
36
36
async initialize ( ) {
37
- this . _logger . debug ( 'Initializing PublicClientApplicationManager' ) ;
37
+ this . _logger . debug ( '[initialize] Initializing PublicClientApplicationManager' ) ;
38
38
const keys = await this . _secretStorage . get ( 'publicClientApplications' ) ;
39
39
if ( ! keys ) {
40
40
this . _initialized = true ;
@@ -54,13 +54,13 @@ export class CachedPublicClientApplicationManager implements ICachedPublicClient
54
54
}
55
55
} catch ( e ) {
56
56
// data is corrupted
57
- this . _logger . error ( 'Error initializing PublicClientApplicationManager:' , e ) ;
57
+ this . _logger . error ( '[initialize] Error initializing PublicClientApplicationManager:' , e ) ;
58
58
await this . _secretStorage . delete ( 'publicClientApplications' ) ;
59
59
}
60
60
61
61
// TODO: should we do anything for when this fails?
62
62
await Promise . allSettled ( promises ) ;
63
- this . _logger . debug ( 'PublicClientApplicationManager initialized' ) ;
63
+ this . _logger . debug ( '[initialize] PublicClientApplicationManager initialized' ) ;
64
64
this . _initialized = true ;
65
65
}
66
66
@@ -78,16 +78,16 @@ export class CachedPublicClientApplicationManager implements ICachedPublicClient
78
78
const pcasKey = JSON . stringify ( { clientId, authority } ) ;
79
79
let pca = this . _pcas . get ( pcasKey ) ;
80
80
if ( pca ) {
81
- this . _logger . debug ( clientId , authority , ' PublicClientApplicationManager cache hit' ) ;
81
+ this . _logger . debug ( `[getOrCreate] [ ${ clientId } ] [ ${ authority } ] PublicClientApplicationManager cache hit` ) ;
82
82
return pca ;
83
83
}
84
84
85
- this . _logger . debug ( clientId , authority , ' PublicClientApplicationManager cache miss, creating new PCA...' ) ;
85
+ this . _logger . debug ( `[getOrCreate] [ ${ clientId } ] [ ${ authority } ] PublicClientApplicationManager cache miss, creating new PCA...` ) ;
86
86
pca = new CachedPublicClientApplication ( clientId , authority , this . _globalMemento , this . _secretStorage , this . _accountChangeHandler , this . _logger ) ;
87
87
this . _pcas . set ( pcasKey , pca ) ;
88
88
await pca . initialize ( ) ;
89
89
await this . _storePublicClientApplications ( ) ;
90
- this . _logger . debug ( clientId , authority , ' PublicClientApplicationManager PCA created' ) ;
90
+ this . _logger . debug ( `[getOrCreate] [ ${ clientId } ] [ ${ authority } ] PublicClientApplicationManager PCA created` ) ;
91
91
return pca ;
92
92
}
93
93
@@ -103,24 +103,24 @@ export class CachedPublicClientApplicationManager implements ICachedPublicClient
103
103
return ;
104
104
}
105
105
106
- this . _logger . debug ( ' PublicClientApplicationManager secret storage change:' , e . key ) ;
106
+ this . _logger . debug ( `[handleSecretStorageChange] PublicClientApplicationManager secret storage change: ${ e . key } ` ) ;
107
107
const result = await this . _secretStorage . get ( e . key ) ;
108
108
const pcasKey = e . key . split ( _keyPrefix ) [ 1 ] ;
109
109
110
110
// If the cache was deleted, or the PCA has zero accounts left, remove the PCA
111
111
if ( ! result || this . _pcas . get ( pcasKey ) ?. accounts . length === 0 ) {
112
- this . _logger . debug ( ' PublicClientApplicationManager removing PCA:' , pcasKey ) ;
112
+ this . _logger . debug ( `[handleSecretStorageChange] PublicClientApplicationManager removing PCA: ${ pcasKey } ` ) ;
113
113
this . _pcas . delete ( pcasKey ) ;
114
114
await this . _storePublicClientApplications ( ) ;
115
- this . _logger . debug ( ' PublicClientApplicationManager PCA removed:' , pcasKey ) ;
115
+ this . _logger . debug ( `[handleSecretStorageChange] PublicClientApplicationManager PCA removed: ${ pcasKey } ` ) ;
116
116
return ;
117
117
}
118
118
119
119
// Load the PCA in memory if it's not already loaded
120
120
const { clientId, authority } = JSON . parse ( pcasKey ) as IPublicClientApplicationInfo ;
121
- this . _logger . debug ( ' PublicClientApplicationManager loading PCA:' , pcasKey ) ;
121
+ this . _logger . debug ( `[handleSecretStorageChange] PublicClientApplicationManager loading PCA: ${ pcasKey } ` ) ;
122
122
await this . getOrCreate ( clientId , authority ) ;
123
- this . _logger . debug ( ' PublicClientApplicationManager PCA loaded:' , pcasKey ) ;
123
+ this . _logger . debug ( `[handleSecretStorageChange] PublicClientApplicationManager PCA loaded: ${ pcasKey } ` ) ;
124
124
}
125
125
126
126
private async _storePublicClientApplications ( ) {
@@ -134,8 +134,6 @@ export class CachedPublicClientApplicationManager implements ICachedPublicClient
134
134
class CachedPublicClientApplication implements ICachedPublicClientApplication {
135
135
private _pca : PublicClientApplication ;
136
136
137
- private readonly _refreshDelayer = new DelayerByKey < any > ( ) ;
138
-
139
137
private _accounts : AccountInfo [ ] = [ ] ;
140
138
private readonly _disposable : Disposable ;
141
139
@@ -149,6 +147,7 @@ class CachedPublicClientApplication implements ICachedPublicClientApplication {
149
147
auth : { clientId : this . _clientId , authority : this . _authority } ,
150
148
system : {
151
149
loggerOptions : {
150
+ correlationId : `${ this . _clientId } ] [${ this . _authority } ` ,
152
151
loggerCallback : ( level , message , containsPii ) => this . _loggerOptions . loggerCallback ( level , message , containsPii ) ,
153
152
}
154
153
} ,
@@ -191,27 +190,24 @@ class CachedPublicClientApplication implements ICachedPublicClientApplication {
191
190
}
192
191
193
192
async acquireTokenSilent ( request : SilentFlowRequest ) : Promise < AuthenticationResult > {
193
+ this . _logger . debug ( `[acquireTokenSilent] [${ this . _clientId } ] [${ this . _authority } ] [${ request . scopes . join ( ' ' ) } ]` ) ;
194
194
const result = await this . _pca . acquireTokenSilent ( request ) ;
195
- this . _setupRefresh ( result ) ;
196
195
if ( result . account && ! result . fromCache ) {
197
196
this . _accountChangeHandler ( { added : [ ] , changed : [ result . account ] , deleted : [ ] } ) ;
198
197
}
199
198
return result ;
200
199
}
201
200
202
201
async acquireTokenInteractive ( request : InteractiveRequest ) : Promise < AuthenticationResult > {
202
+ this . _logger . debug ( `[acquireTokenInteractive] [${ this . _clientId } ] [${ this . _authority } ] [${ request . scopes ?. join ( ' ' ) } ] loopbackClientOverride: ${ request . loopbackClient ? 'true' : 'false' } ` ) ;
203
203
return await window . withProgress (
204
204
{
205
205
location : ProgressLocation . Notification ,
206
206
cancellable : true ,
207
207
title : l10n . t ( 'Signing in to Microsoft...' )
208
208
} ,
209
209
( _process , token ) => raceCancellationAndTimeoutError (
210
- ( async ( ) => {
211
- const result = await this . _pca . acquireTokenInteractive ( request ) ;
212
- this . _setupRefresh ( result ) ;
213
- return result ;
214
- } ) ( ) ,
210
+ this . _pca . acquireTokenInteractive ( request ) ,
215
211
token ,
216
212
1000 * 60 * 5
217
213
) , // 5 minutes
@@ -227,38 +223,20 @@ class CachedPublicClientApplication implements ICachedPublicClientApplication {
227
223
return this . _secretStorageCachePlugin . onDidChange ( ( ) => this . _update ( ) ) ;
228
224
}
229
225
230
- private _setupRefresh ( result : AuthenticationResult ) {
231
- const on = result . refreshOn || result . expiresOn ;
232
- if ( ! result . account || ! on ) {
233
- return ;
234
- }
235
-
236
- const account = result . account ;
237
- const scopes = result . scopes ;
238
- const timeToRefresh = on . getTime ( ) - Date . now ( ) - 5 * 60 * 1000 ; // 5 minutes before expiry
239
- const key = JSON . stringify ( { accountId : account . homeAccountId , scopes } ) ;
240
- this . _refreshDelayer . trigger (
241
- key ,
242
- // This may need the redirectUri when we switch to the broker
243
- ( ) => this . acquireTokenSilent ( { account, scopes, redirectUri : undefined , forceRefresh : true } ) ,
244
- timeToRefresh > 0 ? timeToRefresh : 0
245
- ) ;
246
- }
247
-
248
226
private async _update ( ) {
249
227
const before = this . _accounts ;
250
- this . _logger . debug ( this . _clientId , this . _authority , ' CachedPublicClientApplication update before:' , before . length ) ;
228
+ this . _logger . debug ( `[update] [ ${ this . _clientId } ] [ ${ this . _authority } ] CachedPublicClientApplication update before: ${ before . length } ` ) ;
251
229
// Dates are stored as strings in the memento
252
230
const lastRemovalDate = this . _globalMemento . get < string > ( `lastRemoval:${ this . _clientId } :${ this . _authority } ` ) ;
253
231
if ( lastRemovalDate && this . _lastCreated && Date . parse ( lastRemovalDate ) > this . _lastCreated . getTime ( ) ) {
254
- this . _logger . debug ( this . _clientId , this . _authority , ' CachedPublicClientApplication removal detected... recreating PCA...' ) ;
232
+ this . _logger . debug ( `[update] [ ${ this . _clientId } ] [ ${ this . _authority } ] CachedPublicClientApplication removal detected... recreating PCA...` ) ;
255
233
this . _pca = new PublicClientApplication ( this . _config ) ;
256
234
this . _lastCreated = new Date ( ) ;
257
235
}
258
236
259
237
const after = await this . _pca . getAllAccounts ( ) ;
260
238
this . _accounts = after ;
261
- this . _logger . debug ( this . _clientId , this . _authority , ' CachedPublicClientApplication update after:' , after . length ) ;
239
+ this . _logger . debug ( `[update] [ ${ this . _clientId } ] [ ${ this . _authority } ] CachedPublicClientApplication update after: ${ after . length } ` ) ;
262
240
263
241
const beforeSet = new Set ( before . map ( b => b . homeAccountId ) ) ;
264
242
const afterSet = new Set ( after . map ( a => a . homeAccountId ) ) ;
@@ -267,22 +245,8 @@ class CachedPublicClientApplication implements ICachedPublicClientApplication {
267
245
const deleted = before . filter ( b => ! afterSet . has ( b . homeAccountId ) ) ;
268
246
if ( added . length > 0 || deleted . length > 0 ) {
269
247
this . _accountChangeHandler ( { added, changed : [ ] , deleted } ) ;
270
- this . _logger . debug ( this . _clientId , this . _authority , ' CachedPublicClientApplication accounts changed. added, deleted:' , added . length , deleted . length ) ;
248
+ this . _logger . debug ( `[update] [ ${ this . _clientId } ] [ ${ this . _authority } ] CachedPublicClientApplication accounts changed. added: ${ added . length } , deleted: ${ deleted . length } ` ) ;
271
249
}
272
- this . _logger . debug ( this . _clientId , this . _authority , 'CachedPublicClientApplication update complete' ) ;
273
- }
274
- }
275
-
276
- class DelayerByKey < T > {
277
- private _delayers = new Map < string , Delayer < T > > ( ) ;
278
-
279
- trigger ( key : string , fn : ( ) => Promise < T > , delay : number ) : Promise < T > {
280
- let delayer = this . _delayers . get ( key ) ;
281
- if ( ! delayer ) {
282
- delayer = new Delayer < T > ( delay ) ;
283
- this . _delayers . set ( key , delayer ) ;
284
- }
285
-
286
- return delayer . trigger ( fn , delay ) ;
250
+ this . _logger . debug ( `[update] [${ this . _clientId } ] [${ this . _authority } ] CachedPublicClientApplication update complete` ) ;
287
251
}
288
252
}
0 commit comments