身份验证后 Lambda 触发器 - Amazon Cognito
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

身份验证后 Lambda 触发器

由于 Amazon Cognito 在登录用户后调用此触发器,您可以在 Amazon Cognito 对用户进行身份验证后添加自定义逻辑。

身份验证流概述


                身份验证后 Lambda 触发器 – 客户端流程

有关更多信息,请参阅用户池身份验证流程

身份验证后 Lambda 触发器参数

Amazon Cognito 传递给此 Lambda 函数的请求是以下参数和 Amazon Cognito 添加到所有请求中的常用参数的组合。

JSON
{ "request": { "userAttributes": { "string": "string", . . . }, "newDeviceUsed": boolean, "clientMetadata": { "string": "string", . . . } }, "response": {} }

身份验证后请求参数

newDeviceUsed

此标记指示用户是否已在新设备上登录。Amazon Cognito 仅在用户池的记住的设备值设置为 AlwaysUser Opt-In 时设置此标记。

userAttributes

表示用户属性的一个或多个名称/值对。

clientMetadata

一个或多个键值对,您可以将其作为自定义输入内容提供给为身份验证后触发器指定的 Lambda 函数。要将此数据传递给 Lambda 函数,您可以在 AdminRespondToAuthChallengeRespondToAuthChallenge API 操作中使用 ClientMetadata 参数。在传递到身份验证后函数的请求中,Amazon Cognito 不包括 AdminInitiateAuthInitiateAuth API 操作的 ClientMetadata 参数中传递的数据。

身份验证后响应参数

Amazon Cognito 不需要响应中任何额外的返回信息。您的函数可以使用 API 操作来查询和修改资源,或者将事件元数据记录到外部系统。

身份验证教程

Amazon Cognito 登录用户之后,将会立即激活身份验证后 Lambda 函数。请参阅这些适用于 JavaScript、Android 和 iOS 的登录教程。

平台 教程
JavaScript 身份开发工具包 通过 JavaScript 登录用户
Android 身份开发工具包 通过 Android 登录用户
iOS 身份开发工具包 通过 iOS 登录用户

身份验证后示例

此身份验证后示例 Lambda 函数将成功登录数据发送到 CloudWatch Logs。

Node.js
const handler = async (event) => { // Send post authentication data to Amazon CloudWatch logs console.log("Authentication successful"); console.log("Trigger function =", event.triggerSource); console.log("User pool = ", event.userPoolId); console.log("App client ID = ", event.callerContext.clientId); console.log("User ID = ", event.userName); return event; }; export { handler }
Python
import os def lambda_handler(event, context): # Send post authentication data to Cloudwatch logs print ("Authentication successful") print ("Trigger function =", event['triggerSource']) print ("User pool = ", event['userPoolId']) print ("App client ID = ", event['callerContext']['clientId']) print ("User ID = ", event['userName']) # Return to Amazon Cognito return event

Amazon Cognito 将事件信息传递给 Lambda 函数。随后,该函数将相同事件对象随同响应中的任何更改返回给 Amazon Cognito。在 Lambda 控制台中,您可以设置一个测试事件,该事件包含与您的 Lambda 触发器相关的数据。以下是此代码示例的一个测试事件:

JSON
{ "triggerSource": "testTrigger", "userPoolId": "testPool", "userName": "testName", "callerContext": { "clientId": "12345" }, "response": {} }