aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2023-01-16 09:21:49 +0300
committerGravatar Vitaly Takmazov2023-01-16 09:27:20 +0300
commitb836c344d49a8d3ad8f7d164bf0425b59df51384 (patch)
treeaa262c5f85cc4edb1c95295e14e1c40d201d9ff1 /src/main/java/com/juick
parent0c30281a5a0745927b0e1fd39e4cfaefe9021920 (diff)
email: cleanup expired auth codes
Diffstat (limited to 'src/main/java/com/juick')
-rw-r--r--src/main/java/com/juick/EmailManager.java8
-rw-r--r--src/main/java/com/juick/service/EmailService.java3
-rw-r--r--src/main/java/com/juick/service/EmailServiceImpl.java11
3 files changed, 20 insertions, 2 deletions
diff --git a/src/main/java/com/juick/EmailManager.java b/src/main/java/com/juick/EmailManager.java
index 62584273..6180668e 100644
--- a/src/main/java/com/juick/EmailManager.java
+++ b/src/main/java/com/juick/EmailManager.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2008-2020, Juick
+ * Copyright (C) 2008-2023, Juick
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
@@ -45,6 +45,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.Scheduled;
import javax.annotation.Nonnull;
import javax.inject.Inject;
@@ -196,4 +197,9 @@ public class EmailManager implements NotificationListener {
return false;
}
}
+ @Scheduled(fixedRate = 3600000)
+ public void cleanupAuth() {
+ var rowsDeleted = emailService.cleanupAuthCodes();
+ logger.debug("{} auth codes deleted", rowsDeleted);
+ }
}
diff --git a/src/main/java/com/juick/service/EmailService.java b/src/main/java/com/juick/service/EmailService.java
index bdd7d405..614cf412 100644
--- a/src/main/java/com/juick/service/EmailService.java
+++ b/src/main/java/com/juick/service/EmailService.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2008-2020, Juick
+ * Copyright (C) 2008-2023, Juick
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
@@ -38,4 +38,5 @@ public interface EmailService {
List<String> getEmails(Integer userId, boolean active);
String getEmailByAuthCode(String code);
void deleteAuthCode(String code);
+ Integer cleanupAuthCodes();
}
diff --git a/src/main/java/com/juick/service/EmailServiceImpl.java b/src/main/java/com/juick/service/EmailServiceImpl.java
index 977d9a75..7f358533 100644
--- a/src/main/java/com/juick/service/EmailServiceImpl.java
+++ b/src/main/java/com/juick/service/EmailServiceImpl.java
@@ -24,6 +24,10 @@ import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
+import java.sql.Types;
+import java.time.Instant;
+import java.time.ZoneOffset;
+import java.time.temporal.ChronoUnit;
import java.util.List;
/**
@@ -110,4 +114,11 @@ public class EmailServiceImpl extends BaseJdbcService implements EmailService {
getJdbcTemplate().update("DELETE FROM auth WHERE authcode=?", code);
}
+ @Override
+ public Integer cleanupAuthCodes() {
+ Instant day = Instant.now().minus(1, ChronoUnit.DAYS);
+ return getNamedParameterJdbcTemplate().update("DELETE FROM auth WHERE ts < :day",
+ new MapSqlParameterSource()
+ .addValue("day", day.atOffset(ZoneOffset.UTC), Types.TIMESTAMP_WITH_TIMEZONE));
+ }
}