Skip to content

Commit 0948746

Browse files
committed
feat: enable mfa serial input in pop up window
1 parent 9826566 commit 0948746

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

packages/amazonq/src/lsp/client.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import { processUtils } from 'aws-core-vscode/shared'
5858
import { activate as activateChat } from './chat/activation'
5959
import { activate as activeInlineChat } from '../inlineChat/activation'
6060
import { AmazonQResourcePaths } from './lspInstaller'
61-
import { auth2, getMfaTokenFromUser } from 'aws-core-vscode/auth'
61+
import { auth2, getMfaTokenFromUser, getMfaSerialFromUser } from 'aws-core-vscode/auth'
6262
import { ConfigSection, isValidConfigSection, pushConfigUpdate, toAmazonQLSPLogLevel } from './config'
6363
import { telemetry } from 'aws-core-vscode/telemetry'
6464
import { SessionManager } from '../app/inline/sessionManager'
@@ -343,8 +343,17 @@ async function postStartLanguageServer(
343343
client.onRequest(
344344
auth2.notificationTypes.getMfaCode.method,
345345
async (params: GetMfaCodeParams): Promise<GetMfaCodeResult> => {
346-
const mfaCode = await getMfaTokenFromUser(params.mfaSerial, params.profileName)
347-
return { code: mfaCode ?? '' }
346+
if (params.mfaSerial) {
347+
globals.globalState.update('recentMfaSerial', { mfaSerial: params.mfaSerial })
348+
}
349+
const defaultMfaSerial = globals.globalState.tryGet('recentMfaSerial', Object, {
350+
mfaSerial: '',
351+
}).mfaSerial
352+
let mfaSerial = await getMfaSerialFromUser(defaultMfaSerial, params.profileName)
353+
mfaSerial = mfaSerial.trim()
354+
globals.globalState.update('recentMfaSerial', { mfaSerial: mfaSerial })
355+
const mfaCode = await getMfaTokenFromUser(mfaSerial, params.profileName)
356+
return { code: mfaCode ?? '', mfaSerial: mfaSerial ?? '' }
348357
}
349358
)
350359

packages/core/src/auth/credentials/utils.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,34 @@ export class CredentialsSettings extends fromExtensionManifest('aws', { profile:
102102

103103
const errorMessageUserCancelled = localize('AWS.error.mfa.userCancelled', 'User cancelled entering authentication code')
104104

105+
/**
106+
* @description Prompts user for MFA serial number
107+
*
108+
* Entered token is passed to the callback.
109+
* If user cancels out, the callback is passed an error with a fixed message string.
110+
*
111+
* @param profileName Name of Credentials profile we are asking an MFA Token for
112+
* @param callback tokens/errors are passed through here
113+
*/
114+
export async function getMfaSerialFromUser(defaultSerial: string, profileName: string): Promise<string> {
115+
const inputBox = createInputBox({
116+
ignoreFocusOut: true,
117+
placeholder: localize('AWS.prompt.mfa.enterCode.placeholder', 'Enter mfaSerial Number Here'),
118+
title: localize('AWS.prompt.mfa.enterCode.title', 'MFA Challenge for {0}', profileName),
119+
prompt: localize('AWS.prompt.mfa.enterCode.prompt', 'Enter code for MFA device', defaultSerial),
120+
value: defaultSerial, // Pre-fill with default value
121+
})
122+
123+
const token = await inputBox.prompt()
124+
125+
// Distinguish user cancel vs code entry issues with the error message
126+
if (!isValidResponse(token)) {
127+
throw new Error(errorMessageUserCancelled)
128+
}
129+
130+
return token
131+
}
132+
105133
/**
106134
* @description Prompts user for MFA token
107135
*

packages/core/src/auth/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export {
2222
} from './connection'
2323
export { Auth } from './auth'
2424
export { CredentialsStore } from './credentials/store'
25-
export { getMfaTokenFromUser } from './credentials/utils'
25+
export { getMfaTokenFromUser, getMfaSerialFromUser } from './credentials/utils'
2626
export { LoginManager } from './deprecated/loginManager'
2727
export * as constants from './sso/constants'
2828
export * as cache from './sso/cache'

packages/core/src/shared/globalState.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export type globalKey =
7373
| 'recentSso'
7474
| 'recentIamKeys'
7575
| 'recentRoleArn'
76+
| 'recentMfaSerial'
7677
// List of regions enabled in AWS Explorer.
7778
| 'region'
7879
// TODO: implement this via `PromptSettings` instead of globalState.

0 commit comments

Comments
 (0)