generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 905
Solved hw #934
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
Open
OlhaHirniak
wants to merge
2
commits into
mate-academy:master
Choose a base branch
from
OlhaHirniak:hw_user_service
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Solved hw #934
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package mate.academy.dao; | ||
|
||
import java.util.Optional; | ||
import mate.academy.model.User; | ||
|
||
public interface UserDao { | ||
User add(User user); | ||
|
||
Optional<User> get(Long id); | ||
|
||
public Optional<User> findByEmail(String email); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package mate.academy.dao.impl; | ||
|
||
import java.util.Optional; | ||
import mate.academy.dao.UserDao; | ||
import mate.academy.exception.DataProcessingException; | ||
import mate.academy.lib.Dao; | ||
import mate.academy.model.User; | ||
import mate.academy.util.HibernateUtil; | ||
import org.hibernate.Session; | ||
import org.hibernate.Transaction; | ||
|
||
@Dao | ||
public class UserDaoImpl implements UserDao { | ||
@Override | ||
public User add(User user) { | ||
Transaction transaction = null; | ||
Session session = null; | ||
try { | ||
session = HibernateUtil.getSessionFactory().openSession(); | ||
transaction = session.beginTransaction(); | ||
session.persist(user); | ||
transaction.commit(); | ||
return user; | ||
} catch (Exception e) { | ||
if (transaction != null) { | ||
transaction.rollback(); | ||
} | ||
throw new DataProcessingException("Can't insert a user " + user, e); | ||
} finally { | ||
if (session != null) { | ||
session.close(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Optional<User> get(Long id) { | ||
try (Session session = HibernateUtil.getSessionFactory().openSession()) { | ||
return Optional.ofNullable(session.get(User.class, id)); | ||
} catch (Exception e) { | ||
throw new DataProcessingException("Can't get a user by id: " + id, e); | ||
} | ||
} | ||
|
||
@Override | ||
public Optional<User> findByEmail(String email) { | ||
try (Session session = HibernateUtil.getSessionFactory().openSession()) { | ||
return session.createQuery("FROM User u where u.email = :email", User.class) | ||
.setParameter("email", email) | ||
.uniqueResultOptional(); | ||
} catch (Exception e) { | ||
throw new DataProcessingException("Can't find user by email " + email, e); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/exception/AuthenticationException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mate.academy.exception; | ||
|
||
public class AuthenticationException extends Exception { | ||
public AuthenticationException(String message) { | ||
super(message); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/exception/RegistrationException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mate.academy.exception; | ||
|
||
public class RegistrationException extends Exception { | ||
public RegistrationException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package mate.academy.model; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
|
||
@Entity | ||
@Table(name = "users") | ||
public class User { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String email; | ||
private String password; | ||
private byte[] salt; | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public byte[] getSalt() { | ||
return salt; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public void setEmail(String login) { | ||
this.email = login; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
public void setSalt(byte[] salt) { | ||
this.salt = salt; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "User{" | ||
+ "id=" + id | ||
+ ", login='" + email + '\'' | ||
+ '}'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/mate/academy/service/AuthenticationService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package mate.academy.service; | ||
|
||
import mate.academy.exception.AuthenticationException; | ||
import mate.academy.exception.RegistrationException; | ||
import mate.academy.model.User; | ||
|
||
public interface AuthenticationService { | ||
User login(String email, String password) throws AuthenticationException; | ||
|
||
User register(String email, String password) throws RegistrationException; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package mate.academy.service; | ||
|
||
import java.util.Optional; | ||
import mate.academy.model.User; | ||
|
||
public interface UserService { | ||
User add(User user); | ||
|
||
Optional<User> findByEmail(String email); | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/mate/academy/service/impl/AuthenticationServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package mate.academy.service.impl; | ||
|
||
import java.util.Optional; | ||
import mate.academy.exception.AuthenticationException; | ||
import mate.academy.exception.RegistrationException; | ||
import mate.academy.lib.Inject; | ||
import mate.academy.lib.Service; | ||
import mate.academy.model.User; | ||
import mate.academy.service.AuthenticationService; | ||
import mate.academy.service.UserService; | ||
import mate.academy.util.HashUtil; | ||
|
||
@Service | ||
public class AuthenticationServiceImpl implements AuthenticationService { | ||
@Inject | ||
private UserService userService; | ||
|
||
@Override | ||
public User login(String email, String password) throws AuthenticationException { | ||
Optional<User> userFromDbOptional = userService.findByEmail(email); | ||
if (userFromDbOptional.isEmpty() | ||
|| (!userFromDbOptional.get().getPassword().equals( | ||
HashUtil.hashPassword(password, userFromDbOptional.get().getSalt())))) { | ||
throw new AuthenticationException("Can't authenticate user"); | ||
} | ||
return userFromDbOptional.get(); | ||
} | ||
|
||
@Override | ||
public User register(String email, String password) throws RegistrationException { | ||
if (userService.findByEmail(email).isPresent()) { | ||
throw new RegistrationException("User with email " + email + " already exists"); | ||
} | ||
if (email.isEmpty() || password.isEmpty()) { | ||
throw new RegistrationException("Email and password can't be empty"); | ||
} | ||
User user = new User(); | ||
user.setEmail(email); | ||
user.setPassword(password); | ||
Comment on lines
+38
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The salt and hash password should be created in UserService According to the checklist:
|
||
return userService.add(user); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/mate/academy/service/impl/UserServiceImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package mate.academy.service.impl; | ||
|
||
import java.util.Optional; | ||
import mate.academy.dao.UserDao; | ||
import mate.academy.lib.Inject; | ||
import mate.academy.lib.Service; | ||
import mate.academy.model.User; | ||
import mate.academy.service.UserService; | ||
import mate.academy.util.HashUtil; | ||
|
||
@Service | ||
public class UserServiceImpl implements UserService { | ||
@Inject | ||
private UserDao userDao; | ||
|
||
@Override | ||
public User add(User user) { | ||
user.setSalt(HashUtil.getSalt()); | ||
user.setPassword(HashUtil.hashPassword(user.getPassword(), user.getSalt())); | ||
return userDao.add(user); | ||
} | ||
|
||
@Override | ||
public Optional<User> findByEmail(String email) { | ||
return userDao.findByEmail(email); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package mate.academy.util; | ||
|
||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.SecureRandom; | ||
|
||
public class HashUtil { | ||
private static final String CRYPTO_ALGORITHM = "SHA-512"; | ||
|
||
private HashUtil() { | ||
} | ||
|
||
public static byte[] getSalt() { | ||
SecureRandom secureRandom = new SecureRandom(); | ||
byte[] salt = new byte[16]; | ||
secureRandom.nextBytes(salt); | ||
return salt; | ||
} | ||
|
||
public static String hashPassword(String password, byte[] salt) { | ||
StringBuilder hashPassword = new StringBuilder(); | ||
try { | ||
MessageDigest messageDigest = MessageDigest.getInstance(CRYPTO_ALGORITHM); | ||
messageDigest.update(salt); | ||
byte[] digest = messageDigest.digest(password.getBytes()); | ||
for (byte b : digest) { | ||
hashPassword.append(String.format("%02x", b)); | ||
} | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new IllegalStateException("Could not create hash using SHA-512 algorithm", e); | ||
} | ||
return hashPassword.toString(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE hibernate-configuration PUBLIC | ||
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" | ||
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd"> | ||
|
||
<hibernate-configuration> | ||
<session-factory> | ||
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property> | ||
<property name="connection.url">jdbc:mysql://localhost/hib_movies?serverTimezone=UTC</property> | ||
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property> | ||
<property name="connection.username">root</property> | ||
<property name="connection.password">Olga8267davyd))</property> | ||
<property name="show_sql">true</property> | ||
<property name="hbm2ddl.auto">update</property> | ||
|
||
<mapping class="mate.academy.model.Movie"/> | ||
<mapping class="mate.academy.model.CinemaHall"/> | ||
<mapping class="mate.academy.model.MovieSession"/> | ||
<mapping class="mate.academy.model.User"/> | ||
</session-factory> | ||
</hibernate-configuration> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
According to the checklist, you should create only one condition for throwing
AuthenticationException
inlogin()
method. You may combine two checks: whether the user has been found by login and do passwords match.