-
Notifications
You must be signed in to change notification settings - Fork 497
Added FromCustomAuthorizerAttribute and tests. #1466
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
base: dev
Are you sure you want to change the base?
Conversation
normj
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool PR!
Can you fix the whitespaces changes to the LambdaFunctionTemplate.tt file? The whole file is looks different.
Who should we handle the other type of authorizer properties. For example on the APIGatewayHttpApiV2ProxyRequest besides the Lambda property there is also Jwt and IAM properties. Should we make the string accessing the authorizer field have the prefix of the property.
|
Wiil do my best with the withspace. |
|
don't seem to be able to fix the whitespace issue to save my life :( Tried setting EOL to LF and CR, both cause problems with the generated file. The correct one seems to be CRLF . Please feel free to edit. |
|
Would be really nice to get this avail;able. I will gladly fix the conflicts if this can be looked at again. |
|
I can see you did a major refactoring in the dev branch will push again once released. If i might be so bold, I looked at what you did. Just a thought. |
|
@kabaluk I suspect adding new attributes would be done in separate files. For example if we added SQS or S3 I would put them in separate TT. One of my goals with the refactor was to make it easier to add new attributes as the previous monolithic tt file was really hard to reconcile with. Another goal I had was I wanted to generate the exact same code including whitespaces to make the PR review easier and then later I could do more specific refactoring. Otherwise the PR review would have been every line is different and that is hard to review. But going forward I could see putting each of the |
I was talking specifically of the FromXXX attributes. Apologies, I should have been more explicit. Sounds like a great change and it would make it a lot easier to add and test more FromXXX attributes. Looking forward to have that in main so i can re add the FromCustomAuthorizer attribute. |
|
Readded CustomAuthorizerAttribute.. Hope you like it :) |
|
Any possibility of having this looked at again, please? |
normj
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did an initial scan and it looks good. I had one comment so far. I need to test the experience end to end next. I'll try and find time to do that soon.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Templates/APIGatewaySetupParameters.tt
Outdated
Show resolved
Hide resolved
|
im working on rebasing this |
f14bd0b to
48afb69
Compare
| try | ||
| { | ||
| var __authValue_<#= parameter.Name #>__ = __request__.RequestContext.Authorizer["<#= authKey #>"]; | ||
| <#= parameter.Name #> = (<#= parameter.Type.FullName #>)Convert.ChangeType(__authValue_<#= parameter.Name #>__?.ToString(), typeof(<#= parameter.Type.FullNameWithoutAnnotations #>)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why We Need .ToString() for Custom Authorizer Values
The Data Flow
-
Authorizer Lambda returns context: When your custom authorizer runs, it returns a context dictionary:
Context = new Dictionary<string, object> { { "userId", "12345" }, { "permissions", "admin" } }
-
API Gateway passes this to the protected Lambda in the request payload as JSON:
{ "requestContext": { "authorizer": { "userId": "12345", "permissions": "admin" } } } -
Lambda deserializes the request using the configured serializer (System.Text.Json or Newtonsoft.Json).
The Problem: Dictionary<string, object> Deserialization
The Authorizer property is typed as Dictionary<string, object>:
public class APIGatewayCustomAuthorizerContext : Dictionary<string, object>When the serializer encounters a JSON value like "12345" and needs to deserialize it into object, it doesn't know what concrete type to use. So:
- System.Text.Json wraps it in a
JsonElementstruct - Newtonsoft.Json wraps it in a
JToken(likeJValue)
so rather than having statements like
if (__authValue__ is System.Text.Json.JsonElement jsonElement)
{
userId = Convert.ChangeType(jsonElement.ToString(), typeof(string));
}
i just call toString which both of these serializers have.
not sure if there is a better way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@normj not sure if you have any better ideas here of it ToString is good enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Adds a new [FromCustomAuthorizer] parameter-binding attribute to AWS Lambda Annotations so Lambda handlers can directly receive values from API Gateway custom authorizer context (REST API + HTTP API), along with generator/template updates and accompanying tests/examples.
Changes:
- Introduce
FromCustomAuthorizerAttributeand wire it into the source generator attribute modeling and parameter setup templates. - Add source-generator snapshot tests and update test templates/examples to validate generated output.
- Add a new test application (
TestCustomAuthorizerApp) and update docs to demonstrate usage.
Reviewed changes
Copilot reviewed 22 out of 22 changed files in this pull request and generated 13 comments.
Show a summary per file
| File | Description |
|---|---|
| Libraries/test/TestServerlessApp/serverless.template | Adds new test function resources for custom authorizer examples. |
| Libraries/test/TestServerlessApp/CustomAuthorizerRestExample.cs | REST API example using [FromCustomAuthorizer]. |
| Libraries/test/TestServerlessApp/CustomAuthorizerHttpApiExample.cs | HTTP API example using [FromCustomAuthorizer]. |
| Libraries/test/TestCustomAuthorizerApp/serverless.template | New demo stack showing HTTP API + REST API Lambda authorizers and protected endpoints. |
| Libraries/test/TestCustomAuthorizerApp/aws-lambda-tools-defaults.json | Deployment defaults for the new demo app. |
| Libraries/test/TestCustomAuthorizerApp/TestCustomAuthorizerApp.csproj | New demo project configuration and references. |
| Libraries/test/TestCustomAuthorizerApp/README.md | Demo documentation and usage instructions. |
| Libraries/test/TestCustomAuthorizerApp/ProtectedFunction.cs | Demo functions showing authorizer-context usage. |
| Libraries/test/TestCustomAuthorizerApp/AuthorizerFunction.cs | Demo authorizer implementations for HTTP API and REST API. |
| Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SourceGeneratorTests.cs | Adds generator tests for REST + HTTP API (v2) custom authorizer extraction. |
| Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/ServerlessTemplates/authorizerRest.template | Snapshot of expected SAM template output for REST authorizer test. |
| Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/ServerlessTemplates/authorizerHttpApi.template | Snapshot of expected SAM template output for HTTP API authorizer test. |
| Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/CustomAuthorizerRestExample_RestAuthorizer_Generated.g.cs | Snapshot of expected generated handler for REST authorizer extraction. |
| Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/CustomAuthorizerHttpApiExample_HttpApiAuthorizer_Generated.g.cs | Snapshot of expected generated handler for HTTP API authorizer extraction. |
| Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Amazon.Lambda.Annotations.SourceGenerators.Tests.csproj | Updates snapshot item handling for the new templates. |
| Libraries/src/Amazon.Lambda.Annotations/README.md | Documents [FromCustomAuthorizer] usage and behavior. |
| Libraries/src/Amazon.Lambda.Annotations/APIGateway/FromCustomAuthorizerAttribute.cs | Adds the new public attribute. |
| Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/TypeFullNames.cs | Registers the new attribute type name for generator recognition. |
| Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Templates/APIGatewaySetupParameters.tt | Adds template logic to extract values from authorizer context. |
| Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Templates/APIGatewaySetupParameters.cs | Regenerated T4 output reflecting the new parameter extraction logic. |
| Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/FromCustomAuthorizerAttributeBuilder.cs | Adds attribute data builder for the generator model. |
| Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/AttributeModelBuilder.cs | Wires the new attribute builder into attribute model creation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Templates/APIGatewaySetupParameters.tt
Show resolved
Hide resolved
...a.Annotations.SourceGenerators.Tests/Amazon.Lambda.Annotations.SourceGenerators.Tests.csproj
Outdated
Show resolved
Hide resolved
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SourceGeneratorTests.cs
Show resolved
Hide resolved
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Templates/APIGatewaySetupParameters.cs
Outdated
Show resolved
Hide resolved
...ceGenerators.Tests/Snapshots/CustomAuthorizerHttpApiExample_HttpApiAuthorizer_Generated.g.cs
Show resolved
Hide resolved
...s.SourceGenerators.Tests/Snapshots/CustomAuthorizerRestExample_RestAuthorizer_Generated.g.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 25 out of 25 changed files in this pull request and generated 6 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Templates/APIGatewaySetupParameters.tt
Show resolved
Hide resolved
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Templates/APIGatewaySetupParameters.tt
Show resolved
Hide resolved
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Templates/APIGatewaySetupParameters.cs
Show resolved
Hide resolved
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SourceGeneratorTests.cs
Show resolved
Hide resolved
...s.SourceGenerators.Tests/Snapshots/CustomAuthorizerRestExample_RestAuthorizer_Generated.g.cs
Show resolved
Hide resolved
...nerators.Tests/Snapshots/CustomAuthorizerHttpApiV1Example_HttpApiV1Authorizer_Generated.g.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 28 out of 28 changed files in this pull request and generated 7 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Templates/APIGatewaySetupParameters.tt
Show resolved
Hide resolved
...ceGenerators.Tests/Snapshots/CustomAuthorizerHttpApiExample_HttpApiAuthorizer_Generated.g.cs
Show resolved
Hide resolved
.../Snapshots/CustomAuthorizerWithIHttpResultsExample_AuthorizerWithIHttpResults_Generated.g.cs
Show resolved
Hide resolved
.../Snapshots/CustomAuthorizerWithIHttpResultsExample_AuthorizerWithIHttpResults_Generated.g.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 31 out of 31 changed files in this pull request and generated 8 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Description of changes:
Added FromCustomAuthorizerAttribute and tests.
This will allow to retrieve values from CustomAuthorizer context without having to receive the full APIGatewayProxyRequest or APIGatewayHttpApiV2ProxyRequest..
Both REST and HttpApi supported
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.