【问题标题】:Getting cognito user pool username from cognito identity pool identityId从 cognito 身份池 identityId 获取 cognito 用户池用户名
【发布时间】:2020-04-21 08:48:12
【问题描述】:

我正在使用 AWS Congito 用户池进行账户管理,其中 Cognito 身份池将此用户池作为身份提供者。我正在使用它来控制通过向 Lambda 发送请求的 API 网关对 API 的访问。我的 Lambda 是使用 Micronaut 使用 Java 8 实现的。所有这些都运行良好。

在 Lambda 中,我从 Principal 中的 HttpRequest 获取名称:

  protected String resolveUser( HttpRequest request ){
    String ret = null;

    Optional<Principal> principal = request.getUserPrincipal();
    if( principal.isPresent() ){
      ret = principal.get().getName();
    }

    if( ret == null || ret.length() == 0 ){
      ret = "unknown";
    }
    return ret;
  }

在 Cognito identityId 的字符串名称中返回了什么。像这样的:

us-east-1:xxxxe650-53f4-4cba-b553-5dff42bexxxx

我想记录实际的用户登录信息,或者至少有一些方法可以在需要时将 identityId 转换为登录信息。

LookupDeveloperIdentity API 调用似乎是解决此问题的正确方法,但我无法让它工作。

尝试使用 Java 和 AWS Java SDK 2:

  protected String loadUsername( String user ){
    String ret = "unknown:"+user;
    CognitoIdentityClient cognito = CognitoIdentityClient.create();

    LookupDeveloperIdentityRequest request = LookupDeveloperIdentityRequest.builder()
      .identityPoolId( identityPoolId )
      .identityId( user )
      .build();
    LookupDeveloperIdentityResponse response = cognito.lookupDeveloperIdentity( request );
    List<String> identifiers = response.developerUserIdentifierList();
    if( identifiers != null && identifiers.size() > 0 ){
      ret = identifiers.get( 0 );
    }

    return ret;    
  }

抛出异常

software.amazon.awssdk.services.cognitoidentity.model.NotAuthorizedException:您无权访问此身份(服务:CognitoIdentity,状态代码:400,请求 ID:64e36646-612b-4985-91d1-82aca770XXXX)

尝试通过 CLI 执行此操作会产生类似的结果:

aws cognito-identity lookup-developer-identity --identity-id us-east-1:xxxxe650-53f4-4cba-b553-5dff42bexxxx --identity-pool-id us-east-1:xxxx0aa1-89f9-4418 -be04-7e83c838xxxx --max-results=10

调用 LookupDeveloperIdentity 操作时发生错误 (NotAuthorizedException):您无权访问此身份

我已确保现有的 IAM 政策应该能够处理此问题,当我尝试使用没有此政策的角色时,我会收到另一个错误

    {
        "Effect": "Allow",
        "Action": [
            "cognito-identity:LookupDeveloperIdentity"
        ],
        "Resource": [
            "arn:aws:cognito-identity:us-east-1:##########:identitypool/us-east-1:xxxx0aa1-89f9-4418-be04-7e83c838xxxx"
        ]
    }

所以问题归结为:

  • 这是从身份池 ID 获取用户池用户名的最佳方式吗?
    • 如果是 - 我做错了什么?
    • 如果不是 - 有什么更好的方法?

【问题讨论】:

  • 您能否尝试docs.aws.amazon.com/cognitoidentity/latest/APIReference/… 为大容量操作推荐的方法。 Are you sure you are using the credentials from the account which owns the identity pool you are requesting lookupDeveloperIdentity for? - forums.aws.amazon.com/thread.jspa?threadID=231354 对我来说,这看起来像是用户权限,而不是 IAM 角色问题。
  • 我也试过了,得到了同样的错误信息。我确定我使用的是拥有身份池的帐户的凭据 - 池上的其他操作工作正常。作为用户权限似乎......奇怪......但如果是这样,我很想知道如何获得服务器的用户权限。

标签: amazon-web-services amazon-cognito micronaut aws-java-sdk micronaut-aws


【解决方案1】:

替代方法

为了检索用户的用户池用户 ID,您可以在 lambda 中检索:

authProvider = event.requestContext.identity.cognitoAuthenticationProvider;

这将返回一个字符串,其中包含用户的用户池用户 ID,它看起来像:

cognito-idp.us-east-1.amazonaws.com/us-east-1_xxxxxxxxx,cognito-idp.us-east-1.amazonaws.com/us-east-1_aaaaaaaaa:CognitoSignIn:qqqqqqqq-1111-2222-3333-rrrrrrrrrrrr

其中 us-east-1_aaaaaaaaa 是用户池 ID,qqqqqqqq-1111-2222-3333-rrrrrrrrrrrr 是用户池用户 ID。然后,您可以拆分字符串并提取用户 ID。

请注意,这些信息会因您使用的身份验证提供商而异。

然后,如果您需要用户名而不是用户 ID,您可以通过获取该特定用户 ID 的相应详细信息,直接从用户池中提取它。

参考

https://serverless-stack.com/chapters/mapping-cognito-identity-id-and-user-pool-id.html

【讨论】:

  • 接受(迟了些,但很高兴你得到了奖励代表),即使该页面上的文档说:“虽然下面的过程没有记录” 希望有一种实际记录的方法。
  • 是否有任何文档说明如何使用 SDK 实现这一目标? Then if you need the username instead of user ID you can extract it directly from user Pool by getting the appropriate details for that specific user ID. ?老实说,我正在努力寻找任何东西:(
猜你喜欢
  • 2018-02-11
  • 2018-07-17
  • 2016-10-23
  • 2016-09-26
  • 2019-04-01
  • 2017-02-28
  • 2016-11-10
  • 2017-10-25
  • 2018-11-07
相关资源
最近更新 更多