从Cognito身份池的identityId获取Cognito用户池用户名

15
我正在使用AWS Congito用户池来管理帐户,并使用将该用户池作为身份提供程序的Cognito身份池。 我使用这个来通过API Gateway控制对发送到Lambda的API的访问。我的Lambda使用Java 8和Micronaut实现。所有这些都正常工作。
在Lambda中,我从HttpRequest的Principal中获取名称。
  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转换为登录信息。LookupDeveloperIdentityAPI似乎是正确的解决方法,但我无法让它正常工作。尝试使用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获取用户池用户名的最佳方法吗?
    • 如果是 - 我做错了什么?
    • 如果不是 - 有更好的方法吗?

你可以尝试使用https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_GetOpenIdTokenForDeveloperIdentity.html 推荐的方法来处理高容量操作。你确定你正在使用拥有所请求lookupDeveloperIdentity的身份池的账户的凭证吗?- https://forums.aws.amazon.com/thread.jspa?threadID=231354 对我来说,这看起来像是用户权限问题,而不是IAM角色问题。 - Jan Garaj
我也尝试了这个方法,但是得到了相同的错误信息。我确定我正在使用拥有身份池的账户的凭据——对于池中的其他操作都可以正常工作。如果这是用户权限问题...那么似乎有些奇怪...但如果是这样的话,我很想知道如何为服务器获取用户权限。 - Prisoner
1个回答

10

替代方案

为了获取用户池的用户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


1
虽然该页面上的文档说:“尽管下面的过程没有记录”,但已经接受(虽然有些晚了,但很高兴您获得了额外的声望)。希望有一种实际记录的方法来完成这个任务。 - Prisoner
有没有关于如何使用SDK实现这个的文档?如果你需要的是用户名而不是用户ID,你可以通过获取特定用户ID的适当详细信息直接从用户池中提取它。说实话,我很难找到相关文档 :( - Philipp Grigoryev

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