Skip to content
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

feat: V1 and V2 of the API Gateway custom authorizer event and the si… #166

Merged
merged 4 commits into from
Oct 7, 2020
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,59 @@
package com.amazonaws.services.lambda.runtime.events;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;

/**
* The API Gateway customer authorizer event object as described - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-lambda-authorizer.html
*
*/

@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public class APIGatewayCustomAuthorizerEvent {

private String version;
private String type;
private String methodArn;
private String identitySource;
private String authorizationToken;
private String resource;
private String path;
private String httpMethod;
private Map<String, String> headers;
private Map<String, String> queryStringParameters;
private Map<String, String> pathParameters;
private Map<String, String> stageVariables;
private RequestContext requestContext;

@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public static class RequestContext {
private String path;
private String accountId;
private String resourceId;
private String stage;
private String requestId;
private Identity identity;
private String resourcePath;
private String httpMethod;
private String apiId;
}

@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public static class Identity {
private String apiKey;
private String sourceIp;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.amazonaws.services.lambda.runtime.events;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.time.Instant;
import java.util.List;
import java.util.Map;

/**
* The V2 API Gateway customer authorizer event object as described - https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-lambda-authorizer.html
*
*/

@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public class APIGatewayV2CustomAuthorizerEvent {

private String version;
private String type;
private String routeArn;
private List<String> identitySource;
private String routeKey;
private String rawPath;
private String rawQueryString;
private List<String> cookies;
private Map<String, String> headers;
private Map<String, String> queryStringParameters;
private RequestContext requestContext;
private Map<String, String> pathParameters;
private Map<String, String> stageVariables;

@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public static class RequestContext {

private static DateTimeFormatter fmt = DateTimeFormat.forPattern("dd/MMM/yyyy:HH:mm:ss Z");

private String accountId;
private String apiId;
private String domainName;
private String domainPrefix;
private Http http;
private String requestId;
private String routeKey;
private String stage;
private String time;
private long timeEpoch;

public Instant getTimeEpoch() {
return Instant.ofEpochMilli(timeEpoch);
}

public DateTime getTime() {
return fmt.parseDateTime(time);
}
}

@AllArgsConstructor
@Builder(setterPrefix = "with")
@Data
@NoArgsConstructor
public static class Http {

private String method;
private String path;
private String protocol;
private String sourceIp;
private String userAgent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.amazonaws.services.lambda.runtime.events;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;

/**
* The simplified IAM Policy response object as described in https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-lambda-authorizer.html
*
*/

@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public class SimpleIAMPolicyResponse {

private boolean isAuthorized;
private Map<String, String> context;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.amazonaws.services.lambda.runtime.events;

import org.junit.jupiter.api.Test;

import java.time.Instant;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class APIGatewayV2CustomAuthorizerEventTest {

private static final long TIME_EPOCH = 1601306426515L;
private static final String TIME = "28/Sep/2020:15:14:43 +0000";

@Test
public void testEpochLongAsAnInstant() {
APIGatewayV2CustomAuthorizerEvent customAuthorizerEvent = APIGatewayV2CustomAuthorizerEvent.builder()
.withRequestContext(APIGatewayV2CustomAuthorizerEvent.RequestContext.builder()
.withTimeEpoch(TIME_EPOCH)
.build())
.build();

assertEquals(Instant.ofEpochMilli(1601306426515L), customAuthorizerEvent.getRequestContext().getTimeEpoch());
}

@Test
public void testTimeStringAsDateTime() {
APIGatewayV2CustomAuthorizerEvent customAuthorizerEvent = APIGatewayV2CustomAuthorizerEvent.builder()
.withRequestContext(APIGatewayV2CustomAuthorizerEvent.RequestContext.builder()
.withTime(TIME)
.build())
.build();

assertNotNull(customAuthorizerEvent.getRequestContext().getTime());
}
}