Skip to content

Commit ef74cea

Browse files
authored
refactor(auth): remove sharing of direct sessions across extensions (#5169)
* refactor(auth): remove sharing of direct sessions across extensions **Separate sessions commit** Instead of sharing sessions directly (e.g. with tokens, logout logs out of both extensions, etc), we will just share SSO parameters across extensions. The auth can re-use starturl and region but will build its own connection. Existing connections UI remains for this purpose. - Remove unused auth and API code. This includes special handling logic that imports direct sessions from the other extension. - Builder ID will not be displayed as an existing connection because the parameters for this are already known. Users can just log in with buidler ID normally. - Misc fixes in comments, log statements. * refactor: rename "existing" to "imported" logins * feat(auth): re-add checks for existing start urls in same extension
1 parent 0e7b9d4 commit ef74cea

File tree

14 files changed

+73
-421
lines changed

14 files changed

+73
-421
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Removal",
3+
"description": "Auth: No longer share SSO sessions with AWS Toolkit."
4+
}

packages/amazonq/src/auth/util.ts

Lines changed: 0 additions & 68 deletions
This file was deleted.

packages/amazonq/src/extensionCommon.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
activate as activateCodeWhisperer,
1111
shutdown as shutdownCodeWhisperer,
1212
amazonQDismissedKey,
13+
AuthUtil,
1314
} from 'aws-core-vscode/codewhisperer'
1415
import {
1516
ExtContext,
@@ -25,11 +26,10 @@ import {
2526
getLogger,
2627
getMachineId,
2728
} from 'aws-core-vscode/shared'
28-
import { initializeAuth, CredentialsStore, LoginManager, AuthUtils } from 'aws-core-vscode/auth'
29+
import { initializeAuth, CredentialsStore, LoginManager, AuthUtils, SsoConnection } from 'aws-core-vscode/auth'
2930
import { CommonAuthWebview } from 'aws-core-vscode/login'
3031
import { VSCODE_EXTENSION_ID } from 'aws-core-vscode/utils'
3132
import { telemetry, ExtStartUpSources } from 'aws-core-vscode/telemetry'
32-
import { getAuthStatus } from './auth/util'
3333
import { makeEndpointsProvider, registerGenericCommands } from 'aws-core-vscode/common'
3434
import { registerCommands } from './commands'
3535

@@ -131,11 +131,11 @@ export async function activateAmazonQCommon(context: vscode.ExtensionContext, is
131131
telemetry.record({ source: ExtStartUpSources.reload })
132132
}
133133

134-
const { authStatus, authEnabledConnections, authScopes } = await getAuthStatus()
134+
const authState = (await AuthUtil.instance.getChatAuthState()).codewhispererChat
135135
telemetry.record({
136-
authStatus,
137-
authEnabledConnections,
138-
authScopes,
136+
authStatus: authState === 'connected' || authState === 'expired' ? authState : 'notConnected',
137+
authEnabledConnections: AuthUtils.getAuthFormIdsFromConnection(AuthUtil.instance.conn).join(','),
138+
authScopes: ((AuthUtil.instance.conn as SsoConnection)?.scopes ?? []).join(','),
139139
})
140140
})
141141
}

packages/core/src/auth/auth.ts

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -952,22 +952,7 @@ export class Auth implements AuthService, ConnectionManager {
952952
: `${localizedText.iamIdentityCenter} (${truncatedUrl})`
953953
}
954954

955-
// Used by Amazon Q to re-use connection from AWS Toolkit listConnection API response
956-
public async createConnectionFromApi(connection: AwsConnection) {
957-
getLogger().info(`Reusing connection ${connection.id}`)
958-
const profile = {
959-
type: connection.type,
960-
ssoRegion: connection.ssoRegion,
961-
scopes: connection.scopes,
962-
startUrl: connection.startUrl,
963-
} as SsoProfile
964-
const id = connection.id
965-
const storedProfile = await this.store.addProfile(id, profile)
966-
await this.updateConnectionState(id, connection.state)
967-
return this.getSsoConnection(id, storedProfile)
968-
}
969-
970-
// Used by AWS Toolkit to update connection status & scope when this connection is updated by Amazon Q
955+
// Used by AWS Toolkit to update connection status & scope when this connection is updated by another extension.
971956
// If such connection does not exist, create one with same id.
972957
// Otherwise, update its scope and/or state.
973958
public async setConnectionFromApi(connection: AwsConnection) {
@@ -999,35 +984,6 @@ export class Auth implements AuthService, ConnectionManager {
999984
}
1000985
}
1001986

