- 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:
parent
857191f9e5
commit
206c5e958b
@ -7,6 +7,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import ovh.herisson.Clyde.Repositories.TokenRepository;
|
import ovh.herisson.Clyde.Repositories.TokenRepository;
|
||||||
import ovh.herisson.Clyde.Repositories.UserRepository;
|
import ovh.herisson.Clyde.Repositories.UserRepository;
|
||||||
|
import ovh.herisson.Clyde.Services.TokenService;
|
||||||
import ovh.herisson.Clyde.Tables.Role;
|
import ovh.herisson.Clyde.Tables.Role;
|
||||||
import ovh.herisson.Clyde.Tables.Token;
|
import ovh.herisson.Clyde.Tables.Token;
|
||||||
import ovh.herisson.Clyde.Tables.User;
|
import ovh.herisson.Clyde.Tables.User;
|
||||||
@ -23,13 +24,14 @@ public class MockController {
|
|||||||
|
|
||||||
public final UserRepository userRepo;
|
public final UserRepository userRepo;
|
||||||
public final TokenRepository tokenRepo;
|
public final TokenRepository tokenRepo;
|
||||||
|
public final TokenService tokenService;
|
||||||
ArrayList<User> mockUsers;
|
ArrayList<User> mockUsers;
|
||||||
|
|
||||||
|
|
||||||
public MockController(UserRepository userRepo, TokenRepository tokenRepo){
|
public MockController(UserRepository userRepo, TokenRepository tokenRepo, TokenService tokenService){
|
||||||
this.tokenRepo = tokenRepo;
|
this.tokenRepo = tokenRepo;
|
||||||
this.userRepo = userRepo;
|
this.userRepo = userRepo;
|
||||||
|
this.tokenService = tokenService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Saves an example of each user type by :
|
/** Saves an example of each user type by :
|
||||||
@ -51,7 +53,7 @@ public class MockController {
|
|||||||
userRepo.saveAll(mockUsers);
|
userRepo.saveAll(mockUsers);
|
||||||
|
|
||||||
for (User user: mockUsers){
|
for (User user: mockUsers){
|
||||||
tokenRepo.save(new Token(user,user.getPassword()));
|
tokenService.saveToken(new Token(user,user.getPassword()), null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,8 +6,10 @@ import org.springframework.context.annotation.Bean;
|
|||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Profile;
|
import org.springframework.context.annotation.Profile;
|
||||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@EnableScheduling
|
||||||
public class JdbcConfig {
|
public class JdbcConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package ovh.herisson.Clyde.Services;
|
package ovh.herisson.Clyde.Services;
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import ovh.herisson.Clyde.Tables.Token;
|
||||||
import ovh.herisson.Clyde.Tables.User;
|
import ovh.herisson.Clyde.Tables.User;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@ -26,7 +27,7 @@ public class AuthenticatorService {
|
|||||||
if (user == null){return null;}
|
if (user == null){return null;}
|
||||||
if (!userService.checkPassword(user,password)){return null;}
|
if (!userService.checkPassword(user,password)){return null;}
|
||||||
String token = tokenService.generateNewToken();
|
String token = tokenService.generateNewToken();
|
||||||
tokenService.saveToken(token,user,expirationDate);
|
tokenService.saveToken(new Token(user, token),expirationDate);
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,19 @@
|
|||||||
package ovh.herisson.Clyde.Services;
|
package ovh.herisson.Clyde.Services;
|
||||||
|
|
||||||
|
import org.springframework.scheduling.annotation.Async;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import ovh.herisson.Clyde.Repositories.TokenRepository;
|
import ovh.herisson.Clyde.Repositories.TokenRepository;
|
||||||
|
import ovh.herisson.Clyde.Tables.Role;
|
||||||
import ovh.herisson.Clyde.Tables.Token;
|
import ovh.herisson.Clyde.Tables.Token;
|
||||||
import ovh.herisson.Clyde.Tables.User;
|
import ovh.herisson.Clyde.Tables.User;
|
||||||
|
import ovh.herisson.Clyde.TokenCleaner;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class TokenService {
|
public class TokenService {
|
||||||
@ -31,7 +37,9 @@ public class TokenService {
|
|||||||
return tokenRepo.getByToken(token).getUser();
|
return tokenRepo.getByToken(token).getUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveToken(String token, User user, Date expirationDate){// todo faire qlq chose de l'expDate
|
public void saveToken(Token token, Date expirationDate){
|
||||||
tokenRepo.save(new Token(user,token));
|
tokenRepo.save(token);
|
||||||
|
TokenCleaner cleaner = new TokenCleaner(token, tokenRepo);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,6 +1,8 @@
|
|||||||
package ovh.herisson.Clyde.Tables;
|
package ovh.herisson.Clyde.Tables;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import ovh.herisson.Clyde.Repositories.TokenRepository;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class Token {
|
public class Token {
|
||||||
@ -19,6 +21,7 @@ public class Token {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Token(){}
|
public Token(){}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ public class User {
|
|||||||
private int regNo;
|
private int regNo;
|
||||||
private String lastName;
|
private String lastName;
|
||||||
private String firstName;
|
private String firstName;
|
||||||
|
@Column(unique = true)
|
||||||
private String email;
|
private String email;
|
||||||
private String address;
|
private String address;
|
||||||
private String country;
|
private String country;
|
||||||
|
30
backend/src/main/java/ovh/herisson/Clyde/TokenCleaner.java
Normal file
30
backend/src/main/java/ovh/herisson/Clyde/TokenCleaner.java
Normal 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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user