Skip to content

Commit 8efa2b2

Browse files
ErikCHErik Hanchett
andauthored
fix: Issue with QR Code not being seen with MS Authenticator (#1838)
* Fix encoding setup totp URI * Added test * Added changeset * Updated QR test Co-authored-by: Erik Hanchett <[email protected]>
1 parent f2ab1b8 commit 8efa2b2

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

.changeset/nice-candles-change.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@aws-amplify/ui-angular': patch
3+
'@aws-amplify/ui-react': patch
4+
'@aws-amplify/ui-vue': patch
5+
---
6+
7+
Fixed bug with MS Authenticator not working with Setup TOTP page

packages/angular/projects/ui-angular/src/lib/components/authenticator/components/setup-totp/setup-totp.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export class SetupTotpComponent implements OnInit {
5050
const issuer = this.formOverrides?.['QR']?.totpIssuer ?? 'AWSCognito';
5151
const username =
5252
this.formOverrides?.['QR']?.totpUsername ?? user.username;
53-
const totpCode = `otpauth://totp/${issuer}:${username}?secret=${this.secretKey}&issuer=${issuer}`;
53+
const totpCode = encodeURI(
54+
`otpauth://totp/${issuer}:${username}?secret=${this.secretKey}&issuer=${issuer}`
55+
);
5456

5557
logger.info('totp code was generated:', totpCode);
5658
this.qrCodeSource = await QRCode.toDataURL(totpCode);

packages/react/src/components/Authenticator/SetupTOTP/SetupTOTP.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import { RouteContainer, RouteProps } from '../RouteContainer';
2222
const logger = new Logger('SetupTOTP-logger');
2323

2424
export const getTotpCode = (issuer: string, username: string, secret: string) =>
25-
`otpauth://totp/${issuer}:${username}?secret=${secret}&issuer=${issuer}`;
25+
encodeURI(
26+
`otpauth://totp/${issuer}:${username}?secret=${secret}&issuer=${issuer}`
27+
);
2628

2729
export const SetupTOTP = ({
2830
className,

packages/react/src/components/Authenticator/SetupTOTP/__tests__/SetupTOTP.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,21 @@ describe('SetupTOTP', () => {
8888
expect(toDataURLSpy).toHaveBeenCalledTimes(1);
8989
expect(toDataURLSpy).toHaveBeenCalledWith(customTotpCode);
9090
});
91+
92+
describe('QR Tests', () => {
93+
it('handles customTotpIssuer with spaces', async () => {
94+
const customTotpIssuer = 'customTOTPIssuer spaces';
95+
const customTotpUsername = 'customTotpUsername';
96+
97+
const customTotpCode = getTotpCode(
98+
customTotpIssuer,
99+
customTotpUsername,
100+
SECRET_KEY
101+
);
102+
103+
expect(customTotpCode).toBe(
104+
'otpauth://totp/customTOTPIssuer%20spaces:customTotpUsername?secret=secretKey&issuer=customTOTPIssuer%20spaces'
105+
);
106+
});
107+
});
91108
});

packages/vue/src/components/setup-totp.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ onMounted(async () => {
5757
secretKey.value = await Auth.setupTOTP(user);
5858
const issuer = QROR?.totpIssuer ?? 'AWSCognito';
5959
const username = QROR?.totpUsername ?? user.username;
60-
const totpCode = `otpauth://totp/${issuer}:${username}?secret=${secretKey.value}&issuer=${issuer}`;
60+
const totpCode = encodeURI(
61+
`otpauth://totp/${issuer}:${username}?secret=${secretKey.value}&issuer=${issuer}`
62+
);
6163
qrCode.qrCodeImageSource = await QRCode.toDataURL(totpCode);
6264
} catch (error) {
6365
logger.error(error);

0 commit comments

Comments
 (0)