@@ -39,6 +39,7 @@ import { getEnvironmentSpecificMemento } from '../../shared/utilities/mementos'
39
39
import { getCacheDir , getFlareCacheFileName , getRegistrationCacheFile , getTokenCacheFile } from '../../auth/sso/cache'
40
40
import { notifySelectDeveloperProfile } from '../region/utils'
41
41
import { once } from '../../shared/utilities/functionUtils'
42
+ import { CancellationTokenSource , SsoTokenSourceKind } from '@aws/language-server-runtimes/server-interface'
42
43
43
44
const localize = nls . loadMessageBundle ( )
44
45
@@ -64,6 +65,8 @@ export interface IAuthProvider {
64
65
*/
65
66
export class AuthUtil implements IAuthProvider {
66
67
public readonly profileName = VSCODE_EXTENSION_ID . amazonq
68
+ protected logger = getLogger ( 'amazonqAuth' )
69
+
67
70
public readonly regionProfileManager : RegionProfileManager
68
71
69
72
// IAM login currently not supported
@@ -277,7 +280,7 @@ export class AuthUtil implements IAuthProvider {
277
280
}
278
281
279
282
private async cacheChangedHandler ( event : cacheChangedEvent ) {
280
- getLogger ( ) . debug ( `Auth: Cache change event received: ${ event } ` )
283
+ this . logger . debug ( `Cache change event received: ${ event } ` )
281
284
if ( event === 'delete' ) {
282
285
await this . logout ( )
283
286
} else if ( event === 'create' ) {
@@ -291,7 +294,7 @@ export class AuthUtil implements IAuthProvider {
291
294
await this . lspAuth . updateBearerToken ( params ! )
292
295
return
293
296
} else {
294
- getLogger ( ) . info ( `codewhisperer: connection changed to ${ e . state } ` )
297
+ this . logger . info ( `codewhisperer: connection changed to ${ e . state } ` )
295
298
await this . refreshState ( e . state )
296
299
}
297
300
}
@@ -402,58 +405,77 @@ export class AuthUtil implements IAuthProvider {
402
405
403
406
if ( ! profiles ) {
404
407
return
405
- } else {
406
- getLogger ( ) . info ( `codewhisperer: checking for old SSO connections` )
407
- for ( const [ id , p ] of Object . entries ( profiles ) ) {
408
- if ( p . type === 'sso' && hasExactScopes ( p . scopes ?? [ ] , amazonQScopes ) ) {
409
- toImport = p
410
- profileId = id
411
- if ( p . metadata . connectionState === 'valid' ) {
412
- break
413
- }
414
- }
415
- }
408
+ }
416
409
417
- if ( toImport && profileId ) {
418
- getLogger ( ) . info ( `codewhisperer: migrating SSO connection to LSP identity server...` )
410
+ try {
411
+ // Try go get token from LSP auth. If available, skip migration and delete old auth profile
412
+ const token = await this . lspAuth . getSsoToken (
413
+ {
414
+ kind : SsoTokenSourceKind . IamIdentityCenter ,
415
+ profileName : this . profileName ,
416
+ } ,
417
+ false ,
418
+ new CancellationTokenSource ( ) . token
419
+ )
420
+ if ( token ) {
421
+ this . logger . info ( 'existing LSP auth connection found. Skipping migration' )
422
+ await memento . update ( key , undefined )
423
+ return
424
+ }
425
+ } catch {
426
+ this . logger . info ( 'unable to get token from LSP auth, proceeding migration' )
427
+ }
419
428
420
- const registrationKey = {
421
- startUrl : toImport . startUrl ,
422
- region : toImport . ssoRegion ,
423
- scopes : amazonQScopes ,
429
+ this . logger . info ( 'checking for old SSO connections' )
430
+ for ( const [ id , p ] of Object . entries ( profiles ) ) {
431
+ if ( p . type === 'sso' && hasExactScopes ( p . scopes ?? [ ] , amazonQScopes ) ) {
432
+ toImport = p
433
+ profileId = id
434
+ if ( p . metadata . connectionState === 'valid' ) {
435
+ break
424
436
}
437
+ }
438
+ }
425
439
426
- await this . session . updateProfile ( registrationKey )
440
+ if ( toImport && profileId ) {
441
+ this . logger . info ( 'migrating SSO connection to LSP identity server...' )
427
442
428
- const cacheDir = getCacheDir ( )
443
+ const registrationKey = {
444
+ startUrl : toImport . startUrl ,
445
+ region : toImport . ssoRegion ,
446
+ scopes : amazonQScopes ,
447
+ }
429
448
430
- const fromRegistrationFile = getRegistrationCacheFile ( cacheDir , registrationKey )
431
- const toRegistrationFile = path . join (
432
- cacheDir ,
433
- getFlareCacheFileName (
434
- JSON . stringify ( {
435
- region : toImport . ssoRegion ,
436
- startUrl : toImport . startUrl ,
437
- tool : clientName ,
438
- } )
439
- )
440
- )
449
+ await this . session . updateProfile ( registrationKey )
441
450
442
- const fromTokenFile = getTokenCacheFile ( cacheDir , profileId )
443
- const toTokenFile = path . join ( cacheDir , getFlareCacheFileName ( this . profileName ) )
451
+ const cacheDir = getCacheDir ( )
444
452
445
- try {
446
- await fs . rename ( fromRegistrationFile , toRegistrationFile )
447
- await fs . rename ( fromTokenFile , toTokenFile )
448
- getLogger ( ) . debug ( 'Successfully renamed registration and token files' )
449
- } catch ( err ) {
450
- getLogger ( ) . error ( `Failed to rename files during migration: ${ err } ` )
451
- throw err
452
- }
453
-
454
- await memento . update ( key , undefined )
455
- getLogger ( ) . info ( `codewhisperer: successfully migrated SSO connection to LSP identity server` )
453
+ const fromRegistrationFile = getRegistrationCacheFile ( cacheDir , registrationKey )
454
+ const toRegistrationFile = path . join (
455
+ cacheDir ,
456
+ getFlareCacheFileName (
457
+ JSON . stringify ( {
458
+ region : toImport . ssoRegion ,
459
+ startUrl : toImport . startUrl ,
460
+ tool : clientName ,
461
+ } )
462
+ )
463
+ )
464
+
465
+ const fromTokenFile = getTokenCacheFile ( cacheDir , profileId )
466
+ const toTokenFile = path . join ( cacheDir , getFlareCacheFileName ( this . profileName ) )
467
+
468
+ try {
469
+ await fs . rename ( fromRegistrationFile , toRegistrationFile )
470
+ await fs . rename ( fromTokenFile , toTokenFile )
471
+ this . logger . debug ( 'Successfully renamed registration and token files' )
472
+ } catch ( err ) {
473
+ this . logger . error ( `Failed to rename files during migration: ${ err } ` )
474
+ throw err
456
475
}
476
+
477
+ this . logger . info ( 'successfully migrated SSO connection to LSP identity server' )
478
+ await memento . update ( key , undefined )
457
479
}
458
480
}
459
481
}
0 commit comments