

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在中管理身份验证方法 AWS SDKs
<a name="authentication-flows-selection-sdk"></a>

Amazon Cognito 用户池中的用户可以通过多种初始登录选项（也称为*因素*）进行登录。对于某些因素，用户还可以继续完成多重身份验证（MFA）。这些第一重因素包括用户名和密码、一次性密码、通行密钥以及自定义身份验证。有关更多信息，请参阅 [身份验证流程](amazon-cognito-user-pools-authentication-flow-methods.md)。当您的应用程序具有内置 UI 组件并导入 S AWS DK 模块时，您必须构建用于身份验证的应用程序逻辑。此时，您必须从两种主要方法中选择其一，并通过此方法确定要实现的身份验证机制。

您可以实现*基于客户端的身份验证*，即由您的应用程序（或客户端）在一开始就声明所使用的身份验证类型。另一种选择是*基于选择的身份验证*，即您的应用程序先收集用户名，然后请求该用户可用的身份验证类型。您可以根据需求，在同一个应用程序中同时实现这两种模式，或在不同的应用程序客户端之间分别使用。每种方法都有其独有的功能，例如：基于客户端的身份验证支持自定义身份验证，而基于选择的身份验证则支持无密码身份验证。

在使用用户池 API 的 AWS SDK 实现进行身份验证的自定义应用程序中，您的 API 请求结构必须与用户池配置、应用程序客户端配置和客户端首选项保持一致。如果 `InitiateAuth` 会话以 `AuthFlow` 值为 `USER_AUTH` 开始，则启动基于选择的身份验证。Amazon Cognito 会在 API 响应中返回一个质询，内容为用户的首选身份验证方法或可用选项列表。如果会话以 `AuthFlow` 值为 `CUSTOM_AUTH` 开始，则直接进入基于 Lambda 触发器的自定义身份验证流程。

有些身份验证方法固定为两种流程中的一种，有些方法在两种流程中都可用。

