Skip to content

Commit 2bd31c9

Browse files
committed
ClaimAccessor.getClaimAsInstant() converts Long or Date
Fixes gh-5191, Fixes gh-5192
1 parent d4e4598 commit 2bd31c9

File tree

5 files changed

+88
-7
lines changed

5 files changed

+88
-7
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.net.URL;
2222
import java.time.Instant;
2323
import java.util.ArrayList;
24+
import java.util.Date;
2425
import java.util.HashMap;
2526
import java.util.List;
2627
import java.util.Map;
@@ -81,11 +82,18 @@ default Instant getClaimAsInstant(String claim) {
8182
if (!this.containsClaim(claim)) {
8283
return null;
8384
}
84-
try {
85-
return Instant.ofEpochMilli(Long.valueOf(this.getClaimAsString(claim)));
86-
} catch (NumberFormatException ex) {
87-
throw new IllegalArgumentException("Unable to convert claim '" + claim + "' to Instant: " + ex.getMessage(), ex);
85+
Object claimValue = this.getClaims().get(claim);
86+
if (Long.class.isAssignableFrom(claimValue.getClass())) {
87+
return Instant.ofEpochSecond((Long) claimValue);
88+
}
89+
if (Date.class.isAssignableFrom(claimValue.getClass())) {
90+
return ((Date) claimValue).toInstant();
91+
}
92+
if (Instant.class.isAssignableFrom(claimValue.getClass())) {
93+
return (Instant) claimValue;
8894
}
95+
throw new IllegalArgumentException("Unable to convert claim '" + claim +
96+
"' of type '" + claimValue.getClass() + "' to Instant.");
8997
}
9098

9199
/**
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.security.oauth2.core;
17+
18+
import org.junit.Before;
19+
import org.junit.Test;
20+
21+
import java.time.Instant;
22+
import java.util.Date;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link ClaimAccessor}.
30+
*
31+
* @author Joe Grandja
32+
*/
33+
public class ClaimAccessorTests {
34+
private Map<String, Object> claims = new HashMap<>();
35+
private ClaimAccessor claimAccessor = (() -> this.claims);
36+
37+
@Before
38+
public void setup() {
39+
this.claims.clear();
40+
}
41+
42+
// gh-5192
43+
@Test
44+
public void getClaimAsInstantWhenDateTypeThenReturnInstant() {
45+
Instant expectedClaimValue = Instant.now();
46+
String claimName = "date";
47+
this.claims.put(claimName, Date.from(expectedClaimValue));
48+
49+
assertThat(this.claimAccessor.getClaimAsInstant(claimName)).isBetween(
50+
expectedClaimValue.minusSeconds(1), expectedClaimValue.plusSeconds(1));
51+
}
52+
53+
// gh-5191
54+
@Test
55+
public void getClaimAsInstantWhenLongTypeSecondsThenReturnInstant() {
56+
Instant expectedClaimValue = Instant.now();
57+
String claimName = "longSeconds";
58+
this.claims.put(claimName, expectedClaimValue.getEpochSecond());
59+
60+
assertThat(this.claimAccessor.getClaimAsInstant(claimName)).isBetween(
61+
expectedClaimValue.minusSeconds(1), expectedClaimValue.plusSeconds(1));
62+
}
63+
64+
@Test
65+
public void getClaimAsInstantWhenInstantTypeThenReturnInstant() {
66+
Instant expectedClaimValue = Instant.now();
67+
String claimName = "instant";
68+
this.claims.put(claimName, expectedClaimValue);
69+
70+
assertThat(this.claimAccessor.getClaimAsInstant(claimName)).isBetween(
71+
expectedClaimValue.minusSeconds(1), expectedClaimValue.plusSeconds(1));
72+
}
73+
}

oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/OidcIdTokenTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void constructorWhenParametersProvidedAndValidThenCreated() {
110110
assertThat(idToken.getAudience()).isEqualTo(AUD_VALUE);
111111
assertThat(idToken.getIssuedAt().toEpochMilli()).isEqualTo(IAT_VALUE);
112112
assertThat(idToken.getExpiresAt().toEpochMilli()).isEqualTo(EXP_VALUE);
113-
assertThat(idToken.getAuthenticatedAt().toEpochMilli()).isEqualTo(AUTH_TIME_VALUE);
113+
assertThat(idToken.getAuthenticatedAt().getEpochSecond()).isEqualTo(AUTH_TIME_VALUE);
114114
assertThat(idToken.getNonce()).isEqualTo(NONCE_VALUE);
115115
assertThat(idToken.getAuthenticationContextClass()).isEqualTo(ACR_VALUE);
116116
assertThat(idToken.getAuthenticationMethods()).isEqualTo(AMR_VALUE);

oauth2/oauth2-core/src/test/java/org/springframework/security/oauth2/core/oidc/OidcUserInfoTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,6 @@ public void constructorWhenParametersProvidedAndValidThenCreated() {
137137
assertThat(userInfo.getPhoneNumber()).isEqualTo(PHONE_NUMBER_VALUE);
138138
assertThat(userInfo.getPhoneNumberVerified()).isEqualTo(PHONE_NUMBER_VERIFIED_VALUE);
139139
assertThat(userInfo.getAddress()).isEqualTo(new DefaultAddressStandardClaim.Builder(ADDRESS_VALUE).build());
140-
assertThat(userInfo.getUpdatedAt().toEpochMilli()).isEqualTo(UPDATED_AT_VALUE);
140+
assertThat(userInfo.getUpdatedAt().getEpochSecond()).isEqualTo(UPDATED_AT_VALUE);
141141
}
142142
}

oauth2/oauth2-jose/src/test/java/org/springframework/security/oauth2/jwt/JwtTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void constructorWhenParametersProvidedAndValidThenCreated() {
106106
assertThat(jwt.getSubject()).isEqualTo(SUB_VALUE);
107107
assertThat(jwt.getAudience()).isEqualTo(AUD_VALUE);
108108
assertThat(jwt.getExpiresAt().toEpochMilli()).isEqualTo(EXP_VALUE);
109-
assertThat(jwt.getNotBefore().toEpochMilli()).isEqualTo(NBF_VALUE);
109+
assertThat(jwt.getNotBefore().getEpochSecond()).isEqualTo(NBF_VALUE);
110110
assertThat(jwt.getIssuedAt().toEpochMilli()).isEqualTo(IAT_VALUE);
111111
assertThat(jwt.getId()).isEqualTo(JTI_VALUE);
112112
}

0 commit comments

Comments
 (0)