Skip to content

Commit c002c6f

Browse files
grimsajzheaux
authored andcommitted
Add ClaimAccessor#hasClaim
The new method is intended to replace ClaimAccessor#containsClaim, the return type of which was non-primitive Boolean. The existing containsClaim method is now deprecated. Closes gh-9201
1 parent 050e4a9 commit c002c6f

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/ClaimAccessor.java

+22-8
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,34 @@ public interface ClaimAccessor {
5050
*/
5151
@SuppressWarnings("unchecked")
5252
default <T> T getClaim(String claim) {
53-
return !containsClaim(claim) ? null : (T) getClaims().get(claim);
53+
return !hasClaim(claim) ? null : (T) getClaims().get(claim);
5454
}
5555

5656
/**
5757
* Returns {@code true} if the claim exists in {@link #getClaims()}, otherwise
5858
* {@code false}.
5959
* @param claim the name of the claim
6060
* @return {@code true} if the claim exists, otherwise {@code false}
61+
* @since 5.5
6162
*/
62-
default Boolean containsClaim(String claim) {
63+
default boolean hasClaim(String claim) {
6364
Assert.notNull(claim, "claim cannot be null");
6465
return getClaims().containsKey(claim);
6566
}
6667

68+
/**
69+
* Returns {@code true} if the claim exists in {@link #getClaims()}, otherwise
70+
* {@code false}.
71+
* @param claim the name of the claim
72+
* @return {@code true} if the claim exists, otherwise {@code false}
73+
* @deprecated Use
74+
* {@link org.springframework.security.oauth2.core.ClaimAccessor#hasClaim} instead.
75+
*/
76+
@Deprecated
77+
default Boolean containsClaim(String claim) {
78+
return hasClaim(claim);
79+
}
80+
6781
/**
6882
* Returns the claim value as a {@code String} or {@code null} if it does not exist or
6983
* is equal to {@code null}.
@@ -72,7 +86,7 @@ default Boolean containsClaim(String claim) {
7286
* {@code null}
7387
*/
7488
default String getClaimAsString(String claim) {
75-
return !containsClaim(claim) ? null
89+
return !hasClaim(claim) ? null
7690
: ClaimConversionService.getSharedInstance().convert(getClaims().get(claim), String.class);
7791
}
7892

@@ -82,7 +96,7 @@ default String getClaimAsString(String claim) {
8296
* @return the claim value or {@code null} if it does not exist
8397
*/
8498
default Boolean getClaimAsBoolean(String claim) {
85-
return !containsClaim(claim) ? null
99+
return !hasClaim(claim) ? null
86100
: ClaimConversionService.getSharedInstance().convert(getClaims().get(claim), Boolean.class);
87101
}
88102

@@ -92,7 +106,7 @@ default Boolean getClaimAsBoolean(String claim) {
92106
* @return the claim value or {@code null} if it does not exist
93107
*/
94108
default Instant getClaimAsInstant(String claim) {
95-
if (!containsClaim(claim)) {
109+
if (!hasClaim(claim)) {
96110
return null;
97111
}
98112
Object claimValue = getClaims().get(claim);
@@ -108,7 +122,7 @@ default Instant getClaimAsInstant(String claim) {
108122
* @return the claim value or {@code null} if it does not exist
109123
*/
110124
default URL getClaimAsURL(String claim) {
111-
if (!containsClaim(claim)) {
125+
if (!hasClaim(claim)) {
112126
return null;
113127
}
114128
Object claimValue = getClaims().get(claim);
@@ -127,7 +141,7 @@ default URL getClaimAsURL(String claim) {
127141
*/
128142
@SuppressWarnings("unchecked")
129143
default Map<String, Object> getClaimAsMap(String claim) {
130-
if (!containsClaim(claim)) {
144+
if (!hasClaim(claim)) {
131145
return null;
132146
}
133147
final TypeDescriptor sourceDescriptor = TypeDescriptor.valueOf(Object.class);
@@ -150,7 +164,7 @@ default Map<String, Object> getClaimAsMap(String claim) {
150164
*/
151165
@SuppressWarnings("unchecked")
152166
default List<String> getClaimAsStringList(String claim) {
153-
if (!containsClaim(claim)) {
167+
if (!hasClaim(claim)) {
154168
return null;
155169
}
156170
final TypeDescriptor sourceDescriptor = TypeDescriptor.valueOf(Object.class);

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public void decodeWhenClaimSetConverterFailsThenBadJwtException() {
261261
public void decodeWhenSignedThenOk() {
262262
NimbusJwtDecoder jwtDecoder = new NimbusJwtDecoder(withSigning(JWK_SET));
263263
Jwt jwt = jwtDecoder.decode(SIGNED_JWT);
264-
assertThat(jwt.containsClaim(JwtClaimNames.EXP)).isNotNull();
264+
assertThat(jwt.hasClaim(JwtClaimNames.EXP)).isNotNull();
265265
}
266266

267267
@Test
@@ -435,7 +435,7 @@ public void withPublicKeyWhenUsingCustomTypeHeaderThenSuccessfullyDecodes() thro
435435
)
436436
.build();
437437
// @formatter:on
438-
assertThat(decoder.decode(signedJwt.serialize()).containsClaim(JwtClaimNames.EXP)).isNotNull();
438+
assertThat(decoder.decode(signedJwt.serialize()).hasClaim(JwtClaimNames.EXP)).isNotNull();
439439
}
440440

441441
@Test
@@ -553,7 +553,7 @@ public void withSecretKeyWhenUsingCustomTypeHeaderThenSuccessfullyDecodes() thro
553553
)
554554
.build();
555555
// @formatter:on
556-
assertThat(decoder.decode(signedJwt.serialize()).containsClaim(JwtClaimNames.EXP)).isNotNull();
556+
assertThat(decoder.decode(signedJwt.serialize()).hasClaim(JwtClaimNames.EXP)).isNotNull();
557557
}
558558

559559
@Test

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/authentication/JwtGrantedAuthoritiesConverter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private String getAuthoritiesClaimName(Jwt jwt) {
9494
return this.authoritiesClaimName;
9595
}
9696
for (String claimName : WELL_KNOWN_AUTHORITIES_CLAIM_NAMES) {
97-
if (jwt.containsClaim(claimName)) {
97+
if (jwt.hasClaim(claimName)) {
9898
return claimName;
9999
}
100100
}

0 commit comments

Comments
 (0)