-
Notifications
You must be signed in to change notification settings - Fork 906
hibernate-user-service-hw v(1.0); #867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
-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); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redundant method
User user = new User(); | ||
user.setLogin(login); | ||
user.setPassword(password); | ||
return userService.save(user); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); |
There was a problem hiding this comment.
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;
No description provided.