Browse Source

Improving ForgotPwd structure

main
ddangelorb 3 years ago
parent
commit
26fd304a66
  1. 42
      src/main/java/org/waterproofingdata/wpdauth/controller/ForgotPasswordsController.java
  2. 2
      src/main/java/org/waterproofingdata/wpdauth/dto/ForgotPasswordsQuestionsResponseDTO.java
  3. 2
      src/main/java/org/waterproofingdata/wpdauth/dto/ForgotPasswordsQuestionsUsersAnswersRequestDTO.java
  4. 2
      src/main/java/org/waterproofingdata/wpdauth/model/ForgotPasswordsKeys.java
  5. 2
      src/main/java/org/waterproofingdata/wpdauth/model/ForgotPasswordsQuestions.java
  6. 2
      src/main/java/org/waterproofingdata/wpdauth/model/ForgotPasswordsQuestionsUsersAnswers.java
  7. 7
      src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordQuestionsRepository.java
  8. 10
      src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordQuestionsUsersAnswersRepository.java
  9. 6
      src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordsKeysRepository.java
  10. 7
      src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordsQuestionsRepository.java
  11. 10
      src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordsQuestionsUsersAnswersRepository.java
  12. 42
      src/main/java/org/waterproofingdata/wpdauth/service/ForgotPasswordsService.java
  13. 8
      src/test/java/org/waterproofingdata/wpdauth/integrationtest/ForgotPasswordServiceIntegrationTest.java

42
src/main/java/org/waterproofingdata/wpdauth/controller/ForgotPasswordController.java → src/main/java/org/waterproofingdata/wpdauth/controller/ForgotPasswordsController.java

