Skip to content

Add static factory methods to ApiGatewayRequestIdentity and AwsProxyRequestContext #1496

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Expand Up @@ -14,7 +14,6 @@


import com.amazonaws.serverless.proxy.RequestReader;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;


Expand All @@ -28,6 +27,42 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class ApiGatewayRequestIdentity {

/**
* Creates an ApiGatewayRequestIdentity instance with default values based on a typical API Gateway request.
*
* @return A pre-configured ApiGatewayRequestIdentity instance
*/
public static ApiGatewayRequestIdentity getApiGatewayRequestIdentity() {
ApiGatewayRequestIdentity identity = new ApiGatewayRequestIdentity();
// Set default values based on example API Gateway request
identity.setApiKey(null);
identity.setApiKeyId(null);
identity.setUserArn(null);
identity.setCognitoAuthenticationType(null);
identity.setCaller(null);
identity.setUserAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");
identity.setUser(null);
identity.setCognitoIdentityPoolId(null);
identity.setCognitoIdentityId(null);
identity.setCognitoAuthenticationProvider(null);
identity.setSourceIp("127.0.0.1");
identity.setAccountId(null);
identity.setAccessKey(null);
return identity;
}

/**
* Creates an ApiGatewayRequestIdentity instance with the specified source IP.
*
* @param sourceIp the source IP to set
* @return A pre-configured ApiGatewayRequestIdentity instance
*/
public static ApiGatewayRequestIdentity getApiGatewayRequestIdentity(String sourceIp) {
ApiGatewayRequestIdentity identity = getApiGatewayRequestIdentity();
identity.setSourceIp(sourceIp);
return identity;
}

//-------------------------------------------------------------
// Variables - Private
//-------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@


import com.amazonaws.serverless.proxy.RequestReader;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;


Expand All @@ -29,6 +28,48 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class AwsProxyRequestContext {

/**
* Creates an AwsProxyRequestContext instance with default values based on a typical API Gateway request.
*
* @return A pre-configured AwsProxyRequestContext instance
*/
public static AwsProxyRequestContext getAwsProxyRequestContext() {
AwsProxyRequestContext context = new AwsProxyRequestContext();

// Set default values based on typical API Gateway request
context.setAccountId("123456789012");
context.setApiId("id");
context.setResourceId("$default");
context.setResourcePath("/my/path");
context.setHttpMethod("POST");
context.setRequestId("id");
context.setExtendedRequestId("");
context.setStage("$default");
context.setPath("/my/path");
context.setProtocol("HTTP/1.1");
context.setRequestTimeEpoch(System.currentTimeMillis()); // Current time as reasonable default

// Create and set identity with defaults (already includes sourceIp and userAgent)
context.setIdentity(ApiGatewayRequestIdentity.getApiGatewayRequestIdentity());

// Initialize authorizer
context.setAuthorizer(new ApiGatewayAuthorizerContext());

return context;
}

/**
* Creates an AwsProxyRequestContext instance with the specified HTTP method.
*
* @param httpMethod the HTTP method to set
* @return A pre-configured AwsProxyRequestContext instance
*/
public static AwsProxyRequestContext getAwsProxyRequestContext(String httpMethod) {
AwsProxyRequestContext context = getAwsProxyRequestContext();
context.setHttpMethod(httpMethod);
return context;
}

//-------------------------------------------------------------
// Variables - Private
//-------------------------------------------------------------
Expand Down