AWS Cognito - 凭证问题

4

我正在尝试使用AWS Cognito进行身份验证(使用谷歌),并授权用户,以分配给已授权的用户IAM角色。

到目前为止,我已经按照以下步骤进行了操作:

  1. Use the authorization end point to fire up Google OAuth process http://docs.aws.amazon.com/cognito/latest/developerguide/authorization-endpoint.html

    I am using "Grant flow" I receive a such as code=b3e8bca6-5a01-45db-b4c6-cd6900d0xxxx

  2. Make a post request to oath/token http://docs.aws.amazon.com/cognito/latest/developerguide/token-endpoint.html

    I receive the following information:

    "id_token": "eyJraWQiOiJJR2NVdHJcL3pOa3pQK1lre...........",
    "access_token": "eyJraWQiOiJCbWx0cjJvMnJlVGhHW..........",
    "refresh_token": "eyJjdHkiOiJKV1QiLCJlbmMiOi............",
    "expires_in": 3600,
    "token_type": "Bearer"
    
  3. Try to fetch the AWS credntions using the CognitoIdentityCredentials

    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: environment.identityPoolId, // Federated ID eu-west-2:af47703f-350c-4018-ae6a-xxxxxx
        RoleArn: environment.roleArn,// IAM role
        Logins: { 'accounts.google.com': data.id_token },
    });
    
    AWS.config.getCredentials((error) => {
        if(error) console.log("Error: ", error);
        this.creds = AWS.config.credentials;
    });
    
我遇到了一个错误请求500错误。
{"__type":"NotAuthorizedException","message":"Invalid login token. Issuer doesn't match providerName"}

几个问题

  1. 执行步骤的顺序是否正确?
  2. 如何获取CognitoUserId?id_token是一个非常长的字符串,但不确定可以从中提取什么信息?
  3. 最后,如何获取accessKey以进行AWS调用?

任何帮助或指导将不胜感激。

谢谢

1个回答

4

步骤顺序是否正确?

我认为是的。

id_token 是一个非常长的字符串,但不确定能从中提取什么信息?

id_tokenaccess_token 都是 JWT。您可以对点之间的字符串进行base64解码以提取令牌内容。通常我们关注中间部分或 负载

您可以将这些令牌粘贴到像 this one 这样的解码器中,在浏览器中查看内容。在javascript中,atob() 的使用方式与您期望的相同。

我不确定您想要哪个用户ID,但如果用户名足够,id_token 包含一个 cognito:username 键。

最后,我如何获取accessKey以进行AWS调用?

将您的Logins映射中的提供程序更改。

如果您直接与Google交谈,而不是通过Cognito(通过/oauth2/authorize)与Google交谈,则应在Logins映射中使用accounts.google.com,就像您的示例所示。

但是,您收到的令牌来自Cognito,而不是来自Google。两个令牌都包含一个iss(发行者)键,这很可能是您的用户池ID。这是您应该在Logins映射中使用的值。假设发行者是您的用户池:

Logins: {
    'cognito-idp.us-east-1.amazonaws.com/us-east-1_xxxxxxxx': token
}

谢谢!我已经成功解码令牌并使用上述 iss 密钥更正了 Logins 映射,但我仍然收到相同的错误消息 {"__type":"NotAuthorizedException","message":"Invalid login token. Issuer doesn't match providerName"}。在 Login 映射中,通过“令牌”我假设您是指 id_token。 - maxkart
以下是我的id_token详细信息:“identities”:[ { "userId": "1068...", "providerName": "Google", "providerType": "Google", "issuer": null, // << 由于这个是发行者和提供者不匹配的问题吗?? "primary": "true", } ], - maxkart
1
谢谢!终于成功了,我不得不从iss密钥中去掉https://。 - maxkart
@maxkart,您在data.id_token中使用哪个令牌? - Teddy Kossoko
1
@TeddyKossoko,我不理解你的问题。这里有三个令牌:id、access和refresh。我使用id来获取用户详细信息,使用access进行授权。 - maxkart

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接