Skip to content

Add Authorizer struct to APIGateway Event #34

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

Merged

Conversation

richwolf
Copy link
Contributor

@richwolf richwolf commented May 3, 2023

Add an Authorizer struct to the APIGateway (v1) event

Motivation:

When setting up an API Gateway REST API (as opposed to the v2 HTTP API) with Lambda integration, a resource endpoint can be configured to use "Lambda Proxy Integration"…which results in the v1-style APIGatewayRequest event object being sent to a Swift Lambda handler (as opposed to the APIGatewayV2Request object, which gets sent whenever API Gateway is configured as an "HTTP" API). The APIGatewayRequest event struct does not decode an authorizer…which is included within Lambda Proxy Integration JSON whenever Cognito is attached to REST endpoints. The authorizer contains a claims dictionary (very much like the one found in the APIGatewayV2Request object):

  claims: {
    sub: '2592124a-27bf-4e30-b95f-2f21c862fc82',
    event_id: 'fbf7193a-e3b4-462b-95b4-4df60a9fe410',
    token_use: 'access',
    scope: 'aws.cognito.signin.user.admin phone openid profile email',
    auth_time: '1683085806',
    iss: 'https://cognito-idp.us-east-1.amazonaws.com/us-east-1_pxAHZcSSX',
    exp: 'Thu May 04 03:50:06 UTC 2023',
    version: '2',
    iat: 'Wed May 03 03:50:06 UTC 2023',
    client_id: '6rupb256qn12tju82occ9eppqr',
    jti: '5d92ca29-677e-40d9-a074-2d86fbcb4023',
    username: 'richwolf'
  }

Just as with the APIGatewayV2 event, the claims dictionary would be useful to Swift Lambda event handlers (particularly because it contains the Cognito username authorized to invoke an API endpoint).

Modifications:

I added an (optional) Authorizer struct to the APIGatewayRequest object. The struct contains a single public property…an optional claims object (dictionary of type [String: String] in a way that mirrors the authorizer implementation in the APIGatewayV2Request object.

Result:

The APIGatewayRequest object now successfully decodes the authorizer sent by the Lambda Proxy Integration JSON and can be used within a Swift Lambda event handler.

@tomerd
Copy link
Contributor

tomerd commented May 3, 2023

looks good. can we update / add a test?

@tomerd
Copy link
Contributor

tomerd commented May 3, 2023

@swift-server-bot test this please

@richwolf
Copy link
Contributor Author

richwolf commented May 3, 2023

looks good. can we update / add a test?

Oh sure! Sorry, in hindsight, I should have done that. I'll add a test and update the PR.

@tomerd
Copy link
Contributor

tomerd commented May 3, 2023

@swift-server-bot test this please

@tomerd
Copy link
Contributor

tomerd commented May 3, 2023

thanks for this @richwolf looks like the soundness job is failing due to formatting issue. you can run it locally to get this all lined up:

docker-compose -f docker/docker-compose.yaml run --rm soundness

@richwolf
Copy link
Contributor Author

richwolf commented May 3, 2023

Yeah, it doesn't surprise me that there could be a formatting issue. I have my Xcode set to 2 space tabs, but notice that the project is 4 spaces ... uh ... spaces. I thought I caught all the tabs, but maybe not? Running the soundness thingy now. Very cool tool. :)

@tomerd
Copy link
Contributor

tomerd commented May 3, 2023

@swift-server-bot test this please

@tomerd tomerd enabled auto-merge (squash) May 3, 2023 23:55
@tomerd tomerd merged commit 63176f5 into swift-server:main May 4, 2023
@tomerd
Copy link
Contributor

tomerd commented May 4, 2023

thanks @richwolf

@richwolf richwolf deleted the feature/authorizer-for-apigateway-events branch May 4, 2023 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants