据我所知,AWS Lambda函数无法查找我的Cognito用户的用户名(我正在使用UserPools) .
这看起来非常奇怪,因为我认为各地的应用程序几乎总是依赖于操纵用户名 .
我已成功获得Cognito IdentityId但我无法看到任何方式将IdentityId与查找IdentityId所涉及的Cognito用户的任何内容相关联 .
有没有获得用户名的方法? IdentityId和用户名之间有什么关系?
您可以从Authorization标头获取JWT令牌,然后使用适合您语言的库对其进行解码 .
在JWT的有效载荷中是用户名 .
或者您可以使用 {...}authorizer.claims.sub 中的 sub 过滤器在 CognitoIdentityServiceProvider (http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#listUsers-property)上调用 listUsers .
{...}authorizer.claims.sub
sub
CognitoIdentityServiceProvider
listUsers
如果您使用API网关支持Lambda函数,则可以使用Cognito Authorizer直接验证您的用户池令牌,并通过 $context.authorizer.claims.preferred_username 传递从令牌中提取的用户名 .
有关此集成的更多详细信息,请访问:http://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-integrate-with-cognito.html
详细说明@ doorstuck的答案,如果您正在使用由APIG调用的Lambda和AWS_IAM授权 . 然后,您可以获得用户名和其他属性,如下所示:
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是用户池中用户的子 . 您可以轻松解码字符串以获取sub并在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' }, ..... ]
请注意,您还可以使用过滤返回的属性
AttributesToGet: [ 'STRING_VALUE', /* more items */ ],
在Params .
我们可以在body body模板中操作上下文来获取sub(用户名),它对我来说很好 . 试试这个而不是拆分你的lamda函数 .
#set($sub = $context.identity.cognitoAuthenticationProvider.split(':')[2]) { "tenantId": "$sub" }
4 回答
您可以从Authorization标头获取JWT令牌,然后使用适合您语言的库对其进行解码 .
在JWT的有效载荷中是用户名 .
或者您可以使用
{...}authorizer.claims.sub
中的sub
过滤器在CognitoIdentityServiceProvider
(http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#listUsers-property)上调用listUsers
.如果您使用API网关支持Lambda函数,则可以使用Cognito Authorizer直接验证您的用户池令牌,并通过 $context.authorizer.claims.preferred_username 传递从令牌中提取的用户名 .
有关此集成的更多详细信息,请访问:http://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-integrate-with-cognito.html
详细说明@ doorstuck的答案,如果您正在使用由APIG调用的Lambda和AWS_IAM授权 . 然后,您可以获得用户名和其他属性,如下所示:
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是用户池中用户的子 . 您可以轻松解码字符串以获取sub并在listUsers的过滤器中使用它 .
例:
数据包含有关返回用户的有用信息,其中data.Users [0] .Attributes具有您的所有用户属性 . 结果是
data.Users [0] .Attributes是
请注意,您还可以使用过滤返回的属性
在Params .
我们可以在body body模板中操作上下文来获取sub(用户名),它对我来说很好 . 试试这个而不是拆分你的lamda函数 .