Skip to content

Conversation

rus1980707
Copy link

No description provided.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a 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.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a 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.

  1. Unique Email Constraint: Ensure that the email field in the User model is unique to prevent duplicate entries in the database.

  2. Salt and Password Hashing: Implement proper handling of salt and password hashing in the UserServiceImpl class to ensure secure storage and validation of passwords.

  3. login Method Logic: Modify the login method in AuthenticationServiceImpl to return the authenticated user object instead of null upon successful authentication.

  4. Password Handling in toString: Remove or mask the password in the toString method of the User class to prevent security risks.

  5. Salt Handling in HashUtil: Correct the handling of salt in the HashUtil 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

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.

Comment on lines 21 to 23
if (userOptional.isPresent() && HashUtil.isValidPassword(password,userOptional
.get().getPassword())) {
return null;

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().

Comment on lines 18 to 19
String salt = hashedPassword.substring(0, 16);
return hashedPassword.equals(hash(password, salt.getBytes()));

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);

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.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a 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:

  1. 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.

  2. 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.

  3. 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 using md.update(salt); before md.digest(password.getBytes());.

  4. 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! ✨

Comment on lines +20 to +21
private static final String EMAIL = "email";
private static final String PASSWORD = "Password";

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.

Comment on lines +76 to +77
User user = authenticationService.login(EMAIL, PASSWORD);
System.out.println("User logged in: " + user);

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);

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;

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());

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());.

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.

2 participants