From ee90e6e97f3d45f1b20c3078fd964e4c5fd3cd5a Mon Sep 17 00:00:00 2001 From: Mauro Mugnaini Date: Wed, 5 Jun 2024 10:31:55 +0200 Subject: [PATCH] Added JSON parsed instance of the refresh token to `JWTToken` class, the getter and methods to checks its presence and expiration --- CHANGELOG.md | 3 +++ pom.xml | 2 +- .../java/org/gcube/oidc/rest/JWTToken.java | 26 +++++++++++++++++++ .../org/gcube/oidc/rest/JWTTokenTest.java | 3 +++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2029bd3..409fc7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm # Changelog for "oidc-library" +## [v1.3.3-SNAPSHOT] +Added JSON parsed instance of the refresh token to `JWTToken` class, the getter and methods to checks its presence and expiration + ## [v1.3.2] - Header X-D4Science-Context in query exchange and refresh - Token exchange (#27099) diff --git a/pom.xml b/pom.xml index 39006a8..57bdb69 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ org.gcube.common oidc-library - 1.3.2 + 1.3.3-SNAPSHOT diff --git a/src/main/java/org/gcube/oidc/rest/JWTToken.java b/src/main/java/org/gcube/oidc/rest/JWTToken.java index c304d42..ab192bb 100644 --- a/src/main/java/org/gcube/oidc/rest/JWTToken.java +++ b/src/main/java/org/gcube/oidc/rest/JWTToken.java @@ -28,6 +28,7 @@ public class JWTToken implements Serializable { private String raw; private JSONObject tokens; private JSONObject payload; + private JSONObject refreshToken; public static JWTToken fromString(String tokenString) { if (tokenString == null) { @@ -50,6 +51,11 @@ public class JWTToken implements Serializable { tokens = (JSONObject) new JSONParser().parse(this.raw); String[] parts = getAccessTokenString().split("\\."); payload = (JSONObject) new JSONParser().parse(new String(Base64.getDecoder().decode(parts[1]))); + String refreshTokenString = getRefreshTokenString(); + if (refreshTokenString != null) { + refreshToken = (JSONObject) new JSONParser() + .parse(new String(Base64.getDecoder().decode(refreshTokenString.split("\\.")[1]))); + } } public String getRaw() { @@ -81,6 +87,14 @@ public class JWTToken implements Serializable { return payload; } + public boolean hasRefreshToken() { + return refreshToken != null; + } + + public JSONObject getRefreshToken() { + return refreshToken; + } + public String getAzp() { return (String) getPayload().get("azp"); } @@ -89,10 +103,18 @@ public class JWTToken implements Serializable { return (Long) getPayload().get("exp"); } + public Long getRefreshTokenExp() { + return hasRefreshToken() ? (Long) getRefreshToken().get("exp") : 0; + } + public Date getExpAsDate() { return new Date(getExp() * 1000); } + public Date getRefreshTokenExpAsDate() { + return new Date(getRefreshTokenExp() * 1000); + } + public Calendar getExpAsCalendar() { Calendar cal = Calendar.getInstance(); cal.setTime(getExpAsDate()); @@ -103,6 +125,10 @@ public class JWTToken implements Serializable { return new Date().after(getExpAsDate()); } + public boolean isRefreshTokenExpired() { + return new Date().after(getRefreshTokenExpAsDate()); + } + public List getAud() { List audienceStrings = new ArrayList<>(); Object audience = getPayload().get("aud"); diff --git a/src/test/java/org/gcube/oidc/rest/JWTTokenTest.java b/src/test/java/org/gcube/oidc/rest/JWTTokenTest.java index 67b5b82..1a2ac96 100644 --- a/src/test/java/org/gcube/oidc/rest/JWTTokenTest.java +++ b/src/test/java/org/gcube/oidc/rest/JWTTokenTest.java @@ -18,6 +18,9 @@ public class JWTTokenTest { Assert.assertNotNull(token.getAccessTokenString()); Assert.assertNotNull(token.getSub()); Assert.assertNotNull(token.getExp()); + Assert.assertTrue(token.isExpired()); + Assert.assertNotNull(token.getRefreshToken()); + Assert.assertTrue(token.isRefreshTokenExpired()); } }