Skip to content

Commit a4608e6

Browse files
committed
use constructor
1 parent d171555 commit a4608e6

7 files changed

Lines changed: 43 additions & 24 deletions

File tree

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/HttpApiAuthorizerAttributeBuilder.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ public static class HttpApiAuthorizerAttributeBuilder
1616
/// <returns>The populated attribute instance</returns>
1717
public static HttpApiAuthorizerAttribute Build(AttributeData att)
1818
{
19-
var attribute = new HttpApiAuthorizerAttribute();
19+
// Name is the first constructor argument
20+
var name = att.ConstructorArguments.Length > 0
21+
? att.ConstructorArguments[0].Value as string
22+
: null;
23+
24+
var attribute = new HttpApiAuthorizerAttribute(name);
2025

2126
foreach (var namedArg in att.NamedArguments)
2227
{
2328
switch (namedArg.Key)
2429
{
25-
case nameof(HttpApiAuthorizerAttribute.Name):
26-
attribute.Name = namedArg.Value.Value as string;
27-
break;
2830
case nameof(HttpApiAuthorizerAttribute.IdentityHeader):
2931
attribute.IdentityHeader = namedArg.Value.Value as string ?? "Authorization";
3032
break;

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/RestApiAuthorizerAttributeBuilder.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ public static class RestApiAuthorizerAttributeBuilder
1616
/// <returns>The populated attribute instance</returns>
1717
public static RestApiAuthorizerAttribute Build(AttributeData att)
1818
{
19-
var attribute = new RestApiAuthorizerAttribute();
19+
// Name is the first constructor argument
20+
var name = att.ConstructorArguments.Length > 0
21+
? att.ConstructorArguments[0].Value as string
22+
: null;
23+
24+
var attribute = new RestApiAuthorizerAttribute(name);
2025

2126
foreach (var namedArg in att.NamedArguments)
2227
{
2328
switch (namedArg.Key)
2429
{
25-
case nameof(RestApiAuthorizerAttribute.Name):
26-
attribute.Name = namedArg.Value.Value as string;
27-
break;
2830
case nameof(RestApiAuthorizerAttribute.IdentityHeader):
2931
attribute.IdentityHeader = namedArg.Value.Value as string ?? "Authorization";
3032
break;

Libraries/src/Amazon.Lambda.Annotations/APIGateway/HttpApiAuthorizerAttribute.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Amazon.Lambda.Annotations.APIGateway
1515
/// <example>
1616
/// <code>
1717
/// [LambdaFunction]
18-
/// [HttpApiAuthorizer(Name = "MyAuthorizer")]
18+
/// [HttpApiAuthorizer("MyAuthorizer")]
1919
/// public APIGatewayCustomAuthorizerV2SimpleResponse Authorize(APIGatewayCustomAuthorizerV2Request request)
2020
/// {
2121
/// // Validate token and return authorization response
@@ -32,6 +32,16 @@ namespace Amazon.Lambda.Annotations.APIGateway
3232
[AttributeUsage(AttributeTargets.Method)]
3333
public class HttpApiAuthorizerAttribute : Attribute
3434
{
35+
/// <summary>
36+
/// Creates a new HTTP API authorizer attribute with the specified name.
37+
/// </summary>
38+
/// <param name="name">Unique name to identify this authorizer. Other functions reference this name
39+
/// via the <see cref="HttpApiAttribute.Authorizer"/> property.</param>
40+
public HttpApiAuthorizerAttribute(string name)
41+
{
42+
Name = name;
43+
}
44+
3545
/// <summary>
3646
/// Required. Unique name to identify this authorizer. Other functions reference this name
3747
/// via the <see cref="HttpApiAttribute.Authorizer"/> property.

Libraries/src/Amazon.Lambda.Annotations/APIGateway/RestApiAuthorizerAttribute.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public enum RestApiAuthorizerType
3131
/// <example>
3232
/// <code>
3333
/// [LambdaFunction]
34-
/// [RestApiAuthorizer(Name = "TokenAuthorizer", Type = RestApiAuthorizerType.Token)]
34+
/// [RestApiAuthorizer("TokenAuthorizer", Type = RestApiAuthorizerType.Token)]
3535
/// public APIGatewayCustomAuthorizerResponse Authorize(APIGatewayCustomAuthorizerRequest request)
3636
/// {
3737
/// var token = request.AuthorizationToken;
@@ -49,6 +49,16 @@ public enum RestApiAuthorizerType
4949
[AttributeUsage(AttributeTargets.Method)]
5050
public class RestApiAuthorizerAttribute : Attribute
5151
{
52+
/// <summary>
53+
/// Creates a new REST API authorizer attribute with the specified name.
54+
/// </summary>
55+
/// <param name="name">Unique name to identify this authorizer. Other functions reference this name
56+
/// via the <see cref="RestApiAttribute.Authorizer"/> property.</param>
57+
public RestApiAuthorizerAttribute(string name)
58+
{
59+
Name = name;
60+
}
61+
5262
/// <summary>
5363
/// Required. Unique name to identify this authorizer. Other functions reference this name
5464
/// via the <see cref="RestApiAttribute.Authorizer"/> property.

Libraries/src/Amazon.Lambda.Annotations/README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ The authorizer function receives an `APIGatewayCustomAuthorizerV2Request` and re
869869

870870
```csharp
871871
[LambdaFunction(ResourceName = "HttpApiAuthorizer", PackageType = LambdaPackageType.Image)]
872-
[HttpApiAuthorizer(Name = "MyHttpAuthorizer")]
872+
[HttpApiAuthorizer("MyHttpAuthorizer")]
873873
public APIGatewayCustomAuthorizerV2SimpleResponse AuthorizeHttpApi(
874874
APIGatewayCustomAuthorizerV2Request request,
875875
ILambdaContext context)
@@ -945,7 +945,7 @@ REST API authorizers work similarly but use `[RestApiAuthorizer]` and `[RestApi]
945945

946946
```csharp
947947
[LambdaFunction(ResourceName = "RestApiAuthorizer", PackageType = LambdaPackageType.Image)]
948-
[RestApiAuthorizer(Name = "MyRestAuthorizer", Type = RestApiAuthorizerType.Token)]
948+
[RestApiAuthorizer("MyRestAuthorizer", Type = RestApiAuthorizerType.Token)]
949949
public APIGatewayCustomAuthorizerResponse AuthorizeRestApi(
950950
APIGatewayCustomAuthorizerRequest request,
951951
ILambdaContext context)
@@ -1035,8 +1035,7 @@ public object GetRestProtectedResource(
10351035

10361036
```csharp
10371037
[LambdaFunction(ResourceName = "ApiKeyAuthorizer", PackageType = LambdaPackageType.Image)]
1038-
[HttpApiAuthorizer(
1039-
Name = "ApiKeyAuth",
1038+
[HttpApiAuthorizer("ApiKeyAuth",
10401039
IdentityHeader = "X-Api-Key",
10411040
ResultTtlInSeconds = 300)]
10421041
public APIGatewayCustomAuthorizerV2SimpleResponse ValidateApiKey(
@@ -1104,9 +1103,9 @@ parameter to the `LambdaFunction` must be the event object and the event source
11041103
* HttpApi
11051104
* Configures the Lambda function to be called from an API Gateway HTTP API. The HTTP method, HTTP API payload version and resource path are required to be set on the attribute. Use the `Authorizer` property to reference an `HttpApiAuthorizer` by name.
11061105
* HttpApiAuthorizer
1107-
* Marks a Lambda function as an HTTP API (API Gateway V2) custom authorizer. Set the `Name` property to give the authorizer a unique name that can be referenced by `HttpApi.Authorizer`.
1106+
* Marks a Lambda function as an HTTP API (API Gateway V2) custom authorizer. Pass the authorizer name as the constructor argument to give it a unique name that can be referenced by `HttpApi.Authorizer`.
11081107
* RestApiAuthorizer
1109-
* Marks a Lambda function as a REST API (API Gateway V1) custom authorizer. Set the `Name` property to give the authorizer a unique name that can be referenced by `RestApi.Authorizer`. Use the `Type` property to choose between `Token` and `Request` authorizer types.
1108+
* Marks a Lambda function as a REST API (API Gateway V1) custom authorizer. Pass the authorizer name as the constructor argument to give it a unique name that can be referenced by `RestApi.Authorizer`. Use the `Type` property to choose between `Token` and `Request` authorizer types.
11101109
* SQSEvent
11111110
* Sets up event source mapping between the Lambda function and SQS queues. The SQS queue ARN is required to be set on the attribute. If users want to pass a reference to an existing SQS queue resource defined in their CloudFormation template, they can pass the SQS queue resource name prefixed with the '@' symbol.
11121111

Libraries/test/TestCustomAuthorizerApp/AuthorizerFunction.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public class AuthorizerFunction
3939
/// Returns authorized status along with custom context that can be accessed via [FromCustomAuthorizer]
4040
/// </summary>
4141
[LambdaFunction(ResourceName = "CustomAuthorizer")]
42-
[HttpApiAuthorizer(
43-
Name = "HttpApiLambdaAuthorizer",
42+
[HttpApiAuthorizer("HttpApiLambdaAuthorizer",
4443
IdentityHeader = "authorization",
4544
EnableSimpleResponses = true,
4645
AuthorizerPayloadFormatVersion = AuthorizerPayloadFormatVersion.V2)]
@@ -121,8 +120,7 @@ public APIGatewayCustomAuthorizerV2SimpleResponse HttpApiAuthorize(
121120
/// Used by HTTP API V1 endpoints that need payload format 1.0
122121
/// </summary>
123122
[LambdaFunction(ResourceName = "CustomAuthorizerV1")]
124-
[HttpApiAuthorizer(
125-
Name = "HttpApiLambdaAuthorizerV1",
123+
[HttpApiAuthorizer("HttpApiLambdaAuthorizerV1",
126124
IdentityHeader = "authorization",
127125
EnableSimpleResponses = false,
128126
AuthorizerPayloadFormatVersion = AuthorizerPayloadFormatVersion.V1)]
@@ -204,8 +202,7 @@ public APIGatewayCustomAuthorizerResponse HttpApiAuthorizeV1(
204202
/// Returns an IAM policy document along with custom context values
205203
/// </summary>
206204
[LambdaFunction(ResourceName = "RestApiAuthorizer")]
207-
[RestApiAuthorizer(
208-
Name = "RestApiLambdaAuthorizer",
205+
[RestApiAuthorizer("RestApiLambdaAuthorizer",
209206
Type = RestApiAuthorizerType.Token,
210207
IdentityHeader = "Authorization")]
211208
public APIGatewayCustomAuthorizerResponse RestApiAuthorize(

Libraries/test/TestServerlessApp/FromScratch/MissingLambdaFunctionWithAuthorizer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ namespace TestServerlessApp.FromScratch
77
{
88
public class MissingLambdaFunctionWithAuthorizer
99
{
10-
[HttpApiAuthorizer(
11-
Name = "MyAuthorizer",
10+
[HttpApiAuthorizer("MyAuthorizer",
1211
AuthorizerPayloadFormatVersion = AuthorizerPayloadFormatVersion.V2)]
1312
public APIGatewayCustomAuthorizerV2SimpleResponse Authorize(
1413
APIGatewayCustomAuthorizerV2Request request,

0 commit comments

Comments
 (0)