From 9d655b79215b6ccd22ce4b387e27f27aa865d0c9 Mon Sep 17 00:00:00 2001 From: Yancey Date: Thu, 26 Jan 2017 14:17:29 -0800 Subject: [PATCH] Adding in CustomAuthorizer classes and tests. --- .../APIGatewayCustomAuthorizerContext.cs | 29 +++++++++++++ .../APIGatewayCustomAuthorizerPolicy.cs | 41 ++++++++++++++++++ .../APIGatewayCustomAuthorizerRequest.cs | 23 ++++++++++ .../APIGatewayCustomAuthorizerResponse.cs | 29 +++++++++++++ .../APIGatewayProxyRequest.cs | 4 ++ .../project.json | 3 +- .../TestCallingWebAPI.cs | 43 +++++++++++++++++++ ...et-customauthorizer-apigatway-request.json | 39 +++++++++++++++++ 8 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerContext.cs create mode 100644 Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerPolicy.cs create mode 100644 Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerRequest.cs create mode 100644 Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerResponse.cs create mode 100644 Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-customauthorizer-apigatway-request.json diff --git a/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerContext.cs b/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerContext.cs new file mode 100644 index 000000000..34b2f49ba --- /dev/null +++ b/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerContext.cs @@ -0,0 +1,29 @@ +namespace Amazon.Lambda.APIGatewayEvents +{ + using System.Runtime.Serialization; + + /// + /// An object representing the expected format of an API Gateway custom authorizer response. + /// + [DataContract] + public class APIGatewayCustomAuthorizerContext + { + /// + /// Gets or sets the 'stringKey' property. + /// + [DataMember(Name = "stringKey", IsRequired = false)] + public string StringKey { get; set; } + + /// + /// Gets or sets the 'numKey' property. + /// + [DataMember(Name = "numKey", IsRequired = false)] + public int? NumKey { get; set; } + + /// + /// Gets or sets the 'boolKey' property. + /// + [DataMember(Name = "boolKey", IsRequired = false)] + public bool? BoolKey { get; set; } + } +} diff --git a/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerPolicy.cs b/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerPolicy.cs new file mode 100644 index 000000000..33fada3c6 --- /dev/null +++ b/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerPolicy.cs @@ -0,0 +1,41 @@ +namespace Amazon.Lambda.APIGatewayEvents +{ + using System.Collections.Generic; + + /// + /// An object representing an IAM policy. + /// + public class APIGatewayCustomAuthorizerPolicy + { + /// + /// Gets or sets the IAM API version. + /// + public string Version { get; set; } = "2012-10-17"; + + /// + /// Gets or sets a list of IAM policy statements to apply. + /// + public List Statement { get; set; } = new List(); + + /// + /// A class representing an IAM Policy Statement. + /// + public class IAMPolicyStatement + { + /// + /// Gets or sets the effect the statement has. + /// + public string Effect { get; set; } = "Allow"; + + /// + /// Gets or sets the action/s the statement has. + /// + public HashSet Action { get; set; } + + /// + /// Gets or sets the resources the statement applies to. + /// + public HashSet Resource { get; set; } + } + } +} diff --git a/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerRequest.cs b/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerRequest.cs new file mode 100644 index 000000000..8ced77d1f --- /dev/null +++ b/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerRequest.cs @@ -0,0 +1,23 @@ +namespace Amazon.Lambda.APIGatewayEvents +{ + /// + /// For requests coming in to a custom API Gateway authorizer function. + /// + public class APIGatewayCustomAuthorizerRequest + { + /// + /// Gets or sets the 'type' property. + /// + public string Type { get; set; } + + /// + /// Gets or sets the 'authorizationToken' property. + /// + public string AuthorizationToken { get; set; } + + /// + /// Gets or sets the 'methodArn' property. + /// + public string MethodArn { get; set; } + } +} diff --git a/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerResponse.cs b/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerResponse.cs new file mode 100644 index 000000000..51218d842 --- /dev/null +++ b/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerResponse.cs @@ -0,0 +1,29 @@ +namespace Amazon.Lambda.APIGatewayEvents +{ + using System.Runtime.Serialization; + + /// + /// An object representing the expected format of an API Gateway authorization response. + /// + [DataContract] + public class APIGatewayCustomAuthorizerResponse + { + /// + /// Gets or sets the ID of the principal. + /// + [DataMember(Name = "principalId")] + public string PrincipalID { get; set; } + + /// + /// Gets or sets the policy document. + /// + [DataMember(Name = "policyDocument")] + public APIGatewayCustomAuthorizerPolicy PolicyDocument { get; set; } = new APIGatewayCustomAuthorizerPolicy(); + + /// + /// Gets or sets the property. + /// + [DataMember(Name = "context")] + public APIGatewayCustomAuthorizerContext Context { get; set; } + } +} diff --git a/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayProxyRequest.cs b/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayProxyRequest.cs index bdeef1c6b..bf75a2938 100644 --- a/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayProxyRequest.cs +++ b/Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayProxyRequest.cs @@ -106,6 +106,10 @@ public class ProxyRequestContext /// public string ApiId { get; set; } + /// + /// The APIGatewayCustomAuthorizerContext containing the custom properties set by a custom authorizer. + /// + public APIGatewayCustomAuthorizerContext Authorizer { get; set; } } /// diff --git a/Libraries/src/Amazon.Lambda.APIGatewayEvents/project.json b/Libraries/src/Amazon.Lambda.APIGatewayEvents/project.json index 821db3406..cdb7af4ad 100644 --- a/Libraries/src/Amazon.Lambda.APIGatewayEvents/project.json +++ b/Libraries/src/Amazon.Lambda.APIGatewayEvents/project.json @@ -1,4 +1,4 @@ -{ +{ "name": "Amazon.Lambda.APIGatewayEvents", "version": "1.0.1-*", "title": "Amazon.Lambda.APIGatewayEvents", @@ -18,6 +18,7 @@ "warningsAsErrors": true }, "dependencies": { + "System.Collections": "4.0.11", "System.Runtime": "4.1.0", "System.Runtime.Serialization.Primitives": "4.1.1" }, diff --git a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/TestCallingWebAPI.cs b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/TestCallingWebAPI.cs index 6d8b98f4a..9187fd53b 100644 --- a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/TestCallingWebAPI.cs +++ b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/TestCallingWebAPI.cs @@ -102,5 +102,48 @@ public async Task TestGettingSwaggerDefinition() Assert.True(response.Body.Length > 0); Assert.Equal("application/json", response.Headers["Content-Type"]); } + + [Fact] + public void TestGetCustomAuthorizerValue() + { + var requestStr = File.ReadAllText("values-get-customauthorizer-apigatway-request.json"); + var request = JsonConvert.DeserializeObject(requestStr); + Assert.NotNull(request.RequestContext.Authorizer); + Assert.NotNull(request.RequestContext.Authorizer.StringKey); + Assert.Equal(9, request.RequestContext.Authorizer.NumKey); + Assert.True(request.RequestContext.Authorizer.BoolKey); + } + + [Fact] + public void TestCustomAuthorizerSerialization() + { + var response = new APIGatewayCustomAuthorizerResponse + { + PrincipalID = "com.amazon.someuser", + Context = new APIGatewayCustomAuthorizerContext + { + StringKey = "Hey I'm a string", + BoolKey = true, + NumKey = 9 + }, + PolicyDocument = new APIGatewayCustomAuthorizerPolicy + { + Statement = new List + { + new APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement + { + Effect = "Allow", + Action = new HashSet { "execute-api:Invoke" }, + Resource = new HashSet { "arn:aws:execute-api:us-west-2:1234567890:apit123d45/Prod/GET/*" } + } + } + } + }; + + var json = JsonConvert.SerializeObject(response); + Assert.NotNull(json); + var expected = "{\"principalId\":\"com.amazon.someuser\",\"policyDocument\":{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":[\"execute-api:Invoke\"],\"Resource\":[\"arn:aws:execute-api:us-west-2:1234567890:apit123d45/Prod/GET/*\"]}]},\"context\":{\"stringKey\":\"Hey I'm a string\",\"numKey\":9,\"boolKey\":true}}"; + Assert.Equal(expected, json); + } } } diff --git a/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-customauthorizer-apigatway-request.json b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-customauthorizer-apigatway-request.json new file mode 100644 index 000000000..14295ebcb --- /dev/null +++ b/Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/values-get-customauthorizer-apigatway-request.json @@ -0,0 +1,39 @@ +{ + "resource": "/{proxy+}", + "path": "/api/resourcepath/5", + "httpMethod": "GET", + "headers": null, + "queryStringParameters": null, + "pathParameters": { + "proxy": "api/values" + }, + "stageVariables": null, + "requestContext": { + "accountId": "AAAAAAAAAAAA", + "resourceId": "5agfss", + "stage": "test-invoke-stage", + "requestId": "test-invoke-request", + "identity": { + "cognitoIdentityPoolId": null, + "accountId": "AAAAAAAAAAAA", + "cognitoIdentityId": null, + "caller": "BBBBBBBBBBBB", + "apiKey": "test-invoke-api-key", + "sourceIp": "test-invoke-source-ip", + "cognitoAuthenticationType": null, + "cognitoAuthenticationProvider": null, + "userArn": "arn:aws:iam::AAAAAAAAAAAA:root", + "userAgent": "Apache-HttpClient/4.5.x (Java/1.8.0_102)", + "user": "AAAAAAAAAAAA" + }, + "authorizer": { + "stringKey": "Hey there I'm a string!", + "numKey": 9, + "boolKey": true + }, + "resourcePath": "/{proxy+}", + "httpMethod": "GET", + "apiId": "t2yh6sjnmk" + }, + "body": null +} \ No newline at end of file