diff options
author | Matteo Mara <matteo.mara@sonarsource.com> | 2022-06-30 10:44:29 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-07-01 20:03:06 +0000 |
commit | 5159eb8d7cda29c357aa06868a595d07fbc6f633 (patch) | |
tree | 7caf459539e67ed23199256d6df9ba18911ee069 /server/sonar-db-dao/src | |
parent | 05ebcc134533b03a324a2710c3fe6d711850a5c4 (diff) | |
download | sonarqube-5159eb8d7cda29c357aa06868a595d07fbc6f633.tar.gz sonarqube-5159eb8d7cda29c357aa06868a595d07fbc6f633.zip |
SONAR-16565 handle token expiration at the authentication level
Diffstat (limited to 'server/sonar-db-dao/src')
-rw-r--r-- | server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDto.java | 4 | ||||
-rw-r--r-- | server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDtoTest.java | 15 |
2 files changed, 19 insertions, 0 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDto.java index bcd9572efee..d749aa7b184 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDto.java @@ -148,4 +148,8 @@ public class UserTokenDto { this.projectUuid = projectUuid; return this; } + + public boolean isExpired() { + return (this.expirationDate != null && this.getExpirationDate() < System.currentTimeMillis()); + } } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDtoTest.java index 6da9f5b6d02..eca56374bd5 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserTokenDtoTest.java @@ -19,9 +19,12 @@ */ package org.sonar.db.user; +import java.time.ZoneId; +import java.time.ZonedDateTime; import org.junit.Test; import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; public class UserTokenDtoTest { @@ -32,4 +35,16 @@ public class UserTokenDtoTest { .isInstanceOf(IllegalStateException.class) .hasMessage("Token hash length (256) is longer than the maximum authorized (255)"); } + + @Test + public void token_isExpired_is_properly_calculated() { + UserTokenDto tokenWithNoExpirationDate = new UserTokenDto(); + UserTokenDto expiredToken = new UserTokenDto().setExpirationDate(0L); + UserTokenDto nonExpiredToken = new UserTokenDto().setExpirationDate(ZonedDateTime.now(ZoneId.systemDefault()).plusDays(10).toInstant().toEpochMilli()); + + assertThat(tokenWithNoExpirationDate.isExpired()).isFalse(); + assertThat(expiredToken.isExpired()).isTrue(); + assertThat(nonExpiredToken.isExpired()).isFalse(); + } + } |