How to get the Cognito Identity id in AWS Lambda

2020-02-20 08:06发布

How can I get the identity id of the user (logged in by AWS Cognito) that invoked an AWS Lambda function? Do I have to use the SDK on the Lambda function to get the identity id?

7条回答
干净又极端
2楼-- · 2020-02-20 08:31

For a Python Lambda, invoked via Javascript AWS SDK / Cognito / Amplify...

https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html

context.identity.cognito_identity_id

It should look something like this:

{aws region}:{ GUID }

Assuming you are using an Identity Pool, this will return the Cognito Federated Identity, that can be used for fine grained access control. This is safer than relying on the Javascript payload containing the identity id.

The Cognito Identity Pool Auth Role will need to have Lambda:InvokeFunction policy, otherwise the user won't be able to invoke the function in the first place.

Edit: This works when calling the Lambda function DIRECTLY, not via API Gateway.

Edit2: The Cognito user is allowed to call the lambda because it is explicitly set in the IAM Cognito Auth role.

查看更多
叼着烟拽天下
3楼-- · 2020-02-20 08:32

Per the docs, it looks like information about the identity provider would only be available for an invoke through the Mobile SDK.

To get around this, one option is to pass the identity ID to the function manually as part of the event. Assuming you are doing something like AWS.config.credentials = new AWS.CognitoIdentityCredentials(...) then you should be able to get the ID via AWS.config.credentials.identityId (after the credentials are refreshed).

EDIT: A better option for identity validation is to let Cognito/IAM handle it, and assume that if a user can successfully invoke a Lambda function, that means they are allowed to. In this case to manage per-user validation, take a look at whitelisting.

查看更多
相关推荐>>
4楼-- · 2020-02-20 08:44

My observation is the following.

If you call the API Gateway with a signed Request where you actually provide the accesskey, secret and sessionToken which you can extract via (JS SDK):

AWS.config.credentials = new AWS.CognitoIdentityCredentials(...)
AWS.config.credentials.get(..)

And assumed that your lambda is called from API-Gateway via LAMBDA_PROXY and Authorizer AWS_IAM. You can only access user stuff in lambda with:

exports.create = function (event, context) {
   secdata = event.requestContext.identity.cognitoAuthenticationProvider;
}

Then you will get, apart from other stuff, the "sub" of the cognito UserPool User. So if you really want to know more about the user, it seems you need to ask AWS again via SDK call.

查看更多
▲ chillily
5楼-- · 2020-02-20 08:45

In AWS javascript SDK inside lambda function just use context.identity.cognitoIdentityId It is working for me

查看更多
啃猪蹄的小仙女
6楼-- · 2020-02-20 08:45

If you go through API Gateway, you can pass the cognito id (as well as the user arn and other useful information) to Lambda. This solved the issue for me.

http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

查看更多
▲ chillily
7楼-- · 2020-02-20 08:50

I was using Kotlin and my Lambda handler was

override fun handleRequest(event: APIGatewayProxyRequestEvent, context: Context): APIGatewayProxyResponseEvent 

But event.requestContext had no authorizer. The solution was to upgrade the dependency in build.gradle from com.amazonaws:aws-lambda-java-events:2.1.0 to com.amazonaws:aws-lambda-java-events:2.2.7. After that, I got the username as follows.

val claims = requestContext.authorizer["claims"] as Map<String, String>
println(claims["cognito:username"])
println(claims["email"])
查看更多
登录 后发表回答