Skip to content

Commit e328aab

Browse files
address review comments
1 parent a2022fc commit e328aab

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

x-pack/plugin/security/qa/jwt-realm/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/jwt/JwtRestIT.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -456,13 +456,13 @@ public void testFailureOnInvalidHMACSignature() throws Exception {
456456

457457
{
458458
// This is the correct HMAC passphrase (from build.gradle)
459-
final SignedJWT jwt = signHmacJwt(claimsSet, HMAC_PASSPHRASE);
459+
final SignedJWT jwt = signHmacJwt(claimsSet, HMAC_PASSPHRASE, false);
460460
final TestSecurityClient client = getSecurityClient(jwt, Optional.of(VALID_SHARED_SECRET));
461461
assertThat(client.authenticate(), hasEntry(User.Fields.USERNAME.getPreferredName(), username));
462462
}
463463
{
464464
// This is not the correct HMAC passphrase
465-
final SignedJWT invalidJwt = signHmacJwt(claimsSet, "invalid-HMAC-passphrase-" + randomAlphaOfLength(12));
465+
final SignedJWT invalidJwt = signHmacJwt(claimsSet, "invalid-HMAC-passphrase-" + randomAlphaOfLength(12), false);
466466
final TestSecurityClient client = getSecurityClient(invalidJwt, Optional.of(VALID_SHARED_SECRET));
467467
// This fails because the HMAC is wrong
468468
final ResponseException exception = expectThrows(ResponseException.class, client::authenticate);
@@ -487,7 +487,7 @@ public void testFailureOnRequiredClaims() throws JOSEException, IOException {
487487
data.put("token_use", randomValueOtherThan("access", () -> randomAlphaOfLengthBetween(3, 10)));
488488
}
489489
final JWTClaimsSet claimsSet = buildJwt(data, Instant.now(), false, false);
490-
final SignedJWT jwt = signHmacJwt(claimsSet, "test-HMAC/secret passphrase-value");
490+
final SignedJWT jwt = signHmacJwt(claimsSet, "test-HMAC/secret passphrase-value", false);
491491
final TestSecurityClient client = getSecurityClient(jwt, Optional.of(VALID_SHARED_SECRET));
492492
final ResponseException exception = expectThrows(ResponseException.class, client::authenticate);
493493
assertThat(exception.getResponse(), hasStatusCode(RestStatus.UNAUTHORIZED));
@@ -747,18 +747,18 @@ private SignedJWT buildAndSignJwtForRealm3(String principal, Instant issueTime)
747747

748748
private SignedJWT signJwtForRealm1(JWTClaimsSet claimsSet) throws IOException, JOSEException, ParseException {
749749
final RSASSASigner signer = loadRsaSigner();
750-
return signJWT(signer, "RS256", claimsSet);
750+
return signJWT(signer, "RS256", claimsSet, false);
751751
}
752752

753-
private SignedJWT signJwtForRealm2(JWTClaimsSet claimsSet) throws JOSEException, ParseException {
753+
private SignedJWT signJwtForRealm2(JWTClaimsSet claimsSet) throws JOSEException {
754754
// Input string is configured in build.gradle
755-
return signHmacJwt(claimsSet, "test-HMAC/secret passphrase-value");
755+
return signHmacJwt(claimsSet, "test-HMAC/secret passphrase-value", true);
756756
}
757757

758758
private SignedJWT signJwtForRealm3(JWTClaimsSet claimsSet) throws JOSEException, ParseException, IOException {
759759
final int bitSize = randomFrom(384, 512);
760760
final MACSigner signer = loadHmacSigner("test-hmac-" + bitSize);
761-
return signJWT(signer, "HS" + bitSize, claimsSet);
761+
return signJWT(signer, "HS" + bitSize, claimsSet, false);
762762
}
763763

764764
private RSASSASigner loadRsaSigner() throws IOException, ParseException, JOSEException {
@@ -781,10 +781,10 @@ private MACSigner loadHmacSigner(String keyId) throws IOException, ParseExceptio
781781
}
782782
}
783783

784-
private SignedJWT signHmacJwt(JWTClaimsSet claimsSet, String hmacPassphrase) throws JOSEException {
784+
private SignedJWT signHmacJwt(JWTClaimsSet claimsSet, String hmacPassphrase, boolean allowAtJwtType) throws JOSEException {
785785
final OctetSequenceKey hmac = JwkValidateUtil.buildHmacKeyFromString(hmacPassphrase);
786786
final JWSSigner signer = new MACSigner(hmac);
787-
return signJWT(signer, "HS256", claimsSet);
787+
return signJWT(signer, "HS256", claimsSet, allowAtJwtType);
788788
}
789789

790790
// JWT construction
@@ -822,10 +822,14 @@ static JWTClaimsSet buildJwt(Map<String, Object> claims, Instant issueTime, bool
822822
return builder.build();
823823
}
824824

825-
static SignedJWT signJWT(JWSSigner signer, String algorithm, JWTClaimsSet claimsSet) throws JOSEException {
825+
static SignedJWT signJWT(JWSSigner signer, String algorithm, JWTClaimsSet claimsSet, boolean allowAtJwtType) throws JOSEException {
826826
final JWSHeader.Builder builder = new JWSHeader.Builder(JWSAlgorithm.parse(algorithm));
827827
if (randomBoolean()) {
828-
builder.type(JOSEObjectType.JWT);
828+
if (allowAtJwtType && randomBoolean()) {
829+
builder.type(new JOSEObjectType("at+jwt"));
830+
} else {
831+
builder.type(JOSEObjectType.JWT);
832+
}
829833
}
830834
final JWSHeader jwtHeader = builder.build();
831835
final SignedJWT jwt = new SignedJWT(jwtHeader, claimsSet);

x-pack/plugin/security/qa/jwt-realm/src/javaRestTest/java/org/elasticsearch/xpack/security/authc/jwt/JwtWithUnavailableSecurityIndexRestIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ private SignedJWT buildAndSignJwt(String principal, String dn, Instant issueTime
279279
issueTime
280280
);
281281
final RSASSASigner signer = loadRsaSigner();
282-
return JwtRestIT.signJWT(signer, "RS256", claimsSet);
282+
return JwtRestIT.signJWT(signer, "RS256", claimsSet, false);
283283
}
284284

285285
private RSASSASigner loadRsaSigner() throws IOException, ParseException, JOSEException {

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/jwt/JwtTypeValidator.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@
1818
public class JwtTypeValidator implements JwtFieldValidator {
1919

2020
private final JOSEObjectTypeVerifier<SecurityContext> JWT_HEADER_TYPE_VERIFIER;
21+
private static final JOSEObjectType AT_PLUS_JWT = new JOSEObjectType("at+jwt");
2122

2223
public static final JwtTypeValidator ID_TOKEN_INSTANCE = new JwtTypeValidator(JOSEObjectType.JWT, null);
2324

2425
// strictly speaking, this should only permit `at+jwt`, but removing the other two options is a breaking change
25-
public static final JwtTypeValidator ACCESS_TOKEN_INSTANCE = new JwtTypeValidator(
26-
JOSEObjectType.JWT,
27-
new JOSEObjectType("at+jwt"),
28-
null
29-
);
26+
public static final JwtTypeValidator ACCESS_TOKEN_INSTANCE = new JwtTypeValidator(JOSEObjectType.JWT, AT_PLUS_JWT, null);
3027

3128
private JwtTypeValidator(JOSEObjectType... allowedTypes) {
3229
JWT_HEADER_TYPE_VERIFIER = new DefaultJOSEObjectTypeVerifier<>(allowedTypes);

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/jwt/JwtTypeValidatorTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ public void testValidAccessTokenType() throws ParseException {
5858

5959
public void testInvalidType() throws ParseException {
6060
final JwtTypeValidator validator = randomFrom(JwtTypeValidator.ID_TOKEN_INSTANCE, JwtTypeValidator.ACCESS_TOKEN_INSTANCE);
61+
final String type = randomBoolean() ? randomAlphaOfLengthBetween(4, 8) : "AT+JWT";
6162

6263
final JWSHeader jwsHeader = JWSHeader.parse(
63-
Map.of("typ", randomAlphaOfLengthBetween(4, 8), "alg", randomAlphaOfLengthBetween(3, 8))
64+
Map.of("typ", type, "alg", randomAlphaOfLengthBetween(3, 8))
6465
);
6566

6667
final IllegalArgumentException e = expectThrows(

0 commit comments

Comments
 (0)