Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handler events.APIGatewayProxyRequest Cognito fields empty #133

Open
jackwellsxyz opened this issue Oct 16, 2018 · 1 comment
Open

Handler events.APIGatewayProxyRequest Cognito fields empty #133

jackwellsxyz opened this issue Oct 16, 2018 · 1 comment
Labels

Comments

@jackwellsxyz
Copy link

jackwellsxyz commented Oct 16, 2018

I've deployed an API Gateway that calls a lambda_proxy handler. My handler function receives in a context and an events.APIGatewayProxyRequest. Inside the request, there are fields for the Cognito Identity ID and Pool ID, but both are empty. This may be related to Issue #106. While I saw a workaround to manually verify JWTs, that would seem to largely defeat the purpose of API Gateway Cognito authentication and certainly the request's cognito fields.

Code to reproduce the error:

package main

func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
	lc, _ := lambdacontext.FromContext(ctx)

	fmt.Println("Context cognito identity id:", lc.Identity.CognitoIdentityID)
	fmt.Println("Context Cognito pool:", lc.Identity.CognitoIdentityPoolID)
	fmt.Println("Request context cognito id:", request.RequestContext.Identity.CognitoIdentityID)

	return events.APIGatewayProxyResponse{}, nil
}

func main() {
  lambda.Start(Handler)
}

I also dug into the aws-lambda-go/lambda/function.go Invoke() function and verified that the req.CognitoIdentityID and req.CognitoIdentityPoolId are both empty strings. I verified this using a deployed Lambda function that I tested using API Gateway and Postman (with valid JWT Cognito tokens) using Printlns also in the function.go Invoke() function.

@piotrkubisa
Copy link
Contributor

piotrkubisa commented Oct 23, 2018

Whether you define AuthorizationType in API Gateway method as a COGNITO_USER_POOLS then you don't need write logic to validate JWT by yourself. Below an extract from CloudFormation template written in yaml syntax:

# snip...

  ApiGatewayMethodCreateItem:
    Type: AWS::ApiGateway::Method
    Properties:
      RestApiId: !Ref ApiGatewayApi
      ResourceId: !GetAtt ApiGatewayApi.RootResourceId
      HttpMethod: POST
      AuthorizationType: "COGNITO_USER_POOLS"
      AuthorizerId: !Ref ApiGatewayAuthorizer
      Integration:
        Type: AWS_PROXY
        IntegrationHttpMethod: POST
        Uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${LambdaFunction.Arn}/invocations

  ApiGatewayAuthorizer:
    Type: AWS::ApiGateway::Authorizer
    Properties:
      Name: !Sub "${AWS::StackName}-authorizer"
      Type: "COGNITO_USER_POOLS"
      ProviderARNs:
        - Fn::Sub:
          - "arn:aws:cognito-idp:${AWS::Region}:${AWS::AccountId}:userpool/${UserPool}"
          - UserPool: "XXXXX"
      IdentitySource: "method.request.header.Authorization"
      RestApiId: !Ref ApiGatewayApi

  LambdaApiGatewayExecutionPermission:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      FunctionName: !GetAtt LambdaFunction.Arn
      Principal: apigateway.amazonaws.com
      SourceArn: !Sub "arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ApiGatewayApi}/*/*"

If do recall correctly, also request.RequestContext.Identity.CognitoIdentityID (and other fields related to Cognito) are populated whether you use AuthorizationType: "AWS_IAM" and you are using the IAM keys obtained from AWS Cognito Identity Pool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants