@@ -43,7 +43,12 @@ import {
4343 ConnectionMetadata ,
4444 getConnectionMetadataRequestType ,
4545 iamCredentialsUpdateRequestType ,
46+ Profile ,
47+ SsoSession ,
4648 IamSession ,
49+ invalidateStsCredentialRequestType ,
50+ InvalidateStsCredentialParams ,
51+ InvalidateStsCredentialResult ,
4752} from '@aws/language-server-runtimes/protocol'
4853import { LanguageClient } from 'vscode-languageclient'
4954import { getLogger } from '../shared/logger/logger'
@@ -82,13 +87,6 @@ export type Login = SsoLogin | IamLogin
8287
8388export type TokenSource = IamIdentityCenterSsoTokenSource | AwsBuilderIdSsoTokenSource
8489
85- /**
86- * Interface for authentication management
87- */
88- export interface BaseLogin {
89- readonly loginType : LoginType
90- }
91-
9290/**
9391 * Handles auth requests to the Identity Server in the Amazon Q LSP.
9492 */
@@ -193,9 +191,10 @@ export class LanguageClientAuth {
193191 const ssoSession = profile ?. settings ?. sso_session
194192 ? response . ssoSessions . find ( ( session ) => session . name === profile ! . settings ! . sso_session )
195193 : undefined
196- const iamSession = profile ?. settings ?. sso_session
197- ? response . iamSessions ?. find ( ( session ) => session . name === profile ! . settings ! . sso_session )
198- : undefined
194+ const iamSession = undefined
195+ // const iamSession = profile?.settings?.sso_session
196+ // ? response.iamSessions?.find((session) => session.name === profile!.settings!.sso_session)
197+ // : undefined
199198
200199 return { profile, ssoSession, iamSession }
201200 }
@@ -222,6 +221,12 @@ export class LanguageClientAuth {
222221 } satisfies InvalidateSsoTokenParams ) as Promise < InvalidateSsoTokenResult >
223222 }
224223
224+ invalidateStsCredential ( tokenId : string ) {
225+ return this . client . sendRequest ( invalidateStsCredentialRequestType . method , {
226+ stsCredentialId : tokenId ,
227+ } satisfies InvalidateStsCredentialParams ) as Promise < InvalidateStsCredentialResult >
228+ }
229+
225230 registerSsoTokenChangedHandler ( ssoTokenChangedHandler : ( params : SsoTokenChangedParams ) => any ) {
226231 this . client . onNotification ( ssoTokenChangedRequestType . method , ssoTokenChangedHandler )
227232 }
@@ -237,30 +242,83 @@ export class LanguageClientAuth {
237242}
238243
239244/**
240- * Manages an SSO connection.
245+ * Abstract class for connection management
241246 */
242- export class SsoLogin implements BaseLogin {
243- readonly loginType = LoginTypes . SSO
244-
245- // Cached information from the identity server for easy reference
246- private ssoTokenId : string | undefined
247- private connectionState : AuthState = 'notConnected'
248- private _data : { startUrl ?: string ; region ?: string ; accessKey ?: string ; secretKey ?: string } | undefined
249-
250- private cancellationToken : CancellationTokenSource | undefined
247+ export abstract class BaseLogin {
248+ protected connectionState : AuthState = 'notConnected'
249+ protected cancellationToken : CancellationTokenSource | undefined
250+ protected _data : { startUrl ?: string ; region ?: string ; accessKey ?: string ; secretKey ?: string } | undefined
251251
252252 constructor (
253253 public readonly profileName : string ,
254- private readonly lspAuth : LanguageClientAuth ,
255- private readonly eventEmitter : vscode . EventEmitter < AuthStateEvent >
256- ) {
257- lspAuth . registerSsoTokenChangedHandler ( ( params : SsoTokenChangedParams ) => this . ssoTokenChangedHandler ( params ) )
258- }
254+ protected readonly lspAuth : LanguageClientAuth ,
255+ protected readonly eventEmitter : vscode . EventEmitter < AuthStateEvent >
256+ ) { }
257+
258+ abstract login ( opts : any ) : Promise < GetSsoTokenResult | GetStsCredentialResult | undefined >
259+ abstract reauthenticate ( ) : Promise < GetSsoTokenResult | GetStsCredentialResult | undefined >
260+ abstract logout ( ) : void
261+ abstract restore ( ) : void
262+ abstract getToken ( ) : Promise < { token : string ; updateCredentialsParams : UpdateCredentialsParams } >
259263
260264 get data ( ) {
261265 return this . _data
262266 }
263267
268+ /**
269+ * Cancels running active login flows.
270+ */
271+ cancelLogin ( ) {
272+ this . cancellationToken ?. cancel ( )
273+ this . cancellationToken ?. dispose ( )
274+ this . cancellationToken = undefined
275+ }
276+
277+ /**
278+ * Gets the profile and session associated with a profile name
279+ */
280+ async getProfile ( ) : Promise < {
281+ profile : Profile | undefined
282+ ssoSession : SsoSession | undefined
283+ iamSession : IamSession | undefined
284+ } > {
285+ return await this . lspAuth . getProfile ( this . profileName )
286+ }
287+
288+ /**
289+ * Gets the current connection state
290+ */
291+ getConnectionState ( ) : AuthState {
292+ return this . connectionState
293+ }
294+
295+ /**
296+ * Sets the connection state and fires an event if the state changed
297+ */
298+ protected updateConnectionState ( state : AuthState ) {
299+ const oldState = this . connectionState
300+ const newState = state
301+
302+ this . connectionState = newState
303+
304+ if ( oldState !== newState ) {
305+ this . eventEmitter . fire ( { id : this . profileName , state : this . connectionState } )
306+ }
307+ }
308+ }
309+
310+ /**
311+ * Manages an SSO connection.
312+ */
313+ export class SsoLogin extends BaseLogin {
314+ // Cached information from the identity server for easy reference
315+ private ssoTokenId : string | undefined
316+
317+ constructor ( profileName : string , lspAuth : LanguageClientAuth , eventEmitter : vscode . EventEmitter < AuthStateEvent > ) {
318+ super ( profileName , lspAuth , eventEmitter )
319+ lspAuth . registerSsoTokenChangedHandler ( ( params : SsoTokenChangedParams ) => this . ssoTokenChangedHandler ( params ) )
320+ }
321+
264322 async login ( opts : { startUrl : string ; region : string ; scopes : string [ ] } ) {
265323 await this . updateProfile ( opts )
266324 return this . _getSsoToken ( true )
@@ -282,10 +340,6 @@ export class SsoLogin implements BaseLogin {
282340 // TODO: DeleteProfile api in Identity Service (this doesn't exist yet)
283341 }
284342
285- async getProfile ( ) {
286- return await this . lspAuth . getProfile ( this . profileName )
287- }
288-
289343 async updateProfile ( opts : { startUrl : string ; region : string ; scopes : string [ ] } ) {
290344 await this . lspAuth . updateSsoProfile ( this . profileName , opts . startUrl , opts . region , opts . scopes )
291345 this . _data = {
@@ -314,15 +368,6 @@ export class SsoLogin implements BaseLogin {
314368 }
315369 }
316370
317- /**
318- * Cancels running active login flows.
319- */
320- cancelLogin ( ) {
321- this . cancellationToken ?. cancel ( )
322- this . cancellationToken ?. dispose ( )
323- this . cancellationToken = undefined
324- }
325-
326371 /**
327372 * Returns both the decrypted access token and the payload to send to the `updateCredentials` LSP API
328373 * with encrypted token
@@ -390,21 +435,6 @@ export class SsoLogin implements BaseLogin {
390435 return response
391436 }
392437
393- getConnectionState ( ) {
394- return this . connectionState
395- }
396-
397- private updateConnectionState ( state : AuthState ) {
398- const oldState = this . connectionState
399- const newState = state
400-
401- this . connectionState = newState
402-
403- if ( oldState !== newState ) {
404- this . eventEmitter . fire ( { id : this . profileName , state : this . connectionState } )
405- }
406- }
407-
408438 private ssoTokenChangedHandler ( params : SsoTokenChangedParams ) {
409439 if ( params . ssoTokenId === this . ssoTokenId ) {
410440 if ( params . kind === CredentialChangedKind . Expired ) {
@@ -420,30 +450,17 @@ export class SsoLogin implements BaseLogin {
420450/**
421451 * Manages an IAM credentials connection.
422452 */
423- export class IamLogin implements BaseLogin {
424- readonly loginType = LoginTypes . IAM
425-
453+ export class IamLogin extends BaseLogin {
426454 // Cached information from the identity server for easy reference
427455 private stsCredentialId : string | undefined
428- private connectionState : AuthState = 'notConnected'
429- private _data : { startUrl ?: string ; region ?: string ; accessKey ?: string ; secretKey ?: string } | undefined
430-
431- private cancellationToken : CancellationTokenSource | undefined
432456
433- constructor (
434- public readonly profileName : string ,
435- private readonly lspAuth : LanguageClientAuth ,
436- private readonly eventEmitter : vscode . EventEmitter < AuthStateEvent >
437- ) {
457+ constructor ( profileName : string , lspAuth : LanguageClientAuth , eventEmitter : vscode . EventEmitter < AuthStateEvent > ) {
458+ super ( profileName , lspAuth , eventEmitter )
438459 lspAuth . registerStsCredentialChangedHandler ( ( params : StsCredentialChangedParams ) =>
439460 this . stsCredentialChangedHandler ( params )
440461 )
441462 }
442463
443- get data ( ) {
444- return this . _data
445- }
446-
447464 async login ( opts : { accessKey : string ; secretKey : string } ) {
448465 await this . updateProfile ( opts )
449466 return this . _getStsCredential ( true )
@@ -458,17 +475,13 @@ export class IamLogin implements BaseLogin {
458475
459476 async logout ( ) {
460477 if ( this . stsCredentialId ) {
461- await this . lspAuth . invalidateSsoToken ( this . stsCredentialId )
478+ await this . lspAuth . invalidateStsCredential ( this . stsCredentialId )
462479 }
463480 this . updateConnectionState ( 'notConnected' )
464481 this . _data = undefined
465482 // TODO: DeleteProfile api in Identity Service (this doesn't exist yet)
466483 }
467484
468- async getProfile ( ) {
469- return await this . lspAuth . getProfile ( this . profileName )
470- }
471-
472485 async updateProfile ( opts : { accessKey : string ; secretKey : string } ) {
473486 await this . lspAuth . updateIamProfile ( this . profileName , opts . accessKey , opts . secretKey )
474487 this . _data = {
@@ -496,15 +509,6 @@ export class IamLogin implements BaseLogin {
496509 }
497510 }
498511
499- /**
500- * Cancels running active login flows.
501- */
502- cancelLogin ( ) {
503- this . cancellationToken ?. cancel ( )
504- this . cancellationToken ?. dispose ( )
505- this . cancellationToken = undefined
506- }
507-
508512 /**
509513 * Returns both the decrypted access token and the payload to send to the `updateCredentials` LSP API
510514 * with encrypted token
@@ -560,21 +564,6 @@ export class IamLogin implements BaseLogin {
560564 return response
561565 }
562566
563- getConnectionState ( ) {
564- return this . connectionState
565- }
566-
567- private updateConnectionState ( state : AuthState ) {
568- const oldState = this . connectionState
569- const newState = state
570-
571- this . connectionState = newState
572-
573- if ( oldState !== newState ) {
574- this . eventEmitter . fire ( { id : this . profileName , state : this . connectionState } )
575- }
576- }
577-
578567 private stsCredentialChangedHandler ( params : StsCredentialChangedParams ) {
579568 if ( params . stsCredentialId === this . stsCredentialId ) {
580569 if ( params . kind === CredentialChangedKind . Expired ) {
0 commit comments