@ -10,11 +10,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import org.waterproofingdata.wpdauth.dto.ForgotPasswordQuestionsResponseDTO;
import org.waterproofingdata.wpdauth.dto.ForgotPasswordQuestionsUsersAnswersRequestDTO;
import org.waterproofingdata.wpdauth.model.ForgotPasswordQuestionsUsersAnswers;
import org.waterproofingdata.wpdauth.dto.ForgotPasswordsQuestionsResponseDTO;
import org.waterproofingdata.wpdauth.dto.ForgotPasswordsQuestionsUsersAnswersRequestDTO;
import org.waterproofingdata.wpdauth.model.ForgotPasswordsQuestionsUsersAnswers;
import org.waterproofingdata.wpdauth.dto.CustomMapper;
import org.waterproofingdata.wpdauth.service.ForgotPasswordService;
import org.waterproofingdata.wpdauth.service.ForgotPasswordsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -26,71 +26,71 @@ import io.swagger.annotations.Authorization;
@RestController
@RequestMapping("/forgotpassword")
@Api(tags = "forgotpassword")
public class ForgotPasswordController {
public class ForgotPasswordsController {
@Autowired
private ForgotPasswordService forgotPasswordService;
private ForgotPasswordsService forgotPasswordsService;
@PostMapping("/sendkeybyemail")
@ApiOperation(value = "${ForgotPasswordController.sendkeybyemail}")
@ApiOperation(value = "${ForgotPasswordsController.sendkeybyemail}")
@ApiResponses(value = {//
@ApiResponse(code = 400, message = "Something went wrong"), //
@ApiResponse(code = 422, message = "Invalid email supplied")})
public void sendkeybyemail(@ApiParam("Email") @RequestParam String email) {
forgotPasswordService.sendKeyByEmail(email);
forgotPasswordsService.sendKeyByEmail(email);
}
@PostMapping("/loginbyemailandkey")
@ApiOperation(value = "${ForgotPasswordController.loginbyemailandkey}")
@ApiOperation(value = "${ForgotPasswordsController.loginbyemailandkey}")
@ApiResponses(value = {//
@ApiResponse(code = 400, message = "Something went wrong"), //
@ApiResponse(code = 422, message = "Invalid username/password supplied")})
public String loginbyemailandkey(//
@ApiParam("Email") @RequestParam String email, //
@ApiParam("Key") @RequestParam String key) {
return forgotPasswordService.loginByEmailAndKey(email, key);
return forgotPasswordsService.loginByEmailAndKey(email, key);
}
@PostMapping("/passwordupdatebyemail")
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_INSTITUTION') or hasRole('ROLE_CLIENT')")
@ApiOperation(value = "${ForgotPasswordController.passwordupdatebyemail}", authorizations = { @Authorization(value="apiKey") })
@ApiOperation(value = "${ForgotPasswordsController.passwordupdatebyemail}", authorizations = { @Authorization(value="apiKey") })
@ApiResponses(value = {//
@ApiResponse(code = 400, message = "Something went wrong"), //
@ApiResponse(code = 403, message = "Access denied"), //
@ApiResponse(code = 404, message = "The user doesn't exist"), //
@ApiResponse(code = 500, message = "Expired or invalid JWT token")})
public void passwordupdatebyemail(@ApiParam("Email") @RequestParam String email, @ApiParam("NewPassword") @RequestParam String newPassword) {
forgotPasswordService.passwordUpdateByEmail(email, newPassword);
forgotPasswordsService.passwordUpdateByEmail(email, newPassword);
}
@PostMapping("/findallforgotpasswordquestions")
@ApiOperation(value = "${ForgotPasswordController.findallforgotpasswordquestions}")
@ApiOperation(value = "${ForgotPasswordsController.findallforgotpasswordquestions}")
@ApiResponses(value = {//
@ApiResponse(code = 400, message = "Something went wrong")})
public List<ForgotPasswordQuestionsResponseDTO> findallforgotpasswordquestions() {
return CustomMapper.mapAll(forgotPasswordService.findAllForgotPasswordQuestions(), ForgotPasswordQuestionsResponseDTO.class);
public List<ForgotPasswordsQuestionsResponseDTO> findallforgotpasswordquestions() {
return CustomMapper.mapAll(forgotPasswordsService.findAllForgotPasswordQuestions(), ForgotPasswordsQuestionsResponseDTO.class);
}
@PostMapping("/saveforgotpasswordquestionsusersanswers")
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_INSTITUTION') or hasRole('ROLE_CLIENT')")
@ApiOperation(value = "${ForgotPasswordController.saveforgotpasswordquestionsusersanswers}", authorizations = { @Authorization(value="apiKey") })
@ApiOperation(value = "${ForgotPasswordsController.saveforgotpasswordquestionsusersanswers}", authorizations = { @Authorization(value="apiKey") })
@ApiResponses(value = {//
@ApiResponse(code = 400, message = "Something went wrong"), //
@ApiResponse(code = 403, message = "Access denied"), //
@ApiResponse(code = 404, message = "The user or forgot password questions don't exist"), //
@ApiResponse(code = 500, message = "Expired or invalid JWT token")})
public void saveforgotpasswordquestionsusersanswers(@ApiParam("Forgot Password Questions Users Answers") @RequestBody ForgotPasswordQuestionsUsersAnswersRequestDTO answer) {
forgotPasswordService.saveForgotPasswordQuestionsUsersAnswers(CustomMapper.map(answer, ForgotPasswordQuestionsUsersAnswers.class));
public void saveforgotpasswordquestionsusersanswers(@ApiParam("Forgot Password Questions Users Answers") @RequestBody ForgotPasswordsQuestionsUsersAnswersRequestDTO answer) {
forgotPasswordsService.saveForgotPasswordQuestionsUsersAnswers(CustomMapper.map(answer, ForgotPasswordsQuestionsUsersAnswers.class));
}
@PostMapping("/loginbyemailandanswers")
@ApiOperation(value = "${ForgotPasswordController.loginbyemailandanswers}")
@ApiOperation(value = "${ForgotPasswordsController.loginbyemailandanswers}")
@ApiResponses(value = {//
@ApiResponse(code = 400, message = "Something went wrong"), //
@ApiResponse(code = 404, message = "The user or answer don't exist"), //
@ApiResponse(code = 422, message = "Invalid answers supplied to login. Must have at least 2 correct ones.")})
public String loginbyemailandanswers(//
@ApiParam("Email") @RequestParam String email, //
@ApiParam("Answers") @RequestParam List<ForgotPasswordQuestionsUsersAnswersRequestDTO> answers) {
return forgotPasswordService.loginByEmailAndAnswers(email, CustomMapper.mapAll(answers, ForgotPasswordQuestionsUsersAnswers.class));
@ApiParam("Answers") @RequestParam List<ForgotPasswordsQuestionsUsersAnswersRequestDTO> answers) {
return forgotPasswordsService.loginByEmailAndAnswers(email, CustomMapper.mapAll(answers, ForgotPasswordsQuestionsUsersAnswers.class));
}
}

2
src/main/java/org/waterproofingdata/wpdauth/dto/ForgotPasswordQuestionsResponseDTO.java → src/main/java/org/waterproofingdata/wpdauth/dto/ForgotPasswordsQuestionsResponseDTO.java

@ -6,7 +6,7 @@ import lombok.Setter;
@Getter
@Setter
public class ForgotPasswordQuestionsResponseDTO {
public class ForgotPasswordsQuestionsResponseDTO {
@ApiModelProperty(position = 0)
private Integer id;

2
src/main/java/org/waterproofingdata/wpdauth/dto/ForgotPasswordQuestionsUsersAnswersRequestDTO.java → src/main/java/org/waterproofingdata/wpdauth/dto/ForgotPasswordsQuestionsUsersAnswersRequestDTO.java

@ -7,7 +7,7 @@ import lombok.Setter;
@Getter
@Setter
public class ForgotPasswordQuestionsUsersAnswersRequestDTO {
public class ForgotPasswordsQuestionsUsersAnswersRequestDTO {
@ApiModelProperty(position = 0)
private Integer id;

2
src/main/java/org/waterproofingdata/wpdauth/model/ForgotPasswordKeys.java → src/main/java/org/waterproofingdata/wpdauth/model/ForgotPasswordsKeys.java

@ -14,7 +14,7 @@ import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
public class ForgotPasswordKeys {
public class ForgotPasswordsKeys {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

2
src/main/java/org/waterproofingdata/wpdauth/model/ForgotPasswordQuestions.java → src/main/java/org/waterproofingdata/wpdauth/model/ForgotPasswordsQuestions.java

@ -14,7 +14,7 @@ import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
public class ForgotPasswordQuestions {
public class ForgotPasswordsQuestions {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

2
src/main/java/org/waterproofingdata/wpdauth/model/ForgotPasswordQuestionsUsersAnswers.java → src/main/java/org/waterproofingdata/wpdauth/model/ForgotPasswordsQuestionsUsersAnswers.java

@ -14,7 +14,7 @@ import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
public class ForgotPasswordQuestionsUsersAnswers {
public class ForgotPasswordsQuestionsUsersAnswers {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

7
src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordQuestionsRepository.java

@ -1,7 +0,0 @@
package org.waterproofingdata.wpdauth.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.waterproofingdata.wpdauth.model.ForgotPasswordQuestions;
public interface ForgotPasswordQuestionsRepository extends JpaRepository<ForgotPasswordQuestions, Integer> {
}

10
src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordQuestionsUsersAnswersRepository.java

@ -1,10 +0,0 @@
package org.waterproofingdata.wpdauth.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.waterproofingdata.wpdauth.model.ForgotPasswordQuestionsUsersAnswers;
public interface ForgotPasswordQuestionsUsersAnswersRepository extends JpaRepository<ForgotPasswordQuestionsUsersAnswers, Integer> {
@Query(value = "SELECT fqua.* FROM forgotpassword_questions_users_answers fqua WHERE fqua.forgotpassword_questions_id = ?1 AND fqua.users_id = ?2", nativeQuery = true)
ForgotPasswordQuestionsUsersAnswers findByForgotPasswordQuestionsAndUserid(Integer forgotpasswordquestionsid, Integer usersid);
}

6
src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordKeysRepository.java → src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordsKeysRepository.java

@ -2,9 +2,9 @@ package org.waterproofingdata.wpdauth.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.waterproofingdata.wpdauth.model.ForgotPasswordKeys;
import org.waterproofingdata.wpdauth.model.ForgotPasswordsKeys;
public interface ForgotPasswordKeysRepository extends JpaRepository<ForgotPasswordKeys, Integer> {
public interface ForgotPasswordsKeysRepository extends JpaRepository<ForgotPasswordsKeys, Integer> {
@Query(value = "SELECT f.* FROM forgotpassword_keys f WHERE f.email = ?1 AND f.key = ?2 AND f.created_at >= current_date::timestamp AND f.created_at < current_date::timestamp + interval '1 day' ORDER BY f.id DESC LIMIT 1", nativeQuery = true)
ForgotPasswordKeys findTodayRecordByEmailANDKey(String email, String key);
ForgotPasswordsKeys findTodayRecordByEmailANDKey(String email, String key);
}

7
src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordsQuestionsRepository.java

@ -0,0 +1,7 @@
package org.waterproofingdata.wpdauth.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.waterproofingdata.wpdauth.model.ForgotPasswordsQuestions;
public interface ForgotPasswordsQuestionsRepository extends JpaRepository<ForgotPasswordsQuestions, Integer> {
}

10
src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordsQuestionsUsersAnswersRepository.java

@ -0,0 +1,10 @@
package org.waterproofingdata.wpdauth.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.waterproofingdata.wpdauth.model.ForgotPasswordsQuestionsUsersAnswers;
public interface ForgotPasswordsQuestionsUsersAnswersRepository extends JpaRepository<ForgotPasswordsQuestionsUsersAnswers, Integer> {
@Query(value = "SELECT fqua.* FROM forgotpassword_questions_users_answers fqua WHERE fqua.forgotpassword_questions_id = ?1 AND fqua.users_id = ?2", nativeQuery = true)
ForgotPasswordsQuestionsUsersAnswers findByForgotPasswordQuestionsAndUserid(Integer forgotpasswordquestionsid, Integer usersid);
}

42
src/main/java/org/waterproofingdata/wpdauth/service/ForgotPasswordService.java → src/main/java/org/waterproofingdata/wpdauth/service/ForgotPasswordsService.java

@ -10,26 +10,26 @@ import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.waterproofingdata.wpdauth.exception.CustomException;
import org.waterproofingdata.wpdauth.model.ForgotPasswordKeys;
import org.waterproofingdata.wpdauth.model.ForgotPasswordQuestions;
import org.waterproofingdata.wpdauth.model.ForgotPasswordQuestionsUsersAnswers;
import org.waterproofingdata.wpdauth.model.ForgotPasswordsKeys;
import org.waterproofingdata.wpdauth.model.ForgotPasswordsQuestions;
import org.waterproofingdata.wpdauth.model.ForgotPasswordsQuestionsUsersAnswers;
import org.waterproofingdata.wpdauth.model.Users;
import org.waterproofingdata.wpdauth.repository.ForgotPasswordKeysRepository;
import org.waterproofingdata.wpdauth.repository.ForgotPasswordQuestionsRepository;
import org.waterproofingdata.wpdauth.repository.ForgotPasswordQuestionsUsersAnswersRepository;
import org.waterproofingdata.wpdauth.repository.ForgotPasswordsKeysRepository;
import org.waterproofingdata.wpdauth.repository.ForgotPasswordsQuestionsRepository;
import org.waterproofingdata.wpdauth.repository.ForgotPasswordsQuestionsUsersAnswersRepository;
import org.waterproofingdata.wpdauth.repository.UsersRepository;
import org.waterproofingdata.wpdauth.security.JwtTokenProvider;
@Service
public class ForgotPasswordService {
public class ForgotPasswordsService {
@Autowired
private ForgotPasswordKeysRepository forgotPasswordKeysRepository;
private ForgotPasswordsKeysRepository forgotPasswordsKeysRepository;
@Autowired
private ForgotPasswordQuestionsRepository forgotPasswordQuestionsRepository;
private ForgotPasswordsQuestionsRepository forgotPasswordsQuestionsRepository;
@Autowired
private ForgotPasswordQuestionsUsersAnswersRepository forgotPasswordQuestionsUsersAnswersRepository;
private ForgotPasswordsQuestionsUsersAnswersRepository forgotPasswordsQuestionsUsersAnswersRepository;
@Autowired
private UsersRepository usersRepository;
@ -53,10 +53,10 @@ public class ForgotPasswordService {
String key = String.format("%04d", rand.nextInt(10000));
//System.out.printf("%04d%n", rand.nextInt(10000));
ForgotPasswordKeys entity = new ForgotPasswordKeys();
ForgotPasswordsKeys entity = new ForgotPasswordsKeys();
entity.setEmail(email);
entity.setKey(key);
forgotPasswordKeysRepository.save(entity);
forgotPasswordsKeysRepository.save(entity);
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("noreply@wp6.com");
@ -67,7 +67,7 @@ public class ForgotPasswordService {
}
public String loginByEmailAndKey(String email, String key) {
ForgotPasswordKeys entity = forgotPasswordKeysRepository.findTodayRecordByEmailANDKey(email, key);
ForgotPasswordsKeys entity = forgotPasswordsKeysRepository.findTodayRecordByEmailANDKey(email, key);
if (entity == null) {
throw new CustomException("The email and forgot key don't exist", HttpStatus.NOT_FOUND);
}
@ -90,12 +90,12 @@ public class ForgotPasswordService {
usersRepository.save(user);
}
public List<ForgotPasswordQuestions> findAllForgotPasswordQuestions() {
return forgotPasswordQuestionsRepository.findAll();
public List<ForgotPasswordsQuestions> findAllForgotPasswordQuestions() {
return forgotPasswordsQuestionsRepository.findAll();
}
public void saveForgotPasswordQuestionsUsersAnswers(ForgotPasswordQuestionsUsersAnswers forgotPasswordQuestionsUsersAnswer) {
if (!forgotPasswordQuestionsRepository.existsById(forgotPasswordQuestionsUsersAnswer.getForgotpasswordquestionsid())) {
public void saveForgotPasswordQuestionsUsersAnswers(ForgotPasswordsQuestionsUsersAnswers forgotPasswordQuestionsUsersAnswer) {
if (!forgotPasswordsQuestionsRepository.existsById(forgotPasswordQuestionsUsersAnswer.getForgotpasswordquestionsid())) {
throw new CustomException("The Forgot Password Questions provided doesn't exist", HttpStatus.NOT_FOUND);
}
@ -103,18 +103,18 @@ public class ForgotPasswordService {
throw new CustomException("The User provided doesn't exist", HttpStatus.NOT_FOUND);
}
forgotPasswordQuestionsUsersAnswersRepository.save(forgotPasswordQuestionsUsersAnswer);
forgotPasswordsQuestionsUsersAnswersRepository.save(forgotPasswordQuestionsUsersAnswer);
}
public String loginByEmailAndAnswers(String email, List<ForgotPasswordQuestionsUsersAnswers> answers) {
public String loginByEmailAndAnswers(String email, List<ForgotPasswordsQuestionsUsersAnswers> answers) {
Users user = usersRepository.findByEmail(email);
if (user == null) {
throw new CustomException("The user email doesn't exist", HttpStatus.NOT_FOUND);
}
int correctAnswers = 0;
for (ForgotPasswordQuestionsUsersAnswers answer : answers) {
ForgotPasswordQuestionsUsersAnswers answerComparison = forgotPasswordQuestionsUsersAnswersRepository.findByForgotPasswordQuestionsAndUserid(answer.getForgotpasswordquestionsid(), answer.getUsersid());
for (ForgotPasswordsQuestionsUsersAnswers answer : answers) {
ForgotPasswordsQuestionsUsersAnswers answerComparison = forgotPasswordsQuestionsUsersAnswersRepository.findByForgotPasswordQuestionsAndUserid(answer.getForgotpasswordquestionsid(), answer.getUsersid());
if (answerComparison == null) {
throw new CustomException("The comparison answer doesn't exist", HttpStatus.NOT_FOUND);
}

8
src/test/java/org/waterproofingdata/wpdauth/integrationtest/ForgotPasswordServiceIntegrationTest.java

@ -7,17 +7,17 @@ import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.waterproofingdata.wpdauth.model.ForgotPasswordQuestions;
import org.waterproofingdata.wpdauth.service.ForgotPasswordService;
import org.waterproofingdata.wpdauth.model.ForgotPasswordsQuestions;
import org.waterproofingdata.wpdauth.service.ForgotPasswordsService;
@SpringBootTest
public class ForgotPasswordServiceIntegrationTest {
@Autowired
private ForgotPasswordService forgotPasswordService;
private ForgotPasswordsService forgotPasswordService;
@Test
public void testFindAllForgotPasswordQuestions() {
List<ForgotPasswordQuestions> r = forgotPasswordService.findAllForgotPasswordQuestions();
List<ForgotPasswordsQuestions> r = forgotPasswordService.findAllForgotPasswordQuestions();
assertTrue(r.size() > 0);
}

Loading…
Cancel
Save