Amazon Cognito のリフレッシュトークンローテーションが CloudFormation でもサポートされたので設定を追加してみた

Amazon Cognito のリフレッシュトークンローテーションが CloudFormation でもサポートされたので設定を追加してみた

Clock Icon2025.05.14

いわさです。

先日のアップデートで Cognito ユーザープールのリフレッシュトークンがローテーションをサポートしました。

https://dev.classmethod.jp/articles/cognito-refresh-token-rotation/

個人的にかなり嬉しいアップデートだったのですが、普段 CloudFormation でユーザープールをデプロイすることが多くて、可能であれば CloudFormation で有効化までしたいと考えていました。

上記アップデート直後はまだ CloudFormation 側で使えなさそうだったのでカスタムリソースを作成しようかなと考えていたるところ、ちょうど CloudFormation のアップデートがありリフレッシュトークンローテーション設定もすぐに CloudFormation が使えるようになりました。

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpoolclient.html#cfn-cognito-userpoolclient-refreshtokenrotation

設定方法

リフレッシュトークンローテーションで設定できるのはマネジメントコンソール上は有効/無効と、有効にした時にローテーション後に旧トークンを無効化するまでの猶予期間でした。
今回のアップデートでAWS::Cognito::UserPoolClientRefreshTokenRotationが追加できるようになりました。
RefreshTokenRotationの中でFeatureを使ってローテーションの有効化を行い、RetryGracePeriodSecondsで旧トークンを失効させるまでの猶予期間(秒)を設定できます。

AWSTemplateFormatVersion: '2010-09-09'
Description: ---

Resources:
  # Cognito ユーザープール
  CognitoUserPool:
    Type: AWS::Cognito::UserPool
    Properties:
      UserPoolName: hoge0514userpool
      AutoVerifiedAttributes:
        - email
      UsernameAttributes:
        - email
      Schema:
        - Name: email
          AttributeDataType: String
          Mutable: false
          Required: true
        - Name: name
          AttributeDataType: String
          Mutable: true
          Required: true
      Policies:
        PasswordPolicy:
          MinimumLength: 8
          RequireLowercase: true
          RequireNumbers: true
          RequireSymbols: true
          RequireUppercase: true
      EmailConfiguration:
        EmailSendingAccount: COGNITO_DEFAULT
      AdminCreateUserConfig:
        AllowAdminCreateUserOnly: false
      AccountRecoverySetting:
        RecoveryMechanisms:
          - Name: verified_email
            Priority: 1

  CognitoUserPoolClient:
    Type: AWS::Cognito::UserPoolClient
    Properties:
      UserPoolId: !Ref CognitoUserPool
      ClientName: hoge0514userpoolclient1
      GenerateSecret: true
      AllowedOAuthFlows:
        - implicit
        - code
      AllowedOAuthFlowsUserPoolClient: true
      AllowedOAuthScopes:
        - phone
        - email
        - openid
        - profile
      CallbackURLs:
        - https://example.com/callback
      LogoutURLs:
        - https://example.com/logout
      ExplicitAuthFlows:
        - ALLOW_USER_SRP_AUTH
        - ALLOW_REFRESH_TOKEN_AUTH
        - ALLOW_USER_PASSWORD_AUTH
      TokenValidityUnits:
        IdToken: minutes
        AccessToken: minutes
        RefreshToken: days
      IdTokenValidity: 60
      AccessTokenValidity: 60
      RefreshTokenValidity: 30
      RefreshTokenRotation: 
        Feature: ENABLED
        RetryGracePeriodSeconds: 30

上記は Cognito ユーザープールと、ユーザープールクライアントをひとつ作成しています。
YAMLファイルの一番下でRefreshTokenRotationを設定してみました。

ALLOW_REFRESH_TOKEN_AUTH と併用できない

ただし早速上記をデプロイしてみたところ、スタック作成時に次のエラーとなりました。

"ALLOW_REFRESH_TOKEN_AUTH is not a permitted ExplicitAuthFlow when refresh token rotation is enabled.

そう、ExplicitAuthFlowsでいつもどおりALLOW_REFRESH_TOKEN_AUTHを指定していたのですが、リフレッシュトークンのローテーションを使う場合はこちらを外す必要があります。前回の記事でマネジメントコンソールを使った際にも確かに有効化が出来ませんでしたね。

ALLOW_REFRESH_TOKEN_AUTHを除外しました。

:

  CognitoUserPoolClient:
    Type: AWS::Cognito::UserPoolClient
    Properties:
      UserPoolId: !Ref CognitoUserPool
      ClientName: hoge0514userpoolclient1
      GenerateSecret: true
      AllowedOAuthFlows:
        - implicit
        - code
      AllowedOAuthFlowsUserPoolClient: true
      AllowedOAuthScopes:
        - phone
        - email
        - openid
        - profile
      CallbackURLs:
        - https://example.com/callback
      LogoutURLs:
        - https://example.com/logout
      ExplicitAuthFlows:
        - ALLOW_USER_SRP_AUTH
        # - ALLOW_REFRESH_TOKEN_AUTH
        - ALLOW_USER_PASSWORD_AUTH
      TokenValidityUnits:
        IdToken: minutes
        AccessToken: minutes
        RefreshToken: days
      IdTokenValidity: 60
      AccessTokenValidity: 60
      RefreshTokenValidity: 30
      RefreshTokenRotation: 
        Feature: ENABLED
        RetryGracePeriodSeconds: 30

今度はスタックの作成に成功し、コンソール上からリフレッシュトークン設定を確認してみると、期待どおりローテーション構成されていることが確認できました。

E9FFB285-9D89-4A4F-98EA-EDA2710D3265_1_105_c.jpeg

さいごに

本日は Amazon Cognito のリフレッシュトークンローテーションが CloudFormation でも設定できるようになったので設定を追加してみました。

機能アップデート直後しばらくは CloudFormation で実装されないことが多かったりするのですが、今回はすぐに対応されましたね。良かった。

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.

OSZAR »