AWS Cognito中用户需要MFA时的身份验证流程

Mat*_*les 6 javascript sdk amazon-web-services multi-factor aws-cognito

我正在尝试将用于用户身份验证的MFA添加到AWS Cognito中用于设备管理的现有解决方案(内置于Angular中).

从用户体验的角度来看,我无法弄清楚如何很好地处理这个特定的响应.它实际上感觉很破碎,如果有其他人在这里经历过痛点,那就太爱了.

参见用例23.例如实现,我的如下:

authenticate(username: string, password: string): Observable<any> {

    // init cognitoUser here

    return new Observable((observer) => {
        cognitoUser.authenticateUser(authenticationDetails, {
            onSuccess: (result: any) => {},
            onFailure: (err: Error) => {},
            mfaRequired: (codeDeliveryDetails: any) => {

                // SMS has just been sent automatically 
                // and it needs to be confirmed within this scope

                // The example linked requests the code via `confirm()`
                // which is awful UX...and since this is a service
                // probably non-compliant with best practice
                // However, without this `confirm` at this point in                     
                // time, we have no confirmationCode below

                cognitoUser.sendMFACode(confirmationCode, {
                    onSuccess: (result) => {
                        observer.next(result);
                        observer.complete();
                    }, onFailure: (err: Error) => {
                        observer.error(err);
                        observer.complete();
                    }
                });
            }
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

预期:

  • 如果用户成功进行身份验证但尚未通过MFA添加此设备,我们可以管理重定向到相应的确认代码表单页面并sendMFACode手动触发该功能(可能通过某种有限的会话?)

发行/ S:

  • 我们没有会话,所以我们无法向用户询问在此登录屏幕之外自动发送的MFA代码... catch 22?
  • 在登录表单中添加另一个显示/隐藏字段不起作用,因为它会sendMfaCode多次命中该功能,导致发送多个SMS代码.

有没有人有任何运气退出这一流程?

stw*_*ilz 7

虽然我确信非常有才华的人在amazon-cognito-identity-js API 上工作过,但它只是设计得很糟糕。这就是为什么它被贬低了。我个人的建议是迁移到Amplify,这让我不那么生气。

使用Amplify,您可以做到这些。???

import Amplify from 'aws-amplify'
import Auth from '@aws-amplify/auth'

let mfaRequired = false

Amplify.configure({
    Auth: {
        userPoolWebClientId: '',
        userPoolId: ''
    }
})

const logUserIn = (user) => {
  // Go forth and be happy
}

// Run me on your login form's submit event
const login = async (username, password) => {
  const user = await Auth.signIn(username, password)

  if (user.challengeName === 'SMS_MFA') {
    // Change UI to show MFA Code input
    mfaRequired = true
    return
  }
  return logUserIn(user)
}

// Run me when the user submits theire MFA code
const senfMfaCode = async (mfaCode) => {
  const user = await Auth.confirmSignIn(mfaCode)
  return logUserIn(user)
}
Run Code Online (Sandbox Code Playgroud)

但是,如果出于某种可悲的原因您需要继续使用amazon-cognito-identity-js,请不要担心。我接到你了。

只需将cognitoUser对象存储在回调之外。该文档有点误导,因为它只显示了自包含的示例,但没有理由在需要 MFA 时通知您的 UI,然后再调用cognitoUser.sendMFACode()

请记住,文档显示了传递thistosendMFACode()范围(这很糟糕),但您可以将回调声明为变量并在您的authenticateUser()sendMFACode()函数(或您喜欢的任意数量的函数)之间共享它。

import { CognitoUserPool, AuthenticationDetails, CognitoUser } from 'amazon-cognito-identity-js'

export let mfaRequired = false
export let cognitoUser = null

export const cognitoCallbacks = {
  mfaRequired () {
    // Implement you functionality to show UI for MFA form
    mfaRequired = true
  },
  onSuccess (response) {
    // Dance for joy the code gods be glorious.
  },
  onFailure () {
    // Cry.
  }
}

export const logUserIn = payload => {
  cognitoUser = new CognitoUser({
    Username: 'Matt Damon',
    Pool: new CognitoUserPool({
      UserPoolId: '',
      ClientId: ''
    })
  })
  return cognitoUser.authenticateUser(new AuthenticationDetails(payload), cognitoCallbacks)
}

export const sendMfaCode = MFACode => {
  cognitoUser.sendMFACode(MFACode, cognitoCallbacks)
}
Run Code Online (Sandbox Code Playgroud)

这是一个超级基本的实现,最重要的是你可以,

  1. 只需覆盖mfaRequired外部模块中的函数即可执行任何您想要的操作。
  2. 将整个内容包装在 pub/sub 插件中并订阅事件。

希望有帮助!

  • 都是好人 我即时起草了该代码,因此如果您需要一个工作示例,我可以通过 JSFiddle 进行链接。 (2认同)