Skip to content
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

Added a check weather forget-password token is of same user requesting #97

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.pecacm.backend.entities;

public class PasswordVerificationToken {
}
Comment on lines +1 to +4
Copy link
Collaborator

Choose a reason for hiding this comment

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

Where are you using this ? Please delete this if unnecessary

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pecacm.backend.repository;

import com.pecacm.backend.entities.User;
import com.pecacm.backend.entities.VerificationToken;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
Expand All @@ -17,11 +18,11 @@ public interface VerificationTokenRepository extends JpaRepository<VerificationT

@Query("SELECT "+
"CASE " +
"WHEN :tokenId = (SELECT v.token from VerificationToken v ORDER BY v.createdDate DESC LIMIT 1)" +
"WHEN :tokenId = (SELECT v.token from VerificationToken v WHERE v.user=:user ORDER BY v.createdDate DESC LIMIT 1)" +
"THEN TRUE " +
"ELSE FALSE " +
"END as result")
Boolean checkVerificationToken(UUID tokenId);
Boolean checkVerificationToken(UUID tokenId, User user);
Copy link
Collaborator

Choose a reason for hiding this comment

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

can compare username (email) instead of the entire user


@Modifying
void deleteByToken(UUID tokenId);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/pecacm/backend/services/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ public void addUser(User user, PasswordEncoder passwordEncoder) {

@Transactional
public void changePassword(UUID tokenId, String username, String password, PasswordEncoder passwordEncoder) {
if (!userRepository.existsByEmail(username)) {
Optional<User> user = userRepository.findByEmail(username);
if (user.isEmpty()) {
throw new AcmException("Email provided does not match any of the registered users", HttpStatus.NOT_FOUND);
}
if (!verificationTokenRepository.checkVerificationToken(tokenId)) {
if (!verificationTokenRepository.checkVerificationToken(tokenId,user.get())) {
throw new AcmException("UUID token provided does not match, it might be expired", HttpStatus.NOT_FOUND);
}
if (password.isBlank() || password.isEmpty()) {
Expand Down