Skip to content

Commit 1bbae83

Browse files
committed
ClaimAccessor.getClaimAsString() checks null claim value
Fixes gh-5608
1 parent de640a1 commit 1bbae83

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -53,13 +53,17 @@ default Boolean containsClaim(String claim) {
5353
}
5454

5555
/**
56-
* Returns the claim value as a {@code String} or {@code null} if it does not exist.
56+
* Returns the claim value as a {@code String} or {@code null} if it does not exist or is equal to {@code null}.
5757
*
5858
* @param claim the name of the claim
59-
* @return the claim value or {@code null} if it does not exist
59+
* @return the claim value or {@code null} if it does not exist or is equal to {@code null}
6060
*/
6161
default String getClaimAsString(String claim) {
62-
return (this.containsClaim(claim) ? this.getClaims().get(claim).toString() : null);
62+
if (!this.containsClaim(claim)) {
63+
return null;
64+
}
65+
Object claimValue = this.getClaims().get(claim);
66+
return (claimValue != null ? claimValue.toString() : null);
6367
}
6468

6569
/**

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,13 @@ public void getClaimAsInstantWhenInstantTypeThenReturnInstant() {
7070
assertThat(this.claimAccessor.getClaimAsInstant(claimName)).isBetween(
7171
expectedClaimValue.minusSeconds(1), expectedClaimValue.plusSeconds(1));
7272
}
73+
74+
// gh-5608
75+
@Test
76+
public void getClaimAsStringWhenValueIsNullThenReturnNull() {
77+
String claimName = "claim-with-null-value";
78+
this.claims.put(claimName, null);
79+
80+
assertThat(this.claimAccessor.getClaimAsString(claimName)).isEqualTo(null);
81+
}
7382
}

0 commit comments

Comments
 (0)