Duk*_*gal 5 javascript amazon-web-services jwt amazon-cognito aws-lambda
据我所知,AWS Lambda 函数无法查找我的 Cognito 用户的用户名(我使用的是 UserPools)。
这看起来非常奇怪,因为我原以为到处的应用程序几乎总是依赖于操作用户名。
我已成功获得 Cognito IdentityId,但我看不到任何方法将 IdentityId 与查找与 IdentityId 相关的 Cognito 用户的任何内容相关联。
有没有办法获取用户名?IdentityId 和用户名有什么关系?
我有一段时间很难找到这个问题的答案,因为网上这些线程都没有任何简洁的回应。
Authorization听起来您正在尝试在用户Authenticated使用自定义属性获得针对您的 Cognito 用户池的凭据后制定有效的策略。
我创建了一个库,用于导出一些函数,这些函数允许我捕获经过身份验证的UserPoolId用户的和,以便我可以在 lambda 中捕获我需要的,以便我实现的条件可以使用剩余的 API AWS 服务 我需要为通过我的应用程序进行身份验证的每个用户提供授权。Usernamecustom:<attribute>
这是我的图书馆:
import AWS from "aws-sdk";
// ensure correct AWS region is set
AWS.config.update({
region: "us-east-2"
});
// function will parse the user pool id from a string
export function parseUserPoolId(str) {
let regex = /[^[/]+(?=,)/g;
let match = regex.exec(str)[0].toString();
console.log("Here is the user pool id: ", match);
return match.toString();
}
// function will parse the username from a string
export function parseUserName(str) {
let regex = /[a-z,A-Z,0-9,-]+(?![^:]*:)/g;
let match = regex.exec(str)[0].toString();
console.log("Here is the username: ", match);
return match.toString();
}
// function retries UserAttributes array from cognito
export function getCustomUserAttributes(upid, un) {
// instantiate the cognito IdP
const cognito = new AWS.CognitoIdentityServiceProvider({
apiVersion: "2016-04-18"
});
const params = {
UserPoolId: upid,
Username: un
};
console.log("UserPoolId....: ", params.UserPoolId);
console.log("Username....: ", params.Username);
try {
const getUser = cognito.adminGetUser(params).promise();
console.log("GET USER....: ", getUser);
// return all of the attributes from cognito
return getUser;
} catch (err) {
console.log("ERROR in getCustomUserAttributes....: ", err.message);
return err;
}
}
Run Code Online (Sandbox Code Playgroud)
实现这个库后,您现在需要为其创建授权策略的任何 lambda 都可以使用它。
在 lambda 内部,您需要导入上面的库(我省略了下面的导入语句,您需要添加这些语句以便可以访问导出的函数),并且您可以像这样实现它们的使用:
export async function main(event, context) {
const upId = parseUserPoolId(
event.requestContext.identity.cognitoAuthenticationProvider
);
// Step 2 --> Get the UserName from the requestContext
const usrnm = parseUserName(
event.requestContext.identity.cognitoAuthenticationProvider
);
// Request body is passed to a json encoded string in
// the 'event.body'
const data = JSON.parse(event.body);
try {
// TODO: Make separate lambda for AUTHORIZATION
let res = await getCustomUserAttributes(upId, usrnm);
console.log("THIS IS THE custom:primaryAccountId: ", res.UserAttributes[4].Value);
console.log("THIS IS THE custom:ROLE: ", res.UserAttributes[3].Value);
console.log("THIS IS THE custom:userName: ", res.UserAttributes[1].Value);
const primaryAccountId = res.UserAttributes[4].Value;
} catch (err) {
// eslint-disable-next-line
console.log("This call failed to getattributes");
return failure({
status: false
});
}
}
Run Code Online (Sandbox Code Playgroud)
Cognito 的响应将提供一个包含您需要的自定义属性的数组。Console.log 来自 Cognito 的响应console.log("THIS IS THE Cognito response: ", res.UserAttributes);,检查 CloudWatch 日志中所需属性的索引号,并使用以下命令调整所需的索引:
res.UserAttributes[n]
现在,您拥有了一种authorization机制,您可以在 lambda 中结合不同条件使用该机制,以允许用户 POST 到 DynamoDB,或使用您的应用程序中的任何其他 AWS 服务,并为每个经过身份验证的用户提供正确的授权。
在您可以看到的响应中,您将看到您正在寻找的subres.UserAttributes[n]属性。
您可以从授权标头获取 JWT 令牌,然后使用适合您的语言的某个库对其进行解码。
JWT 的有效负载中是用户名。
或者,您可以使用您获得的过滤器listUsers调用CognitoIdentityServiceProvider( http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#listUsers-property ) 。sub{...}authorizer.claims.sub
| 归档时间: |
|
| 查看次数: |
7318 次 |
| 最近记录: |