Skip to content

Commit 89c50c2

Browse files
committed
Fixed a ClassCastException happening while parsing access tokens with Gson
1 parent e7f51e0 commit 89c50c2

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

library/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.mastercard.developer</groupId>
88
<artifactId>oauth2-client-java-root</artifactId>
9-
<version>1.1.0-SNAPSHOT</version>
9+
<version>1.0.2</version>
1010
</parent>
1111
<artifactId>oauth2-client-java</artifactId>
1212
<packaging>jar</packaging>

library/src/main/java/com/mastercard/developer/oauth2/core/OAuth2Handler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,11 @@ public static AccessTokenResponse parseAccessTokenJson(String accessTokenRespons
323323
if (null == tokenValue) {
324324
throw new OAuth2ClientException("Missing value in access token response: access_token");
325325
}
326-
var expiresInSeconds = (Integer) jsonMap.get("expires_in");
326+
var expiresInSeconds = (Number) jsonMap.get("expires_in");
327327
if (null == expiresInSeconds) {
328328
throw new OAuth2ClientException("Missing value in access token response: expires_in");
329329
}
330-
Instant expiry = Instant.now().plusSeconds(expiresInSeconds);
330+
Instant expiry = Instant.now().plusSeconds(expiresInSeconds.intValue());
331331
String scopeNode = (String) jsonMap.get("scope");
332332
Set<String> scopes = (scopeNode == null) ? Set.of() : Set.of(scopeNode.trim().split("\\s+"));
333333
return new AccessTokenResponse(tokenValue, scopes, expiry);

library/src/test/java/com/mastercard/developer/oauth2/internal/json/JsonProvidersTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,26 @@ void parse_ShouldParseAccessTokenResponse(JsonProvider provider) throws Exceptio
4444
// THEN
4545
assertEquals("eyJ4NXQjUzI1NiI6Ii...oPIq4PZf2WaMxLow", map.get("access_token"));
4646
assertEquals("DPoP", map.get("token_type"));
47-
assertEquals(900.0, ((Number) map.get("expires_in")).doubleValue());
47+
assertEquals(900, ((Number) map.get("expires_in")).intValue());
4848
assertEquals("service:scope1 service:scope2", map.get("scope"));
4949
}
5050

51+
@ParameterizedTest
52+
@MethodSource("jsonProviders")
53+
void parse_ShouldParseExpireInDecimalValues(JsonProvider provider) throws Exception {
54+
// GIVEN
55+
var json = """
56+
{
57+
"expires_in": 900.0
58+
}""";
59+
60+
// WHEN
61+
Map<String, Object> map = provider.parse(json);
62+
63+
// THEN
64+
assertEquals(900, ((Number) map.get("expires_in")).intValue());
65+
}
66+
5167
@ParameterizedTest
5268
@MethodSource("jsonProviders")
5369
void parse_ShouldParseJwk(JsonProvider provider) throws Exception {

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<modelVersion>4.0.0</modelVersion>
77
<groupId>com.mastercard.developer</groupId>
88
<artifactId>oauth2-client-java-root</artifactId>
9-
<version>1.1.0-SNAPSHOT</version>
9+
<version>1.0.2</version>
1010
<packaging>pom</packaging>
1111

1212
<properties>

test-clients/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.mastercard.developer</groupId>
88
<artifactId>oauth2-client-java-root</artifactId>
9-
<version>1.1.0-SNAPSHOT</version>
9+
<version>1.0.2</version>
1010
</parent>
1111
<artifactId>oauth2-client-java-test-clients</artifactId>
1212
<packaging>jar</packaging>

0 commit comments

Comments
 (0)