added Token and User Services
This commit is contained in:
		| @ -0,0 +1,36 @@ | ||||
| package ovh.herisson.Clyde.Services; | ||||
|  | ||||
| import org.springframework.stereotype.Service; | ||||
| import ovh.herisson.Clyde.Repositories.TokenRepository; | ||||
| import ovh.herisson.Clyde.Tables.Token; | ||||
| import ovh.herisson.Clyde.Tables.User; | ||||
|  | ||||
| import java.nio.charset.StandardCharsets; | ||||
| import java.security.SecureRandom; | ||||
| import java.util.Date; | ||||
|  | ||||
| @Service | ||||
| public class TokenService { | ||||
|  | ||||
|     TokenRepository tokenRepo; | ||||
|  | ||||
|     public TokenService(TokenRepository tokenRepo){ | ||||
|         this.tokenRepo = tokenRepo; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     public String generateNewToken(){ | ||||
|         byte[] bytes = new byte[64]; | ||||
|         new SecureRandom().nextBytes(bytes); | ||||
|         String token = new String(bytes, StandardCharsets.US_ASCII); | ||||
|         System.out.println(token); | ||||
|         return token; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     //todo potentiellement return bool pour savoir si token bien add | ||||
|     public void saveToken(String token, User user, Date expirationDate){ | ||||
|         tokenRepo.save(new Token(user,token)); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @ -0,0 +1,36 @@ | ||||
| package ovh.herisson.Clyde.Services; | ||||
|  | ||||
| import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||||
| import org.springframework.stereotype.Service; | ||||
| import ovh.herisson.Clyde.Repositories.UserRepository; | ||||
| import ovh.herisson.Clyde.Tables.User; | ||||
|  | ||||
| @Service | ||||
| public class UserService { | ||||
|  | ||||
|     private final UserRepository userRepo; | ||||
|     private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); | ||||
|  | ||||
|  | ||||
|     public UserService(UserRepository userRepo){ | ||||
|         this.userRepo = userRepo; | ||||
|     } | ||||
|  | ||||
|  | ||||
|     public User getUser(String identifier){ | ||||
|         if (identifier == null) return null; | ||||
|         try { | ||||
|             int id = Integer.parseInt(identifier); | ||||
|             return userRepo.findById(id); | ||||
|         } | ||||
|         catch (NumberFormatException nfe){ | ||||
|             return userRepo.findByEmail(identifier); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|  | ||||
|     public boolean checkPassword(User user, String tryingPassword){ | ||||
|         return passwordEncoder.matches(tryingPassword,  user.getPassword()); | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user