user1283776
user1283776

Reputation: 21764

Get I Access Cognito Identity in Lambda function? Full example with some part of the puzzle missing

I have read some AWS docs and many questions about accessing Cognito Identity in a Lambda function and this is my best attempt so far. But it is not working. Tell me what might be missing!

Client

I call the Lambda function from my client

fetch('api/public/libraries/sign-out-discourse', {
    method: 'GET',
    headers: new Headers([
        ['Accept', 'application/json'],
        ['Content-Type', 'application/json'],
        // I get the idToken from CognitoUser.getSession => getIdToken(). It is possible to get a jwtToken without decoded payload with getIdToken().getJwtToken(). I think it is the idToken and not the jwtToken I should send to the API Gateway, but I am not sure.
        ['Authorization', idToken],
    ]),
})

API Gateway

Resource method

GET

Settings

Auth: AWS_IAM
Integration type: Lambda Function
// I don't know if Execution role is relevant
Execution role: arn:aws:iam::*:user/*
Invoke with caller credentials: true
// I don't know if Credentials cache is relevant
Credentials cache: Add caller's principal to the cache key

Mapping template

I have no idea what I am doing here I don't know if this template does anything for GET requests where there is no body being sent from the client.

Settings

Request body passthrough: When there are no templates defined (recommended)
Content-Type: application/json

Template code

// The template is based on the default template. The only thing I have added is username to event and context.

#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
    #set($params = $allParams.get($type))
"$type" : {
    #foreach($paramName in $params.keySet())
    "$paramName" : "$util.escapeJavaScript($params.get($paramName))"
        #if($foreach.hasNext),#end
    #end
}
    #if($foreach.hasNext),#end
#end
},
"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
    #if($foreach.hasNext),#end
#end
},
"context" : {
    "account-id" : "$context.identity.accountId",
    "api-id" : "$context.apiId",
    "api-key" : "$context.identity.apiKey",
    "authorizer-principal-id" : "$context.authorizer.principalId",
    "caller" : "$context.identity.caller",
    "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
    "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
    "cognito-identity-id" : "$context.identity.cognitoIdentityId",
    "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
    "http-method" : "$context.httpMethod",
    "stage" : "$context.stage",
    "source-ip" : "$context.identity.sourceIp",
    "user" : "$context.identity.user",
    "user-agent" : "$context.identity.userAgent",
    "user-arn" : "$context.identity.userArn",
    "request-id" : "$context.requestId",
    "resource-id" : "$context.resourceId",
    "resource-path" : "$context.resourcePath",
    "username" : "$context.authorizer.claims['cognito:username']"
    }
}
"event" : {
    "username" : "$context.authorizer.claims['cognito:username']"
}

Authorizer

I added an authorizer:

Name: Something something
Type: Cognito
Cognito user pool: Choose the user pool from the drop down
Token Source: Authorization
Token Validation: //blank

Lambda function

The runtime is Node 6.

'use strict';

const util = require('util');

exports.handler = (event, context, callback) => {
    console.log(util.inspect(event, {
        showHidden: false,
        depth: null
    }));
    console.log(util.inspect(context, {
        showHidden: false,
        depth: null
    }));
    console.log(util.inspect(callback, {
        showHidden: false,
        depth: null
    }));
}

Console logs

username is not a property on event and not a property on context.

the event object has a disappointing identity property.

identity: 
{
    cognitoIdentityPoolId: null,
    accountId: null,
    cognitoIdentityId: null,
    caller: null,
    sourceIp: 'redacted',
    accessKey: null,
    cognitoAuthenticationType: null,
    cognitoAuthenticationProvider: null,
    userArn: null,
    userAgent: 'Amazon CloudFront',
    user: null },
    apiId: 'redacted' },
    body: null,
    isBase64Encoded: false 
}

Upvotes: 0

Views: 2280

Answers (1)

Quetzalcoatl
Quetzalcoatl

Reputation: 89

If you have Use Lambda Proxy Integration selected in the Integration Request for the lambda then all the token's claims will be passed through on event.requestContext.authorizer.claims.

Got the answer from here: get-cognito-user-pool-identity-in-lambda-function

Upvotes: 1

Related Questions