1002-
// Used by Amazon Q to update connection status & scope when this connection is updated by AWS Toolkit
1003-
// do not create connection in Q for each change event from Toolkit
1004-
public async onConnectionUpdate(connection: AwsConnection) {
1005-
const conn = await this.getConnection({ id: connection.id })
1006-
if (conn) {
1007-
const profile = {
1008-
type: connection.type,
1009-
ssoRegion: connection.ssoRegion,
1010-
scopes: connection.scopes,
1011-
startUrl: connection.startUrl,
1012-
} as SsoProfile
1013-
await this.store.updateProfile(connection.id, profile)
1014-
1015-
await this.store.updateMetadata(connection.id, { connectionState: connection.state })
1016-
}
1017-
}
1018-
1019-
// Used by Amazon Q to delete connection status & scope when this deletion is made by AWS Toolkit
1020-
// NO event should be emitted from this deletion
1021-
// Do not actually perform the delete because toolkit has done the deletion
1022-
// Delete the momento states only.
1023-
public async onDeleteConnection(id: string) {
1024-
const profile = this.store.getProfile(id)
1025-
if (profile) {
1026-
await this.store.deleteProfile(id)
1027-
await this.store.setCurrentProfileId(undefined)
1028-
}
1029-
}
1030-
1031987
public declareConnectionFromApi(conn: Pick<AwsConnection, 'startUrl' | 'ssoRegion'>, source: string) {
1032988
getLogger().debug(`Declared connection '${conn.startUrl}' from ${source}.`)
1033989
this._declaredConnections[conn.startUrl] = {

packages/core/src/auth/secondaryAuth.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,6 @@ export class SecondaryAuth<T extends Connection = Connection> {
265265
return conn
266266
}
267267
}
268-
269-
// Used by Amazon Q to delete connection status & scope when this deletion is made by AWS Toolkit
270-
// NO event should be emitted from this deletion to avoid infinite loop
271-
public async onDeleteConnection(id: string) {
272-
await this.auth.onDeleteConnection(id)
273-
if (id === this.activeConnection?.id) {
274-
await this.memento.update(this.key, undefined)
275-
this.#savedConnection = undefined
276-
this.#activeConnection = undefined
277-
}
278-
}
279268
}
280269

281270
/**

packages/core/src/codecatalyst/auth.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -345,22 +345,6 @@ export class CodeCatalystAuthenticationProvider {
345345
}
346346
}
347347
}
348-
/**
349-
* Try to use an existing connection for CodeCatalyst Login
350-
*/
351-
public async tryUseConnection(conn: SsoConnection) {
352-
const connId = conn.id
353-
if (isValidCodeCatalystConnection(conn)) {
354-
getLogger().info(`auth: re-use connection from existing connection id ${connId}`)
355-
await this.secondaryAuth.useNewConnection(conn)
356-
await this.isConnectionOnboarded(conn, true)
357-
} else {
358-
getLogger().info(`auth: re-use(new scope) to connection from existing connection id ${connId}`)
359-
const newConn = await this.secondaryAuth.addScopes(conn, defaultScopes)
360-
await this.secondaryAuth.useNewConnection(newConn)
361-
await this.isConnectionOnboarded(newConn, true)
362-
}
363-
}
364348

365349
public async reauthenticate(conn: SsoConnection) {
366350
try {

packages/core/src/codewhisperer/util/authUtil.ts

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
scopesFeatureDev,
2525
scopesGumby,
2626
isIdcSsoConnection,
27-
AwsConnection,
2827
hasExactScopes,
2928
} from '../../auth/connection'
3029
import { getLogger } from '../../shared/logger'
@@ -151,26 +150,6 @@ export class AuthUtil {
151150
)
152151
}
153152

154-
/* Callback used by Amazon Q to delete connection status & scope when this deletion is made by AWS Toolkit
155-
** 1. NO event should be emitted from this deletion
156-
** 2. Should update the context key to update UX
157-
*/
158-
public async onDeleteConnection(id: string) {
159-
await this.secondaryAuth.onDeleteConnection(id)
160-
await this.setVscodeContextProps()
161-
await vscode.commands.executeCommand('aws.amazonq.refreshStatusBar')
162-
}
163-
164-
/* Callback used by Amazon Q to delete connection status & scope when this deletion is made by AWS Toolkit
165-
** 1. NO event should be emitted from this deletion
166-
** 2. Should update the context key to update UX
167-
*/
168-
public async onUpdateConnection(connection: AwsConnection) {
169-
await this.auth.onConnectionUpdate(connection)
170-
await this.setVscodeContextProps()
171-
await vscode.commands.executeCommand('aws.amazonq.refreshStatusBar')
172-
}
173-
174153
public reformatStartUrl(startUrl: string | undefined) {
175154
return !startUrl ? undefined : startUrl.replace(/[\/#]+$/g, '')
176155
}
@@ -433,25 +412,6 @@ export class AuthUtil {
433412

434413
return state
435414
}
436-
437-
/**
438-
* From the given connections, returns a connection that has some connection to Q.
439-
*
440-
* HACK: There is an edge case where we want to connect to the connection that only has
441-
* the old CW scopes, but not all Q scopes. So this function at the bare minimum returns
442-
* a connection if it has some CW scopes.
443-
*/
444-
findMinimalQConnection(connections: AwsConnection[]): AwsConnection | undefined {
445-
const hasQScopes = (c: AwsConnection) => codeWhispererCoreScopes.every(s => c.scopes?.includes(s))
446-
const score = (c: AwsConnection) => Number(hasQScopes(c)) * 10 + Number(c.state === 'valid')
447-
connections.sort(function (a, b) {
448-
return score(b) - score(a)
449-
})
450-
if (hasQScopes(connections[0])) {
451-
return connections[0]
452-
}
453-
return undefined
454-
}
455415
}
456416

457417
/**

0 commit comments

Comments
 (0)