据我所知,AWS Lambda函数无法查找Cognito用户的用户名(我正在使用UserPools)。有没有办法在AWS Lambda中获得Cognito用户名?
这似乎非常奇怪,因为我一直认为应用程序无处不在总是取决于操作用户名。
我已成功获取Cognito IdentityId,但我无法看到任何将IdentityId与查找IdentityId所涉及的Cognito用户的任何内容相关联的方法。
有没有获取用户名的方法? IdentityId和用户名之间有什么关系?
据我所知,AWS Lambda函数无法查找Cognito用户的用户名(我正在使用UserPools)。有没有办法在AWS Lambda中获得Cognito用户名?
这似乎非常奇怪,因为我一直认为应用程序无处不在总是取决于操作用户名。
我已成功获取Cognito IdentityId,但我无法看到任何将IdentityId与查找IdentityId所涉及的Cognito用户的任何内容相关联的方法。
有没有获取用户名的方法? IdentityId和用户名之间有什么关系?
您可以从授权标头获取JWT令牌,然后使用您的语言的某个库对其进行解码。
在JWT的有效载荷中是用户名。
或者,你可以调用与sub
的过滤器,你在{...}authorizer.claims.sub
得到listUsers
上CognitoIdentityServiceProvider
(http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#listUsers-property)。
如果你面前有API网关的lambda函数可以使用Cognito授权者直接验证您的用户池令牌,并传递通过$ context.authorizer.claims.preferred_username从令牌提取的用户名
有关此集成的更多详细信息,请访问:http://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-integrate-with-cognito.html
详细说明@ doorstuck的答案,如果您使用APIG与AWS_IAM授权调用的Lambda。然后,你可以得到的用户名和其他属性如下:
的event.requestContext.identity.cognitoAuthenticationProvider是一个字符串,看起来像
“cognito-idp.ap-northeast-1.amazonaws.com/ ap-northeast-1_xxxxxxx,cognito-idp.ap-northeast-1.amazonaws.com/ ap-northeast-1_xxxxxx:CognitoSignIn:SSSSSSSS“
SSSSSSSS是User Pool中用户的子集。您可以轻松地解码字符串以获取该子并在listUsers的过滤器中使用它。
实施例:
const provider =
event.requestContext.identity.cognitoAuthenticationProvider;
const sub=provider.split(':')[2];
const Params = {
UserPoolId: 'xxxxxxxxx', /* required */
Filter: "sub=\""+ sub + "\"",
Limit: 1
};
cognitoidentityserviceprovider.listUsers(Params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data.Users[0].Attributes);
});
的数据包括有关所述返回的用户,其中data.Users [0] .Attributes具有所有的用户属性的有用信息。 结果是
[ { Username: 'xxxxx',
Attributes: [Object],
UserCreateDate: 2017-09-12T04:52:50.589Z,
UserLastModifiedDate: 2017-10-24T01:50:00.109Z,
Enabled: true,
UserStatus: 'CONFIRMED' } ] }
data.Users [0] .Attributes是
[ { Name: 'sub', Value: 'SSSSSSS' },
{ Name: 'address', Value: 'xxxxxxxxi' },
{ Name: 'email_verified', Value: 'true' },
..... ]
请注意,您还可以通过使用PARAMS
AttributesToGet: [
'STRING_VALUE',
/* more items */
],
过滤返回的属性。
我需要在后端得到这个。你的建议可以在后台完成吗? –
@DukeDougal我的建议是针对后端。查看后端的授权标题。它是一个编码的JWT令牌,您可以解码并查看。包含有用的数据。 – doorstuck