Skip to content

Commit bdb0389

Browse files
Fix OAuth2ResourceServerBeanDefinitionParserTests and OAuth2ResourceServerConfigurerTests testcase
Signed-off-by: Tran Ngoc Nhan <ngocnhan.tran1996@gmail.com>
1 parent 02bf130 commit bdb0389

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

config/src/test/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ public void postWhenUsingDefaultsWithExpiredBearerTokenAndNoCsrfThenInvalidToken
604604
// @formatter:off
605605
this.mvc.perform(post("/authenticated").header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE).with(bearerToken(token)))
606606
.andExpect(status().isUnauthorized())
607-
.andExpect(invalidTokenHeader("An error occurred while attempting to decode the Jwt","/authenticated"));
607+
.andExpect(invalidTokenHeader("An error occurred while attempting to decode the Jwt", "/authenticated"));
608608
// @formatter:on
609609
}
610610

@@ -1314,7 +1314,7 @@ public void getWhenMultipleIssuersThenUsesIssuerClaimToDifferentiate() throws Ex
13141314
// @formatter:off
13151315
this.mvc.perform(get("/authenticated").with(bearerToken(jwtThree)))
13161316
.andExpect(status().isUnauthorized())
1317-
.andExpect(invalidTokenHeader("Invalid issuer","/authenticated"));
1317+
.andExpect(invalidTokenHeader("Invalid issuer", "/authenticated"));
13181318
// @formatter:on
13191319
}
13201320

config/src/test/java/org/springframework/security/config/http/OAuth2ResourceServerBeanDefinitionParserTests.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void getWhenExpiredBearerTokenThenInvalidToken() throws Exception {
183183
// @formatter:off
184184
this.mvc.perform(get("/").header("Authorization", "Bearer " + token))
185185
.andExpect(status().isUnauthorized())
186-
.andExpect(invalidTokenHeader("An error occurred while attempting to decode the Jwt"));
186+
.andExpect(invalidTokenHeader("An error occurred while attempting to decode the Jwt", "/"));
187187
// @formatter:on
188188
}
189189

@@ -215,7 +215,7 @@ public void getWhenMalformedBearerTokenThenInvalidToken() throws Exception {
215215
// @formatter:off
216216
this.mvc.perform(get("/").header("Authorization", "Bearer an\"invalid\"token"))
217217
.andExpect(status().isUnauthorized())
218-
.andExpect(invalidTokenHeader("Bearer token is malformed"));
218+
.andExpect(invalidTokenHeader("Bearer token is malformed", "/"));
219219
// @formatter:on
220220
}
221221

@@ -227,7 +227,7 @@ public void getWhenMalformedPayloadThenInvalidToken() throws Exception {
227227
// @formatter:off
228228
this.mvc.perform(get("/").header("Authorization", "Bearer " + token))
229229
.andExpect(status().isUnauthorized())
230-
.andExpect(invalidTokenHeader("An error occurred while attempting to decode the Jwt: Malformed payload"));
230+
.andExpect(invalidTokenHeader("An error occurred while attempting to decode the Jwt: Malformed payload", "/"));
231231
// @formatter:on
232232
}
233233

@@ -238,7 +238,7 @@ public void getWhenUnsignedBearerTokenThenInvalidToken() throws Exception {
238238
// @formatter:off
239239
this.mvc.perform(get("/").header("Authorization", "Bearer " + token))
240240
.andExpect(status().isUnauthorized())
241-
.andExpect(invalidTokenHeader("Unsupported algorithm of none"));
241+
.andExpect(invalidTokenHeader("Unsupported algorithm of none", "/"));
242242
// @formatter:on
243243
}
244244

@@ -250,7 +250,7 @@ public void getWhenBearerTokenBeforeNotBeforeThenInvalidToken() throws Exception
250250
// @formatter:off
251251
this.mvc.perform(get("/").header("Authorization", "Bearer " + token))
252252
.andExpect(status().isUnauthorized())
253-
.andExpect(invalidTokenHeader("An error occurred while attempting to decode the Jwt"));
253+
.andExpect(invalidTokenHeader("An error occurred while attempting to decode the Jwt", "/"));
254254
// @formatter:on
255255
}
256256

