Skip to content

Commit 361347f

Browse files
author
douxf
committed
enhancement for NimbusJwtEncoder to supporting key rotation
close 16170
1 parent 4eb3a3b commit 361347f

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/NimbusJwtEncoder.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ private JWK selectJwk(JwsHeader headers) {
111111
try {
112112
JWKSelector jwkSelector = new JWKSelector(createJwkMatcher(headers));
113113
jwks = this.jwkSource.get(jwkSelector, null);
114-
} catch (Exception ex) {
114+
}
115+
catch (Exception ex) {
115116
throw new JwtEncodingException(String.format(ENCODING_ERROR_MESSAGE_TEMPLATE,
116117
"Failed to select a JWK signing key -> " + ex.getMessage()), ex);
117118
}
@@ -139,7 +140,8 @@ private String serialize(JwsHeader headers, JwtClaimsSet claims, JWK jwk) {
139140
SignedJWT signedJwt = new SignedJWT(jwsHeader, jwtClaimsSet);
140141
try {
141142
signedJwt.sign(jwsSigner);
142-
} catch (JOSEException ex) {
143+
}
144+
catch (JOSEException ex) {
143145
throw new JwtEncodingException(
144146
String.format(ENCODING_ERROR_MESSAGE_TEMPLATE, "Failed to sign the JWT -> " + ex.getMessage()), ex);
145147
}
@@ -159,7 +161,8 @@ private static JWKMatcher createJwkMatcher(JwsHeader headers) {
159161
.x509CertSHA256Thumbprint(Base64URL.from(headers.getX509SHA256Thumbprint()))
160162
.build();
161163
// @formatter:on
162-
} else if (JWSAlgorithm.Family.HMAC_SHA.contains(jwsAlgorithm)) {
164+
}
165+
else if (JWSAlgorithm.Family.HMAC_SHA.contains(jwsAlgorithm)) {
163166
// @formatter:off
164167
return new JWKMatcher.Builder()
165168
.keyType(KeyType.forAlgorithm(jwsAlgorithm))
@@ -197,7 +200,8 @@ private static JwsHeader addKeyIdentifierHeadersIfNecessary(JwsHeader headers, J
197200
private static JWSSigner createSigner(JWK jwk) {
198201
try {
199202
return JWS_SIGNER_FACTORY.createJWSSigner(jwk);
200-
} catch (JOSEException ex) {
203+
}
204+
catch (JOSEException ex) {
201205
throw new JwtEncodingException(String.format(ENCODING_ERROR_MESSAGE_TEMPLATE,
202206
"Failed to create a JWS Signer -> " + ex.getMessage()), ex);
203207
}
@@ -214,7 +218,8 @@ private static JWSHeader convert(JwsHeader headers) {
214218
if (!CollectionUtils.isEmpty(jwk)) {
215219
try {
216220
builder.jwk(JWK.parse(jwk));
217-
} catch (Exception ex) {
221+
}
222+
catch (Exception ex) {
218223
throw new JwtEncodingException(String.format(ENCODING_ERROR_MESSAGE_TEMPLATE,
219224
"Unable to convert '" + JoseHeaderNames.JWK + "' JOSE header"), ex);
220225
}
@@ -331,7 +336,8 @@ private static JWTClaimsSet convert(JwtClaimsSet claims) {
331336
private static URI convertAsURI(String header, URL url) {
332337
try {
333338
return url.toURI();
334-
} catch (Exception ex) {
339+
}
340+
catch (Exception ex) {
335341
throw new JwtEncodingException(String.format(ENCODING_ERROR_MESSAGE_TEMPLATE,
336342
"Unable to convert '" + header + "' JOSE header to a URI"), ex);
337343
}

oauth2/oauth2-jose/src/test/java/org/springframework/security/oauth2/jwt/NimbusJwtEncoderTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void encodeWhenJwkSelectFailedThenThrowJwtEncodingException() throws Exce
108108
}
109109

110110
@Test
111-
public void encodeWhenJwkMultipleSelectedThenThrowJwtEncodingException() throws Exception {
111+
public void encodeWhenJwkMultipleSelectedThenThrowJwtEncodingException() {
112112
RSAKey rsaJwk = TestJwks.DEFAULT_RSA_JWK;
113113
this.jwkList.add(rsaJwk);
114114
this.jwkList.add(rsaJwk);
@@ -122,7 +122,7 @@ public void encodeWhenJwkMultipleSelectedThenThrowJwtEncodingException() throws
122122
}
123123

124124
@Test
125-
public void encodeWhenJwkMultipleSelectedWithJwkSelector() throws Exception {
125+
public void encodeWhenJwkMultipleSelectedWithJwkSelector() {
126126
RSAKey rsaJwk = TestJwks.DEFAULT_RSA_JWK;
127127
this.jwkList.add(rsaJwk);
128128
this.jwkList.add(rsaJwk);
@@ -131,17 +131,16 @@ public void encodeWhenJwkMultipleSelectedWithJwkSelector() throws Exception {
131131
JwsHeader jwsHeader = JwsHeader.with(SignatureAlgorithm.RS256).build();
132132
JwtClaimsSet jwtClaimsSet = TestJwtClaimsSets.jwtClaimsSet().build();
133133

134-
Jwt encodedJws = this.jwtEncoder.encode(JwtEncoderParameters.from(jwtClaimsSet));
134+
Jwt encodedJws = this.jwtEncoder.encode(JwtEncoderParameters.from(jwsHeader,jwtClaimsSet));
135135
assertThat(encodedJws.getHeaders()).containsEntry(JoseHeaderNames.ALG, SignatureAlgorithm.RS256);
136136

137-
this.jwtEncoder.setJwkSelector(jwkSelector -> jwkSelector.get(jwkSelector.size()-1));
138-
jwtClaimsSet = TestJwtClaimsSets.jwtClaimsSet().build();
139-
encodedJws = this.jwtEncoder.encode(JwtEncoderParameters.from(jwtClaimsSet));
137+
this.jwtEncoder.setJwkSelector(jwkSelector -> jwkSelector.get(jwkSelector.size() - 1));
138+
jwtClaimsSet = TestJwtClaimsSets.jwtClaimsSet().build();
139+
encodedJws = this.jwtEncoder.encode(JwtEncoderParameters.from(jwtClaimsSet));
140140
assertThat(encodedJws.getHeaders()).containsEntry(JoseHeaderNames.ALG, SignatureAlgorithm.RS256);
141141
this.jwtEncoder.setJwkSelector(null);
142142
}
143143

144-
145144
@Test
146145
public void encodeWhenJwkSelectEmptyThenThrowJwtEncodingException() {
147146
JwsHeader jwsHeader = JwsHeader.with(SignatureAlgorithm.RS256).build();

0 commit comments

Comments
 (0)