Authenticationモジュール
Authenticationモジュールは、認証(AuthN)と認可(AuthZ)の機能を提供する。AWS Cognito ユーザプール を介して、FacebookやGoogleなどのOpenIDプロバイダーの情報を含むユーザ情報を格納し、ユーザの認証を行う。また、AWS Cognito Federated Identitiesを介して、例えば、S3へのファイルアップロードできる権限を付与するなど、AWSリソースに対しての認可の機能も提供する。AWS CLIは、アクセス制御ポリシーの自動化と、GraphQLを用いたきめ細やかなアクセス制御機能を提供する。
シンプルな認証では、AWS Cognito ユーザプール を通してユーザの認証のみを行い、この認証に通れば、(ログイン)アプリが利用するAWSリソースと通信が可能となる。一方で、ユーザ別にコンテンツを出し分ける必要がある場合などには、そのユーザに関連するAWSリソースのみにアクセス権限を付与しなければならない。この場合は、AWS Cognito Federated Identitiesを通して、そのユーザに必要な権限のみを許可された、AWS Credentialがやりとりされる。
セットアップ
Amplifyプロジェクトのルートディレクトリでamplify add authコマンドを実行することで、ウィザードに従って自動セットアップを行うことができる。
設定内容の例はこちら。
設定項目 | 内容 |
---|---|
リソース名 | myproject |
User Pool名 | myproject |
Sign-Inに使用する属性 | |
MFA | 無効 |
ユーザ登録等にEメールを使用 | 有効 |
認証メールのタイトル | MyProject から 認証コード をお送りします |
認証メールの本文 | あなたの認証コードは {####} です。 |
Sign-Up時に必須の属性 | Email, Name |
Tokenの有効期間 | 30日 |
アクセスできるユーザ属性の限定 | なし |
その他の機能 | ホワイトリストによるEmailドメインのフィルタ |
OAuthの使用 | なし |
Lambda Triggers | Pre Sign-up |
ホワイトリストに指定するドメイン | surbiton.jp |
上の内容を設定するために、以下のような対話式ウィザードによって必要事項を埋めていく。
$ amplify add auth Do you want to use the default authentication and security configuration? Manual configuration Select the authentication/authorization services that you want to use: User Sign-Up & Sign-In only (Best used with a cloud API only) Please provide a friendly name for your resource that will be used to label this category in the project: myproject Please provide a name for your user pool: myproject Warning: you will not be able to edit these selections. How do you want users to be able to sign in? Email Multifactor authentication (MFA) user login options: OFF Email based user registration/forgot password: Enabled (Requires per-user email entry at registration) Please specify an email verification subject: MyProject から 認証コード をお送りします Please specify an email verification message: あなたの認証コードは {####} です。 Do you want to override the default password policy for this User Pool? No Warning: you will not be able to edit these selections. What attributes are required for signing up? Email, Name Specify the app's refresh token expiration period (in days): 30 Do you want to specify the user attributes this app can read and write? No Do you want to enable any of the following capabilities? Email Domain Filtering (whitelist) Do you want to use an OAuth flow? No Do you want to configure Lambda Triggers for Cognito? Yes Which triggers do you want to enable for Cognito (Press <space> to select, <a> to toggle all, <i> to invert selection)Pre Sign-up What functionality do you want to use for Pre Sign-up (Press <space> to select, <a> to toggle all, <i> to invert selection)Sign-Up email filtering (whitelist) Enter a comma-delimited list of allowed email domains (example: 'mydomain.com, myotherdomain.com'). surbiton.jp Succesfully added the Lambda function locally Press enter to continue Successfully added resource myproject locally
Lambdaトリガー
Amplify CLIを用いることで、ホワイトリスト内のユーザのみに新規登録を認める処理を加えるなど、AWS Cognito ユーザプール のトリガに対応したLambdaを設定することができる。
Vue Components
認証モジュールに関連して使用できるVue Componentsは以下の通り。
Components | 内容 |
---|---|
<amplify-authenticator></amplify-authenticator> | 認証全般 |
<amplify-sign-in></amplify-sign-in> | Sign-In |
<amplify-confirm-sign-in></amplify-confirm-sign-in> | Sign-Inの確認 |
<amplify-sign-up></amplify-sign-up> | Sign-Up |
<amplify-forgot-password></amplify-forgot-password> | パスワード忘れ時 |
<amplify-sign-out></amplify-sign-out> | Sign-Out |
<amplify-set-mfa></amplify-set-mfa> | MFAの設定 |
Example
認証モジュールを組み込んだVue Componentの実装例は以下の通り。
<template> <div id="auth"> <div v-if="signInStatus === 'signedIn'"> <!-- サインアウト --> <amplify-sign-out></amplify-sign-out> </div> <div v-else> <!-- サインイン --> <amplify-authenticator v-bind:authConfig="authConfig"></amplify-authenticator> </div> </div> </template> <script> import { AmplifyEventBus } from 'aws-amplify-vue' import { Auth } from 'aws-amplify' export default { name: 'Auth', components: {}, data: function() { return { signInStatus: 'signedOut', authConfig: { // サインインにEmailを使用 usernameAttributes: 'Email', signUpConfig: { hideAllDefaults: true, // サインアップに使用する項目 signUpFields: [ { label: 'Email', key: 'email', required: true, displayOrder: 1, type: 'string', signUpWith: true }, { label: 'Name', key: 'name', required: true, displayOrder: 2, type: 'string' }, { label: 'Password', key: 'password', required: true, displayOrder: 3, type: 'password' }, ] } } } }, async beforeCreate() { try { // 現在のユーザ await Auth.currentAuthenticatedUser() this.signInStatus = 'signedIn' } catch (err) { this.signInStatus = 'signedOut' } AmplifyEventBus.$on('authState', info => { switch (info) { case 'signedIn': this.signInStatus = 'signedIn' break default: this.signInStatus = 'signedOut' break } }); } } </script> <style> </style>