-
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?
Changes from all commits
48afb69
18b463c
5d718fa
4d0eeff
35e3636
b7cb413
6942037
0eec76e
37dc685
20afc6c
14352e5
b2c1e28
3d28316
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using Amazon.Lambda.Annotations.APIGateway; | ||
| using Microsoft.CodeAnalysis; | ||
|
|
||
| namespace Amazon.Lambda.Annotations.SourceGenerator.Models.Attributes | ||
| { | ||
| public class FromCustomAuthorizerAttributeBuilder | ||
| { | ||
| public static FromCustomAuthorizerAttribute Build(AttributeData att) | ||
| { | ||
| var data = new FromCustomAuthorizerAttribute(); | ||
| foreach (var pair in att.NamedArguments) | ||
| { | ||
| if (pair.Key == nameof(data.Name) && pair.Value.Value is string value) | ||
| { | ||
| data.Name = value; | ||
| } | ||
| } | ||
|
|
||
| return data; | ||
| } | ||
| } | ||
| } | ||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -247,6 +247,157 @@ | |
| validationErrors.Add($"Value {__request__.Body} at 'body' failed to satisfy constraint: {e.Message}"); | ||
| } | ||
|
|
||
| <# | ||
| } | ||
| } | ||
| else if (parameter.Attributes.Any(att => att.Type.FullName == TypeFullNames.FromCustomAuthorizerAttribute)) | ||
| { | ||
| var fromAuthorizerAttribute = parameter.Attributes?.FirstOrDefault(att => att.Type.FullName == TypeFullNames.FromCustomAuthorizerAttribute) as AttributeModel<Amazon.Lambda.Annotations.APIGateway.FromCustomAuthorizerAttribute>; | ||
|
|
||
| // Use parameter name as key, if Name has not specified explicitly in the attribute definition. | ||
| var authKey = fromAuthorizerAttribute?.Data?.Name ?? parameter.Name; | ||
GarrettBeatty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // REST API and HTTP API v1 both use APIGatewayProxyRequest where RequestContext.Authorizer is a dictionary. | ||
| // Only HTTP API v2 uses APIGatewayHttpApiV2ProxyRequest with RequestContext.Authorizer.Lambda as the dictionary. | ||
| if(restApiAttribute != null || httpApiAttribute?.Data.Version == Amazon.Lambda.Annotations.APIGateway.HttpApiVersion.V1) | ||
| { | ||
| #> | ||
| var <#= parameter.Name #> = default(<#= parameter.Type.FullName #>); | ||
| if (__request__.RequestContext?.Authorizer == null || __request__.RequestContext?.Authorizer.ContainsKey("<#= authKey #>") == false) | ||
| { | ||
| var __unauthorized__ = new <#= restApiAttribute != null || httpApiAttribute?.Data.Version == Amazon.Lambda.Annotations.APIGateway.HttpApiVersion.V1 ? "Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse" : "Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyResponse" #> | ||
| { | ||
| Headers = new Dictionary<string, string> | ||
| { | ||
| {"Content-Type", "application/json"}, | ||
| {"x-amzn-ErrorType", "AccessDeniedException"} | ||
| }, | ||
| StatusCode = 401 | ||
| }; | ||
| <# | ||
| if(_model.LambdaMethod.ReturnsIHttpResults) | ||
| { | ||
| #> | ||
| var __unauthorizedStream__ = new System.IO.MemoryStream(); | ||
| serializer.Serialize(__unauthorized__, __unauthorizedStream__); | ||
| __unauthorizedStream__.Position = 0; | ||
| return __unauthorizedStream__; | ||
| <# | ||
| } | ||
| else | ||
| { | ||
| #> | ||
| return __unauthorized__; | ||
| <# | ||
| } | ||
| #> | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var __authValue_<#= parameter.Name #>__ = __request__.RequestContext.Authorizer["<#= authKey #>"]; | ||
| <#= parameter.Name #> = (<#= parameter.Type.FullName #>)Convert.ChangeType(__authValue_<#= parameter.Name #>__?.ToString(), typeof(<#= parameter.Type.FullNameWithoutAnnotations #>)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why We Need
|
||
| } | ||
| catch (Exception e) when (e is InvalidCastException || e is FormatException || e is OverflowException || e is ArgumentException) | ||
| { | ||
| var __unauthorized__ = new <#= restApiAttribute != null || httpApiAttribute?.Data.Version == Amazon.Lambda.Annotations.APIGateway.HttpApiVersion.V1 ? "Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse" : "Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyResponse" #> | ||
| { | ||
| Headers = new Dictionary<string, string> | ||
| { | ||
| {"Content-Type", "application/json"}, | ||
| {"x-amzn-ErrorType", "AccessDeniedException"} | ||
| }, | ||
| StatusCode = 401 | ||
| }; | ||
| <# | ||
| if(_model.LambdaMethod.ReturnsIHttpResults) | ||
| { | ||
| #> | ||
| var __unauthorizedStream__ = new System.IO.MemoryStream(); | ||
| serializer.Serialize(__unauthorized__, __unauthorizedStream__); | ||
| __unauthorizedStream__.Position = 0; | ||
| return __unauthorizedStream__; | ||
| <# | ||
| } | ||
| else | ||
| { | ||
| #> | ||
| return __unauthorized__; | ||
| <# | ||
| } | ||
| #> | ||
| } | ||
|
|
||
| <# | ||
| } | ||
| else | ||
| { | ||
| #> | ||
| var <#= parameter.Name #> = default(<#= parameter.Type.FullName #>); | ||
| if (__request__.RequestContext?.Authorizer?.Lambda == null || __request__.RequestContext?.Authorizer?.Lambda.ContainsKey("<#= authKey #>") == false) | ||
| { | ||
| var __unauthorized__ = new <#= restApiAttribute != null || httpApiAttribute?.Data.Version == Amazon.Lambda.Annotations.APIGateway.HttpApiVersion.V1 ? "Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse" : "Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyResponse" #> | ||
| { | ||
| Headers = new Dictionary<string, string> | ||
| { | ||
| {"Content-Type", "application/json"}, | ||
| {"x-amzn-ErrorType", "AccessDeniedException"} | ||
| }, | ||
| StatusCode = 401 | ||
| }; | ||
| <# | ||
| if(_model.LambdaMethod.ReturnsIHttpResults) | ||
| { | ||
| #> | ||
| var __unauthorizedStream__ = new System.IO.MemoryStream(); | ||
| serializer.Serialize(__unauthorized__, __unauthorizedStream__); | ||
| __unauthorizedStream__.Position = 0; | ||
| return __unauthorizedStream__; | ||
| <# | ||
| } | ||
| else | ||
| { | ||
| #> | ||
| return __unauthorized__; | ||
| <# | ||
| } | ||
| #> | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var __authValue_<#= parameter.Name #>__ = __request__.RequestContext.Authorizer.Lambda["<#= authKey #>"]; | ||
| <#= parameter.Name #> = (<#= parameter.Type.FullName #>)Convert.ChangeType(__authValue_<#= parameter.Name #>__?.ToString(), typeof(<#= parameter.Type.FullNameWithoutAnnotations #>)); | ||
| } | ||
| catch (Exception e) when (e is InvalidCastException || e is FormatException || e is OverflowException || e is ArgumentException) | ||
| { | ||
| var __unauthorized__ = new <#= restApiAttribute != null || httpApiAttribute?.Data.Version == Amazon.Lambda.Annotations.APIGateway.HttpApiVersion.V1 ? "Amazon.Lambda.APIGatewayEvents.APIGatewayProxyResponse" : "Amazon.Lambda.APIGatewayEvents.APIGatewayHttpApiV2ProxyResponse" #> | ||
| { | ||
| Headers = new Dictionary<string, string> | ||
| { | ||
| {"Content-Type", "application/json"}, | ||
| {"x-amzn-ErrorType", "AccessDeniedException"} | ||
| }, | ||
| StatusCode = 401 | ||
| }; | ||
| <# | ||
| if(_model.LambdaMethod.ReturnsIHttpResults) | ||
| { | ||
| #> | ||
| var __unauthorizedStream__ = new System.IO.MemoryStream(); | ||
| serializer.Serialize(__unauthorized__, __unauthorizedStream__); | ||
| __unauthorizedStream__.Position = 0; | ||
| return __unauthorizedStream__; | ||
| <# | ||
| } | ||
| else | ||
| { | ||
| #> | ||
| return __unauthorized__; | ||
| <# | ||
| } | ||
| #> | ||
| } | ||
|
|
||
| <# | ||
| } | ||
| } | ||
|
|
@@ -315,4 +466,4 @@ | |
|
|
||
| <# | ||
| } | ||
| #> | ||
| #> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| using System; | ||
|
|
||
| namespace Amazon.Lambda.Annotations.APIGateway | ||
| { | ||
| /// <summary> | ||
| /// Maps this parameter to a custom authorizer item | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Will try to get the specified key from Custom Authorizer values | ||
| /// </remarks> | ||
| [AttributeUsage(AttributeTargets.Parameter)] | ||
| public class FromCustomAuthorizerAttribute : Attribute, INamedAttribute | ||
| { | ||
| /// <summary> | ||
| /// Key of the value | ||
| /// </summary> | ||
| public string Name { get; set; } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.