Skip to content

Commit 64e9ac9

Browse files
qavidsjohnr
authored andcommitted
getClaimAsBoolean() should not be falsy
Closes gh-10148
1 parent 00084cf commit 64e9ac9

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,23 @@ default String getClaimAsString(String claim) {
9191
}
9292

9393
/**
94-
* Returns the claim value as a {@code Boolean} or {@code null} if it does not exist.
94+
* Returns the claim value as a {@code Boolean} or {@code null} if the claim does not
95+
* exist.
9596
* @param claim the name of the claim
96-
* @return the claim value or {@code null} if it does not exist
97+
* @return the claim value or {@code null} if the claim does not exist
98+
* @throws IllegalArgumentException if the claim value cannot be converted to a
99+
* {@code Boolean}
100+
* @throws NullPointerException if the claim value is {@code null}
97101
*/
98102
default Boolean getClaimAsBoolean(String claim) {
99-
return !hasClaim(claim) ? null
100-
: ClaimConversionService.getSharedInstance().convert(getClaims().get(claim), Boolean.class);
103+
if (!hasClaim(claim)) {
104+
return null;
105+
}
106+
Object claimValue = getClaims().get(claim);
107+
Boolean convertedValue = ClaimConversionService.getSharedInstance().convert(claimValue, Boolean.class);
108+
Assert.notNull(convertedValue,
109+
() -> "Unable to convert claim '" + claim + "' of type '" + claimValue.getClass() + "' to Boolean.");
110+
return convertedValue;
101111
}
102112

103113
/**

oauth2/oauth2-core/src/main/java/org/springframework/security/oauth2/core/converter/ObjectToBooleanConverter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,7 +41,10 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
4141
if (source instanceof Boolean) {
4242
return source;
4343
}
44-
return Boolean.valueOf(source.toString());
44+
if (source instanceof String) {
45+
return Boolean.valueOf((String) source);
46+
}
47+
return null;
4548
}
4649

4750
}

oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/ClaimAccessorTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,33 @@ public void getClaimAsStringWhenValueIsNullThenReturnNull() {
105105
assertThat(this.claimAccessor.getClaimAsString(claimName)).isNull();
106106
}
107107

108+
@Test
109+
public void getClaimAsBooleanWhenBooleanTypeThenReturnBoolean() {
110+
Boolean expectedClaimValue = Boolean.TRUE;
111+
String claimName = "boolean";
112+
this.claims.put(claimName, expectedClaimValue);
113+
assertThat(this.claimAccessor.getClaimAsBoolean(claimName)).isEqualTo(expectedClaimValue);
114+
}
115+
116+
@Test
117+
public void getClaimAsBooleanWhenStringTypeThenReturnBoolean() {
118+
Boolean expectedClaimValue = Boolean.TRUE;
119+
String claimName = "boolean";
120+
this.claims.put(claimName, expectedClaimValue.toString());
121+
assertThat(this.claimAccessor.getClaimAsBoolean(claimName)).isEqualTo(expectedClaimValue);
122+
}
123+
124+
// gh-10148
125+
@Test
126+
public void getClaimAsBooleanWhenNonBooleanTypeThenThrowIllegalArgumentException() {
127+
String claimName = "boolean";
128+
Map<Object, Object> claimValue = new HashMap<>();
129+
this.claims.put(claimName, claimValue);
130+
assertThatIllegalArgumentException().isThrownBy(() -> this.claimAccessor.getClaimAsBoolean(claimName))
131+
.withMessage("Unable to convert claim '" + claimName + "' of type '" + claimValue.getClass()
132+
+ "' to Boolean.");
133+
}
134+
108135
@Test
109136
public void getClaimAsMapWhenNotExistingThenReturnNull() {
110137
assertThat(this.claimAccessor.getClaimAsMap("map")).isNull();

oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/OAuth2TokenIntrospectionClaimAccessorTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.junit.jupiter.api.Test;
2929

3030
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
3132

3233
/**
3334
* Tests for {@link OAuth2TokenIntrospectionClaimAccessor}.
@@ -51,9 +52,9 @@ public void isActiveWhenActiveClaimNotExistingThenReturnFalse() {
5152
}
5253

5354
@Test
54-
public void isActiveWhenActiveClaimValueIsNullThenReturnFalse() {
55+
public void isActiveWhenActiveClaimValueIsNullThenThrowsNullPointerException() {
5556
this.claims.put(OAuth2TokenIntrospectionClaimNames.ACTIVE, null);
56-
assertThat(this.claimAccessor.isActive()).isFalse();
57+
assertThatNullPointerException().isThrownBy(this.claimAccessor::isActive);
5758
}
5859

5960
@Test

0 commit comments

Comments
 (0)