1
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 1 year has passed since last update.

AWS CLIで、リフレッシュトークンを使ってAmazon Cognitoのアクセストークン・IDトークンを再取得したい

Posted at

Whats'?

AWS CLI(v2)で、Amazon Cognitoからリフレッシュトークンを使ってアクセストークン、IDトークンを再取得するコマンドをメモ、ということで。

認証のコマンド例はよく見つかるのですが、こちらはなかなか見当たらないな、と思いまして。

--auth-flow REFRESH_TOKEN_AUTH

ヒントは、こちらに書かれています。

ユーザープール API により新しい ID とアクセストークンを取得する際に、更新トークンを使用するには、API オペレーションの AdminInitiateAuth または InitiateAuth を使用します。AuthFlow パラメータの REFRESH_TOKEN_AUTH を渡します。認証パラメータの AuthParameters は、キーが "REFRESH_TOKEN" であり、値が実際の更新トークンであるキーバリューマップです。

更新トークンの使用 / 新しい更新トークン(API)を開始

AWS CLI v2のcognito-idpコマンド、admin-initiate-authサブコマンドを使用することになります。

--auth-flowにはREFRESH_TOKEN_AUTHを指定し、--auth-parametersとしてREFRESH_TOKENにリフレッシュトークンを指定すればよい、となりますね。

環境

確認環境はこちら。

$ aws --version
aws-cli/2.13.0 Python/3.11.4 Linux/5.15.0-76-generic exe/x86_64.ubuntu.22 prompt/off

確認

以下のコマンドを実行します。

$ aws cognito-idp admin-initiate-auth --user-pool-id [ユーザープールid] --client-id [クライアントID] --auth-flow REFRESH_TOKEN_AUTH --auth-parameters REFRESH_TOKEN=[リフレッシュトークン] --query 'AuthenticationResult.IdToken' --output text

実行結果はこちら。

{
    "ChallengeParameters": {},
    "AuthenticationResult": {
        "AccessToken": "[アクセストークン]",
        "ExpiresIn": [有効期限],
        "TokenType": "[トークンタイプ]",
        "IdToken": "[IDトークン]"
    }
}

特定の項目だけ抽出する場合。たとえば、--query 'AuthenticationResult.IdToken'

$ aws cognito-idp admin-initiate-auth --user-pool-id [ユーザープールid] --client-id [クライアントID] --auth-flow REFRESH_TOKEN_AUTH --auth-parameters REFRESH_TOKEN=[リフレッシュトークン] --query 'AuthenticationResult.IdToken'

変数に格納したい場合。--output textで。

$ ID_TOKEN=$(aws cognito-idp admin-initiate-auth --user-pool-id [ユーザープールid] --client-id [クライアントID] --auth-flow REFRESH_TOKEN_AUTH --auth-parameters REFRESH_TOKEN=[リフレッシュトークン] --query 'AuthenticationResult.IdToken' --output text)
1
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
1
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?