Skip to content

Conversation

ShastkivRuslan
Copy link

No description provided.

-added customs exceptions;
-added user class;
-added HashUtil class with hashPassword() method;
-modified HashUtil class adding getsalt() method;
-some changes in User.class;
-all tasks completed;
@@ -44,7 +59,8 @@ public static void main(String[] args) {
yesterdayMovieSession.setMovie(fastAndFurious);
yesterdayMovieSession.setShowTime(LocalDateTime.now().minusDays(1L));

MovieSessionService movieSessionService = null;
MovieSessionService movieSessionService =
(MovieSessionService) INJECTOR.getInstance(MovieSessionService.class);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the mate/academy/Main.main() method create an instance of AuthenticationService using injector and test all methods from it.

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String login;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private String login;
private String email;

Let's make the email unique. We need to check the email during user registration and make the email a unique field in the database.


}

public User authenticate(String login, String password) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant method

Comment on lines 19 to 22
User user = new User();
user.setLogin(login);
user.setPassword(password);
return userService.save(user);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
User user = new User();
user.setLogin(login);
user.setPassword(password);
return userService.save(user);
validateRegisterData(email, password);
return userService.save(new User(email, password));


@Override
public User login(String login, String password) {
Optional<User> userByLogin = userService.findByLogin(login);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Optional<User> userByLogin = userService.findByLogin(login);
Optional<User> user = userService.findByEmail(email);
if (user.isEmpty() || !isValidPassword(user.get(), password)) {
throw new AuthenticationException(
"Authentication failed for user with email: " + email);
}
return user.get();

where

private boolean isValidPassword(User user, String password) {
        return password != null && user.getPassword()
                .equals(HashUtil.hashPassword(password, user.getSalt()));
    }

hashedPassword.append(String.format("%02x", b));
}
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("Could not create hash using SHA-512 algorithm", e);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use your constant CRYPTO_ALGORITHM in message

Changes after review of mentor Olena.
- used constant CRYPTO_ALGORITHGM in throw message;
-modified logic in register and login method:
-added a check before registration whether such an email exists in the database;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants