Amazon Cognito ID プールを使用してユーザーに AWS サービスへのアクセスを許可する方法を教えてください。

所要時間2分
0

Amazon Cognito ユーザープールを使用して、ユーザーが AWS リソースにアクセスできるようにしたいと考えています。

解決方法

Amazon Cognito ID プールを Amazon Cognito ユーザープールと統合して、AWS リソースにアクセスするための一時的な認証情報を発行できます。ユーザープールで ID プールを設定して、認証プロバイダーを設定します。ユーザープールでユーザーを認証したら、認証フローでユーザー ID トークンを使用して ID プールから一時的な認証情報を取得します。基本的な認証フローと、一時的な認証情報を取得するための拡張認証フローがあります。

重要: 次の AWS コマンドラインインターフェイス (AWS CLI) コマンド例では、サンプル文字列のすべてのインスタンスを、ご自分の値に置き換えてください。(例えば、「example_identity_pool_id」を自分の ID プール ID に置き換えたり、「abcdef」 をお使いの AWS リージョンのアイデンティティ ID に置き換えたりします)。

基本フロー

注: AWS コマンドラインインターフェイス (AWS CLI) のコマンド実行中にエラーが発生した場合は、AWS CLI の最新バージョンを使用していることを確認してください

以下の手順を実行して、基本認証フローを使用して一時的な AWS 認証情報を取得します。

1.    古い Amazon Cognito コンソールから、[ID プールを管理] を選択します。更新する Amazon Cognito ID プールの名前を選択し、[ID プールを編集] をクリックします。[認証フローの設定] で、[基本 (クラシック) フローを許可する] を選択し、変更を保存します。

2.    ID プールでユーザー ID を生成または取得するには、GetId API を呼び出します。

get-id コマンドの例:

aws cognito-identity get-id --identity-pool-id "example_identity_pool_id" --logins cognito-idp.example_region.amazonaws.com/example_cognito_user_pool_id=example_cognito_user_id_token

出力例:

{
    "IdentityId": "region:abcdef"
}

3.    オープン ID トークンを取得するには、ステップ 2 の結果を使用して GetOpenIdToken API を呼び出します。

get-open-id-token コマンドの例:

aws cognito-identity get-open-id-token --identity-id "region:abcdef" --logins cognito-idp.example_region.amazonaws.com/example_user_pool_id=example_cognito_user_id_token

出力例:

{
    "IdentityId": "region:abcdef",
    "Token": "HIJKLMN"
}

4.    認証されたロールの一時的な認証情報を取得するには、ステップ 3 のトークンを使用して AssumeRoleWithWebIdentity API を呼び出します。

assume-role-with-web-identity コマンドの例:

aws sts assume-role-with-web-identity --role-arn "example_auth_role_arn_of_the_identity_pool" --web-identity-token "HIJKLMN" --role-session-name "example_session_name"

出力例:

{
    "Credentials": {
        AccessKeyId": "xxxxxxxxxxxx",
        SecretAccessKey": "xxxxxxxxxxxx",
        SessionToken": "xxxxxxxxxxxx",
        Expiration": ""
    },
    "SubjectFromWebIdentityToken": "",
    "AssumedRoleUser": {
        "AssumedRoleId": "",
        "Arn": ""
    },
    "Provider": "cognito-identity.amazonaws.com",
    "Audience": ""
}

強化されたフロー

拡張認証フローを使用して一時的な AWS 認証情報を取得するには、次の手順を実行します。

1.    Amazon Cognito ID プールでユーザー ID を生成または取得するには、GetId API を呼び出します。

get-id コマンドの例:

aws cognito-identity get-id --identity-pool-id "example_identity_pool_id" --logins cognito-idp.example_region.amazonaws.com/example_cognito_user_pool_id=example_cognito_user_id_token

出力例:

{
    "IdentityId": "region:abcdef"
}

2.    ID プールから一時的な認証情報を取得するには、GetCredentialsForIdentity API を呼び出します。

get-credentials-for-identity コマンドの例:

aws cognito-identity get-credentials-for-identity --identity-id region:abcdef --logins cognito-idp.example_region.amazonaws.com/example_cognito_user_pool_id = example_cognito_user_id_token

出力例:

{
    "IdentityId": "region:abcdef",
    "Credentials": {
        "AccessKeyId": "xxxxxxxxxx",
        "SecretKey": "xxxxxxxxxx",
        "SessionToken": "xxxxxxxxxx",
        "Expiration": ""
    }
}

複雑なロールベースのアクセス制御と属性ベースのアクセス制御の詳細については、「Amazon Cognito ID プールによるきめ細かなアクセス制御」を参照してください。


関連情報

ID プール (フェデレーティッドアイデンティティ) の認証フロー

AWS公式
AWS公式更新しました 1年前
コメントはありません

関連するコンテンツ