MFA設定を管理する
Authカテゴリーはユーザーサインインフローのマルチファクタ認証(MFA)をサポートしています。MFAはアカウントへのアクセスを試みるユーザーが本人であることを確認するために使用される追加のセキュリティレイヤーです。ユーザーに追加情報を提供してアイデンティティを検証することを要求します。Amplify Authは時間ベースのワンタイムパスワード(TOTP)およびテキストメッセージ(SMS)を使用したMFA方式をサポートしています。このガイドでは、TOTPとSMSを使用してMFAを設定する方法と、アプリケーションに適した設定を選択するために役立つこれらの方式のトレードオフについて確認します。また、デバイスを記憶するようにMFAを設定し、ユーザーのサインイン時の摩擦を減らす方法についても確認します。
始める前に、以下が必要です:
- Amplifyライブラリがインストールされ、設定されていること
- オプション SMSを使用する場合は、SNS/Pinpointでプロビジョニングされた電話番号が必要です
マルチファクタ認証を有効にする
以下は、Amplify CLIを使用してSMSまたはTOTPでMFAを設定するために使用できるステップです。Amplifyライブラリは、Amazon Cognitoリソースを別途設定した場合でも、MFAと連携するように設計されています。
MFAオプションを理解する
MFAを有効にするとき、2つの主要な決定を下す必要があります:
- MFA実行方式: このセットアップの一部として、MFAの実行方式を決定します。MFAログインを「ON」に設定してMFAを要求すると、すべてのユーザーはサインインするためにMFAを完了する必要があります。「Optional」に保つと、ユーザーはアカウントのMFAを有効にするかどうかを選択できます。
- MFA方式: また、使用しているMFA方式(TOTP(時間ベースのワンタイムパスワード)、SMS(テキストメッセージ)、または両方)を指定します。TOTPベースのMFAはより安全であり、SMSをアカウント回復用に予約できるため、使用することをお勧めします。
詳細を確認TOTPおよびSMS MFA方式を比較する
| 時間ベースのワンタイムパスワード(TOTP) | ショートメッセージサービス(SMS) | |
|---|---|---|
| 説明 | 認証アプリを使用して共有秘密鍵と現在時刻を含むユーザー認証用の短命の数値コードを生成します。 | テキストメッセージで共有されるワンタイムコードを生成し、他の認証情報と一緒に入力してユーザー認証を行います。 |
| 利点 | SMSよりも安全です。コードはローカルに生成され、ネットワーク上で送信されないため。また、TOTPアプリがインストールされていれば、携帯電話サービスがなくても機能します。 | ユーザーが提供する電話番号で簡単に設定でき、一般的な認証方式として使い慣れています。 |
| 制約 | コードを生成するアプリが必要で、アカウントの初期設定に追加されます。また、コードの有効期限も短く、生成後すぐに使用する必要があります。 | SMSは携帯電話サービスが必要で、ユーザーに追加コストが発生する可能性があります。まれですが、SMSメッセージは傍受される可能性もあります。 |
Amplify CLIでMFAを有効にする
amplify add authを実行して新しいCognito Authリソースを作成し、MFAをフローに統合する方法に応じて、以下のプロンプトに従います。
MFAを「ON」に設定するとすべてのユーザーに必須になり、「Optional」にするとユーザーごとに有効にできます。
SMS MFA
$ amplify add auth
? Do you want to use the default authentication and security configuration? Manual configuration
# Answer as appropriate
? Multifactor authentication (MFA) user login options: ON (Required for all logins, can not be enabled later)? For user login, select the MFA types: SMS Text Message? Please specify an SMS authentication message: Your authentication code is {####}
# Answer as appropriate
Some next steps:"amplify push" will build all your local backend resources and provision it in the cloud"amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloudTOTP MFA
$ amplify add auth
? Do you want to use the default authentication and security configuration? Manual configuration
# Answer as appropriate
? Multifactor authentication (MFA) user login options: ON (Required for all logins, can not be enabled later)? For user login, select the MFA types: Time-Based One-Time Password (TOTP)
# Answer as appropriate
Some next steps:"amplify push" will build all your local backend resources and provision it in the cloud"amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloudamplify update authを実行し、以下のガイダンスに従ってプロンプトに従います。
以下のステップは、ユーザーの「Optional」としてMFAを有効にする方法を示しています。このモードでは、MFAはユーザーごとに有効にする必要があり、Admin SDK(例えば、サインアップロセスの一部としてLambdaトリガー経由)または手動でCognitoコンソールで有効にできます。
MFAをユーザーに必須にしたい場合は、最初にamplify remove authを実行してauthリソースを削除し、このページの新規プロジェクトフローに従う必要があります。
SMS MFA
$ amplify update auth
? What do you want to do? Walkthrough all the auth configurations
# Answer as appropriate
? Multifactor authentication (MFA) user login options: OPTIONAL (Individual users can use MFA)? For user login, select the MFA types: SMS Text Message? Please specify an SMS authentication message: Your authentication code is {####}
# Answer as appropriate
Some next steps:"amplify push" will build all your local backend resources and provision it in the cloud"amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloudTOTP MFA
$ amplify update auth
? What do you want to do? Walkthrough all the auth configurations
# Answer as appropriate
? Multifactor authentication (MFA) user login options: OPTIONAL (Individual users can use MFA)? For user login, select the MFA types: Time-Based One-Time Password (TOTP)
# Answer as appropriate
Some next steps:"amplify push" will build all your local backend resources and provision it in the cloud"amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloudMFAを有効にした後、ユーザーがサインアップするときにMFA設定を含める必要があります。これはSMS、TOTP、または両方を有効にするかどうかによって異なります。
SMSを使用したマルチファクタ認証
上記のようにSMSをMFAの第2認証レイヤーとして設定した後、ユーザーはユーザー名とパスワードでサインインした後、サインインを完了するためのテキストメッセージで認証コードを受け取ります。
まだ作成していない場合は、Amazon Simple Notification Service(Amazon SNS)で使用するIAMロールを設定して、SMSメッセージを管理する必要があります。その後、Amazon Cognitoコンソールでユーザープールの「Messaging」タブの下にこのロールを追加します。詳細については、Amazon Cognito SMSテキストメッセージMFAドキュメントを参照してください。
サインアップ中にSMS MFAを有効にする
サインアップ中にユーザーのSMS MFAを有効にするには、ユーザー属性としてphone_numberを渡す必要があります。ただし、Cognitoリソースの主なサインインメカニズムがphone_number(usernameを有効にしない)の場合、属性として渡す必要はありません。
import { signUp } from 'aws-amplify/auth';
async function handleSignUp( username: string, password: string, phone_number: string, email: string) { try { await signUp({ username, password, options: { userAttributes: { phone_number, email } } }); } catch (error) { console.log(error); }}import { signUp } from 'aws-amplify/auth';
async function handleSignUp(username, password, phone_number, email) { try { await signUp({ username, password, options: { userAttributes: { phone_number, email } } }); } catch (error) { console.log(error); }}デフォルトでは、confirmSignUp APIを使用してサインアップ後にユーザーアカウントを検証する必要があります。これにより、Amazon Cognito設定に応じて、ユーザーの電話番号またはメールアドレスにワンタイムパスワードが送信されます。
import { confirmSignUp } from 'aws-amplify/auth';
async function handleSignUpConfirmation( username: string, confirmationCode: string) { try { await confirmSignUp({ username, confirmationCode }); } catch (error) { console.log(error); }}import { confirmSignUp } from 'aws-amplify/auth';
async function handleSignUpConfirmation(username, confirmationCode) { try { await confirmSignUp({ username, confirmationCode }); } catch (error) { console.log(error); }}サインイン中にSMS MFAを管理する
ユーザーがサインインした後、MFAがアカウントに対して有効な場合、チャレンジが返されます。その場合、confirmSignIn APIを呼び出す必要があります。ここでユーザーは電話番号に送信された確認コードを提供します。
import { signIn } from 'aws-amplify/auth';
async function handleSignIn(username: string, password: string) { try { await signIn({ username, password }); } catch (error) { console.log(error); }}import { signIn } from 'aws-amplify/auth';
async function handleSignIn(username, password) { try { await signIn({ username, password }); } catch (error) { console.log(error); }}MFAがONであるか、ユーザーに対して有効な場合は、電話に送信されたOTPでconfirmSignInを呼び出す必要があります。
import { confirmSignIn } from 'aws-amplify/auth';
async function handleSignInConfirmation(otpCode: string) { try { await confirmSignIn({ challengeResponse: otpCode }); } catch (error) { console.log(error); }}import { confirmSignIn } from 'aws-amplify/auth';
async function handleSignInConfirmation(otpCode) { try { await confirmSignIn({ challengeResponse: otpCode }); } catch (error) { console.log(error); }}ユーザーがサインインした後、updateMFAPreferenceを呼び出して、MFA方式をユーザーに対して有効として記録し、オプションでそれを優先に設定して、その後のログインがこのMFA方式を使用するようにデフォルト設定します。
import { updateMFAPreference } from 'aws-amplify/auth';
async function handleUpdateMFAPreference() { try { await updateMFAPreference({ sms: 'PREFERRED' }); } catch (error) { console.log(error); }}TOTPを使用したマルチファクタ認証
時間ベースのワンタイムパスワード(TOTP)をウェブまたはモバイルアプリケーションのマルチファクタ認証(MFA)に使用できます。Amplify Authカテゴリーには、認証アプリを使用したTOTPセットアップと検証のサポートが含まれており、統合ソリューションとユーザーの強化されたセキュリティを提供します。Google AuthenticatorやMicrosoft Authenticatorなどのこれらのアプリには、TOTPアルゴリズムが組み込まれており、共有秘密鍵と現在時刻を使用して短命の6桁のパスワードを生成することで機能します。
ユーザーのTOTPを設定する
signIn APIでユーザーサインインを開始すると、MFA方式としてTOTPセットアップが必要な場合、API呼び出しはCONTINUE_SIGN_IN_WITH_TOTP_SETUPをチャレンジとして返し、アプリで処理する次のステップです。次の条件が満たされた場合、このチャレンジが返されます:
- MFAがユーザープールで必須としてマークされている。
- TOTPがユーザープールで有効になっている。
- ユーザーはまだTOTP MFAをセットアップしていない。
CONTINUE_SIGN_IN_WITH_TOTP_SETUPステップは、ユーザーがサインインする前にTOTPをセットアップする必要があることを意味します。ステップは、TOTPSetupDetails型の関連値を返します。これは、Microsoft AuthenticatorやGoogle Authenticatorなどの認証アプリを構成するために使用する必要があります。TOTPSetupDetailsは、getSetupURIと呼ばれるヘルパーメソッドを提供します。このメソッドは、ユーザーがインストールした認証アプリを開くボタンなどで使用できるURIを生成します。より高度なユースケースでは、TOTPSetupDetailsにはsharedSecretも含まれており、QRコードの生成またはQRコードを手動で認証アプリに入力するために使用できます。
認証アプリがセットアップされたら、ユーザーはTOTPコードを生成し、ライブラリに提供してサインインプロセスを完了できます。
import { signIn, SignInOutput } from 'aws-amplify/auth';
async function handleSignIn(username: string, password: string) { try { const output = await signIn({ username, password });
handleSignInNextSteps(output); } catch (error) { console.log(error); }}
function handleSignInNextSteps(output: SignInOutput) { const { nextStep } = output; switch (nextStep.signInStep) { // ... case 'CONTINUE_SIGN_IN_WITH_TOTP_SETUP': const totpSetupDetails = nextStep.totpSetupDetails; const appName = 'my_app_name'; const setupUri = totpSetupDetails.getSetupUri(appName); // Open setupUri with an authenticator APP to retrieve an OTP code break; // ... }}import { signIn } from 'aws-amplify/auth';
async function handleSignIn(username, password) { try { const output = await signIn({ username, password });
handleSignInNextSteps(output); } catch (error) { console.log(error); }}
function handleSignInNextSteps(output) { const { nextStep } = output; switch (nextStep.signInStep) { // ... case 'CONTINUE_SIGN_IN_WITH_TOTP_SETUP': const totpSetupDetails = nextStep.totpSetupDetails; const appName = 'my_app_name'; const setupUri = totpSetupDetails.getSetupUri(appName); // Open setupUri with an authenticator APP to retrieve an OTP code break; // ... }}TOTPコードはテキストフィールドまたは他の手段でユーザーから取得できます。ユーザーがTOTPコードを提供したら、challengeResponseパラメータとしてTOTPコードでconfirmSignInを呼び出します。
import { confirmSignIn } from 'aws-amplify/auth';
async function handleSignInConfirmation(totpCode: string) { try { await confirmSignIn({ challengeResponse: totpCode }); } catch (error) { console.log(error); }}import { confirmSignIn } from 'aws-amplify/auth';
async function handleSignInConfirmation(totpCode) { try { await confirmSignIn({ challengeResponse: totpCode }); } catch (error) { console.log(error); }}ユーザーがサインインした後、updateMFAPreferenceを呼び出して、MFA方式をユーザーに対して有効として記録し、オプションでそれを優先に設定して、その後のログインがこのMFA方式を使用するようにデフォルト設定します。
import { updateMFAPreference } from 'aws-amplify/auth';
async function handleUpdateMFAPreference() { try { await updateMFAPreference({ totp: 'PREFERRED' }); } catch (error) { console.log(error); }}ユーザーがサインインした後にTOTPを有効にする
TOTP MFAはユーザーがサインインした後に設定できます。これは次の条件が満たされた場合に実行できます:
- MFAがユーザープールでオプションまたは必須としてマークされている。
- TOTPが有効なMFA方式としてユーザープール内でマークされている。
TOTP は、AuthカテゴリのsetUpTOTPおよびverifyTOTPSetup APIを呼び出して設定できます。
setUpTOTP APIを呼び出して、TOTPSetupDetailsオブジェクトを生成します。これは、Microsoft AuthenticatorやGoogle Authenticatorなどの認証アプリを構成するために使用する必要があります。TOTPSetupDetailsは、getSetupURIと呼ばれるヘルパーメソッドを提供します。このメソッドは、ユーザーがインストールした認証アプリを開くボタンなどで使用できるURIを生成します。より高度なユースケースでは、TOTPSetupDetailsにはsharedSecretも含まれており、QRコードの生成またはQRコードを手動で認証アプリに入力するために使用できます。
sharedSecretを含み、QRコードを生成するために、または認証アプリに手動で入力するために使用できます。
import { setUpTOTP } from 'aws-amplify/auth';
async function handleTOTPSetup() { try { const totpSetupDetails = await setUpTOTP(); const appName = 'my_app_name'; const setupUri = totpSetupDetails.getSetupUri(appName); // Open setupUri with an authenticator APP to retrieve an OTP code } catch (error) { console.log(error); }}認証アプリがセットアップされたら、ユーザーはTOTPコードを生成し、ライブラリに提供する必要があります。TOTP設定プロセスを完了するために、コードをverifyTOTPSetupに渡します。
import { verifyTOTPSetup } from 'aws-amplify/auth';
async function handleTOTPVerification(totpCode: string) { try { await verifyTOTPSetup({ code: totpCode }); } catch (error) { console.log(error); }}import { verifyTOTPSetup } from 'aws-amplify/auth';
async function handleTOTPVerification(totpCode) { try { await verifyTOTPSetup({ code: totpCode }); } catch (error) { console.log(error); }}TOTP設定が完了したら、updateMFAPreferenceを呼び出して、MFA方式をユーザーに対して有効として記録し、オプションでそれを優先に設定して、その後のログインがこのMFA方式を使用するようにデフォルト設定します。
import { updateMFAPreference } from 'aws-amplify/auth';
async function handleUpdateMFAPreference() { try { await updateMFAPreference({ sms: 'ENABLED', totp: 'PREFERRED' }); } catch (error) { console.log(error); }}紛失したTOTPデバイスから回復する
MFAがCognito User Poolで「必須」としてマークされ、別のMFA方式が設定されていないシナリオでは、管理者は最初にAdminUpdateUserAttributes呼び出しを開始し、ユーザーの電話番号属性を更新する必要があります。これが完了したら、管理者は上記で提案されているようにMFA優先をSMSに変更するのを続けることができます。
ユーザーの優先MFA方式を設定する
現在のユーザーのMFA優先を取得する
以下のAPIを呼び出して、現在のユーザーの現在のMFA優先と有効なMFAタイプ(ある場合)を取得します。
import { fetchMFAPreference } from 'aws-amplify/auth';
async function handleFetchMFAPreference() { try { const output = await fetchMFAPreference(); console.log(`Enabled MFA types for the user: ${output.enabled}`); console.log(`Preferred MFA type for the user: ${output.preferred}`); } catch (error) { console.log(error); }}現在のユーザーのMFA優先を更新する
以下のAPIを呼び出して、現在のユーザーのMFA優先を更新します。
import { updateMFAPreference } from 'aws-amplify/auth';
async function handleUpdateMFAPreference() { try { await updateMFAPreference({ sms: 'ENABLED', totp: 'PREFERRED' }); } catch (error) { console.log(error); }}複数のMFA方式がユーザーに対して有効な場合、signIn APIはCONTINUE_SIGN_IN_WITH_MFA_SELECTIONを認証フローの次のステップとして返します。このシナリオでは、ユーザーはサインインに使用するMFA方式を選択するようプロンプトされ、その優先をconfirmSignInに渡す必要があります。
import { confirmSignIn, SignInOutput } from 'aws-amplify/auth';
function handleSignInNextSteps(output: SignInOutput) { const { nextStep } = output; switch (nextStep.signInStep) { // ... case 'CONTINUE_SIGN_IN_WITH_MFA_SELECTION': const allowedMFATypes = nextStep.allowedMFATypes; const mfaType = promptUserForMFAType(allowedMFATypes); case 'CONFIRM_SIGN_IN_WITH_SMS_CODE': // display user to enter otp code; break; case 'CONFIRM_SIGN_IN_WITH_TOTP_CODE': // display user to enter otp code; break; // ... }}
function promptUserForMFAType( allowedMFATypes?: ('SMS' | 'TOTP')[]): 'SMS' | 'TOTP' { // Prompt user to select MFA type}
async function handleMFASelection(mfaType: 'SMS' | 'TOTP') { try { const output = await confirmSignIn({ challengeResponse: mfaType }); handleSignInNextSteps(output); } catch (error) { console.log(error); }}function handleSignInNextSteps(output) { const { nextStep } = output; switch (nextStep.signInStep) { // ... case 'CONTINUE_SIGN_IN_WITH_MFA_SELECTION': const allowedMFATypes = nextStep.allowedMFATypes; const mfaType = promptUserForMFAType(allowedMFATypes); case 'CONFIRM_SIGN_IN_WITH_SMS_CODE': // display user to enter otp code; break; case 'CONFIRM_SIGN_IN_WITH_TOTP_CODE': // display user to enter otp code; break; // ... }}
function promptUserForMFAType(allowedMFATypes) { // Prompt user to select MFA type}
async function handleMFASelection(mfaType) { try { const output = await confirmSignIn({ challengeResponse: mfaType }); handleSignInNextSteps(output); } catch (error) { console.log(error); }}デバイスを記憶する
デバイスを記憶することはMFAと組み合わせて使用する場合に便利です。ユーザーがそのデバイスでサインインするときに第2要因の要件を自動的に満たすことを許可し、サインイン時の摩擦を減らします。
デバイス追跡を構成する
Cognito User Poolコンソールでデバイスを記憶する機能を有効にできます。開始するには、プロジェクトディレクトリに移動し、以下のコマンドを実行します:
amplify auth consoleCognito User Poolコンソールを開く次のオプションを選択します:
? Which Console User Poolコンソールが開いたら、「Device Tracking」セクションまでスクロールダウンし、「Edit」ボタンをクリックします。これにより、ユーザーのデバイスを記憶するための優先を構成できる以下のページが表示されます。
「Always remember」を選択してデフォルトでユーザーのデバイスを記憶するか、「User Opt-in」を選択してユーザーが選択できるようにしてください。
MFAが有効な場合、記憶されたデバイスに対して第2要因を抑制するオプションがあります。信頼できるデバイスを第2要因メカニズムとして使用する場合は「はい」を選択します。
選択を完了したら、「Save changes」をクリックします。これで、デバイスを記憶するコードの更新を開始する準備ができました。
詳細を確認デバイス追跡に使用される主要な用語を理解する
デバイスを記憶、忘却、追跡する場合に留意すべき違いがあります。
- 追跡: ユーザーが新しいデバイスでサインインするたびに、クライアントは正常な認証イベントの終了時にデバイスキーを受け取ります。このデバイスキーを使用して、
ConfirmDeviceAPIを呼び出すために使用されるソルトとパスワード検証者を生成します。この時点で、デバイスは「追跡」状態と見なされます。デバイスが追跡状態にある場合、Amazon Cognitoコンソールを使用して、追跡を開始した時間、最後の認証時間、およびそのデバイスに関するその他の情報を表示できます。 - 記憶: 記憶されたデバイスも追跡されます。ユーザー認証中に、記憶されたデバイスに割り当てられたデバイスキーとシークレットペアを使用して、デバイスを認証し、ユーザーが以前にサインインしたのと同じデバイスであることを確認します。
- 記憶しない: 記憶されていないデバイスは、ユーザーがデバイスを記憶するようにCognitoが設定されているが、ユーザーがデバイスを記憶しないことを選択した追跡デバイスです。このユースケースは、自分が所有していないデバイスからアプリケーションにサインインするユーザー向けです。
- 忘却: デバイスを記憶または追跡しなくなった場合、
forgetDevice()APIを使用してデバイスを記憶と追跡から削除できます。
デバイスを記憶する
次を使用してデバイスを記憶できます:
import { rememberDevice } from 'aws-amplify/auth';
export async function handleRememberDevice() { try { await rememberDevice(); } catch (error) { console.log(error); }}import { rememberDevice } from 'aws-amplify/auth';
export async function handleRememberDevice() { try { await rememberDevice(); } catch (error) { console.log(error); }}デバイスを忘却する
デバイスを忘却することもできますが、忘却されたデバイスは記憶も追跡もされないことに注意してください。
import { forgetDevice } from 'aws-amplify/auth';
export async function handleForgetDevice() { try { await forgetDevice(); } catch (error) { console.log(error); }}import { forgetDevice } from 'aws-amplify/auth';
export async function handleForgetDevice() { try { await forgetDevice(); } catch (error) { console.log(error); }}デバイスを取得する
次を使用して、記憶されたデバイスのリストを取得できます:
import { fetchDevices } from 'aws-amplify/auth';
export async function handleFetchDevices() { try { const output = await fetchDevices(); console.log(output); } catch (error) { console.log(error); }}import { fetchDevices } from 'aws-amplify/auth';
async function handleFetchDevices() { try { const output = await Auth.fetchDevices(); console.log(output); } catch (error) { console.log(error); }}デバイスを記憶、忘却、取得するようにセットアップできるようになりました。
まとめ
おめでとうございます!MFA設定を管理するガイドを完了しました。このガイドでは、ユーザーのMFAをセットアップおよび構成し、MFA優先とデバイスを記憶するオプションを提供しました。
次のステップ
マルチファクタ認証のセットアップを完了したので、さらなるカスタマイズを追加することもできます。以下についてもっと学ぶことをお勧めします: