

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

# 確認驗證挑戰回應 Lambda 觸發程序
<a name="user-pool-lambda-verify-auth-challenge-response"></a>

驗證身分驗證挑戰觸發程序是一種 Lambda 函數，可將使用者提供的回應與已知答案進行比較。此函數會告知使用者集區使用者是否正確回答挑戰。當驗證身分驗證挑戰觸發條件以 `answerCorrect`的 回應時`true`，身分驗證序列可以繼續。

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


**驗證身分驗證挑戰回應**  
Amazon Cognito 會叫用此觸發程序來驗證使用者對自訂驗證挑戰的回應是否有效。這屬於使用者集區[自訂身分驗證流程](https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-authentication-flow.html#amazon-cognito-user-pools-custom-authentication-flow)的一部分。

此觸發程序的請求包含 `privateChallengeParameters`和 `challengeAnswer` 參數。「建立驗證挑戰 Lambda 觸發程序」傳回 `privateChallengeParameters` 值，其中包含預期的使用者回應。`challengeAnswer`參數包含使用者對挑戰的回應。

回應包含 `answerCorrect` 屬性。如果使用者成功完成挑戰，Amazon Cognito 會將屬性值設定為 `true`。如果使用者未能成功完成挑戰，Amazon Cognito 會將此值設定為 `false`。

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

**Topics**
+ [確認驗證挑戰 Lambda 觸發程序參數](#cognito-user-pools-lambda-trigger-syntax-verify-auth-challenge)
+ [確認驗證挑戰回應範例](#aws-lambda-triggers-verify-auth-challenge-response-example)

## 確認驗證挑戰 Lambda 觸發程序參數
<a name="cognito-user-pools-lambda-trigger-syntax-verify-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",
            . . .
        },
        "privateChallengeParameters": {
            "string": "string",
            . . .
        },
        "challengeAnswer": "string",
        "clientMetadata": {
            "string": "string",
            . . .
        },
        "userNotFound": boolean
    },
    "response": {
        "answerCorrect": boolean
    }
}
```

------

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

**userAttributes**  
此參數包含代表使用者屬性的一或多個名稱/值對。

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

**privateChallengeParameters**  
此參數來自建立身分驗證挑戰觸發程序。為了確定使用者是否已通過挑戰，Amazon Cognito 會將此參數與使用者的 **challengeAnswer** 進行比較。  
此參數包含驗證使用者對挑戰的回應時，所有必要的資訊。此訊息包含亞 Amazon Cognito 向使用者提出的問題 (`publicChallengeParameters`)，以及問題的有效答案 (`privateChallengeParameters`)。只有「確認身分驗證挑戰回應 Lambda 觸發程序」會使用此參數。

**challengeAnswer**  
此參數值是使用者對挑戰回應的答案。

**clientMetadata**  
此參數包含您可以做為 Lambda 函數的自訂輸入提供的一個或多個鍵值組，該函數是您用於確認身分驗證挑戰觸發程序所指定。若要將此資料傳遞至您的 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 參數。Amazon Cognito 不包含其傳遞至確認身分驗證挑戰函數之請求中的 [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-verify-auth-challenge-response"></a>

**answerCorrect**  
如果使用者成功完成挑戰，Amazon Cognito 會將此參數設定為 `true`。如果使用者未能成功完成挑戰，Amazon Cognito 會將此參數設定為 `false`。

## 確認驗證挑戰回應範例
<a name="aws-lambda-triggers-verify-auth-challenge-response-example"></a>

此驗證驗證挑戰函數會檢查使用者對挑戰的回應是否符合預期的回應。使用者的答案是由您應用程式的輸入定義，而偏好的答案是由建立[身分驗證挑戰觸發](user-pool-lambda-create-auth-challenge.md#aws-lambda-triggers-create-auth-challenge-example)回應的回應`privateChallengeParameters.answer`中定義的。正確答案和指定的答案都是此函數輸入事件的一部分。

在此範例中，如果使用者的回應符合預期的回應，Amazon Cognito 會將 `answerCorrect` 參數設定為 `true`。

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

```
const handler = async (event) => {
  if (
    event.request.privateChallengeParameters.answer ===
    event.request.challengeAnswer
  ) {
    event.response.answerCorrect = true;
  } else {
    event.response.answerCorrect = false;
  }

  return event;
};

export { handler };
```

------