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
This commit is contained in:
grimsa 2020-11-24 19:47:03 +02:00 committed by Josh Cummings
parent 050e4a98b4
commit c002c6f9f3
No known key found for this signature in database
GPG Key ID: 49EF60DD7FF83443
3 changed files with 26 additions and 12 deletions

View File

@ -50,7 +50,7 @@ public interface ClaimAccessor {
*/
@SuppressWarnings("unchecked")
default <T> T getClaim(String claim) {
return !containsClaim(claim) ? null : (T) getClaims().get(claim);
return !hasClaim(claim) ? null : (T) getClaims().get(claim);
}
/**
@ -58,12 +58,26 @@ public interface ClaimAccessor {
* {@code false}.
* @param claim the name of the claim
* @return {@code true} if the claim exists, otherwise {@code false}
* @since 5.5
*/
default Boolean containsClaim(String claim) {
default boolean hasClaim(String claim) {
Assert.notNull(claim, "claim cannot be null");
return getClaims().containsKey(claim);
}
/**
* Returns {@code true} if the claim exists in {@link #getClaims()}, otherwise
* {@code false}.
* @param claim the name of the claim
* @return {@code true} if the claim exists, otherwise {@code false}
* @deprecated Use
* {@link org.springframework.security.oauth2.core.ClaimAccessor#hasClaim} instead.
*/
@Deprecated
default Boolean containsClaim(String claim) {
return hasClaim(claim);
}
/**
* Returns the claim value as a {@code String} or {@code null} if it does not exist or
* is equal to {@code null}.
@ -72,7 +86,7 @@ public interface ClaimAccessor {
* {@code null}
*/
default String getClaimAsString(String claim) {
return !containsClaim(claim) ? null
return !hasClaim(claim) ? null
: ClaimConversionService.getSharedInstance().convert(getClaims().get(claim), String.class);
}
@ -82,7 +96,7 @@ public interface ClaimAccessor {
* @return the claim value or {@code null} if it does not exist
*/
default Boolean getClaimAsBoolean(String claim) {
return !containsClaim(claim) ? null
return !hasClaim(claim) ? null
: ClaimConversionService.getSharedInstance().convert(getClaims().get(claim), Boolean.class);
}
@ -92,7 +106,7 @@ public interface ClaimAccessor {
* @return the claim value or {@code null} if it does not exist
*/
default Instant getClaimAsInstant(String claim) {
if (!containsClaim(claim)) {
if (!hasClaim(claim)) {
return null;
}
Object claimValue = getClaims().get(claim);
@ -108,7 +122,7 @@ public interface ClaimAccessor {
* @return the claim value or {@code null} if it does not exist
*/
default URL getClaimAsURL(String claim) {
if (!containsClaim(claim)) {
if (!hasClaim(claim)) {
return null;
}
Object claimValue = getClaims().get(claim);
@ -127,7 +141,7 @@ public interface ClaimAccessor {
*/
@SuppressWarnings("unchecked")
default Map<String, Object> getClaimAsMap(String claim) {
if (!containsClaim(claim)) {
if (!hasClaim(claim)) {
return null;
}
final TypeDescriptor sourceDescriptor = TypeDescriptor.valueOf(Object.class);
@ -150,7 +164,7 @@ public interface ClaimAccessor {
*/
@SuppressWarnings("unchecked")
default List<String> getClaimAsStringList(String claim) {
if (!containsClaim(claim)) {
if (!hasClaim(claim)) {
return null;
}
final TypeDescriptor sourceDescriptor = TypeDescriptor.valueOf(Object.class);

View File

@ -261,7 +261,7 @@ public class NimbusJwtDecoderTests {
public void decodeWhenSignedThenOk() {
NimbusJwtDecoder jwtDecoder = new NimbusJwtDecoder(withSigning(JWK_SET));
Jwt jwt = jwtDecoder.decode(SIGNED_JWT);
assertThat(jwt.containsClaim(JwtClaimNames.EXP)).isNotNull();
assertThat(jwt.hasClaim(JwtClaimNames.EXP)).isNotNull();
}
@Test
@ -435,7 +435,7 @@ public class NimbusJwtDecoderTests {
)
.build();
// @formatter:on
assertThat(decoder.decode(signedJwt.serialize()).containsClaim(JwtClaimNames.EXP)).isNotNull();
assertThat(decoder.decode(signedJwt.serialize()).hasClaim(JwtClaimNames.EXP)).isNotNull();
}
@Test
@ -553,7 +553,7 @@ public class NimbusJwtDecoderTests {
)
.build();
// @formatter:on
assertThat(decoder.decode(signedJwt.serialize()).containsClaim(JwtClaimNames.EXP)).isNotNull();
assertThat(decoder.decode(signedJwt.serialize()).hasClaim(JwtClaimNames.EXP)).isNotNull();
}
@Test

View File

@ -94,7 +94,7 @@ public final class JwtGrantedAuthoritiesConverter implements Converter<Jwt, Coll
return this.authoritiesClaimName;
}
for (String claimName : WELL_KNOWN_AUTHORITIES_CLAIM_NAMES) {
if (jwt.containsClaim(claimName)) {
if (jwt.hasClaim(claimName)) {
return claimName;
}
}