3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[AWS CloudFormation][AWS SAM] Cognito ユーザプール、アプリクライアント定義

Posted at

ユーザプール定義

構文については公式ドキュメント参照

設定したい項目以外はデフォルトで定義したものが以下

  UserPool:
    Type: AWS::Cognito::UserPool
    Properties:
      UserPoolName: !Sub '${StackName}_UserPool'
      UsernameConfiguration: 
        CaseSensitive: false
      AdminCreateUserConfig:
        AllowAdminCreateUserOnly: true
      AccountRecoverySetting:
        RecoveryMechanisms:
          - Name: admin_only
            Priority: 1
      LambdaConfig:
        PreAuthentication: !GetAtt PreAuthenticationHook.Arn

マネージメントコンソールで内容確認

属性

3.png

  • サインインに使用する属性はユーザ名

    Emailや、電話番号を使用する場合は、AutoVerifiedAttributesで指定する

  • 追加属性はなし

    追加したい場合は、AliasAttributesで指定する

  • ユーザ名入力で大文字、小文字を区別しない

テンプレートの以下部分で設定

      UsernameConfiguration:
        CaseSensitive: false

ポリシー

4.png

  • 管理者のみサインアップ可能とする

    テンプレートの以下部分で設定

      AdminCreateUserConfig:
        AllowAdminCreateUserOnly: true

MFAそして確認

5.png

  • アカウント回復方法をなしとする

    テンプレートの以下部分で設定

          AccountRecoverySetting:
            RecoveryMechanisms:
              - Name: admin_only
                Priority: 1
    

トリガ

9.png

  • 認証前にフックするLambdaを指定

    テンプレートの以下部分で設定

          LambdaConfig:
            PreAuthentication: !GetAtt PreAuthenticationHook.Arn
    

    上記以外のタイミングでフックするLambda設定に関しては公式ドキュメント参照

    以下、Lambdaの定義例
    関数本体と、Cognitoに与えるLambda実行権限を定義

      PreAuthenticationHook:
        Type: AWS::Serverless::Function
        Properties:
          FunctionName: !Sub '${StackName}_PreAuthenticationHook'
          CodeUri: pre-Authentication-hook/
          Handler: function.lambda_handler
          Runtime: python3.8
    
      LambdaAddPermission:
          Type: AWS::Lambda::Permission
          Properties:
            Action: lambda:InvokeFunction
            FunctionName: !GetAtt PreAuthenticationHook.Arn 
            Principal: cognito-idp.amazonaws.com
    

アプリクライアント定義

構文については公式ドキュメント参照

設定したい項目以外はデフォルトで定義したものが以下

  UserPoolClient:
    Type: AWS::Cognito::UserPoolClient
    Properties:
      UserPoolId: !Ref UserPool # 定義したユーザプールを参照
      ClientName: !Sub '${StackName}_clientApp'
      GenerateSecret: false
      PreventUserExistenceErrors: ENABLED

マネージメントコンソールで内容確認

アプリクライアント

6.png

  • アプリクライアントのシークレットなし

    テンプレートの以下部分で設定

    GenerateSecret: false
    
  • ユーザの存在エラーを防ぐを有効設定

    テンプレートの以下部分で設定

    PreventUserExistenceErrors: ENABLED
    

トークンの有効期限設定

  UserPoolClient:
    Type: AWS::Cognito::UserPoolClient
    Properties:
      UserPoolId: !Ref UserPool
      ClientName: !Sub '${StackName}_clientApp'
      GenerateSecret: false
      RefreshTokenValidity: 2
      AccessTokenValidity: 1
      IdTokenValidity: 1
      PreventUserExistenceErrors: ENABLED

上記のように

  • 更新トークンの有効期限:2
  • アクセストークンの有効期限:1
  • IDトークンの有効期限:1

と設定してみたところ、以下のように日に値が設定された

7.png

分単位の設定ができない。。

以下、各設定値の最大値から単位は秒と考えられるが、マネジメントコンソールで秒は設定できないのでそれもおかしい

8.png

とりあえずAWSに不具合があるようなので、有効期限は全てデフォルトで運用

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?