Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Amazon.Lambda.APIGatewayEvents
{
using System.Runtime.Serialization;

/// <summary>
/// An object representing the expected format of an API Gateway custom authorizer response.
/// </summary>
[DataContract]
public class APIGatewayCustomAuthorizerContext
{
/// <summary>
/// Gets or sets the 'principalId' property.
/// </summary>
[DataMember(Name = "principalId", IsRequired = false)]
public string PrincipalId { get; set; }

/// <summary>
/// Gets or sets the 'stringKey' property.
/// </summary>
[DataMember(Name = "stringKey", IsRequired = false)]
public string StringKey { get; set; }

/// <summary>
/// Gets or sets the 'numKey' property.
/// </summary>
[DataMember(Name = "numKey", IsRequired = false)]
public int? NumKey { get; set; }

/// <summary>
/// Gets or sets the 'boolKey' property.
/// </summary>
[DataMember(Name = "boolKey", IsRequired = false)]
public bool? BoolKey { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace Amazon.Lambda.APIGatewayEvents
{
using System.Collections.Generic;

/// <summary>
/// An object representing an IAM policy.
/// </summary>
public class APIGatewayCustomAuthorizerPolicy
{
/// <summary>
/// Gets or sets the IAM API version.
/// </summary>
public string Version { get; set; } = "2012-10-17";

/// <summary>
/// Gets or sets a list of IAM policy statements to apply.
/// </summary>
public List<IAMPolicyStatement> Statement { get; set; } = new List<IAMPolicyStatement>();

/// <summary>
/// A class representing an IAM Policy Statement.
/// </summary>
public class IAMPolicyStatement
{
/// <summary>
/// Gets or sets the effect the statement has.
/// </summary>
public string Effect { get; set; } = "Allow";

/// <summary>
/// Gets or sets the action/s the statement has.
/// </summary>
public HashSet<string> Action { get; set; }

/// <summary>
/// Gets or sets the resources the statement applies to.
/// </summary>
public HashSet<string> Resource { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Amazon.Lambda.APIGatewayEvents
{
/// <summary>
/// For requests coming in to a custom API Gateway authorizer function.
/// </summary>
public class APIGatewayCustomAuthorizerRequest
{
/// <summary>
/// Gets or sets the 'type' property.
/// </summary>
public string Type { get; set; }

/// <summary>
/// Gets or sets the 'authorizationToken' property.
/// </summary>
public string AuthorizationToken { get; set; }

/// <summary>
/// Gets or sets the 'methodArn' property.
/// </summary>
public string MethodArn { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Amazon.Lambda.APIGatewayEvents
{
using System.Runtime.Serialization;

/// <summary>
/// An object representing the expected format of an API Gateway authorization response.
/// </summary>
[DataContract]
public class APIGatewayCustomAuthorizerResponse
{
/// <summary>
/// Gets or sets the ID of the principal.
/// </summary>
[DataMember(Name = "principalId")]
public string PrincipalID { get; set; }

/// <summary>
/// Gets or sets the <see cref="APIGatewayCustomAuthorizerPolicy"/> policy document.
/// </summary>
[DataMember(Name = "policyDocument")]
public APIGatewayCustomAuthorizerPolicy PolicyDocument { get; set; } = new APIGatewayCustomAuthorizerPolicy();

/// <summary>
/// Gets or sets the <see cref="APIGatewayCustomAuthorizerContext"/> property.
/// </summary>
[DataMember(Name = "context")]
public APIGatewayCustomAuthorizerContext Context { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ public class ProxyRequestContext
/// </summary>
public string ApiId { get; set; }

/// <summary>
/// The APIGatewayCustomAuthorizerContext containing the custom properties set by a custom authorizer.
/// </summary>
public APIGatewayCustomAuthorizerContext Authorizer { get; set; }
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion Libraries/src/Amazon.Lambda.APIGatewayEvents/project.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"name": "Amazon.Lambda.APIGatewayEvents",
"version": "1.0.1-*",
"title": "Amazon.Lambda.APIGatewayEvents",
Expand All @@ -18,6 +18,7 @@
"warningsAsErrors": true
},
"dependencies": {
"System.Collections": "4.0.11",
"System.Runtime": "4.1.0",
"System.Runtime.Serialization.Primitives": "4.1.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<APIGatewayProxyRequest>(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<APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement>
{
new APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement
{
Effect = "Allow",
Action = new HashSet<string> { "execute-api:Invoke" },
Resource = new HashSet<string> { "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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}