diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java index 2141e60..f4ae1b0 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java +++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MockController.java @@ -14,6 +14,7 @@ import ovh.herisson.Clyde.Tables.User; import java.util.ArrayList; import java.util.Arrays; +import java.util.Calendar; import java.util.Date; @RestController @@ -53,7 +54,16 @@ public class MockController { userRepo.saveAll(mockUsers); for (User user: mockUsers){ - tokenService.saveToken(new Token(user,user.getPassword()), null); + //Petit test pour ĂȘtre sur de delete les bons tokens + if (user.getLastName().equals("brine")){ + Calendar c = Calendar.getInstance(); + c.setTime(new Date()); + c.add(Calendar.DAY_OF_WEEK, 1); + tokenService.saveToken(new Token(user,user.getPassword(), c.getTime())); + }else{ + tokenService.saveToken(new Token(user,user.getPassword(), new Date())); + } + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java index 0ad8941..4cd1939 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/AuthenticatorService.java @@ -27,7 +27,7 @@ public class AuthenticatorService { if (user == null){return null;} if (!userService.checkPassword(user,password)){return null;} String token = tokenService.generateNewToken(); - tokenService.saveToken(new Token(user, token),expirationDate); + tokenService.saveToken(new Token(user, token,expirationDate)); return token; } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/Services/TokenService.java b/backend/src/main/java/ovh/herisson/Clyde/Services/TokenService.java index f497bc2..2e09bcc 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Services/TokenService.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Services/TokenService.java @@ -1,19 +1,15 @@ package ovh.herisson.Clyde.Services; -import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import ovh.herisson.Clyde.Repositories.TokenRepository; -import ovh.herisson.Clyde.Tables.Role; import ovh.herisson.Clyde.Tables.Token; import ovh.herisson.Clyde.Tables.User; -import ovh.herisson.Clyde.TokenCleaner; import java.nio.charset.StandardCharsets; import java.security.SecureRandom; +import java.util.Calendar; import java.util.Date; -import java.util.Timer; -import java.util.TimerTask; @Service public class TokenService { @@ -37,9 +33,22 @@ public class TokenService { return tokenRepo.getByToken(token).getUser(); } - public void saveToken(Token token, Date expirationDate){ + public void saveToken(Token token){ tokenRepo.save(token); - TokenCleaner cleaner = new TokenCleaner(token, tokenRepo); } + //Tous les jours a minuit + @Scheduled(cron = "0 0 0 * * ?") + public void autoDeleteToken() { + for (Token t: tokenRepo.findAll()){ + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date()); + + Calendar cal2 = Calendar.getInstance(); + cal2.setTime(t.getExpirationDate()); + if (cal.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)){ + tokenRepo.delete(t); + } + } + }; } \ No newline at end of file diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Token.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Token.java index 20d6bf9..00d7ea0 100644 --- a/backend/src/main/java/ovh/herisson/Clyde/Tables/Token.java +++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Token.java @@ -4,6 +4,8 @@ import jakarta.persistence.*; import org.springframework.scheduling.annotation.Scheduled; import ovh.herisson.Clyde.Repositories.TokenRepository; +import java.util.Date; + @Entity public class Token { @GeneratedValue(strategy = GenerationType.AUTO) @@ -14,10 +16,12 @@ public class Token { @JoinColumn(name ="Users") private User user; private String token; + private Date expirationDate; - public Token(User user, String token){ + public Token(User user, String token, Date expirationDate){ this.user = user; this.token = token; + this.expirationDate = expirationDate; } public Token(){} @@ -40,4 +44,12 @@ public class Token { public void setToken(String data) { this.token = data; } + + public void setExpirationDate(Date date){ + this.expirationDate = date; + } + + public Date getExpirationDate(){ + return expirationDate; + } } diff --git a/backend/src/main/java/ovh/herisson/Clyde/TokenCleaner.java b/backend/src/main/java/ovh/herisson/Clyde/TokenCleaner.java deleted file mode 100644 index 1102741..0000000 --- a/backend/src/main/java/ovh/herisson/Clyde/TokenCleaner.java +++ /dev/null @@ -1,30 +0,0 @@ -package ovh.herisson.Clyde; - -import org.springframework.scheduling.annotation.Scheduled; -import ovh.herisson.Clyde.Repositories.TokenRepository; -import ovh.herisson.Clyde.Tables.Role; -import ovh.herisson.Clyde.Tables.Token; -import ovh.herisson.Clyde.Tables.User; - -import java.util.Date; -import java.util.Timer; -import java.util.TimerTask; - -public class TokenCleaner { - private Token token; - private TokenRepository tokenRepo; - - public TokenCleaner(Token token, TokenRepository tokenRepo){ - this.token = token; - this.tokenRepo = tokenRepo; - Timer timer = new Timer(); - timer.schedule(autoDeleteTokenTask, 30000); - } - - TimerTask autoDeleteTokenTask = new TimerTask() { - public void run() { - tokenRepo.delete(token); - } - }; - -}