Skip to content

Conversation

@calebpollman
Copy link
Member

@calebpollman calebpollman commented Jan 21, 2023

Description of changes

Migrate Authenticator TOTP secret code generation from UI components to state machine:

  • remove race condition causing transient errors in sign-in-totp-mfa integ test by ensuring that Auth.setupTOTP is only called once
  • fix issue with multiple QR code images getting rendered in React Authenticator
  • align Vue setupTOTP UX with other Authenticator implementations
  • expose totpSecretCode in useAuthenticator (code is generated only for setupTOTP route, value is null for all other routes)
  • prevent getQRFields from being called for non-setupTOTP routes in useAuthenticator
  • remove extraneous usage of useMemo in useAuthenticator
  • remove unit tests making assertions against TOTP secret code generation from the framework packages

Issue #, if available

NA

Description of how you validated changes

Manually tested, integ tests

Checklist

  • PR description included
  • yarn test passes
  • Tests are updated
  • No side effects or sideEffects field updated
  • Relevant documentation is changed or added (and PR referenced)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@changeset-bot
Copy link

changeset-bot bot commented Jan 21, 2023

🦋 Changeset detected

Latest commit: 61882f5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@aws-amplify/ui-react-core Patch
@aws-amplify/ui-react-native Patch
@aws-amplify/ui-react Patch
@aws-amplify/ui Patch
@aws-amplify/ui-vue Patch
@aws-amplify/ui-angular Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@calebpollman calebpollman temporarily deployed to ci January 21, 2023 03:52 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 21, 2023 03:52 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 21, 2023 03:52 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 21, 2023 03:52 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 23, 2023 21:06 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 23, 2023 21:06 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 23, 2023 21:06 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 23, 2023 21:38 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 23, 2023 21:38 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 23, 2023 21:38 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 23, 2023 21:38 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 23, 2023 22:38 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 23, 2023 22:38 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 23, 2023 22:38 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 23, 2023 22:38 — with GitHub Actions Inactive
const serviceSnapshot = service.getSnapshot() as AuthMachineState;

// legacy `QRFields` values only used for SetupTOTP page to retrieve issuer information, will be removed in future
const QRFields = useMemo(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the useMemo wrappers for QRFields and fields as serviceSnapshot does not maintain a stable reference, leading to to re-evaluation on every to call to useAuthenticator.

Additionally added logic to ensure that getQRFields is only called while route is equal to setupTOTP since QRFields is not relevant to other routes.

totpSecretCode,
unverifiedContactMethods,
user,
/** @deprecated For internal use only */
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything in the return below this line inherits this JSDoc comment.

},
onError: {
target: 'edit',
actions: 'setRemoteError',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to consider adding error handling logic that allows the UI to render a "try again" button if TOTP code generation fails, but am considering that out of scope for this PR

'plugin:vue/vue3-essential',
'eslint:recommended',
'@vue/eslint-config-typescript/recommended',
'@vue/eslint-config-prettier',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed as this was causing prettier eslint warnings that do not align with our prettier config

const actorState = computed(() =>
getActorState(state.value)
) as ComputedRef<SignInState>;
const { totpSecretCode, user } = actorState.value.context;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading from the actor context here rather than useAuthenticator since it is not typed. Probably should be addressed in the future


<base-wrapper class="amplify-flex amplify-authenticator__column">
<base-wrapper class="amplify-flex amplify-authenticator__column">
<template v-if="qrCode.isLoading">
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the conditional rendering logic to align with other implementations. Previously the entire template was prevented from rendering while the QR code was loading, now just the QR code image itself has that behavior

formOverrides?.['QR'] ?? {};
const totpCode =
typeof totpSecretCode === 'string' && typeof totpUsername === 'string'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to add these conditionals to appease TS (same with line 58)

type FormFieldsProps = {
isPending: AuthenticatorMachineContext['isPending'];
validationErrors?: AuthenticatorMachineContext['validationErrors'];
isPending: UseAuthenticator['isPending'];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced usage of AuthenticatorMachineContext with UseAuthenticator for these downstream types since they are ultimately returned by useAuthenticator

@calebpollman calebpollman changed the title [Draft] refactor(authenticator): migrate totpSecretCode generation to state machine fix(authenticator): migrate totpSecretCode generation to state machine Jan 23, 2023
@calebpollman calebpollman marked this pull request as ready for review January 23, 2023 22:44
@calebpollman calebpollman requested a review from a team as a code owner January 23, 2023 22:44
@calebpollman
Copy link
Member Author

calebpollman commented Jan 24, 2023

Moving back in to draft for the time being Ready for review!

@calebpollman calebpollman changed the title [WIP] fix(authenticator): migrate totpSecretCode generation to state machine fix(authenticator): migrate totpSecretCode generation to state machine Jan 24, 2023
@calebpollman calebpollman marked this pull request as ready for review January 24, 2023 02:57
@calebpollman calebpollman temporarily deployed to ci January 24, 2023 03:17 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 24, 2023 03:17 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 24, 2023 03:17 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 24, 2023 03:17 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 24, 2023 04:10 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 24, 2023 04:10 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 24, 2023 04:10 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 24, 2023 04:10 — with GitHub Actions Inactive
ErikCH
ErikCH previously approved these changes Jan 25, 2023
Copy link
Contributor

@ErikCH ErikCH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked over the Vue changes and that will work. The useAuthenticator composable is a little tricky to add Types too. That could certainly be something that we can add in the future.

Copy link
Contributor

@wlee221 wlee221 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@calebpollman calebpollman temporarily deployed to ci January 26, 2023 00:38 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 26, 2023 00:38 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 26, 2023 00:38 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 26, 2023 00:38 — with GitHub Actions Inactive
@calebpollman calebpollman enabled auto-merge (squash) January 26, 2023 00:47
@calebpollman calebpollman merged commit 4ba0fb5 into main Jan 26, 2023
@calebpollman calebpollman deleted the authenticator/refactor-totp-code-generation branch January 26, 2023 00:49
@github-actions github-actions bot mentioned this pull request Jan 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants