Skip to content

Commit

Permalink
JWT Auth micro optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
luneo7 committed Nov 15, 2024
1 parent 71d80a8 commit 09a274d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ public static void setContextTokenCookie(JWTAuthContextInfo contextInfo, Optiona
}

public static void setTokenSchemes(JWTAuthContextInfo contextInfo, String tokenSchemes) {
final List<String> schemes = new ArrayList<>();
for (final String s : tokenSchemes.split(",")) {
String[] splitTokenSchemes = tokenSchemes.split(",");
final List<String> schemes = new ArrayList<>(splitTokenSchemes.length);
for (final String s : splitTokenSchemes) {
schemes.add(s.trim());
}
contextInfo.setTokenSchemes(schemes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Long getClaimAsLong(InjectionPoint ip) {
JsonNumber jsonValue = (JsonNumber) value;
returnValue = jsonValue.longValue();
} else {
returnValue = Long.parseLong(value.toString());
returnValue = Long.valueOf(value.toString());
}
}
return returnValue;
Expand All @@ -113,7 +113,7 @@ Double getClaimAsDouble(InjectionPoint ip) {
JsonNumber jsonValue = (JsonNumber) value;
returnValue = jsonValue.doubleValue();
} else {
returnValue = Double.parseDouble(value.toString());
returnValue = Double.valueOf(value.toString());
}
}
return returnValue;
Expand All @@ -135,9 +135,9 @@ Boolean getClaimAsBoolean(InjectionPoint ip) {
if (value instanceof JsonValue) {
final JsonValue.ValueType valueType = ((JsonValue) value).getValueType();
if (valueType.equals(JsonValue.ValueType.TRUE)) {
returnValue = true;
returnValue = Boolean.TRUE;
} else if (valueType.equals(JsonValue.ValueType.FALSE)) {
returnValue = false;
returnValue = Boolean.FALSE;
}
} else {
returnValue = Boolean.valueOf(value.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static java.util.Collections.emptyList;

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -91,19 +90,23 @@ private String decryptSignedToken(String token, JWTAuthContextInfo authContextIn
}

private String[] signatureAlgorithms(JWTAuthContextInfo authContextInfo) {
Set<String> algorithms = new LinkedHashSet<>();
for (SignatureAlgorithm keyEncAlgo : authContextInfo.getSignatureAlgorithm()) {
algorithms.add(keyEncAlgo.getAlgorithm());
Set<SignatureAlgorithm> signatureAlgorithm = authContextInfo.getSignatureAlgorithm();
String[] algorithms = new String[signatureAlgorithm.size()];
int counter = 0;
for (SignatureAlgorithm keyEncAlgo : signatureAlgorithm) {
algorithms[counter++] = keyEncAlgo.getAlgorithm();
}
return algorithms.toArray(new String[] {});
return algorithms;
}

private String[] encryptionAlgorithms(JWTAuthContextInfo authContextInfo) {
Set<String> algorithms = new LinkedHashSet<>();
for (KeyEncryptionAlgorithm keyEncAlgo : authContextInfo.getKeyEncryptionAlgorithm()) {
algorithms.add(keyEncAlgo.getAlgorithm());
Set<KeyEncryptionAlgorithm> keyEncryptionAlgorithm = authContextInfo.getKeyEncryptionAlgorithm();
String[] algorithms = new String[keyEncryptionAlgorithm.size()];
int counter = 0;
for (KeyEncryptionAlgorithm keyEncAlgo : keyEncryptionAlgorithm) {
algorithms[counter++] = keyEncAlgo.getAlgorithm();
}
return algorithms.toArray(new String[] {});
return algorithms;
}

private JwtContext parseClaims(String token, JWTAuthContextInfo authContextInfo, ProtectionLevel level)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Key resolveKey(JsonWebSignature jws, List<JsonWebStructure> nestingContex

if (theKey == null) {
try {
if (httpsJwks != null && httpsJwks.getJsonWebKeys() != null && jws != null
if (httpsJwks != null && jws != null && httpsJwks.getJsonWebKeys() != null
&& jws.getKeyIdHeaderValue() != null) {
throw PrincipalMessages.msg.unmatchedTokenKidException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ Optional<JWTAuthContextInfo> getOptionalContextInfo() {
Set<SignatureAlgorithm> resolvedAlgorithm = mpJwtPublicKeyAlgorithm;
if (signatureAlgorithm.isPresent()) {
if (signatureAlgorithm.get().getAlgorithm().startsWith("HS")) {
if (!NONE.equals(resolvedVerifyKeyLocation) && resolvedVerifyKeyLocation == mpJwtLocation) {
if (verificationKeyLocationSet && resolvedVerifyKeyLocation == mpJwtLocation) {
throw ConfigMessages.msg.hmacNotSupported();
}
} else {
Expand Down

0 comments on commit 09a274d

Please sign in to comment.