根据 API 网关中的 Cognito 身份池标识查找 Cognito 用户池用户
Lookup cognito userpool uset based on cognito dentity pool identity in API Gateway
我是 AWS 新手,所以请放轻松 :)
我已经设置了一个概念证明,以证明由 lambda 支持的经过身份验证的 API 具有以下组件。
API 网关 -> 由 Lambda 支持
AWS Cognito UserPool 支持的联合身份
我在 API 网关中设置了授权方,以使用联合身份池提供的 IAM 角色。
我可以看到身份 (ap-southeast-2:<GUID>
) 从 https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference
进入网关(在我的集成请求映射模板 "$context.identity.cognitoIdentityId"
中使用它)
如何从网关或 lambda 将 'ap-southeast-2:<GUID>'
解析回驻留在用户池中的源身份。 (例如,从中提取自定义属性之一)
其他相关信息,我正在为调用 API 网关的客户端使用 Amplify-AWS。
TIA。
如您所见,作为身份提供者的 Cognito 与作为用户池的 Cognito 不同。
Federated Identities 提供了一种方法,可以让已识别的人访问您的 AWS 资源。身份提供者给你的 identity_id
几乎可以被认为是一个跟踪代码。 CIP(Congito [Federated] Identity Provider)允许您通过任意数量的提供者(不仅仅是用户池)登录来获取身份 ID,甚至根本不登录。
用户池为您提供了一种管理应用程序用户的方法(即一组用户名、电子邮件、密码等)。
这就是很难从 identity_id
返回用户池用户的原因(因为,不能保证它 是 用户池用户,它很可能成为来自 Facebook 的人)。
但是,根据您所说的,identity_id
来自 UserPool 身份验证的假设是安全的。这意味着您有两个选择:
官方方式将使用identity:GetOpenIdToken to convert identity_id
(you can ignore the logins
part of the request) into an OpenId token. You can then use this token against the userpools:GetUser端点。这里有一些陷阱,比如确保您使用允许您查看您关心的所有属性的范围进行身份验证。
然而奇怪的是,cognitoAuthenticationProvider
的值不是不透明的,并且可以(非正式地)解码:
// Cognito authentication provider looks like:
// 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
// Where us-east-1_aaaaaaaaa is the User Pool id
// And qqqqqqqq-1111-2222-3333-rrrrrrrrrrrr is the User Pool User Id
上面的示例,包含有关如何将其与 userpools:AdminGetUser can be found here: https://serverless-stack.com/chapters/mapping-cognito-identity-id-and-user-pool-id.html
一起使用的更多详细信息
我是 AWS 新手,所以请放轻松 :) 我已经设置了一个概念证明,以证明由 lambda 支持的经过身份验证的 API 具有以下组件。
API 网关 -> 由 Lambda 支持 AWS Cognito UserPool 支持的联合身份 我在 API 网关中设置了授权方,以使用联合身份池提供的 IAM 角色。
我可以看到身份 (ap-southeast-2:<GUID>
) 从 https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference
"$context.identity.cognitoIdentityId"
中使用它)
如何从网关或 lambda 将 'ap-southeast-2:<GUID>'
解析回驻留在用户池中的源身份。 (例如,从中提取自定义属性之一)
其他相关信息,我正在为调用 API 网关的客户端使用 Amplify-AWS。
TIA。
如您所见,作为身份提供者的 Cognito 与作为用户池的 Cognito 不同。
Federated Identities 提供了一种方法,可以让已识别的人访问您的 AWS 资源。身份提供者给你的
identity_id
几乎可以被认为是一个跟踪代码。 CIP(Congito [Federated] Identity Provider)允许您通过任意数量的提供者(不仅仅是用户池)登录来获取身份 ID,甚至根本不登录。用户池为您提供了一种管理应用程序用户的方法(即一组用户名、电子邮件、密码等)。
这就是很难从 identity_id
返回用户池用户的原因(因为,不能保证它 是 用户池用户,它很可能成为来自 Facebook 的人)。
但是,根据您所说的,identity_id
来自 UserPool 身份验证的假设是安全的。这意味着您有两个选择:
官方方式将使用identity:GetOpenIdToken to convert identity_id
(you can ignore the logins
part of the request) into an OpenId token. You can then use this token against the userpools:GetUser端点。这里有一些陷阱,比如确保您使用允许您查看您关心的所有属性的范围进行身份验证。
然而奇怪的是,cognitoAuthenticationProvider
的值不是不透明的,并且可以(非正式地)解码:
// Cognito authentication provider looks like:
// 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
// Where us-east-1_aaaaaaaaa is the User Pool id
// And qqqqqqqq-1111-2222-3333-rrrrrrrrrrrr is the User Pool User Id
上面的示例,包含有关如何将其与 userpools:AdminGetUser can be found here: https://serverless-stack.com/chapters/mapping-cognito-identity-id-and-user-pool-id.html
一起使用的更多详细信息