Added JSON parsed instance of the refresh token to `JWTToken` class, the getter and methods to checks its presence and expiration

This commit is contained in:
Mauro Mugnaini 2024-06-05 10:31:55 +02:00
parent ccce7944ee
commit ee90e6e97f
Signed by: mauro.mugnaini
GPG Key ID: 2440CFD0EB321EA8
4 changed files with 33 additions and 1 deletions

View File

@ -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)

View File

@ -13,7 +13,7 @@
<groupId>org.gcube.common</groupId>
<artifactId>oidc-library</artifactId>
<version>1.3.2</version>
<version>1.3.3-SNAPSHOT</version>
<dependencyManagement>
<dependencies>

View File

@ -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<String> getAud() {
List<String> audienceStrings = new ArrayList<>();
Object audience = getPayload().get("aud");

View File

@ -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());
}
}