Skip to content

Commit 8f0c00b

Browse files
[FEATURE] usage of JWKS with JWT (w/o OpenID connect)
Signed-off-by: Sebastian Michalski <shekerama@gmail.com>
1 parent e799bf6 commit 8f0c00b

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

src/test/java/com/amazon/dlic/auth/http/jwt/keybyoidc/HTTPJwtKeyByOpenIdConnectAuthenticatorTest.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,45 @@ public void jwksUriTest() {
8585
}
8686

8787
@Test
88-
public void jwksMissingRequiredAudienceAndIssuerTest() {
88+
public void jwksMatchingRequiredIssuerInClaimTest() {
89+
String requiredIssuer = "requiredIssuer";
8990
Settings settings = Settings.builder()
9091
.put("jwks_uri", mockIdpServer.getJwksUri())
92+
.put("required_issuer", requiredIssuer)
9193
.build();
9294

9395
HTTPJwtKeyByOpenIdConnectAuthenticator jwtAuth = new HTTPJwtKeyByOpenIdConnectAuthenticator(settings, null);
9496

9597
AuthCredentials creds = jwtAuth.extractCredentials(new FakeRestRequest(
96-
ImmutableMap.of("Authorization", TestJwts.MC_COY_SIGNED_OCT_1), new HashMap<>()), null);
98+
ImmutableMap.of("Authorization", TestJwts.MC_COY_SIGNED_OCT_2), new HashMap<>()), null);
9799

98100
Assert.assertNotNull(creds);
99101
Assert.assertEquals(TestJwts.MCCOY_SUBJECT, creds.getUsername());
100102
Assert.assertEquals(TestJwts.TEST_AUDIENCE, creds.getAttributes().get("attr.jwt.aud"));
101103
Assert.assertEquals(0, creds.getBackendRoles().size());
102-
Assert.assertEquals(3, creds.getAttributes().size());
103-
Assert.assertNull(jwtAuth.getRequiredAudience());
104-
Assert.assertNull(jwtAuth.getRequiredIssuer());
104+
Assert.assertEquals(4, creds.getAttributes().size());
105+
Assert.assertFalse(creds.getAttributes().get("attr.jwt.iss").contains(jwtAuth.getRequiredIssuer()));
106+
}
107+
108+
@Test
109+
public void jwksNotMatchingRequiredAudienceInClaimTest() {
110+
String requiredAudience = "requiredAudience";
111+
Settings settings = Settings.builder()
112+
.put("jwks_uri", mockIdpServer.getJwksUri())
113+
.put("required_audience", requiredAudience)
114+
.build();
115+
116+
HTTPJwtKeyByOpenIdConnectAuthenticator jwtAuth = new HTTPJwtKeyByOpenIdConnectAuthenticator(settings, null);
117+
118+
AuthCredentials creds = jwtAuth.extractCredentials(new FakeRestRequest(
119+
ImmutableMap.of("Authorization", TestJwts.MC_COY_SIGNED_OCT_2), new HashMap<>()), null);
120+
121+
Assert.assertNotNull(creds);
122+
Assert.assertEquals(TestJwts.MCCOY_SUBJECT, creds.getUsername());
123+
Assert.assertEquals(TestJwts.TEST_AUDIENCE, creds.getAttributes().get("attr.jwt.aud"));
124+
Assert.assertEquals(0, creds.getBackendRoles().size());
125+
Assert.assertEquals(4, creds.getAttributes().size());
126+
Assert.assertFalse(creds.getAttributes().get("attr.jwt.aud").contains(jwtAuth.getRequiredAudience()));
105127
}
106128

107129
@Test

src/test/java/com/amazon/dlic/auth/http/jwt/keybyoidc/TestJwts.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,18 @@ class TestJwts {
3333

3434
static final String MCCOY_SUBJECT = "Leonard McCoy";
3535

36-
static final JwtToken MC_COY = create(MCCOY_SUBJECT, TEST_AUDIENCE, ROLES_CLAIM, TEST_ROLES_STRING);
36+
static final String ISS = "ISSUER";
3737

38-
static final JwtToken MC_COY_EXPIRED = create(MCCOY_SUBJECT, TEST_AUDIENCE, ROLES_CLAIM, TEST_ROLES_STRING, JwtConstants.CLAIM_EXPIRY, 10);
38+
static final JwtToken MC_COY = create(MCCOY_SUBJECT, TEST_AUDIENCE, null, ROLES_CLAIM, TEST_ROLES_STRING);
39+
40+
static final JwtToken MC_COY_2 = create(MCCOY_SUBJECT, TEST_AUDIENCE, ISS, ROLES_CLAIM, TEST_ROLES_STRING);
41+
42+
static final JwtToken MC_COY_EXPIRED = create(MCCOY_SUBJECT, TEST_AUDIENCE, null, ROLES_CLAIM, TEST_ROLES_STRING, JwtConstants.CLAIM_EXPIRY, 10);
3943

4044
static final String MC_COY_SIGNED_OCT_1 = createSigned(MC_COY, TestJwk.OCT_1);
4145

46+
static final String MC_COY_SIGNED_OCT_2 = createSigned(MC_COY_2, TestJwk.OCT_1);
47+
4248
static final String MC_COY_SIGNED_OCT_1_INVALID_KID = createSigned(MC_COY, TestJwk.FORWARD_SLASH_KID_OCT_1);
4349

4450
static final String MC_COY_SIGNED_RSA_1 = createSigned(MC_COY, TestJwk.RSA_1);
@@ -57,11 +63,12 @@ static class PeculiarEscaping {
5763
static final String MC_COY_SIGNED_RSA_1 = createSignedWithPeculiarEscaping(MC_COY, TestJwk.RSA_1);
5864
}
5965

60-
static JwtToken create(String subject, String audience, Object... moreClaims) {
66+
static JwtToken create(String subject, String audience, String issuer, Object... moreClaims) {
6167
JwtClaims claims = new JwtClaims();
6268

6369
claims.setSubject(subject);
6470
claims.setAudience(audience);
71+
claims.setIssuer(issuer);
6572

6673
if (moreClaims != null) {
6774
for (int i = 0; i < moreClaims.length; i += 2) {
@@ -108,8 +115,8 @@ static String createSignedWithPeculiarEscaping(JwtToken baseJwt, JsonWebKey jwk)
108115
static String createMcCoySignedOct1(long nbf, long exp)
109116
{
110117
JwtToken jwt_token = create(
111-
MCCOY_SUBJECT, TEST_AUDIENCE,
112-
ROLES_CLAIM, TEST_ROLES_STRING,
118+
MCCOY_SUBJECT, TEST_AUDIENCE,
119+
null, ROLES_CLAIM, TEST_ROLES_STRING,
113120
JwtConstants.CLAIM_NOT_BEFORE, nbf,
114121
JwtConstants.CLAIM_EXPIRY, exp);
115122

0 commit comments

Comments
 (0)