From 9827f81230b17c5cee598eb51f7624536edfd19e Mon Sep 17 00:00:00 2001 From: csimonis Date: Thu, 15 May 2025 11:19:46 +0200 Subject: [PATCH] wip: stuff --- .../szut/casino/security/AuthController.java | 6 + .../casino/security/service/AuthService.java | 17 +- .../casino/security/service/EmailService.java | 17 ++ .../java/de/szut/casino/user/UserService.java | 4 + .../templates/email/recover-password.html | 160 ++++++++++++++++++ 5 files changed, 202 insertions(+), 2 deletions(-) create mode 100644 backend/src/main/resources/templates/email/recover-password.html diff --git a/backend/src/main/java/de/szut/casino/security/AuthController.java b/backend/src/main/java/de/szut/casino/security/AuthController.java index f833d78..5d8717e 100644 --- a/backend/src/main/java/de/szut/casino/security/AuthController.java +++ b/backend/src/main/java/de/szut/casino/security/AuthController.java @@ -43,4 +43,10 @@ public class AuthController { return ResponseEntity.ok().build(); } + + @PostMapping("/recover-password") + public ResponseEntity recoverPassword(@RequestParam("email") String email) throws MessagingException, IOException { + authService.recoverPassword(email); + return ResponseEntity.ok().build(); + } } diff --git a/backend/src/main/java/de/szut/casino/security/service/AuthService.java b/backend/src/main/java/de/szut/casino/security/service/AuthService.java index f51ff83..380b5e4 100644 --- a/backend/src/main/java/de/szut/casino/security/service/AuthService.java +++ b/backend/src/main/java/de/szut/casino/security/service/AuthService.java @@ -16,6 +16,7 @@ import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; +import javax.swing.text.html.Option; import java.io.IOException; import java.util.Optional; @@ -46,7 +47,7 @@ public class AuthService { SecurityContextHolder.getContext().setAuthentication(authentication); String jwt = jwtUtils.generateToken(authentication); - + return new AuthResponseDto(jwt); } @@ -66,7 +67,7 @@ public class AuthService { public Boolean verifyEmail(String token) throws MessagingException, IOException { Optional optionalUser = userService.getUserByVerificationToken(token); - if(!optionalUser.isPresent()) { + if (!optionalUser.isPresent()) { return false; } @@ -79,4 +80,16 @@ public class AuthService { return true; } + + public void recoverPassword(String email) { + Optional optionalUser = userService.getUserByEmail(email); + + if (optionalUser.isPresent()) { + UserEntity user = optionalUser.get(); + String token = jwtUtils.generateToken(user.getUsername()); + user.setVerificationToken(token); + userService.saveUser(user); + this.emailService.sendPasswordRecoveryEmail(user); + } + } } diff --git a/backend/src/main/java/de/szut/casino/security/service/EmailService.java b/backend/src/main/java/de/szut/casino/security/service/EmailService.java index 4d83262..b7a05b9 100644 --- a/backend/src/main/java/de/szut/casino/security/service/EmailService.java +++ b/backend/src/main/java/de/szut/casino/security/service/EmailService.java @@ -87,6 +87,23 @@ public class EmailService { mailSender.send(message); } + public void sendPasswordRecoveryEmail(UserEntity user) throws IOException, MessagingException { + String template = loadTemplate("email/recover-password.html"); + String htmlContent = template + .replace("${username}", user.getUsername()) + .replace("${feUrl}", feUrl); + + MimeMessage message = mailSender.createMimeMessage(); + MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); + + helper.setFrom(mailConfig.fromAddress); + helper.setTo(user.getEmailAddress()); + helper.setSubject("Zurücksetzen ihres Passworts"); + helper.setText(htmlContent, true); + + mailSender.send(message); + } + private String loadTemplate(String templatePath) throws IOException { ClassPathResource resource = new ClassPathResource("templates/" + templatePath); try (Reader reader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) { diff --git a/backend/src/main/java/de/szut/casino/user/UserService.java b/backend/src/main/java/de/szut/casino/user/UserService.java index 9113864..a04b02e 100644 --- a/backend/src/main/java/de/szut/casino/user/UserService.java +++ b/backend/src/main/java/de/szut/casino/user/UserService.java @@ -62,4 +62,8 @@ public class UserService { return optionalUser.get().getEmailVerified(); } + + public Optional getUserByEmail(String email) { + return userRepository.findByEmail(email); + } } diff --git a/backend/src/main/resources/templates/email/recover-password.html b/backend/src/main/resources/templates/email/recover-password.html new file mode 100644 index 0000000..6d4e1a1 --- /dev/null +++ b/backend/src/main/resources/templates/email/recover-password.html @@ -0,0 +1,160 @@ + + + + + + Passwort zurücksetzen - Trustworthy Casino© + + + +
+
+

Trustworthy Casino

+
+
+

Hallo ${username},

+ +

wir haben eine Anfrage zum Zurücksetzen Ihres Passworts für Ihr Trustworthy Casino Konto erhalten. Um Ihr Passwort zurückzusetzen, klicken Sie bitte auf den folgenden Button:

+ + + +

Alternativ können Sie auch diesen Code verwenden:

+ +
${resetCode}
+ +
+

Hinweis: Dieser Link und Code sind aus Sicherheitsgründen nur 60 Minuten gültig.

+
+ +
+ +
+

Falls Sie diese Anfrage nicht gestellt haben, ignorieren Sie diese E-Mail bitte. In diesem Fall empfehlen wir Ihnen, Ihr Passwort zu ändern und unseren Kundenservice zu kontaktieren, um die Sicherheit Ihres Kontos zu gewährleisten.

+
+ +
+ +

Bei Fragen steht Ihnen unser Support-Team jederzeit zur Verfügung.

+ +

Mit freundlichen Grüßen,
+ Ihr Trustworthy Casino Team

+
+ +
+ + \ No newline at end of file