

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 建立驗證挑戰 Lambda 觸發程序
<a name="user-pool-lambda-create-auth-challenge"></a>

建立身分驗證挑戰觸發程序是一種 Lambda 函數，具有定義身分驗證挑戰觸發程序所宣告之每個挑戰的詳細資訊。它會處理定義身分驗證挑戰觸發程序宣告的挑戰名稱`publicChallengeParameters`，並傳回應用程式必須呈現給使用者的 。然後`privateChallengeParameters`，此函數為您的使用者集區提供使用者集區傳遞給驗證身分驗證挑戰觸發條件之挑戰 的答案。如果您的定義身分驗證挑戰觸發程序管理挑戰序列，您的建立身分驗證挑戰觸發程序會管理挑戰內容。

![\[挑戰 Lambda 觸發程序\]](http://docs.aws.amazon.com/zh_tw/cognito/latest/developerguide/images/lambda-challenges2.png)


**建立身分驗證挑戰**  
如果自訂挑戰已指定為 **Define Auth Challenge (定義驗證挑戰)** 觸發程序的一部分，Amazon Cognito 就會在 **Define Auth Challenge (定義驗證挑戰)** 之後叫用此觸發程序。它會建立[自訂身分驗證流程](https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-authentication-flow.html#amazon-cognito-user-pools-custom-authentication-flow)。

叫用此 Lambda 觸發程序可以建立要向使用者提出的挑戰。此 Lambda 觸發程序的請求包含 `challengeName` 和 `session`。`challengeName`是要向使用者提出之下一個挑戰的名稱字串。此屬性的值是設定在「定義驗證挑戰 Lambda 觸發程序」中。

除非所有挑戰都已回答，否則挑戰迴圈會不斷重複。

**Topics**
+ [建立驗證挑戰 Lambda 觸發程序參數](#cognito-user-pools-lambda-trigger-syntax-create-auth-challenge)
+ [建立驗證挑戰範例](#aws-lambda-triggers-create-auth-challenge-example)

## 建立驗證挑戰 Lambda 觸發程序參數
<a name="cognito-user-pools-lambda-trigger-syntax-create-auth-challenge"></a>

Amazon Cognito 傳遞至此 Lambda 函數的請求，是以下參數和 Amazon Cognito 新增至所有請求的[常用參數](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-working-with-lambda-triggers.html#cognito-user-pools-lambda-trigger-syntax-shared)之組合。

------
#### [ JSON ]

```
{
    "request": {
        "userAttributes": {
            "string": "string",
            . . .
        },
        "challengeName": "string",
        "session": [
            ChallengeResult,
            . . .
        ],
        "clientMetadata": {
            "string": "string",
            . . .
        },
        "userNotFound": boolean
    },
    "response": {
        "publicChallengeParameters": {
            "string": "string",
            . . .
        },
        "privateChallengeParameters": {
            "string": "string",
            . . .
        },
        "challengeMetadata": "string"
    }
}
```

------

### 建立驗證挑戰請求參數
<a name="cognito-user-pools-lambda-trigger-syntax-create-auth-challenge-request"></a>

**userAttributes**  
代表使用者屬性的一或多組名稱/值。

**userNotFound**  
當您的使用者集區用戶端的 `PreventUserExistenceErrors` 設定為 `ENABLED` 時，將會填入此布林值。

**challengeName**  
新挑戰的名稱。

**工作階段**  
工作階段元素是 `ChallengeResult`元素陣列，每個陣列都包含下列元素：    
**challengeName**  
挑戰類型。其中之一：`"CUSTOM_CHALLENGE"`、`"PASSWORD_VERIFIER"`、`"SMS_MFA"`、`"DEVICE_SRP_AUTH"`、`"NEW_PASSWORD_REQUIRED"`、 `"DEVICE_PASSWORD_VERIFIER"`或 `"ADMIN_NO_SRP_AUTH"`。  
**challengeResult**  
如果使用者順利完成挑戰，則設定為 `true`，否則設定為 `false`。  
**challengeMetadata**  
您的自訂挑戰名稱。唯有在 `challengeName` 為 `"CUSTOM_CHALLENGE"` 時使用。

**clientMetadata**  
您可以做為 Lambda 函數的自訂輸入提供的一個或多個鍵值組，該函數是您用於建立驗證挑戰觸發程序所指定。您可以使用 [AdminRespondToAuthChallenge](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminRespondToAuthChallenge.html) 和 [RespondToAuthChallenge](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_RespondToAuthChallenge.html) API 動作中的 ClientMetadata 參數，將此資料傳遞至您的 Lambda 函數。叫用建立身分驗證挑戰函數的請求不包含傳遞至 [AdminInitiateAuth](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminInitiateAuth.html) 和 [InitiateAuth](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html) API 操作的 ClientMetadata 參數中的資料。

### 建立驗證挑戰回應參數
<a name="cognito-user-pools-lambda-trigger-syntax-create-auth-challenge-response"></a>

**publicChallengeParameters**  
可讓用戶端應用程式用在要向使用者提出的挑戰中的一或多個鍵值組。此參數應包含所有必要資訊，以準確地向使用者提出挑戰。

**privateChallengeParameters**  
此參數僅供「確認驗證挑戰回應 Lambda 觸發程序」使用。此參數應包含驗證使用者對挑戰的回應時，所有必要的資訊。換言之，`publicChallengeParameters`參數包含向使用者提出的問題，而 `privateChallengeParameters` 包含問題的有效答案。

**challengeMetadata**  
您的自訂挑戰名稱 (如果這是自訂挑戰)。

## 建立驗證挑戰範例
<a name="aws-lambda-triggers-create-auth-challenge-example"></a>

此函數有兩個自訂挑戰，對應到[定義身分驗證挑戰範例中](user-pool-lambda-define-auth-challenge.md#aws-lambda-triggers-define-auth-challenge-example)的挑戰序列。前兩個挑戰是 SRP 身分驗證。對於第三個挑戰，此函數會在挑戰回應中傳回 CAPTCHA URL 到您的應用程式。您的應用程式會在指定的 URL 轉譯 CAPTCHA，並傳回使用者的輸入。CAPTCHA 影像的 URL 會以 "`captchaUrl`" 新增至公有挑戰參數，而預期的答案會新增至私有挑戰參數。

對於第四個挑戰，此函數會傳回安全性問題。您的應用程式會呈現問題，並提示使用者回答。使用者解決這兩個自訂挑戰後，定義身分驗證挑戰觸發程序會確認您的使用者集區可以發出權杖。

------
#### [ Node.js ]

```
const handler = async (event) => {
  if (event.request.challengeName !== "CUSTOM_CHALLENGE") {
    return event;
  }

  if (event.request.session.length === 2) {
    event.response.publicChallengeParameters = {};
    event.response.privateChallengeParameters = {};
    event.response.publicChallengeParameters.captchaUrl = "url/123.jpg";
    event.response.privateChallengeParameters.answer = "5";
  }

  if (event.request.session.length === 3) {
    event.response.publicChallengeParameters = {};
    event.response.privateChallengeParameters = {};
    event.response.publicChallengeParameters.securityQuestion =
      "Who is your favorite team mascot?";
    event.response.privateChallengeParameters.answer = "Peccy";
  }

  return event;
};

export { handler };
```

------