Skip to content

Commit fff64db

Browse files
committed
Improve ClaimAccessor getClaimAsInstant
Fixes gh-5250
1 parent 2356749 commit fff64db

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ default Instant getClaimAsInstant(String claim) {
8383
return null;
8484
}
8585
Object claimValue = this.getClaims().get(claim);
86-
if (Long.class.isAssignableFrom(claimValue.getClass())) {
87-
return Instant.ofEpochSecond((Long) claimValue);
86+
if (Long.class.isAssignableFrom(claimValue.getClass()) ||
87+
Integer.class.isAssignableFrom(claimValue.getClass()) ||
88+
Double.class.isAssignableFrom(claimValue.getClass())) {
89+
return Instant.ofEpochSecond(((Number) claimValue).longValue());
8890
}
8991
if (Date.class.isAssignableFrom(claimValue.getClass())) {
9092
return ((Date) claimValue).toInstant();

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

+22
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,26 @@ public void getClaimAsInstantWhenInstantTypeThenReturnInstant() {
7070
assertThat(this.claimAccessor.getClaimAsInstant(claimName)).isBetween(
7171
expectedClaimValue.minusSeconds(1), expectedClaimValue.plusSeconds(1));
7272
}
73+
74+
// gh-5250
75+
@Test
76+
public void getClaimAsInstantWhenIntegerTypeSecondsThenReturnInstant() {
77+
Instant expectedClaimValue = Instant.now();
78+
String claimName = "integerSeconds";
79+
this.claims.put(claimName, Long.valueOf(expectedClaimValue.getEpochSecond()).intValue());
80+
81+
assertThat(this.claimAccessor.getClaimAsInstant(claimName)).isBetween(
82+
expectedClaimValue.minusSeconds(1), expectedClaimValue.plusSeconds(1));
83+
}
84+
85+
// gh-5250
86+
@Test
87+
public void getClaimAsInstantWhenDoubleTypeSecondsThenReturnInstant() {
88+
Instant expectedClaimValue = Instant.now();
89+
String claimName = "doubleSeconds";
90+
this.claims.put(claimName, Long.valueOf(expectedClaimValue.getEpochSecond()).doubleValue());
91+
92+
assertThat(this.claimAccessor.getClaimAsInstant(claimName)).isBetween(
93+
expectedClaimValue.minusSeconds(1), expectedClaimValue.plusSeconds(1));
94+
}
7395
}

0 commit comments

Comments
 (0)