Skip to content

Commit 776ddcc

Browse files
vins01-4scienceMicheleboychuk
authored andcommitted
Merged in task/dspace-cris-2025_02_x/DSC-2791 (pull request DSpace#5579)
DSC-2791 Approved-by: Mykhaylo Boychuk
2 parents 24aa173 + 9b32e3f commit 776ddcc

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

dspace-server-webapp/src/main/java/org/dspace/app/rest/security/jwt/JWTTokenHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ private JWTClaimsSet buildJwtClaimsSet(Context context, HttpServletRequest reque
390390

391391
return builder
392392
.expirationTime(java.util.Date.from(
393-
Instant.ofEpochMilli(Instant.now().toEpochMilli() + getExpirationPeriod())))
393+
Instant.ofEpochMilli(Instant.now().toEpochMilli() + expirationPeriod)))
394394
.build();
395395
}
396396

dspace-server-webapp/src/test/java/org/dspace/app/rest/AuthenticationRestControllerIT.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.io.InputStream;
3939
import java.text.ParseException;
4040
import java.util.Base64;
41+
import java.util.Date;
4142
import java.util.Map;
4243
import java.util.concurrent.atomic.AtomicReference;
4344

@@ -1888,6 +1889,48 @@ private boolean tokenClaimsEqual(String token1, String token2) {
18881889
}
18891890
}
18901891

1892+
private Date getTokenExpiration(String token) throws ParseException {
1893+
SignedJWT signedJWT = SignedJWT.parse(token);
1894+
return signedJWT.getJWTClaimsSet().getExpirationTime();
1895+
}
1896+
1897+
@Test
1898+
public void testMachineTokenExpirationUsesCorrectPeriod() throws Exception {
1899+
configurationService.setProperty("jwt.login.machine-token.expiration", "7200000");
1900+
configurationService.setProperty("jwt.login.token.expiration", "1800000");
1901+
1902+
context.turnOffAuthorisationSystem();
1903+
EPerson user = EPersonBuilder.createEPerson(context)
1904+
.withCanLogin(true)
1905+
.withPassword(password)
1906+
.withEmail("machine-token-exp-test@test.com")
1907+
.build();
1908+
context.restoreAuthSystemState();
1909+
1910+
String loginToken = getAuthToken(user.getEmail(), password);
1911+
1912+
AtomicReference<String> machineToken = new AtomicReference<>();
1913+
getClient(loginToken).perform(post("/api/authn/machinetokens"))
1914+
.andExpect(status().isOk())
1915+
.andExpect(jsonPath("$.token", notNullValue()))
1916+
.andExpect(jsonPath("$.type", is("machinetoken")))
1917+
.andDo(result -> machineToken.set(
1918+
read(result.getResponse().getContentAsString(), "$.token")));
1919+
1920+
Date loginExpiration = getTokenExpiration(loginToken);
1921+
Date machineExpiration = getTokenExpiration(machineToken.get());
1922+
1923+
assertTrue("Machine token should expire after login token",
1924+
machineExpiration.after(loginExpiration));
1925+
1926+
long diffMillis = machineExpiration.getTime() - loginExpiration.getTime();
1927+
assertTrue("Difference should be ~1.5 hours (5400000ms), was: " + diffMillis,
1928+
Math.abs(diffMillis - 5400000) < 60000);
1929+
1930+
getClient(loginToken).perform(post("/api/authn/logout"))
1931+
.andExpect(status().isNoContent());
1932+
}
1933+
18911934
@Test
18921935
public void testShibbolethStaffMappedToStaffAndMembers() throws Exception {
18931936
context.turnOffAuthorisationSystem();

0 commit comments

Comments
 (0)