-
Notifications
You must be signed in to change notification settings - Fork 905
hibernate-user-service-hw #908
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
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.
Sorry, the pull request review was unsuccessful for the following reasons:
Warning
There are no files to review. Please make sure that you have added files to the pull request. Some files and directories may be ignored from the review or failed to load.
[CODE: 4]
Additional info:
Ignored files list:
src/main/java/mate/academy/dao/UserDao.java
src/main/java/mate/academy/dao/impl/UserDaoImpl.java
src/main/java/mate/academy/exception/AuthenticationException.java
src/main/java/mate/academy/exception/RegistrationException.java
src/main/java/mate/academy/model/User.java
src/main/java/mate/academy/service/AuthenticationService.java
src/main/java/mate/academy/service/UserService.java
src/main/java/mate/academy/service/impl/AuthenticationServiceImpl.java
src/main/java/mate/academy/service/impl/UserServiceImpl.java
src/main/java/mate/academy/util/HashUtil.java
src/main/resources/hibernate.cfg.xml
If the issue is within your control, please address it.
The next review attempt will automatically launch in 1 hour.
If you believe this is an error or need assistance, please contact the support team.
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.
There are a few critical issues that need to be addressed before the Pull Request can be approved.
-
Unique Email Constraint: Ensure that the email field in the
User
model is unique to prevent duplicate entries in the database. -
Salt and Password Hashing: Implement proper handling of salt and password hashing in the
UserServiceImpl
class to ensure secure storage and validation of passwords. -
login
Method Logic: Modify thelogin
method inAuthenticationServiceImpl
to return the authenticated user object instead ofnull
upon successful authentication. -
Password Handling in
toString
: Remove or mask the password in thetoString
method of theUser
class to prevent security risks. -
Salt Handling in
HashUtil
: Correct the handling of salt in theHashUtil
class to ensure it is stored and retrieved correctly for password validation.
These changes are necessary to meet the core requirements of the task and ensure the functionality and security of the application. Keep up the good work, and once these issues are resolved, your code will be in great shape! 🚀
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
🔄 Fix the issues, commit the changes, and then re-request my review using the Re-request review
button in the Reviewers tab.
return "User{id=" + id | ||
+ ", email='" + email | ||
+ | ||
", password='****'" + 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.
Including the password in the toString
method can be a security risk. It's better to exclude it or mask it completely to prevent accidental logging or exposure.
if (userOptional.isPresent() && HashUtil.isValidPassword(password,userOptional | ||
.get().getPassword())) { | ||
return null; |
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 login
method, if the user is authenticated successfully, you should return the user object instead of null
. Modify the return statement to return userOptional.get()
.
String salt = hashedPassword.substring(0, 16); | ||
return hashedPassword.equals(hash(password, salt.getBytes())); |
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.
The isValidPassword
method incorrectly assumes that the salt is the first 16 characters of the hashed password. Ensure that the salt is stored separately or encoded in a way that can be reliably extracted for validation.
MessageDigest md = MessageDigest.getInstance(HASH_ALGORITHM); | ||
md.update(salt); | ||
byte[] hashedPassword = md.digest(password.getBytes()); | ||
return new String(salt) + new String(hashedPassword); |
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.
Concatenating the salt and hashed password using new String()
can lead to issues with encoding. Consider using a more reliable method to encode and store the salt and hashed password, such as Base64 encoding.
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.
Great job on implementing the basic setup for testing services and models in your application! 🎉 While the code is functional, there are a few critical areas to address for improved security and functionality:
-
Email Uniqueness: Ensure that the email field in your
User
model is marked as unique in the database schema. This will prevent duplicate user entries and maintain data integrity. -
Password Handling: It's crucial to hash passwords before storing or logging them. Storing passwords in plain text poses a significant security risk. Make sure to incorporate salt in the hashing process to enhance security.
-
Salt Usage: In the
HashUtil
class, ensure that the salt is used in the hashing process. Combine the password and salt before hashing, for example, by usingmd.update(salt);
beforemd.digest(password.getBytes());
. -
Private Constructor in
HashUtil
: Consider adding a private constructor to prevent instantiation of the utility class.
These improvements are essential for the security and reliability of your application. Keep up the good work, and make sure to address these issues in future iterations to enhance your code quality. Happy coding! 🚀
✨ Please rate this review in the Platform's notification center. I will be grateful for your feedback! ✨
private static final String EMAIL = "email"; | ||
private static final String PASSWORD = "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.
Ensure that the email used for registration is unique. This is crucial to prevent duplicate user entries in the database.
User user = authenticationService.login(EMAIL, PASSWORD); | ||
System.out.println("User logged in: " + 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.
Ensure that the password is hashed before being stored or logged. Storing or logging passwords in plain text is a security risk.
Transaction transaction = null; | ||
try (Session session = HibernateUtil.getSessionFactory().openSession()) { | ||
transaction = session.beginTransaction(); | ||
session.persist(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.
Ensure that the database schema enforces a unique constraint on the email field to prevent duplicate user entries.
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String email; |
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.
Ensure that the email field is marked as unique in the database schema to prevent duplicate entries.
public static String hashPassword(String password, byte[] salt) { | ||
try { | ||
MessageDigest md = MessageDigest.getInstance(HASH_ALGORITHM); | ||
byte[] hash = md.digest(password.getBytes()); |
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.
The salt is not being used in the hashing process. You should combine the password and salt before hashing to ensure security. For example, use md.update(salt);
before md.digest(password.getBytes());
.
No description provided.