サインアップ前の Lambda トリガー - Amazon Cognito

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

サインアップ前の Lambda トリガー

Amazon Cognito は、新しいユーザーをサインアップする直前に、サインアップ前の AWS Lambda 関数をアクティブにします。サインアッププロセスの一環として、この関数を使用してカスタム検証を実行し、検証の結果に基づいて、登録リクエストを受け入れるか拒否することができます。

サインアップ前の Lambda フロー

クライアントのサインアップフロー


                    サインアップ前の Lambda トリガー - クライアントフロー

サーバーのサインアップフロー


                    サインアップ前の Lambda トリガー - サーバーフロー

リクエストには、クライアントからの検証データが含まれます。このデータは、ユーザープール SignUp と AdminCreateUser API メソッドに渡されたValidationData値から取得されます。

サインアップ前の Lambda トリガーのパラメータ

Amazon Cognito がこの Lambda 関数に渡すリクエストは、以下のパラメータと Amazon Cognito がすべてのリクエストに追加する共通パラメータを組み合わせたものです。

JSON
{ "request": { "userAttributes": { "string": "string", . . . }, "validationData": { "string": "string", . . . }, "clientMetadata": { "string": "string", . . . } }, "response": { "autoConfirmUser": "boolean", "autoVerifyPhone": "boolean", "autoVerifyEmail": "boolean" } }

サインアップ前のリクエストパラメータ

userAttributes

ユーザー属性を表す 1 つ以上の名前 - 値ペア。属性名はキーです。

validationData

新しいユーザーの作成リクエストでアプリケーションから Amazon Cognito に渡したユーザー属性データを示す 1 つ以上のキーと値のペアです。この情報を AdminCreateUserまたは SignUp API リクエストの ValidationData パラメータで Lambda 関数に送信します。

Amazon Cognito は、作成したユーザーの属性として ValidationData データを設定しません。 ValidationData は、サインアップ前の Lambda トリガーのために指定する一時的なユーザー情報です。

clientMetadata

サインアップ前のトリガーに指定する Lambda 関数へのカスタム入力として提供できる 1 つ、または複数のキー/値ペア。このデータは、、AdminCreateUser、、ForgotPasswordおよび の ClientMetadata API アクションで パラメータを使用して Lambda AdminRespondToAuthChallenge関数に渡すことができますSignUp

サインアップ前のレスポンスパラメータ

ユーザーを自動的に確認する場合は、レスポンスで autoConfirmUsertrue に設定できます。autoVerifyEmailtrue に設定してユーザーの E メールを自動検証できます。autoVerifyPhonetrue に設定してユーザーの電話番号を自動検証できます。

注記

サインアップ前 Lambda 関数が AdminCreateUser API によってトリガーされる場合、レスポンスパラメータの autoVerifyPhoneautoVerifyEmail、および autoConfirmUser は Amazon Cognito によって無視されます。

autoConfirmUser

ユーザーを自動的に確認する場合は true に、それ以外の場合は false に設定します。

autoVerifyEmail

true に設定すると、サインアップしたユーザーの E メールアドレスを検証済みとして設定します。それ以外の場合は false です。autoVerifyEmailtrue に設定されている場合、email 属性は有効な null 以外の値である必要があります。それ以外の場合はエラーが発生し、ユーザーがサインアップを完了できません。

email 属性がエイリアスとして選択されている場合、autoVerifyEmail が設定されていると、ユーザーの E メールアドレスのエイリアスが自動的に作成されます。その E メールアドレスのエイリアスが既に存在している場合は、エイリアスは新規ユーザーに移動され、以前のユーザーの E メールアドレスは未検証としてマークされます。詳細については、「ログイン属性のカスタマイズ」を参照してください。

autoVerifyPhone

true に設定すると、サインアップしたユーザーの電話番号を検証済みとして設定します。それ以外の場合は false です。autoVerifyPhonetrue に設定されている場合、phone_number 属性は有効な null 以外の値である必要があります。それ以外の場合はエラーが発生し、ユーザーがサインアップを完了できません。

phone_number 属性がエイリアスとして選択されている場合、autoVerifyPhone が設定されていると、ユーザーの電話番号のエイリアスが自動的に作成されます。その電話番号のエイリアスが既に存在している場合、エイリアスは新規ユーザーに移動され、以前のユーザーの電話番号は未検証としてマークされます。詳細については、「ログイン属性のカスタマイズ」を参照してください。

サインアップのチュートリアル

サインアップ前の Lambda 関数は、ユーザーのサインアップ前にトリガーされます。 JavaScript、Android、iOS に関する以下の Amazon Cognito サインアップチュートリアルを参照してください。

サインアップ前の例: 登録済みドメインのユーザーを自動確認する

サインアップ前の Lambda トリガーを使用して、ユーザープールにサインアップする新しいユーザーを検証するためのカスタムロジックを追加できます。これは、新しいユーザーをサインアップする方法を示すサンプル JavaScript プログラムです。これは認証の一環としてサインアップ前の Lambda トリガーを呼び出します。

