amazon-web-services - 从 cognito 身份池 identityId 获取 cognito 用户池用户名
问题描述
我正在使用 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:xxxe650-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 获取用户池用户名的最佳方式吗?
- 如果是 - 我做错了什么?
- 如果不是 - 这样做的更好方法是什么?
解决方案
替代方法
为了检索用户的用户池用户 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
推荐阅读
- mysql - MariaDB 启动时出错,不断重启
- python - python - 如何在python中的两个空格之后从字符串中删除所有字符?
- spring-boot - 带有 Spring Boot JUnit 5 测试的 Wiremock:测试运行后使用的地址
- javascript - 运行流星时出错。找不到或执行文件
- webpack - webpack-dev-server 在我的项目中不起作用
- c - C语言中红黑树的简单结构定义
- javascript - 无论如何要检查 vue.js 中是否存在 $parent
- c - 字符串上的 fscanf 导致分段错误
- python - 无法在 ubuntu 中使用 pip3 为 django 安装 mysqlclient
- html - 内容属性不继承 CSS 类