This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from BudgetBuddyDE/4
feat(BB-4): Implement reset-password logic
- Loading branch information
Showing
6 changed files
with
283 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package de.budgetbuddy.backend; | ||
|
||
import de.budgetbuddy.backend.user.User; | ||
import jakarta.annotation.Nullable; | ||
import org.json.JSONObject; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.UUID; | ||
|
||
@Service | ||
public class MailService { | ||
private final Environment environment; | ||
|
||
@Autowired | ||
public MailService(Environment environment) { | ||
this.environment = environment; | ||
} | ||
|
||
@Nullable | ||
public String getMailServiceHost() { | ||
return environment.getProperty("de.budget-buddy.mail-service.address"); | ||
} | ||
|
||
/** | ||
* Triggers the mail service | ||
* @return boolean | ||
*/ | ||
public Boolean trigger(JSONObject payload) { | ||
String mailServiceUrl = getMailServiceHost(); | ||
if (mailServiceUrl == null) { | ||
throw new RuntimeException("Mail-service host-url is not set"); | ||
} | ||
|
||
return WebhookTrigger.send(mailServiceUrl + "/send", payload.toString()); | ||
} | ||
|
||
/** | ||
* Returns the payload for the verification mail | ||
* @return JSONObject | ||
*/ | ||
public static JSONObject getVerificationMailPayload(User user) { | ||
JSONObject payload = new JSONObject(); | ||
payload.put("mail", "welcome"); | ||
payload.put("to", user.getEmail()); | ||
payload.put("uuid", user.getUuid().toString()); | ||
|
||
return payload; | ||
} | ||
|
||
/** | ||
* Returns the payload for the password reset mail | ||
* @return JSONObject | ||
*/ | ||
public static JSONObject getRequestPasswordMailPayload(String email, UUID uuid, UUID otp) { | ||
JSONObject payload = new JSONObject(); | ||
payload.put("mail", "reset_password"); | ||
payload.put("to", email); | ||
payload.put("uuid", uuid.toString()); | ||
payload.put("otp", otp.toString()); | ||
|
||
return payload; | ||
} | ||
|
||
/** | ||
* Returns the payload for the password changed mail | ||
* @return JSONObject | ||
*/ | ||
public static JSONObject getPasswordChangedMailPayload(String email, String name, String targetEmailAddress) { | ||
JSONObject payload = new JSONObject(); | ||
payload.put("mail", "password_changed"); | ||
payload.put("to", email); | ||
payload.put("name", name); | ||
payload.put("targetMailAddress", targetEmailAddress); | ||
|
||
return payload; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/de/budgetbuddy/backend/auth/UserPasswordReset.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package de.budgetbuddy.backend.auth; | ||
|
||
import de.budgetbuddy.backend.user.User; | ||
import jakarta.persistence.*; | ||
import lombok.Data; | ||
import org.hibernate.annotations.ColumnDefault; | ||
|
||
import java.util.Date; | ||
import java.util.UUID; | ||
|
||
@Entity | ||
@Table(name = "user_password_reset", schema = "public") | ||
@Data | ||
public class UserPasswordReset { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id") | ||
private Long id; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "owner") | ||
private User owner; | ||
|
||
@Column(name = "otp") | ||
private UUID otp; | ||
|
||
@Column(name = "used") | ||
private Boolean used; | ||
|
||
@Column(name = "created_at", nullable = false, updatable = false, columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP") | ||
@ColumnDefault("CURRENT_TIMESTAMP") | ||
private Date createdAt = new Date(); | ||
|
||
public UserPasswordReset() {} | ||
|
||
public UserPasswordReset(User user) { | ||
this.owner = user; | ||
this.otp = UUID.randomUUID(); | ||
this.used = false; | ||
} | ||
|
||
public boolean wasUsed() { | ||
return used; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/de/budgetbuddy/backend/auth/UserPasswordResetRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package de.budgetbuddy.backend.auth; | ||
|
||
import de.budgetbuddy.backend.user.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
public interface UserPasswordResetRepository extends JpaRepository<UserPasswordReset, Long> { | ||
Optional<UserPasswordReset> findByOtp(UUID otp); | ||
List<UserPasswordReset> findAllByOwner(User user); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters