|
38 | 38 | import java.io.InputStream; |
39 | 39 | import java.text.ParseException; |
40 | 40 | import java.util.Base64; |
| 41 | +import java.util.Date; |
41 | 42 | import java.util.Map; |
42 | 43 | import java.util.concurrent.atomic.AtomicReference; |
43 | 44 |
|
@@ -1888,6 +1889,48 @@ private boolean tokenClaimsEqual(String token1, String token2) { |
1888 | 1889 | } |
1889 | 1890 | } |
1890 | 1891 |
|
| 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 | + |
1891 | 1934 | @Test |
1892 | 1935 | public void testShibbolethStaffMappedToStaffAndMembers() throws Exception { |
1893 | 1936 | context.turnOffAuthorisationSystem(); |
|
0 commit comments