mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(appsync): accept a bare Cognito or OIDC JWT in the Authorization header (#8399)
This commit is contained in:
1 parent
c3e9dbad96
commit
704f895982
5 files changed
+132
-11
No files matched your search
@@ -45,10 +45,32 @@ public class AuthMiddleware {
|
||||
case API_KEY -> authenticateApiKey(headers, api, configured, info);
|
||||
case AWS_IAM -> authenticateIam(headers, api, configured, info);
|
||||
case BEARER_JWT -> authenticateBearer(headers, api, configured, info);
|
||||
case AWS_LAMBDA -> authenticateLambda(headers, api, configured, info);
|
||||
case AWS_LAMBDA -> authenticateBareJwtOrLambda(headers, api, configured, info);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* AppSync takes the JWT itself as the {@code Authorization} value for Cognito and OIDC APIs, the
|
||||
* {@code Bearer } scheme being optional. A bare JWT on such an API is handled like {@code Bearer <jwt>},
|
||||
* since AppSync does not accept an OIDC token as the Lambda authorization token.
|
||||
*/
|
||||
private AppSyncAuthContext authenticateBareJwtOrLambda(
|
||||
Map<String, String> headers, GraphqlApi api, Set<AuthenticationType> configured, AuthRequestInfo info) {
|
||||
boolean bearerModes = configured.contains(AuthenticationType.AMAZON_COGNITO_USER_POOLS)
|
||||
|| configured.contains(AuthenticationType.OPENID_CONNECT);
|
||||
String authorization = CredentialClassifier.header(headers, "authorization");
|
||||
if (bearerModes && isDecodableBareJwt(authorization)) {
|
||||
return authenticateBearer(headers, api, configured, info);
|
||||
}
|
||||
return authenticateLambda(headers, api, configured, info);
|
||||
}
|
||||
|
||||
private boolean isDecodableBareJwt(String authorization) {
|
||||
return CredentialClassifier.isBareJwt(authorization)
|
||||
&& jwtClaimsDecoder.decodeHeader(authorization).isPresent()
|
||||
&& jwtClaimsDecoder.decode(authorization).isPresent();
|
||||
}
|
||||
|
||||
private AppSyncAuthContext authenticateApiKey(
|
||||
Map<String, String> headers, GraphqlApi api, Set<AuthenticationType> configured, AuthRequestInfo info) {
|
||||
if (!configured.contains(AuthenticationType.API_KEY)) {
|
||||
|
||||
@@ -47,7 +47,34 @@ public final class CredentialClassifier {
|
||||
if (authorization.length() < 7 || !authorization.regionMatches(true, 0, "Bearer ", 0, 7)) {
|
||||
return false;
|
||||
}
|
||||
String token = authorization.substring(7).trim();
|
||||
return hasJwtShape(authorization.substring(7));
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the value is three dot-separated base64url segments, so a Lambda token wrapped with
|
||||
* a prefix or suffix (for example {@code prefix:<jwt>}) is not taken for a JWT.
|
||||
*/
|
||||
static boolean isBareJwt(String value) {
|
||||
if (!hasJwtShape(value)) {
|
||||
return false;
|
||||
}
|
||||
String token = value.trim();
|
||||
for (int i = 0; i < token.length(); i++) {
|
||||
char c = token.charAt(i);
|
||||
boolean base64Url = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
|
||||
|| c == '-' || c == '_';
|
||||
if (c != '.' && !base64Url) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean hasJwtShape(String value) {
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
String token = value.trim();
|
||||
int first = token.indexOf('.');
|
||||
int second = token.indexOf('.', first + 1);
|
||||
if (first <= 0 || second <= first + 1) {
|
||||
|
||||
Reference in new issue
Block a user