From 6a401f73236a70f702b64646d8bdec7c5a90e15d Mon Sep 17 00:00:00 2001 From: Zipeng WU Date: Tue, 5 Jul 2022 10:00:38 +0200 Subject: [PATCH] SONAR-16567 Notify the token creator about expiring tokens via email --- .../java/org/sonar/db/user/UserTokenDao.java | 12 ++ .../org/sonar/db/user/UserTokenMapper.java | 3 + .../org/sonar/db/user/UserTokenMapper.xml | 8 ++ .../org/sonar/db/user/UserTokenDaoTest.java | 33 ++++- .../org/sonar/server/email/BasicEmail.java | 37 +++++ .../org/sonar/server/email/EmailSender.java | 110 ++++++++++++++ .../org/sonar/server/email/package-info.java | 24 ++++ .../sonar/server/email/EmailSenderTest.java | 134 ++++++++++++++++++ .../server/usertoken/UserTokenModule.java | 10 ++ .../notification/TokenExpirationEmail.java | 37 +++++ .../TokenExpirationEmailComposer.java | 72 ++++++++++ ...ExpirationNotificationExecutorService.java | 27 ++++ ...rationNotificationExecutorServiceImpl.java | 37 +++++ ...okenExpirationNotificationInitializer.java | 38 +++++ .../TokenExpirationNotificationScheduler.java | 27 ++++ ...enExpirationNotificationSchedulerImpl.java | 70 +++++++++ .../TokenExpirationNotificationSender.java | 56 ++++++++ .../UserTokenAuthenticationTest.java | 4 +- .../server/usertoken/UserTokenModuleTest.java | 2 +- .../TokenExpirationEmailComposerTest.java | 104 ++++++++++++++ ...onNotificationExecutorServiceImplTest.java | 35 +++++ ...ExpirationNotificationInitializerTest.java | 46 ++++++ ...pirationNotificationSchedulerImplTest.java | 75 ++++++++++ ...TokenExpirationNotificationSenderTest.java | 87 ++++++++++++ .../server/usertoken/ws/GenerateAction.java | 4 +- .../usertoken/ws/GenerateActionTest.java | 3 +- .../usertoken/ws/UserTokenWsModuleTest.java | 2 +- 27 files changed, 1089 insertions(+), 8 deletions(-) create mode 100644 server/sonar-server-common/src/main/java/org/sonar/server/email/BasicEmail.java create mode 100644 server/sonar-server-common/src/main/java/org/sonar/server/email/EmailSender.java create mode 100644 server/sonar-server-common/src/main/java/org/sonar/server/email/package-info.java create mode 100644 server/sonar-server-common/src/test/java/org/sonar/server/email/EmailSenderTest.java create mode 100644 server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/notification/TokenExpirationEmail.java create mode 100644 server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/notification/TokenExpirationEmailComposer.java create mode 100644 server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/notification/TokenExpirationNotificationExecutorService.java create mode 100644 server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/notification/TokenExpirationNotificationExecutorServiceImpl.java create mode 100644 server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/notification/TokenExpirationNotificationInitializer.java create mode 100644 server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/notification/TokenExpirationNotificationScheduler.java create mode 100644 server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/notification/TokenExpirationNotificationSchedulerImpl.java create mode 100644 server/sonar-webserver-auth/src/main/java/org/sonar/server/usertoken/notification/TokenExpirationNotificationSender.java create mode 100644 server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/notification/TokenExpirationEmailComposerTest.java create mode 100644 server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/notification/TokenExpirationNotificationExecutorServiceImplTest.java create mode 100644 server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/notification/TokenExpirationNotificationInitializerTest.java create mode 100644 server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/notification/TokenExpirationNotificationSchedulerImplTest.java create mode 100644 server/sonar-webserver-auth/src/test/java/org/sonar/server/usertoken/notification/TokenExpirationNotificationSenderTest.java diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDao.java index e1191ee266c..581db16e863 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenDao.java @@ -19,6 +19,8 @@ */ package org.sonar.db.user; +import java.time.LocalDate; +import java.time.ZoneOffset; import java.util.Collection; import java.util.HashMap; import java.util.List; @@ -54,6 +56,16 @@ public class UserTokenDao implements Dao { auditPersister.updateUserToken(dbSession, new UserTokenNewValue(userTokenDto, userLogin)); } + public List selectTokensExpiredInDays(DbSession dbSession, long days){ + long timestamp = LocalDate.now().plusDays(days).atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli(); + return mapper(dbSession).selectTokensExpiredOnDate(timestamp); + } + + public List selectExpiredTokens(DbSession dbSession){ + long timestamp = LocalDate.now().atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli(); + return mapper(dbSession).selectTokensExpiredOnDate(timestamp); + } + public void updateWithoutAudit(DbSession dbSession, UserTokenDto userTokenDto) { mapper(dbSession).update(userTokenDto); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenMapper.java index 435d445c35d..173085903e9 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserTokenMapper.java @@ -41,4 +41,7 @@ public interface UserTokenMapper { int deleteByProjectKey(@Param("projectKey") String projectKey); List countTokensByUserUuids(@Param("userUuids") List userUuids); + + List selectTokensExpiredOnDate(@Param("timestamp") long timestamp); + } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserTokenMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserTokenMapper.xml index f99bfb6c5c7..3e52e4907e2 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserTokenMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserTokenMapper.xml @@ -55,6 +55,14 @@ WHERE t.token_hash=#{tokenHash, jdbcType=VARCHAR} + +