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

Initial checkin of feature to allow faking the Cognito user #16956

Merged
merged 1 commit into from
May 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.StandardCharsets;
import java.security.Principal;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -168,6 +169,10 @@ private APIGatewayV2HTTPResponse nettyDispatch(InetSocketAddress clientAddress,
quarkusHeaders.setContextObject(Context.class, context);
quarkusHeaders.setContextObject(APIGatewayV2HTTPEvent.class, request);
quarkusHeaders.setContextObject(APIGatewayV2HTTPEvent.RequestContext.class, request.getRequestContext());
final Principal principal = getPrincipal(request);
if (principal != null) {
quarkusHeaders.setContextObject(Principal.class, principal);
}
DefaultHttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1,
HttpMethod.valueOf(request.getRequestContext().getHttp().getMethod()), ofNullable(request.getRawQueryString())
.filter(q -> !q.isEmpty()).map(q -> request.getRawPath() + '?' + q).orElse(request.getRawPath()),
Expand Down Expand Up @@ -234,4 +239,45 @@ private boolean isBinary(String contentType) {
return false;
}

private Principal getPrincipal(APIGatewayV2HTTPEvent request) {
final Map<String, String> systemEnvironment = System.getenv();
final boolean isSamLocal = Boolean.parseBoolean(systemEnvironment.get("AWS_SAM_LOCAL"));
patriot1burke marked this conversation as resolved.
Show resolved Hide resolved
if (isSamLocal) {
final String forcedUserName = systemEnvironment.get("QUARKUS_AWS_LAMBDA_FORCE_USER_NAME");
if (forcedUserName != null && !forcedUserName.isEmpty()) {
log.info("Forcing local user to " + forcedUserName);
return new Principal() {

@Override
public String getName() {
return forcedUserName;
}

};
}
} else {
final APIGatewayV2HTTPEvent.RequestContext requestContext = request.getRequestContext();
if (requestContext != null) {
final APIGatewayV2HTTPEvent.RequestContext.Authorizer authorizer = requestContext.getAuthorizer();
if (authorizer != null) {
final APIGatewayV2HTTPEvent.RequestContext.Authorizer.JWT jwt = authorizer.getJwt();
if (jwt != null) {
final Map<String, String> claims = jwt.getClaims();
if (claims != null) {
final String jwtUsername = claims.get("cognito:username");
patriot1burke marked this conversation as resolved.
Show resolved Hide resolved
if (jwtUsername != null && !jwtUsername.isEmpty())
return new Principal() {
@Override
public String getName() {
return jwtUsername;
}
};
}
}
}
}
}
return null;
}

}