Skip to content

Commit

Permalink
Previously, ClaimTypeConverter doesn't overwrite claim's value converted
Browse files Browse the repository at this point in the history
to null.

This commit allows ClaimTypeConverter to overwrite and return claim with
value converted to null.

Closes spring-projects#10135
  • Loading branch information
Fabio Guenci committed Jul 27, 2021
1 parent 6370906 commit cc4e376
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ public Map<String, Object> convert(Map<String, Object> claims) {
this.claimTypeConverters.forEach((claimName, typeConverter) -> {
if (claims.containsKey(claimName)) {
Object claim = claims.get(claimName);
Object mappedClaim = typeConverter.convert(claim);
if (mappedClaim != null) {
result.put(claimName, mappedClaim);
}
result.put(claimName, typeConverter.convert(claim));
}
});
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public class ClaimTypeConverterTests {

private static final String JSON_OBJECT_CLAIM = "json-object-claim";

private static final String NULL_OBJECT_CLAIM = "null-object-claim";

private ClaimTypeConverter claimTypeConverter;

@BeforeEach
Expand All @@ -77,6 +79,7 @@ public void setup() {
TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(String.class)));
Converter<Object, ?> mapStringObjectConverter = getConverter(TypeDescriptor.map(Map.class,
TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Object.class)));
Converter<Object, ?> nullConverter = (value) -> null;
Map<String, Converter<Object, ?>> claimTypeConverters = new HashMap<>();
claimTypeConverters.put(STRING_CLAIM, stringConverter);
claimTypeConverters.put(BOOLEAN_CLAIM, booleanConverter);
Expand All @@ -85,6 +88,7 @@ public void setup() {
claimTypeConverters.put(COLLECTION_STRING_CLAIM, collectionStringConverter);
claimTypeConverters.put(LIST_STRING_CLAIM, listStringConverter);
claimTypeConverters.put(MAP_STRING_OBJECT_CLAIM, mapStringObjectConverter);
claimTypeConverters.put(NULL_OBJECT_CLAIM, nullConverter);
this.claimTypeConverter = new ClaimTypeConverter(claimTypeConverters);
}

Expand Down Expand Up @@ -138,6 +142,7 @@ public void convertWhenAllClaimsRequireConversionThenConvertAll() throws Excepti
claims.put(MAP_STRING_OBJECT_CLAIM, mapIntegerObject);
claims.put(JSON_ARRAY_CLAIM, jsonArray);
claims.put(JSON_OBJECT_CLAIM, jsonObject);
claims.put(NULL_OBJECT_CLAIM, instant.toString());
claims = this.claimTypeConverter.convert(claims);
assertThat(claims.get(STRING_CLAIM)).isEqualTo("true");
assertThat(claims.get(BOOLEAN_CLAIM)).isEqualTo(Boolean.TRUE);
Expand All @@ -148,6 +153,7 @@ public void convertWhenAllClaimsRequireConversionThenConvertAll() throws Excepti
assertThat(claims.get(MAP_STRING_OBJECT_CLAIM)).isEqualTo(mapStringObject);
assertThat(claims.get(JSON_ARRAY_CLAIM)).isEqualTo(jsonArrayListString);
assertThat(claims.get(JSON_OBJECT_CLAIM)).isEqualTo(jsonObjectMap);
assertThat(claims.get(NULL_OBJECT_CLAIM)).isNull();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ public void convertWhenUsingCustomConverterThenAllOtherDefaultsAreStillUsed() {

@Test
public void convertWhenConverterReturnsNullThenClaimIsRemoved() {
MappedJwtClaimSetConverter converter = MappedJwtClaimSetConverter
.withDefaults(Collections.singletonMap(JwtClaimNames.NBF, (nbfClaimValue) -> null));
Map<String, Object> source = Collections.singletonMap(JwtClaimNames.NBF, Instant.now());
Map<String, Object> target = converter.convert(source);
assertThat(target).doesNotContainKey(JwtClaimNames.NBF);
}

@Test
public void convertWhenClaimValueIsNullThenClaimIsRemoved() {
MappedJwtClaimSetConverter converter = MappedJwtClaimSetConverter.withDefaults(Collections.emptyMap());
Map<String, Object> source = Collections.singletonMap(JwtClaimNames.ISS, null);
Map<String, Object> target = converter.convert(source);
Expand Down

0 comments on commit cc4e376

Please sign in to comment.