JavaScript
var attributeList = []; var dataEmail = { Name: "email", Value: "...", // your email here }; var dataPhoneNumber = { Name: "phone_number", Value: "...", // your phone number here with +country code and no delimiters in front }; var dataEmailDomain = { Name: "custom:domain", Value: "example.com", }; var attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute(dataEmail); var attributePhoneNumber = new AmazonCognitoIdentity.CognitoUserAttribute( dataPhoneNumber ); var attributeEmailDomain = new AmazonCognitoIdentity.CognitoUserAttribute( dataEmailDomain ); attributeList.push(attributeEmail); attributeList.push(attributePhoneNumber); attributeList.push(attributeEmailDomain); var cognitoUser; userPool.signUp( "username", "password", attributeList, null, function (err, result) { if (err) { alert(err); return; } cognitoUser = result.user; console.log("user name is " + cognitoUser.getUsername()); } );

以下は、ユーザープールサインアップ前の Lambda トリガーを使用してサインアップ直前に呼び出されるサンプル Lambda トリガーです。これは、カスタム属性の [custom:domain] を使用して特定の E メールドメインからの新しいユーザーを自動的に確認します。このカスタムドメインに属していない新しいユーザーは、ユーザープールには追加されますが、自動的には確認されません。

Node.js
exports.handler = (event, context, callback) => { // Set the user pool autoConfirmUser flag after validating the email domain event.response.autoConfirmUser = false; // Split the email address so we can compare domains var address = event.request.userAttributes.email.split("@"); // This example uses a custom attribute "custom:domain" if (event.request.userAttributes.hasOwnProperty("custom:domain")) { if (event.request.userAttributes["custom:domain"] === address[1]) { event.response.autoConfirmUser = true; } } // Return to Amazon Cognito callback(null, event); };
Python
def lambda_handler(event, context): # It sets the user pool autoConfirmUser flag after validating the email domain event['response']['autoConfirmUser'] = False # Split the email address so we can compare domains address = event['request']['userAttributes']['email'].split('@') # This example uses a custom attribute 'custom:domain' if 'custom:domain' in event['request']['userAttributes']: if event['request']['userAttributes']['custom:domain'] == address[1]: event['response']['autoConfirmUser'] = True # Return to Amazon Cognito return event

Amazon Cognito は Lambda 関数にイベント情報を渡します。関数はレスポンスで、同じイベントオブジェクトを変更と共に Amazon Cognito に返します。Lambda コンソールで、Lambda トリガーに関連するデータを使用したテストイベントをセットアップできます。以下は、このコードサンプルのテストイベントです。

JSON
{ "request": { "userAttributes": { "email": "testuser@example.com", "custom:domain": "example.com" } }, "response": {} }

サインアップ前の例: すべてのユーザーを自動確認して自動検証する

次の例では、すべてのユーザーを確認し、ユーザーの email 属性と phone_number 属性を検証済みとして設定します (属性が存在する場合)。また、エイリアシングが有効になっている場合は、自動検証が設定されていると、phone_number および email にエイリアスが作成されます。

注記

同じ電話番号のエイリアスが既に存在している場合は、エイリアスは新規ユーザーに移動され、以前のユーザーの phone_number は未検証としてマークされます。E メールアドレスの場合も同様です。これを防ぐには、ユーザープール ListUsers API を使用して、新しいユーザーの電話番号または E メールアドレスをエイリアスとして既に使用している既存のユーザーがいるかどうかを確認できます。

Node.js
const handler = async (event) => { // Confirm the user event.response.autoConfirmUser = true; // Set the email as verified if it is in the request if (event.request.userAttributes.hasOwnProperty("email")) { event.response.autoVerifyEmail = true; } // Set the phone number as verified if it is in the request if (event.request.userAttributes.hasOwnProperty("phone_number")) { event.response.autoVerifyPhone = true; } return event; }; export { handler };
Python
def lambda_handler(event, context): # Confirm the user event['response']['autoConfirmUser'] = True # Set the email as verified if it is in the request if 'email' in event['request']['userAttributes']: event['response']['autoVerifyEmail'] = True # Set the phone number as verified if it is in the request if 'phone_number' in event['request']['userAttributes']: event['response']['autoVerifyPhone'] = True # Return to Amazon Cognito return event

Amazon Cognito は Lambda 関数にイベント情報を渡します。関数はレスポンスで、同じイベントオブジェクトを変更と共に Amazon Cognito に返します。Lambda コンソールで、Lambda トリガーに関連するデータを使用したテストイベントをセットアップできます。以下は、このコードサンプルのテストイベントです。

JSON
{ "request": { "userAttributes": { "email": "user@example.com", "phone_number": "+12065550100" } }, "response": {} }

サインアップ前の例:ユーザー名が 5 文字未満の場合にサインアップを拒否する

次の例は、サインアップリクエストのユーザー名の長さを調べます。次の例は、ユーザーが 5 文字未満の名前を入力した場合、エラーを返します。

Node.js
exports.handler = (event, context, callback) => { // Impose a condition that the minimum length of the username is 5 is imposed on all user pools. if (event.userName.length < 5) { var error = new Error("Cannot register users with username less than the minimum length of 5"); // Return error to Amazon Cognito callback(error, event); } // Return to Amazon Cognito callback(null, event); };
Python
def lambda_handler(event, context): if len(event['userName']) < 5: raise Exception("Cannot register users with username less than the minimum length of 5") # Return to Amazon Cognito return event

Amazon Cognito は Lambda 関数にイベント情報を渡します。関数はレスポンスで、同じイベントオブジェクトを変更と共に Amazon Cognito に返します。Lambda コンソールで、Lambda トリガーに関連するデータを使用したテストイベントをセットアップできます。以下は、このコードサンプルのテストイベントです。

JSON
{ "userName": "rroe", "response": {} }