**Topics**
+ [基于选择的身份验证](#authentication-flows-selection-choice)
+ [基于客户端的身份验证](#authentication-flows-selection-client)

## 基于选择的身份验证
<a name="authentication-flows-selection-choice"></a>

您的应用程序可以在基于选择的身份验证中请求以下身份验证方法。在[InitiateAuth](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html#CognitoUserPools-InitiateAuth-request-AuthParameters)或的`PREFERRED_CHALLENGE`参数中声明这些选项 [AdminInitiateAuth](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminInitiateAuth.html#CognitoUserPools-AdminInitiateAuth-request-AuthParameters)，或者在[RespondToAuthChallenge](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_RespondToAuthChallenge.html#CognitoUserPools-RespondToAuthChallenge-request-ChallengeName)或的`ChallengeName`参数中声明这些选项[AdminRespondToAuthChallenge](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminRespondToAuthChallenge.html#CognitoUserPools-AdminRespondToAuthChallenge-request-ChallengeName)。

1. `EMAIL_OTP` 和 `SMS_OTP`

   [使用一次性密码进行无密码登录](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-passwordless)

1. `WEB_AUTHN`

   [使用密钥进行无密码登录 WebAuthn](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-passkey)

1. `PASSWORD`

   [使用永久密码登录](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-password)

   [使用永久密码和安全有效载荷登录](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-srp)

   [登录后进行 MFA](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-mfa)

要在其 API 上下文中查看这些选项，请参阅`ChallengeName`中的[RespondToAuthChallenge](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_RespondToAuthChallenge.html#CognitoUserPools-RespondToAuthChallenge-request-ChallengeName)。

基于选择的登录会发出一个质询以响应您的初始请求。该质询要么验证所请求的选项是否可用，要么提供一份可用选项列表。您的应用程序可将这些选项显示给用户，用户随后输入其首选登录方式的凭证，并通过响应质询继续完成身份验证。

您的身份验证流程中有以下基于选择的选项。此类请求均要求您的应用程序首先收集用户名，或从缓存中获取用户名。

1. 仅使用 `USERNAME` 作为 `AuthParameters` 来请求选项。Amazon Cognito 返回 `SELECT_CHALLENGE` 质询。随后，您的应用程序可以提示用户选择一种质询方式，并将该选择作为响应返回给用户池。

1. 通过在 `PREFERRED_CHALLENGE` 的 `AuthParameters` 以及您首选质询（如有）的参数来请求一个首选质询。例如，如果您请求的 `PREFERRED_CHALLENGE` 为 `PASSWORD_SRP`，则还必须包含 `SRP_A`。如果您的用户、用户池和应用程序客户端都针对首选挑战进行了配置，则 Amazon Cognito 会以该挑战的下一步作为响应，例如`PASSWORD_VERIFIER`在`PASSWORD_SRP`流程中或`EMAIL_OTP`和`SMS_OTP`流程[CodeDeliveryDetails](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_CodeDeliveryDetailsType.html)中。如果首选质询不可用，Amazon Cognito 将返回 `SELECT_CHALLENGE`，并附带一份可用质询列表。

1. 首先让用户登录，然后再请求其基于选择的身份验证选项。使用已登录用户的访问令牌的[GetUserAuthFactors](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_GetUserAuthFactors.html)请求会返回他们可用的基于选择的身份验证因素和 MFA 设置。通过此身份验证选项，用户可先使用用户名和密码登录，随后启用其他形式的身份验证。您也可以使用此操作，为已通过首选质询完成登录的用户检查其他可用选项。

要[为应用程序客户端配置](authentication.md#authentication-implement)基于选择的身份验证，请在允许的身份验证流程中添加 `ALLOW_USER_AUTH`。您还必须在用户池配置中选择要允许的基于选择的身份验证因素。以下流程说明了如何选择基于选择的身份验证因素。

------
#### [ Amazon Cognito console ]

**在用户池中配置基于选择的身份验证选项**

1. 登录 AWS 并导航到 [Amazon Cognito 用户池控制台](https://console.aws.amazon.com/cognito/v2/idp)。选择一个用户池或新建一个。

1. 在用户池配置中，选择**登录**菜单。找到**基于选择的登录选项**，然后选择**编辑**。

1. **密码**选项始终可用。这包括 `PASSWORD` 和 `PASSWORD_SRP` 流程。选择要添加到用户选项中的**其他选项**。您可以为 `WEB_AUTHN` 添加**通行密钥**，为 `EMAIL_OTP` 添加**电子邮件消息一次性密码**，为 `SMS_OTP` 添加 **SMS 消息一次性密码**。

1. 选择**保存更改**。

------
#### [ API/SDK ]

以下部分[CreateUserPool](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_CreateUserPool.html)或[UpdateUserPool](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_UpdateUserPool.html)请求正文配置了基于选择的身份验证的所有可用选项。

```
"Policies": {
    "SignInPolicy": {
        "AllowedFirstAuthFactors": [
            "PASSWORD",
            "WEB_AUTHN",
            "EMAIL_OTP",
            "SMS_OTP"
        ]
    }
},
```

------

## 基于客户端的身份验证
<a name="authentication-flows-selection-client"></a>

基于客户端的身份验证支持以下身份验证流程。在[InitiateAuth](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html#CognitoUserPools-InitiateAuth-request-AuthFlow)或的`AuthFlow`参数中声明这些选项[AdminInitiateAuth](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminInitiateAuth.html#CognitoUserPools-AdminInitiateAuth-request-AuthFlow)。

1. `USER_PASSWORD_AUTH` 和 `ADMIN_USER_PASSWORD_AUTH`

   [使用永久密码登录](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-password)

   [登录后进行 MFA](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-mfa)

   此身份验证流程在基于选择的身份验证中等同于 `PASSWORD`。

1. `USER_SRP_AUTH`

   [使用永久密码和安全有效载荷登录](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-srp)

   [登录后进行 MFA](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-mfa)

   此身份验证流程在基于选择的身份验证中等同于 `PASSWORD_SRP`。

1. `REFRESH_TOKEN_AUTH`

   [刷新令牌](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-refresh)

   此身份验证流程仅在基于客户端的身份验证中可用。

1. `CUSTOM_AUTH`

   [自定义身份验证](amazon-cognito-user-pools-authentication-flow-methods.md#amazon-cognito-user-pools-authentication-flow-methods-custom)

   此身份验证流程仅在基于客户端的身份验证中可用。

对于基于客户端的身份验证，Amazon Cognito 假设您已在用户开始身份验证流程之前确定了其希望使用的身份验证方式。确定用户要提供的登录因素的逻辑必须通过默认设置或自定义提示来确定，然后在首次向用户池发起的请求中声明。`InitiateAuth` 请求需声明一个与所列选项之一直接对应的登录 `AuthFlow`，例如 `USER_SRP_AUTH`。通过该声明，该请求还包含启动身份验证所需的参数，例如 `USERNAME`、`SECRET_HASH` 和 `SRP_A`。Amazon Cognito 可能会在该请求之后返回额外的质询，例如针对 SRP 的 `PASSWORD_VERIFIER`，或针对使用 TOTP MFA 的密码登录的 `SOFTWARE_TOKEN_MFA`。

要[为应用程序客户端配置](authentication.md#authentication-implement)基于客户端的身份验证，请在允许的身份验证流程中添加 `ALLOW_USER_AUTH` 之外的任何身份验证流程。示例包括 `ALLOW_USER_PASSWORD_AUTH`、`ALLOW_CUSTOM_AUTH`、`ALLOW_REFRESH_TOKEN_AUTH`。允许基于客户端的身份验证流程无需额外的用户池配置。