|
| 1 | +package mate.academy.service.impl; |
| 2 | + |
| 3 | +import java.security.MessageDigest; |
| 4 | +import java.security.NoSuchAlgorithmException; |
| 5 | +import java.security.SecureRandom; |
| 6 | +import java.util.Optional; |
| 7 | +import mate.academy.exception.AuthenticationException; |
| 8 | +import mate.academy.exception.RegistrationException; |
| 9 | +import mate.academy.lib.Inject; |
| 10 | +import mate.academy.lib.Service; |
| 11 | +import mate.academy.model.User; |
| 12 | +import mate.academy.service.AuthenticationService; |
| 13 | +import mate.academy.service.UserService; |
| 14 | + |
| 15 | +@Service |
| 16 | +public class AuthenticationServiceImpl implements AuthenticationService { |
| 17 | + |
| 18 | + private static final String HASHING_ALGORITHM = "SHA-256"; |
| 19 | + @Inject |
| 20 | + private final UserService userService; |
| 21 | + |
| 22 | + public AuthenticationServiceImpl(UserService userService) { |
| 23 | + this.userService = userService; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public User login(String email, String password) throws AuthenticationException { |
| 28 | + Optional<User> optionalUser = userService.findByEmail(email); |
| 29 | + if (optionalUser.isEmpty()) { |
| 30 | + throw new AuthenticationException("User not found by email: " + email); |
| 31 | + } |
| 32 | + User user = optionalUser.get(); |
| 33 | + String hashedPassword = hashPassword(password, user.getSalt()); |
| 34 | + if (!user.getPassword().equals(hashedPassword)) { |
| 35 | + throw new AuthenticationException("Invalid password"); |
| 36 | + } |
| 37 | + return user; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public User register(String email, String password) throws RegistrationException { |
| 42 | + if (userService.findByEmail(email).isPresent()) { |
| 43 | + throw new RegistrationException("Email is already taken: " + email); |
| 44 | + } |
| 45 | + byte[] salt = generateSalt(); |
| 46 | + String hashedPassword = hashPassword(password, salt); |
| 47 | + User user = new User(email, hashedPassword); |
| 48 | + user.setSalt(salt); |
| 49 | + return userService.add(user); |
| 50 | + } |
| 51 | + |
| 52 | + private String hashPassword(String password, byte[] salt) { |
| 53 | + try { |
| 54 | + MessageDigest digest = MessageDigest.getInstance(HASHING_ALGORITHM); |
| 55 | + digest.update(salt); |
| 56 | + byte[] hashedBytes = digest.digest(password.getBytes()); |
| 57 | + StringBuilder hashedPassword = new StringBuilder(); |
| 58 | + for (byte b : hashedBytes) { |
| 59 | + hashedPassword.append(String.format("%02x", b)); |
| 60 | + } |
| 61 | + return hashedPassword.toString(); |
| 62 | + } catch (NoSuchAlgorithmException e) { |
| 63 | + throw new RuntimeException("Could not hash password", e); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private byte[] generateSalt() { |
| 68 | + byte[] salt = new byte[16]; |
| 69 | + new SecureRandom().nextBytes(salt); |
| 70 | + return salt; |
| 71 | + } |
| 72 | +} |
0 commit comments