【问题标题】:Get identity provider oauth tokens in AWS cognito user pool在 AWS cognito 用户池中获取身份提供商 oauth 令牌
【发布时间】:2018-08-20 19:07:34
【问题描述】:

在为用户池注册身份提供者 oauth 令牌时,是否有任何可能的方法来获取它们?我需要离线访问谷歌用户访问和刷新令牌。到目前为止,我已经尝试过:

1) 使用amazon-cognito-auth-js

我可以创建用户,但无法获取 oauth 访问和刷新令牌。看起来属性映射应该可以实现这一点,但是在映射选择框中有no option for access/refresh tokens

2) 使用google javascript api登录并通过cognitoidentityserviceprovider.adminCreateUser创建用户

这个流程是:

  • 通过gapi登录并获取oauth authorization_code
  • authorization_code 发送到 HTTP lambda 函数以交换访问和刷新令牌。
  • 使用adminCreateUser 创建一个新的用户池用户
  • 向该用户添加刷新/访问令牌。

虽然,似乎没有办法通过身份提供者adminCreateUser。只有用户名/密码。

3) 使用 gapi 登录并进行 ajax 调用到 oauth2/idpresponse

类似于上一个流程,我会:

  • 通过gapi登录并获取oauth authorization_code

  • authorization_code 发送到 HTTP lambda 函数以交换访问和刷新令牌。

  • https:<domain>.auth.us-east-1.amazoncognito.com/oauth2/idpresponse?code=<authorization_code> 发出GET 请求,这似乎是amazon-cognito-auth-js 库中注册新用户的步骤。

  • 向该用户添加刷新/访问令牌。

但是,对oauth2/idpresponse 的 GET 请求总是失败。除了authorization_code 之外,还有其他必需的查询参数,我不知道如何获取(状态?)。

【问题讨论】:

    标签: amazon-web-services oauth amazon-cognito


    【解决方案1】:

    如果有人遇到同样的问题,这里是答案

    注意 - 操作不当,会将敏感属性暴露给客户端。

    您需要创建 2 个版本的属性 - customdev:custom,将 oidc 提供程序属性映射到 custom (看起来像 dev:custom 不可映射),然后在 TokenGeneration_HostedAuth 触发器中您需要获取这些custom 属性,设置dev:custom 属性,然后删除customs。

    似乎是一种调整,但我没有看到其他方法可以做到这一点并确保令牌安全。

    解决方案是在您的用户池中创建自定义属性,然后为身份提供者映射这些属性。看起来像:

    'custom:refresh_token': refresh_token
    'custom:id_token': id_token
    'custom:access_token': access_token
    

    Cloudformation 模板:

    用户池

    ....
    Schema: [
        {
            AttributeDataType: 'String',
            DeveloperOnlyAttribute: true,
            Mutable: true,
            Name: 'refresh_token',
            Required: false,
        },
        {
            AttributeDataType: 'String',
            DeveloperOnlyAttribute: true,
            Mutable: true,
            Name: 'access_token',
            Required: false,
        },
        {
            AttributeDataType: 'String',
            DeveloperOnlyAttribute: true,
            Mutable: true,
            Name: 'id_token',
            Required: false,
        },
        {
            AttributeDataType: 'String',
            Mutable: true,
            Name: 'refresh_token',
            Required: false,
        },
        {
            AttributeDataType: 'String',
            Mutable: true,
            Name: 'access_token',
            Required: false,
        },
        {
            AttributeDataType: 'String',
            Mutable: true,
            Name: 'id_token',
            Required: false,
        },
    ],
    ....
    

    用户池身份提供者

    ....
    AttributeMapping: {
        'custom:refresh_token': 'refresh_token',
        'custom:access_token': 'access_token',
        'custom:id_token': 'id_token',
    },
    ....
    

    【讨论】:

      猜你喜欢
      • 2020-08-14
      • 2018-10-08
      • 2020-06-21
      • 1970-01-01
      • 2020-04-25
      • 2021-11-24
      • 2018-08-29
      • 2018-11-06
      • 2020-04-21
      相关资源
      最近更新 更多