如何通过 api 网关将 headers 中的认知身份验证令牌传递给 lambda 函数
How to pass cognito authentication token in the headers through api gateway to a lambda function
我正在做一个 full-stack 项目。我使用 AWS cognito 来完成身份验证部分。
而在我的front-end上,我可以成功获取idToken并放入方法headers中。
这是获取方法代码:
Axios.get(`url`, {
headers: {
'Authorization': Token
}
})
在后端,我使用 AWS api 网关和 lambda。据我了解,如果我想在lamdba中获取令牌,我必须在APIgateway的集成请求中设置映射模板。
所以这就是我写的,但我无法获得 idToken。
有人知道获取idToken的正确方法吗?
{
"idToken":"$context.authorizer.claims.authorization"
}
我自己关闭这个问题。
经过我的搜索,如果你使用AWS Auth cognit,你想获取token中的属性,请参考这个文档:
idToken 负载
https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html
api 网关映射模板:
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference.
例如:如果你想从 id 令牌中获取用户名,在映射模板上,它应该是:
{
"userName" : "$context.authorizer.claims['cognito:username']"
}
我正在做一个 full-stack 项目。我使用 AWS cognito 来完成身份验证部分。 而在我的front-end上,我可以成功获取idToken并放入方法headers中。 这是获取方法代码:
Axios.get(`url`, {
headers: {
'Authorization': Token
}
})
在后端,我使用 AWS api 网关和 lambda。据我了解,如果我想在lamdba中获取令牌,我必须在APIgateway的集成请求中设置映射模板。 所以这就是我写的,但我无法获得 idToken。 有人知道获取idToken的正确方法吗?
{
"idToken":"$context.authorizer.claims.authorization"
}
我自己关闭这个问题。 经过我的搜索,如果你使用AWS Auth cognit,你想获取token中的属性,请参考这个文档:
idToken 负载 https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html api 网关映射模板: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference.
例如:如果你想从 id 令牌中获取用户名,在映射模板上,它应该是:
{
"userName" : "$context.authorizer.claims['cognito:username']"
}