- L'email passe en unique dans la table User

- utilisation de la méthode saveToken dans les mock
- changement de saveToken pour recevoir un token en paramètre
- crée la classe TokenCleaner qui gère le nettoyage des token (en faire une classe permet d'en avoir plusieurs instances qui run des timer en parallèle)
- première implémentation du timeout (need plus de tests)
This commit is contained in:
LeoMoulin 2024-03-08 10:20:18 +01:00
parent 857191f9e5
commit 206c5e958b
7 changed files with 53 additions and 6 deletions

View File

@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import ovh.herisson.Clyde.Repositories.TokenRepository;
import ovh.herisson.Clyde.Repositories.UserRepository;
import ovh.herisson.Clyde.Services.TokenService;
import ovh.herisson.Clyde.Tables.Role;
import ovh.herisson.Clyde.Tables.Token;
import ovh.herisson.Clyde.Tables.User;
@ -23,13 +24,14 @@ public class MockController {
public final UserRepository userRepo;
public final TokenRepository tokenRepo;
public final TokenService tokenService;
ArrayList<User> mockUsers;
public MockController(UserRepository userRepo, TokenRepository tokenRepo){
public MockController(UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService){
this.tokenRepo = tokenRepo;
this.userRepo = userRepo;
this.tokenService = tokenService;
}
/** Saves an example of each user type by :
@ -51,7 +53,7 @@ public class MockController {
userRepo.saveAll(mockUsers);
for (User user: mockUsers){
tokenRepo.save(new Token(user,user.getPassword()));
tokenService.saveToken(new Token(user,user.getPassword()), null);
}
}

View File

@ -6,8 +6,10 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.scheduling.annotation.EnableScheduling;
@Configuration
@EnableScheduling
public class JdbcConfig {
@Bean

View File

@ -1,6 +1,7 @@
package ovh.herisson.Clyde.Services;
import org.springframework.stereotype.Service;
import ovh.herisson.Clyde.Tables.Token;
import ovh.herisson.Clyde.Tables.User;
import java.util.Date;
@ -26,7 +27,7 @@ public class AuthenticatorService {
if (user == null){return null;}
if (!userService.checkPassword(user,password)){return null;}
String token = tokenService.generateNewToken();
tokenService.saveToken(token,user,expirationDate);
tokenService.saveToken(new Token(user, token),expirationDate);
return token;
}
}

View File

@ -1,13 +1,19 @@
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.Date;
import java.util.Timer;
import java.util.TimerTask;
@Service
public class TokenService {
@ -31,7 +37,9 @@ public class TokenService {
return tokenRepo.getByToken(token).getUser();
}
public void saveToken(String token, User user, Date expirationDate){// todo faire qlq chose de l'expDate
tokenRepo.save(new Token(user,token));
public void saveToken(Token token, Date expirationDate){
tokenRepo.save(token);
TokenCleaner cleaner = new TokenCleaner(token, tokenRepo);
}
}

View File

@ -1,6 +1,8 @@
package ovh.herisson.Clyde.Tables;
import jakarta.persistence.*;
import org.springframework.scheduling.annotation.Scheduled;
import ovh.herisson.Clyde.Repositories.TokenRepository;
@Entity
public class Token {
@ -19,6 +21,7 @@ public class Token {
}
public Token(){}
public int getId() {
return id;
}

View File

@ -16,6 +16,7 @@ public class User {
private int regNo;
private String lastName;
private String firstName;
@Column(unique = true)
private String email;
private String address;
private String country;

View File

@ -0,0 +1,30 @@
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);
}
};
}