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

时间:2018-02-15 06:37:59

标签: javascript amazon-web-services sdk aws-cognito multi-factor

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

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

请参阅Use Case 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();
                    }
                });
            }
        });
    });
}

预期:

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

问题/ S:

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

有没有人有幸退出这一流程?

1 个答案:

答案 0 :(得分:4)

虽然我敢肯定有很多才华横溢的人都在使用 amazon-cognito-identity-js API,但是它的设计很糟糕。这就是为什么它被贬低了。我个人的建议是迁移到 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)
}

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

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

请记住,文档显示将this传递给sendMFACode()进行范围界定(这很糟糕),但是您可以将回调声明为变量,并在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)
}

这是一个超级基本的实现,除了您可以的实现之外,

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

希望有帮助!

相关问题