brevo smtp server (300 mails/day free)
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
Test written for changing Password based on otp some other stuff
This commit is contained in:
@@ -14,12 +14,15 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.mail.MailException;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.security.Principal;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/auth/user")
|
||||
public class UserAuthenticationController {
|
||||
@@ -77,7 +80,7 @@ public class UserAuthenticationController {
|
||||
}
|
||||
|
||||
@PostMapping("/request-password-forgot")
|
||||
public ResponseEntity<String> handleForgotPasswordRequest(@RequestBody String email){
|
||||
public ResponseEntity<String> handleForgotPasswordRequest(@RequestBody String email, Principal principal){
|
||||
System.out.println("####################### Handle Password Reset Request #######################");
|
||||
System.out.println(email);
|
||||
|
||||
@@ -86,6 +89,9 @@ public class UserAuthenticationController {
|
||||
return ResponseEntity.ok("OTP Token erstellt");
|
||||
} catch (UsernameNotFoundException e) {
|
||||
return ResponseEntity.badRequest().body(e.getMessage());
|
||||
} catch(MailException e){
|
||||
System.out.println(" ## MAIL EXCEPTION \n" +e.getMessage());
|
||||
return ResponseEntity.internalServerError().body("Fehler beim versenden der Email, versuche es später erneut");
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.internalServerError().body("Fehler beim erstellen des OTP Tokens");
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package SuMoC.backend.security.dtos;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class PasswordResetDTO {
|
||||
String otp_code;
|
||||
String email;
|
||||
|
||||
@@ -15,6 +15,7 @@ import jakarta.transaction.Transactional;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.mail.MailException;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
@@ -132,7 +133,7 @@ public class AuthenticationService {
|
||||
* @param email the account for which a OTP shall be created to reset the password
|
||||
*/
|
||||
@Transactional
|
||||
public void generateNewPasswordResetTokenForUser(String email) throws UsernameNotFoundException{
|
||||
public void generateNewPasswordResetTokenForUser(String email) throws UsernameNotFoundException, MailException{
|
||||
if (!userRepository.existsByEmail(email)) throw new UsernameNotFoundException("Es existiert kein Account mit dieser Email") ;
|
||||
|
||||
Optional<PasswordResetToken> token = passwordResetTokenRepository.findByEmail(email);
|
||||
@@ -145,12 +146,15 @@ public class AuthenticationService {
|
||||
PasswordResetToken newToken = new PasswordResetToken(email, otp);
|
||||
passwordResetTokenRepository.save(newToken);
|
||||
|
||||
|
||||
emailService.sendPasswordResetEmail(newToken.getEmail(), newToken.getOtp_code());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void resetPassword(PasswordResetDTO passwordResetDTO) throws InvalidTokenException, UsernameNotFoundException {
|
||||
|
||||
System.out.println("## RESETING PASSWORD FOR: " + passwordResetDTO.getEmail() + " with code " + passwordResetDTO.getOtp_code() );
|
||||
|
||||
PasswordResetToken token = passwordResetTokenRepository.findByEmail(passwordResetDTO.getEmail())
|
||||
.orElseThrow(() -> new InvalidTokenException("Token nicht gefunden"));
|
||||
|
||||
@@ -172,8 +176,10 @@ public class AuthenticationService {
|
||||
|
||||
private String generate_six_digit_OTP(){
|
||||
SecureRandom random = new SecureRandom();
|
||||
int code = 100000 + random.nextInt(900000);
|
||||
return String.valueOf(code);
|
||||
// Generiert eine Zahl von 0 bis 999.999
|
||||
int code = random.nextInt(1000000);
|
||||
// Formatiert die Zahl auf 6 Stellen und füllt links mit 0 auf
|
||||
return String.format("%06d", code);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user