-
Notifications
You must be signed in to change notification settings - Fork 300
Add password reset Java object #2765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
150 changes: 150 additions & 0 deletions
150
core/src/main/java/google/registry/model/console/PasswordResetRequest.java
This file contains hidden or 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,150 @@ | ||
| // Copyright 2025 The Nomulus Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package google.registry.model.console; | ||
|
|
||
| import static google.registry.util.PreconditionsUtils.checkArgumentNotNull; | ||
|
|
||
| import google.registry.model.Buildable; | ||
| import google.registry.model.CreateAutoTimestamp; | ||
| import google.registry.model.ImmutableObject; | ||
| import google.registry.persistence.WithVKey; | ||
| import jakarta.persistence.AttributeOverride; | ||
| import jakarta.persistence.AttributeOverrides; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| import jakarta.persistence.Id; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import org.joda.time.DateTime; | ||
|
|
||
| /** | ||
| * Represents a password reset request of some type. | ||
| * | ||
| * <p>Password reset requests must be performed within an hour of the time that they were requested, | ||
| * as well as requiring that the requester and the fulfiller have the proper respective permissions. | ||
| */ | ||
| @Entity | ||
| @WithVKey(String.class) | ||
| public class PasswordResetRequest extends ImmutableObject implements Buildable { | ||
|
|
||
| public enum Type { | ||
| EPP, | ||
| REGISTRY_LOCK | ||
| } | ||
|
|
||
| @Id private String verificationCode; | ||
|
|
||
| @Column(nullable = false) | ||
| @Enumerated(EnumType.STRING) | ||
| Type type; | ||
|
|
||
| @AttributeOverrides({ | ||
| @AttributeOverride( | ||
| name = "creationTime", | ||
| column = @Column(name = "requestTime", nullable = false)) | ||
| }) | ||
| CreateAutoTimestamp requestTime = CreateAutoTimestamp.create(null); | ||
|
|
||
| @Column(nullable = false) | ||
| String requester; | ||
|
|
||
| @Column DateTime fulfillmentTime; | ||
|
|
||
| @Column(nullable = false) | ||
| String destinationEmail; | ||
|
|
||
| @Column(nullable = false) | ||
| String registrarId; | ||
|
|
||
| public String getVerificationCode() { | ||
| return verificationCode; | ||
| } | ||
|
|
||
| public Type getType() { | ||
| return type; | ||
| } | ||
|
|
||
| public DateTime getRequestTime() { | ||
| return requestTime.getTimestamp(); | ||
| } | ||
|
|
||
| public String getRequester() { | ||
| return requester; | ||
| } | ||
|
|
||
| public Optional<DateTime> getFulfillmentTime() { | ||
| return Optional.ofNullable(fulfillmentTime); | ||
| } | ||
|
|
||
| public String getDestinationEmail() { | ||
| return destinationEmail; | ||
| } | ||
|
|
||
| public String getRegistrarId() { | ||
| return registrarId; | ||
| } | ||
|
|
||
| @Override | ||
| public Builder asBuilder() { | ||
| return new Builder(clone(this)); | ||
| } | ||
|
|
||
| /** Builder for constructing immutable {@link PasswordResetRequest} objects. */ | ||
| public static class Builder extends Buildable.Builder<PasswordResetRequest> { | ||
|
|
||
| public Builder() {} | ||
|
|
||
| private Builder(PasswordResetRequest instance) { | ||
| super(instance); | ||
| } | ||
|
|
||
| @Override | ||
| public PasswordResetRequest build() { | ||
| checkArgumentNotNull(getInstance().type, "Type must be specified"); | ||
| checkArgumentNotNull(getInstance().requester, "Requester must be specified"); | ||
| checkArgumentNotNull(getInstance().destinationEmail, "Destination email must be specified"); | ||
| checkArgumentNotNull(getInstance().registrarId, "Registrar ID must be specified"); | ||
| getInstance().verificationCode = UUID.randomUUID().toString(); | ||
| return super.build(); | ||
| } | ||
|
|
||
| public Builder setType(Type type) { | ||
| getInstance().type = type; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder setRequester(String requester) { | ||
| getInstance().requester = requester; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder setDestinationEmail(String destinationEmail) { | ||
| getInstance().destinationEmail = destinationEmail; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder setRegistrarId(String registrarId) { | ||
| getInstance().registrarId = registrarId; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder setFulfillmentTime(DateTime fulfillmentTime) { | ||
| getInstance().fulfillmentTime = fulfillmentTime; | ||
| return this; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
65 changes: 65 additions & 0 deletions
65
core/src/test/java/google/registry/model/console/PasswordResetRequestTest.java
This file contains hidden or 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,65 @@ | ||
| // Copyright 2025 The Nomulus Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package google.registry.model.console; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
| import static google.registry.model.ImmutableObjectSubject.assertAboutImmutableObjects; | ||
| import static google.registry.testing.DatabaseHelper.persistResource; | ||
| import static org.junit.Assert.assertThrows; | ||
|
|
||
| import google.registry.model.EntityTestCase; | ||
| import google.registry.persistence.VKey; | ||
| import google.registry.testing.DatabaseHelper; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** Tests for {@link PasswordResetRequest}. */ | ||
| public class PasswordResetRequestTest extends EntityTestCase { | ||
|
|
||
| PasswordResetRequestTest() { | ||
| super(JpaEntityCoverageCheck.ENABLED); | ||
| } | ||
|
|
||
| @Test | ||
| void testSuccess_persistence() { | ||
| PasswordResetRequest request = | ||
| new PasswordResetRequest.Builder() | ||
| .setRequester("requestor@email.tld") | ||
| .setDestinationEmail("destination@email.tld") | ||
| .setType(PasswordResetRequest.Type.EPP) | ||
| .setRegistrarId("TheRegistrar") | ||
| .build(); | ||
| String verificationCode = request.getVerificationCode(); | ||
| assertThat(verificationCode).isNotEmpty(); | ||
| persistResource(request); | ||
| PasswordResetRequest fromDatabase = | ||
| DatabaseHelper.loadByKey(VKey.create(PasswordResetRequest.class, verificationCode)); | ||
| assertAboutImmutableObjects().that(fromDatabase).isEqualExceptFields(request, "requestTime"); | ||
| assertThat(fromDatabase.getRequestTime()).isEqualTo(fakeClock.nowUtc()); | ||
| } | ||
|
|
||
| @Test | ||
| void testFailure_nullFields() { | ||
| PasswordResetRequest.Builder builder = new PasswordResetRequest.Builder(); | ||
| assertThrows(IllegalArgumentException.class, builder::build); | ||
| builder.setType(PasswordResetRequest.Type.EPP); | ||
| assertThrows(IllegalArgumentException.class, builder::build); | ||
| builder.setRequester("foobar@email.tld"); | ||
| assertThrows(IllegalArgumentException.class, builder::build); | ||
| builder.setDestinationEmail("email@email.tld"); | ||
| assertThrows(IllegalArgumentException.class, builder::build); | ||
| builder.setRegistrarId("TheRegistrar"); | ||
| builder.build(); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
No clone method Error