Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/amazonq/src/app/amazonqScan/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Messenger } from './chat/controller/messenger/messenger'
import { UIMessageListener } from './chat/views/actions/uiMessageListener'
import { debounce } from 'lodash'
import { Commands, placeholder } from 'aws-core-vscode/shared'
import { auth2 } from 'aws-core-vscode/auth'

export function init(appContext: AmazonQAppInitContext) {
const scanChatControllerEventEmitters: ScanChatControllerEventEmitters = {
Expand Down Expand Up @@ -52,7 +53,7 @@ export function init(appContext: AmazonQAppInitContext) {
appContext.registerWebViewToAppMessagePublisher(new MessagePublisher<any>(scanChatUIInputEventEmitter), 'review')

const debouncedEvent = debounce(async () => {
const authenticated = (await AuthUtil.instance.getChatAuthState()).amazonQ === 'connected'
const authenticated = AuthUtil.instance.getAuthState() === 'connected'
let authenticatingSessionID = ''

if (authenticated) {
Expand All @@ -67,7 +68,7 @@ export function init(appContext: AmazonQAppInitContext) {
messenger.sendAuthenticationUpdate(authenticated, [authenticatingSessionID])
}, 500)

AuthUtil.instance.secondaryAuth.onDidChangeActiveConnection(() => {
AuthUtil.instance.onDidChangeConnectionState((e: auth2.AuthStateEvent) => {
return debouncedEvent()
})
AuthUtil.instance.regionProfileManager.onDidChangeRegionProfile(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class ScanController {
telemetry.amazonq_feedback.emit({
featureId: 'amazonQReview',
amazonqConversationId: this.sessionStorage.getSession().scanUuid,
credentialStartUrl: AuthUtil.instance.startUrl,
credentialStartUrl: AuthUtil.instance.connection?.startUrl,
interactionType: data.vote,
})
})
Expand All @@ -122,8 +122,8 @@ export class ScanController {
try {
getLogger().debug(`Q - Review: Session created with id: ${session.tabID}`)

const authState = await AuthUtil.instance.getChatAuthState()
if (authState.amazonQ !== 'connected') {
const authState = AuthUtil.instance.getAuthState()
if (authState !== 'connected') {
void this.messenger.sendAuthNeededExceptionMessage(authState, tabID)
session.isAuthenticating = true
return
Expand Down Expand Up @@ -161,8 +161,8 @@ export class ScanController {
return
}
// check that the session is authenticated
const authState = await AuthUtil.instance.getChatAuthState()
if (authState.amazonQ !== 'connected') {
const authState = AuthUtil.instance.getAuthState()
if (authState !== 'connected') {
void this.messenger.sendAuthNeededExceptionMessage(authState, message.tabID)
session.isAuthenticating = true
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import { AuthFollowUpType, AuthMessageDataMap } from 'aws-core-vscode/amazonq'
import {
FeatureAuthState,
SecurityScanError,
CodeWhispererConstants,
SecurityScanStep,
Expand All @@ -34,6 +33,7 @@ import {
import { i18n } from 'aws-core-vscode/shared'
import { ScanAction, scanProgressMessage } from '../../../models/constants'
import path from 'path'
import { auth2 } from 'aws-core-vscode/auth'

export type UnrecoverableErrorType = 'no-project-found' | 'no-open-file-found' | 'invalid-file-type'

Expand Down Expand Up @@ -78,19 +78,15 @@ export class Messenger {
this.dispatcher.sendUpdatePromptProgress(new UpdatePromptProgressMessage(tabID, progressField))
}

public async sendAuthNeededExceptionMessage(credentialState: FeatureAuthState, tabID: string) {
public async sendAuthNeededExceptionMessage(credentialState: auth2.AuthState, tabID: string) {
let authType: AuthFollowUpType = 'full-auth'
let message = AuthMessageDataMap[authType].message

switch (credentialState.amazonQ) {
case 'disconnected':
switch (credentialState) {
case 'notConnected':
authType = 'full-auth'
message = AuthMessageDataMap[authType].message
break
case 'unsupported':
authType = 'use-supported-auth'
message = AuthMessageDataMap[authType].message
break
case 'expired':
authType = 're-auth'
message = AuthMessageDataMap[authType].message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,8 @@ export class InlineChatProvider {

const tabID = triggerEvent.tabID

const credentialsState = await AuthUtil.instance.getChatAuthState()
if (
!(credentialsState.codewhispererChat === 'connected' && credentialsState.codewhispererCore === 'connected')
) {
const credentialsState = AuthUtil.instance.getAuthState()
if (credentialsState !== 'connected') {
const { message } = extractAuthFollowUp(credentialsState)
this.errorEmitter.fire()
throw new ToolkitError(message)
Expand Down
2 changes: 1 addition & 1 deletion packages/amazonq/src/lsp/activation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import vscode from 'vscode'
import { clientId, encryptionKey, startLanguageServer } from './client'
import { AmazonQLspInstaller } from './lspInstaller'
import { lspSetupStage, ToolkitError } from 'aws-core-vscode/shared'
import { lspSetupStage, ToolkitError, messages } from 'aws-core-vscode/shared'
import { AuthUtil } from 'aws-core-vscode/codewhisperer'
import { auth2 } from 'aws-core-vscode/auth'

Expand Down
2 changes: 1 addition & 1 deletion packages/amazonq/src/lsp/chat/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export function registerMessageListeners(

if (fullAuthTypes.includes(authType)) {
try {
await AuthUtil.instance.secondaryAuth.deleteConnection()
await AuthUtil.instance.logout()
} catch (e) {
languageClient.error(
`[VSCode Client] Failed to authenticate after AUTH_FOLLOW_UP_CLICKED: ${(e as Error).message}`
Expand Down
6 changes: 3 additions & 3 deletions packages/amazonq/test/e2e/amazonq/utils/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import { AuthUtil } from 'aws-core-vscode/codewhisperer'

export async function loginToIdC() {
const authState = await AuthUtil.instance.getChatAuthState()
const authState = AuthUtil.instance.getAuthState()
if (process.env['AWS_TOOLKIT_AUTOMATION'] === 'local') {
if (authState.amazonQ !== 'connected') {
if (authState !== 'connected') {
throw new Error('You will need to login manually before running tests.')
}
return
Expand All @@ -22,5 +22,5 @@ export async function loginToIdC() {
)
}

await AuthUtil.instance.connectToEnterpriseSso(startUrl, region)
await AuthUtil.instance.login(startUrl, region)
}
16 changes: 7 additions & 9 deletions packages/amazonq/test/unit/amazonq/lsp/chat/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { LanguageClient } from 'vscode-languageclient'
import { AuthUtil } from 'aws-core-vscode/codewhisperer'
import { registerMessageListeners } from '../../../../../src/lsp/chat/messages'
import { AmazonQChatViewProvider } from '../../../../../src/lsp/chat/webviewProvider'
import { secondaryAuth, authConnection, AuthFollowUpType } from 'aws-core-vscode/amazonq'
import { AuthFollowUpType } from 'aws-core-vscode/amazonq'
import { messages } from 'aws-core-vscode/shared'

describe('registerMessageListeners', () => {
Expand Down Expand Up @@ -49,7 +49,7 @@ describe('registerMessageListeners', () => {

describe('AUTH_FOLLOW_UP_CLICKED', () => {
let mockAuthUtil: AuthUtil
let deleteConnectionStub: sinon.SinonStub
let logoutStub: sinon.SinonStub
let reauthenticateStub: sinon.SinonStub

const authFollowUpClickedCommand = 'authFollowUpClicked'
Expand All @@ -75,14 +75,12 @@ describe('registerMessageListeners', () => {
}

beforeEach(() => {
deleteConnectionStub = sandbox.stub().resolves()
reauthenticateStub = sandbox.stub().resolves()
logoutStub = sandbox.stub().resolves()

mockAuthUtil = {
reauthenticate: reauthenticateStub,
secondaryAuth: {
deleteConnection: deleteConnectionStub,
} as unknown as secondaryAuth.SecondaryAuth<authConnection.Connection>,
logout: logoutStub,
} as unknown as AuthUtil

sandbox.replaceGetter(AuthUtil, 'instance', () => mockAuthUtil)
Expand All @@ -97,7 +95,7 @@ describe('registerMessageListeners', () => {
})

sinon.assert.calledOnce(reauthenticateStub)
sinon.assert.notCalled(deleteConnectionStub)
sinon.assert.notCalled(logoutStub)
})

it('handles full authentication request', async () => {
Expand All @@ -109,7 +107,7 @@ describe('registerMessageListeners', () => {
})

sinon.assert.notCalled(reauthenticateStub)
sinon.assert.calledOnce(deleteConnectionStub)
sinon.assert.calledOnce(logoutStub)
})

it('logs error if re-authentication fails', async () => {
Expand All @@ -123,7 +121,7 @@ describe('registerMessageListeners', () => {
it('logs error if full authentication fails', async () => {
await testFailure({
authType: 'full-auth',
stubToReject: deleteConnectionStub,
stubToReject: logoutStub,
errorMessage: 'Failed to authenticate',
})
})
Expand Down
Loading
Loading