@@ -342,7 +342,7 @@ public void getWhenAuthorizationServerHasNoMatchingKeyThenInvalidToken() throws
342342
// @formatter:off
343343
this.mvc.perform(get("/").header("Authorization", "Bearer " + token))
344344
.andExpect(status().isUnauthorized())
345-
.andExpect(invalidTokenHeader("An error occurred while attempting to decode the Jwt"));
345+
.andExpect(invalidTokenHeader("An error occurred while attempting to decode the Jwt", "/"));
346346
// @formatter:on
347347
}
348348

@@ -398,7 +398,7 @@ public void postWhenExpiredBearerTokenAndNoCsrfThenInvalidToken() throws Excepti
398398
// @formatter:off
399399
this.mvc.perform(post("/authenticated").header("Authorization", "Bearer " + token))
400400
.andExpect(status().isUnauthorized())
401-
.andExpect(invalidTokenHeader("An error occurred while attempting to decode the Jwt"));
401+
.andExpect(invalidTokenHeader("An error occurred while attempting to decode the Jwt", "/authenticated"));
402402
// @formatter:on
403403
}
404404

@@ -621,7 +621,7 @@ public void requestWhenClockSkewSetButJwtStillTooLateThenReportsExpired() throws
621621
// @formatter:off
622622
this.mvc.perform(get("/").header("Authorization", "Bearer " + token))
623623
.andExpect(status().isUnauthorized())
624-
.andExpect(invalidTokenHeader("Jwt expired at"));
624+
.andExpect(invalidTokenHeader("Jwt expired at", "/"));
625625
// @formatter:on
626626
}
627627

@@ -661,7 +661,7 @@ public void requestWhenUsingPublicKeyAndSignatureFailsThenReturnsInvalidToken()
661661
String token = this.token("WrongSignature");
662662
// @formatter:off
663663
this.mvc.perform(get("/").header("Authorization", "Bearer " + token))
664-
.andExpect(invalidTokenHeader("signature"));
664+
.andExpect(invalidTokenHeader("signature", "/"));
665665
// @formatter:on
666666
}
667667

@@ -671,7 +671,7 @@ public void requestWhenUsingPublicKeyAlgorithmDoesNotMatchThenReturnsInvalidToke
671671
String token = this.token("WrongAlgorithm");
672672
// @formatter:off
673673
this.mvc.perform(get("/").header("Authorization", "Bearer " + token))
674-
.andExpect(invalidTokenHeader("algorithm"));
674+
.andExpect(invalidTokenHeader("algorithm", "/"));
675675
// @formatter:on
676676
}
677677

@@ -779,7 +779,7 @@ public void getWhenMultipleIssuersThenUsesIssuerClaimToDifferentiate() throws Ex
779779
// @formatter:off
780780
this.mvc.perform(get("/authenticated").header("Authorization", "Bearer " + jwtThree))
781781
.andExpect(status().isUnauthorized())
782-
.andExpect(invalidTokenHeader("Invalid issuer"));
782+
.andExpect(invalidTokenHeader("Invalid issuer", "/authenticated"));
783783
// @formatter:on
784784
}
785785

@@ -934,13 +934,15 @@ private static ResultMatcher invalidRequestHeader(String message) {
934934
", " + "resource_metadata=\"http://localhost/.well-known/oauth-protected-resource\"")));
935935
}
936936

937-
private static ResultMatcher invalidTokenHeader(String message) {
937+
private static ResultMatcher invalidTokenHeader(String message, String endpoint) {
938+
String path = "/".equals(endpoint) ? "" : endpoint;
938939
return header().string(HttpHeaders.WWW_AUTHENTICATE,
939940
AllOf.allOf(new StringStartsWith("Bearer " + "error=\"invalid_token\", " + "error_description=\""),
940941
new StringContains(message),
941942
new StringContains(", " + "error_uri=\"https://tools.ietf.org/html/rfc6750#section-3.1\""),
942943
new StringEndsWith(
943-
", " + "resource_metadata=\"http://localhost/.well-known/oauth-protected-resource\"")));
944+
", resource_metadata=\"http://localhost/.well-known/oauth-protected-resource%s\""
945+
.formatted(path))));
944946
}
945947

946948
private static ResultMatcher insufficientScopeHeader() {

0 commit comments

Comments
 (0)