ddangelorb
3 years ago
9 changed files with 191 additions and 9 deletions
-
9db/ddl.sql
-
6pom.xml
-
42src/main/java/org/waterproofingdata/wpdauth/controller/ForgotPasswordController.java
-
30src/main/java/org/waterproofingdata/wpdauth/model/ForgotPasswordKeys.java
-
2src/main/java/org/waterproofingdata/wpdauth/model/Users.java
-
10src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordKeysRepository.java
-
4src/main/java/org/waterproofingdata/wpdauth/repository/UsersRepository.java
-
83src/main/java/org/waterproofingdata/wpdauth/service/ForgotPasswordService.java
-
14src/main/java/org/waterproofingdata/wpdauth/service/UsersService.java
@ -0,0 +1,42 @@ |
|||
package org.waterproofingdata.wpdauth.controller; |
|||
|
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.PostMapping; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RequestParam; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
import org.waterproofingdata.wpdauth.service.ForgotPasswordService; |
|||
|
|||
import io.swagger.annotations.Api; |
|||
import io.swagger.annotations.ApiOperation; |
|||
import io.swagger.annotations.ApiParam; |
|||
import io.swagger.annotations.ApiResponse; |
|||
import io.swagger.annotations.ApiResponses; |
|||
|
|||
@RestController |
|||
@RequestMapping("/forgotpassword") |
|||
@Api(tags = "forgotpassword") |
|||
public class ForgotPasswordController { |
|||
@Autowired |
|||
private ForgotPasswordService forgotPasswordService; |
|||
|
|||
@PostMapping("/sendkey") |
|||
@ApiOperation(value = "${ForgotPasswordController.sendkey}") |
|||
@ApiResponses(value = {// |
|||
@ApiResponse(code = 400, message = "Something went wrong"), // |
|||
@ApiResponse(code = 422, message = "Invalid email supplied")}) |
|||
public void sendkey(@ApiParam("Email") @RequestParam String email) { |
|||
forgotPasswordService.sendkey(email); |
|||
} |
|||
|
|||
@PostMapping("/login") |
|||
@ApiOperation(value = "${UserController.login}") |
|||
@ApiResponses(value = {// |
|||
@ApiResponse(code = 400, message = "Something went wrong"), // |
|||
@ApiResponse(code = 422, message = "Invalid username/password supplied")}) |
|||
public String login(// |
|||
@ApiParam("Email") @RequestParam String email, // |
|||
@ApiParam("Key") @RequestParam String key) { |
|||
return forgotPasswordService.loginByEmailAndKey(email, key); |
|||
} |
|||
} |
@ -0,0 +1,30 @@ |
|||
package org.waterproofingdata.wpdauth.model; |
|||
|
|||
import javax.persistence.Column; |
|||
import javax.persistence.Entity; |
|||
import javax.persistence.GeneratedValue; |
|||
import javax.persistence.GenerationType; |
|||
import javax.persistence.Id; |
|||
|
|||
import lombok.Getter; |
|||
import lombok.NoArgsConstructor; |
|||
import lombok.Setter; |
|||
|
|||
@Entity(name = "forgotpassword_keys") |
|||
@Getter |
|||
@Setter |
|||
@NoArgsConstructor |
|||
public class ForgotPasswordKeys { |
|||
@Id |
|||
@GeneratedValue(strategy = GenerationType.IDENTITY) |
|||
private Integer id; |
|||
|
|||
@Column(nullable = false) |
|||
private String email; |
|||
|
|||
@Column(nullable = false) |
|||
private String key; |
|||
|
|||
@Column(nullable = false) |
|||
private String created_at; |
|||
} |
@ -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.ForgotPasswordKeys; |
|||
|
|||
public interface ForgotPasswordKeysRepository extends JpaRepository<ForgotPasswordKeys, 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); |
|||
} |
@ -0,0 +1,83 @@ |
|||
package org.waterproofingdata.wpdauth.service; |
|||
|
|||
import java.util.Random; |
|||
|
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.http.HttpStatus; |
|||
import org.springframework.mail.SimpleMailMessage; |
|||
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.Users; |
|||
import org.waterproofingdata.wpdauth.repository.ForgotPasswordKeysRepository; |
|||
import org.waterproofingdata.wpdauth.repository.UsersRepository; |
|||
import org.waterproofingdata.wpdauth.security.JwtTokenProvider; |
|||
|
|||
@Service |
|||
public class ForgotPasswordService { |
|||
@Autowired |
|||
private ForgotPasswordKeysRepository forgotPasswordKeysRepository; |
|||
|
|||
@Autowired |
|||
private UsersRepository usersRepository; |
|||
|
|||
@Autowired |
|||
private PasswordEncoder passwordEncoder; |
|||
|
|||
@Autowired |
|||
private JavaMailSender mailSender; |
|||
|
|||
@Autowired |
|||
private JwtTokenProvider jwtTokenProvider; |
|||
|
|||
public void sendkey(String email) { |
|||
Users user = usersRepository.findByEmail(email); |
|||
if (user == null) { |
|||
throw new CustomException("The user email doesn't exist", HttpStatus.NOT_FOUND); |
|||
} |
|||
|
|||
Random rand = new Random(); |
|||
String key = String.format("%04d", rand.nextInt(10000)); |
|||
//System.out.printf("%04d%n", rand.nextInt(10000)); |
|||
|
|||
ForgotPasswordKeys entity = new ForgotPasswordKeys(); |
|||
entity.setEmail(email); |
|||
entity.setKey(key); |
|||
forgotPasswordKeysRepository.save(entity); |
|||
|
|||
SimpleMailMessage message = new SimpleMailMessage(); |
|||
message.setFrom("noreply@wp6.com"); |
|||
message.setTo(email); |
|||
message.setSubject("Envio de código para alteração de senha"); |
|||
message.setText(String.format("Olá! Segue o código para alterar sua senha:'%s'. Informe esse código no aplicativo para prosseguir.", key)); |
|||
mailSender.send(message); |
|||
} |
|||
|
|||
public String loginByEmailAndKey(String email, String key) { |
|||
ForgotPasswordKeys entity = forgotPasswordKeysRepository.findTodayRecordByEmailANDKey(email, key); |
|||
if (entity == null) { |
|||
throw new CustomException("The email and forgot key don't exist", HttpStatus.NOT_FOUND); |
|||
} |
|||
|
|||
Users user = usersRepository.findByEmail(email); |
|||
if (user == null) { |
|||
throw new CustomException("The user email doesn't exist", HttpStatus.NOT_FOUND); |
|||
} |
|||
|
|||
String username = user.getUsername(); |
|||
return jwtTokenProvider.createToken(username, usersRepository.findByUsername(username).getRoles()); |
|||
} |
|||
|
|||
//public String loginByEmailAndAnswers(String email, List<PasswordAnswers> passwordAnswers) |
|||
|
|||
public void passwordUpdate(String email, String newPassword) { |
|||
Users user = usersRepository.findByEmail(email); |
|||
if (user == null) { |
|||
throw new CustomException("The user email doesn't exist", HttpStatus.NOT_FOUND); |
|||
} |
|||
user.setPassword(passwordEncoder.encode(newPassword)); |
|||
usersRepository.save(user); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue