@@ -11,6 +11,7 @@ import type { AzureAuthentication } from './AzureAuthentication';
11
11
import type { AzureSubscription , SubscriptionId , TenantId } from './AzureSubscription' ;
12
12
import type { AzureSubscriptionProvider } from './AzureSubscriptionProvider' ;
13
13
import { NotSignedInError } from './NotSignedInError' ;
14
+ import { getSessionFromVSCode } from './getSessionFromVSCode' ;
14
15
import { getConfiguredAuthProviderId , getConfiguredAzureEnv } from './utils/configuredAzureEnv' ;
15
16
16
17
const EventDebounce = 5 * 1000 ; // 5 seconds
@@ -131,7 +132,7 @@ export class VSCodeAzureSubscriptionProvider extends vscode.Disposable implement
131
132
* @returns True if the user is signed in, false otherwise.
132
133
*/
133
134
public async isSignedIn ( tenantId ?: string ) : Promise < boolean > {
134
- const session = await vscode . authentication . getSession ( getConfiguredAuthProviderId ( ) , this . getScopes ( [ ] , tenantId ) , { createIfNone : false , silent : true } ) ;
135
+ const session = await getSessionFromVSCode ( [ ] , tenantId , { createIfNone : false , silent : true } ) ;
135
136
return ! ! session ;
136
137
}
137
138
@@ -143,7 +144,7 @@ export class VSCodeAzureSubscriptionProvider extends vscode.Disposable implement
143
144
* @returns True if the user is signed in, false otherwise.
144
145
*/
145
146
public async signIn ( tenantId ?: string ) : Promise < boolean > {
146
- const session = await vscode . authentication . getSession ( getConfiguredAuthProviderId ( ) , this . getScopes ( [ ] , tenantId ) , { createIfNone : true , clearSessionPreference : true } ) ;
147
+ const session = await getSessionFromVSCode ( [ ] , tenantId , { createIfNone : true , clearSessionPreference : true } ) ;
147
148
return ! ! session ;
148
149
}
149
150
@@ -237,8 +238,8 @@ export class VSCodeAzureSubscriptionProvider extends vscode.Disposable implement
237
238
private async getSubscriptionClient ( tenantId ?: string ) : Promise < { client : SubscriptionClient , credential : TokenCredential , authentication : AzureAuthentication } > {
238
239
const armSubs = await import ( '@azure/arm-subscriptions' ) ;
239
240
240
- const getSession = async ( scopes ?: string [ ] ) : Promise < vscode . AuthenticationSession > => {
241
- const session = await vscode . authentication . getSession ( getConfiguredAuthProviderId ( ) , this . getScopes ( scopes , tenantId ) , { createIfNone : false , silent : true } ) ;
241
+ const getSession = async ( scopes ?: string [ ] | string ) : Promise < vscode . AuthenticationSession > => {
242
+ const session = await getSessionFromVSCode ( scopes , tenantId , { createIfNone : false , silent : true } ) ;
242
243
if ( ! session ) {
243
244
throw new NotSignedInError ( ) ;
244
245
}
@@ -248,7 +249,7 @@ export class VSCodeAzureSubscriptionProvider extends vscode.Disposable implement
248
249
const credential : TokenCredential = {
249
250
getToken : async ( scopes ) => {
250
251
return {
251
- token : ( await getSession ( this . getScopes ( scopes , tenantId ) ) ) . accessToken ,
252
+ token : ( await getSession ( scopes ) ) . accessToken ,
252
253
expiresOnTimestamp : 0
253
254
} ;
254
255
}
@@ -258,58 +259,18 @@ export class VSCodeAzureSubscriptionProvider extends vscode.Disposable implement
258
259
client : new armSubs . SubscriptionClient ( credential ) ,
259
260
credential : credential ,
260
261
authentication : {
261
- getSession
262
+ getSession,
262
263
}
263
264
} ;
264
265
}
265
266
266
267
private async getToken ( tenantId ?: string ) : Promise < string > {
267
- const session = await vscode . authentication . getSession ( getConfiguredAuthProviderId ( ) , this . getScopes ( [ ] , tenantId ) , { createIfNone : false , silent : true } ) ;
268
+ const session = await getSessionFromVSCode ( [ ] , tenantId , { createIfNone : false , silent : true } )
268
269
269
270
if ( ! session ) {
270
271
throw new NotSignedInError ( ) ;
271
272
}
272
273
273
274
return session . accessToken ;
274
275
}
275
-
276
- /**
277
- * Gets a normalized list of scopes. If no scopes are provided, the return value of {@link getDefaultScope} is used.
278
- *
279
- * Only supports top-level resource scopes (e.g. http://management.azure.com, http://storage.azure.com) or .default scopes.
280
- *
281
- * All resources/scopes will be normalized to the `.default` scope for each resource.
282
- *
283
- * @param scopes An input scope string, list, or undefined
284
- * @param tenantId (Optional) The tenant ID, will be added to the scopes
285
- */
286
- private getScopes ( scopes : string | string [ ] | undefined , tenantId ?: string ) : string [ ] {
287
- if ( scopes === undefined || scopes === "" || scopes . length === 0 ) {
288
- scopes = this . getDefaultScope ( ) ;
289
- }
290
- const arrScopes = ( Array . isArray ( scopes ) ? scopes : [ scopes ] )
291
- . map ( ( scope ) => {
292
- if ( scope . endsWith ( '.default' ) ) {
293
- return scope ;
294
- } else {
295
- return `${ scope } .default` ;
296
- }
297
- } ) ;
298
-
299
- const scopeSet = new Set < string > ( arrScopes ) ;
300
- if ( tenantId ) {
301
- scopeSet . add ( `VSCODE_TENANT:${ tenantId } ` ) ;
302
- }
303
- return Array . from ( scopeSet ) ;
304
- }
305
-
306
- /**
307
- * Gets the default Azure scopes required for resource management,
308
- * depending on the configured endpoint
309
- *
310
- * @returns The default Azure scopes required
311
- */
312
- private getDefaultScope ( ) : string {
313
- return `${ getConfiguredAzureEnv ( ) . resourceManagerEndpointUrl } .default` ;
314
- }
315
276
}
0 commit comments