refactor(auth): dedupe SigV4 Authorization header parsing (#3719)

ExecuteApiSigV4Authorizer (API Gateway) and IamAuthValidator (AppSync)
each carried an identical 15-line block splitting Credential=/
SignedHeaders=/Signature= out of a raw AWS4-HMAC-SHA256 Authorization
header value, found via CPD (#3714). Neither #3657 (closing #3650) nor
#3706 (closing #3700), which already deduplicated related SigV4 logic
between these same two files, touched this specific block.

Extracted it into SigV4AuthorizationHeader in core.common.auth,
alongside CredentialScope and SigV4RequestValidator from #3657, and
pointed both call sites at it.

No behavior change: the parsing is identical, only its location
changed. Both files' existing test suites pass unchanged.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-16 00:13:29 -05:00
1 parent 1f554003be
commit 03ead297a3
3 files changed
+55 -34

No files matched your search

@@ -0,0 +1,41 @@
package io.github.hectorvent.floci.core.common.auth;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* The {@code Credential}/{@code SignedHeaders}/{@code Signature} parameters carried by an
* {@code AWS4-HMAC-SHA256 Authorization} header, split out of its raw comma-separated value.
* Shared by every SigV4 verifier in this codebase that accepts a header-signed request:
* {@code ExecuteApiSigV4Authorizer} (API Gateway) and {@code IamAuthValidator} (AppSync) both
* parsed this independently before this class existed.
*/
public record SigV4AuthorizationHeader(String credential, String signedHeaders, String signature) {
private static final String ALGORITHM = "AWS4-HMAC-SHA256";
/**
* Returns null when {@code authorization} is null or does not start with the expected
* algorithm. A parameter missing from an otherwise well-formed header comes back null on the
* record instead, left for the caller to reject alongside whatever else it requires (e.g. an
* {@code X-Amz-Date} it reads from a different source than this header).
*/
public static SigV4AuthorizationHeader parse(String authorization) {
if (authorization == null) {
return null;
}
String trimmed = authorization.trim();
if (!trimmed.regionMatches(true, 0, ALGORITHM, 0, ALGORITHM.length())) {
return null;
}
Map<String, String> parameters = new LinkedHashMap<>();
for (String part : trimmed.substring(ALGORITHM.length()).split(",")) {
int equals = part.indexOf('=');
if (equals > 0) {
parameters.put(part.substring(0, equals).trim(), part.substring(equals + 1).trim());
}
}
return new SigV4AuthorizationHeader(
parameters.get("Credential"), parameters.get("SignedHeaders"), parameters.get("Signature"));
}
}
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.apigateway;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.auth.CredentialScope;
import io.github.hectorvent.floci.core.common.auth.SigV4AuthorizationHeader;
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
import io.github.hectorvent.floci.services.iam.IamService;
import io.github.hectorvent.floci.services.iam.model.AccessKey;
@@ -22,10 +23,8 @@ import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
* Verifies the SigV4 signature on an execute-api data-plane request, so a route or method whose
@@ -348,28 +347,20 @@ public class ExecuteApiSigV4Authorizer {
String amzDate, Long expiresSeconds) {}
private static SignedRequest headerSignedRequest(String authorization, HttpHeaders headers) {
String trimmed = authorization.trim();
if (!trimmed.regionMatches(true, 0, ALGORITHM, 0, ALGORITHM.length())) {
SigV4AuthorizationHeader parsed = SigV4AuthorizationHeader.parse(authorization);
if (parsed == null) {
return null;
}
Map<String, String> parameters = new LinkedHashMap<>();
for (String part : trimmed.substring(ALGORITHM.length()).split(",")) {
int equals = part.indexOf('=');
if (equals > 0) {
parameters.put(part.substring(0, equals).trim(), part.substring(equals + 1).trim());
}
}
String credential = parameters.get("Credential");
String signedHeaders = parameters.get("SignedHeaders");
String signature = parameters.get("Signature");
String amzDate = headers.getHeaderString("X-Amz-Date");
if (amzDate == null || amzDate.isBlank()) {
amzDate = headers.getHeaderString("Date");
}
if (isBlank(credential) || isBlank(signedHeaders) || isBlank(signature) || isBlank(amzDate)) {
if (isBlank(parsed.credential()) || isBlank(parsed.signedHeaders())
|| isBlank(parsed.signature()) || isBlank(amzDate)) {
return null;
}
return new SignedRequest(credential, signedHeaders.toLowerCase(Locale.ROOT), signature, amzDate, null);
return new SignedRequest(parsed.credential(), parsed.signedHeaders().toLowerCase(Locale.ROOT),
parsed.signature(), amzDate, null);
}
private static SignedRequest presignedRequest(MultivaluedMap<String, String> queryParameters) {
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.appsync.graphql.auth;
import io.github.hectorvent.floci.core.common.AwsRegions;
import io.github.hectorvent.floci.core.common.AccountResolver;
import io.github.hectorvent.floci.core.common.auth.CredentialScope;
import io.github.hectorvent.floci.core.common.auth.SigV4AuthorizationHeader;
import io.github.hectorvent.floci.core.common.auth.SigV4RequestValidator;
import io.github.hectorvent.floci.services.appsync.graphql.AppSyncTransportException;
import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator;
@@ -17,7 +18,6 @@ import java.security.MessageDigest;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
@@ -212,28 +212,17 @@ public class IamAuthValidator {
* regardless.
*/
private static SignedRequest headerSignedRequest(String authorization, Map<String, String> requestHeaders) {
if (authorization == null) {
SigV4AuthorizationHeader parsed = SigV4AuthorizationHeader.parse(authorization);
if (parsed == null) {
return null;
}
String trimmed = authorization.trim();
if (!trimmed.regionMatches(true, 0, ALGORITHM, 0, ALGORITHM.length())) {
return null;
}
Map<String, String> parameters = new LinkedHashMap<>();
for (String part : trimmed.substring(ALGORITHM.length()).split(",")) {
int equals = part.indexOf('=');
if (equals > 0) {
parameters.put(part.substring(0, equals).trim(), part.substring(equals + 1).trim());
}
}
String credential = parameters.get("Credential");
String signedHeaders = parameters.get("SignedHeaders");
String signature = parameters.get("Signature");
String amzDate = header(requestHeaders, "X-Amz-Date");
if (isBlank(credential) || isBlank(signedHeaders) || isBlank(signature) || isBlank(amzDate)) {
if (isBlank(parsed.credential()) || isBlank(parsed.signedHeaders())
|| isBlank(parsed.signature()) || isBlank(amzDate)) {
return null;
}
return new SignedRequest(credential, signedHeaders.toLowerCase(Locale.ROOT), signature, amzDate);
return new SignedRequest(parsed.credential(), parsed.signedHeaders().toLowerCase(Locale.ROOT),
parsed.signature(), amzDate);
}
private static String canonicalHeaders(String signedHeaders, Map<String, String> requestHeaders) {