Skip to content

Commit 80d287f

Browse files
Don't require email for password users
1 parent 6e552c1 commit 80d287f

File tree

5 files changed

+36
-25
lines changed

5 files changed

+36
-25
lines changed

src/main/java/app/fyreplace/api/data/dev/DataSeeder.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import app.fyreplace.api.data.Subscription;
1616
import app.fyreplace.api.data.User;
1717
import app.fyreplace.api.data.Vote;
18+
import io.quarkus.elytron.security.common.BcryptUtil;
1819
import io.quarkus.runtime.ShutdownEvent;
1920
import io.quarkus.runtime.StartupEvent;
2021
import jakarta.enterprise.context.ApplicationScoped;
@@ -46,8 +47,9 @@ public void onShutdown(@Observes final ShutdownEvent event) {
4647

4748
@Transactional
4849
public void insertData() {
49-
range(0, 20).forEach(i -> createUser("user_" + i, true, i % 2 == 0));
50-
range(0, 10).forEach(i -> createUser("user_inactive_" + i, false, false));
50+
createUser("demo", true, false, true);
51+
range(0, 20).forEach(i -> createUser("user_" + i, true, i % 2 == 0, false));
52+
range(0, 10).forEach(i -> createUser("user_inactive_" + i, false, false, false));
5153
final var user = User.findByUsername("user_0");
5254
range(0, 20).forEach(i -> createPost(user, "Post " + i, true, false));
5355
range(0, 20).forEach(i -> createPost(user, "Draft " + i, false, false));
@@ -76,7 +78,8 @@ public void deleteData() {
7678
}
7779

7880
@Transactional(Transactional.TxType.REQUIRES_NEW)
79-
public User createUser(final String username, final boolean active, final boolean hasAvatar) {
81+
public User createUser(
82+
final String username, final boolean active, final boolean hasAvatar, final boolean usesPassword) {
8083
final var user = new User();
8184
user.username = username;
8285
user.active = active;
@@ -94,14 +97,21 @@ public User createUser(final String username, final boolean active, final boolea
9497
}
9598
}
9699

97-
final var email = new Email();
98-
email.user = user;
99-
email.email = username + "@example.org";
100-
email.verified = active;
101-
email.persist();
100+
if (usesPassword) {
101+
final var password = new Password();
102+
password.user = user;
103+
password.password = BcryptUtil.bcryptHash(username + "_password");
104+
password.persist();
105+
} else {
106+
final var email = new Email();
107+
email.user = user;
108+
email.email = username + "@example.org";
109+
email.verified = active;
110+
email.persist();
111+
user.mainEmail = email;
112+
user.persist();
113+
}
102114

103-
user.mainEmail = email;
104-
user.persist();
105115
return user;
106116
}
107117

src/main/java/app/fyreplace/api/endpoints/TokensEndpoint.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ public final class TokensEndpoint {
6060
@CacheResult(cacheName = "requests", keyGenerator = DuplicateRequestKeyGenerator.class)
6161
public Response createToken(@NotNull @Valid final TokenCreation input) {
6262
final var email = getEmail(input.identifier());
63-
final var password = Password.<Password>find("user", email.user).firstResult();
63+
final var password =
64+
Password.<Password>find("user.username", input.identifier()).firstResult();
6465
final RandomCode randomCode;
66+
final User user;
6567

6668
try (final var stream = RandomCode.<RandomCode>stream("email", email)) {
6769
randomCode = stream.filter(rc -> BcryptUtil.matches(input.secret(), rc.code))
@@ -71,18 +73,16 @@ public Response createToken(@NotNull @Valid final TokenCreation input) {
7173

7274
if (randomCode != null) {
7375
randomCode.validateEmail();
76+
user = email.user;
7477
} else if (password != null && BcryptUtil.matches(input.secret(), password.password)) {
75-
email.verified = true;
76-
email.persist();
78+
user = password.user;
7779
} else {
7880
throw new BadRequestException();
7981
}
8082

81-
email.user.active = true;
82-
email.user.persist();
83-
return Response.status(Status.CREATED)
84-
.entity(jwtService.makeJwt(email.user))
85-
.build();
83+
user.active = true;
84+
user.persist();
85+
return Response.status(Status.CREATED).entity(jwtService.makeJwt(user)).build();
8686
}
8787

8888
@GET
@@ -118,7 +118,7 @@ public Response createNewToken(
118118
@NotNull @Valid final NewTokenCreation input,
119119
@QueryParam("customDeepLinks") final boolean customDeepLinks) {
120120
final var email = getEmail(input.identifier());
121-
final var hasPassword = Password.count("user", email.user) > 0;
121+
final var hasPassword = Password.count("user.username", input.identifier()) > 0;
122122

123123
if (hasPassword) {
124124
throw new ForbiddenException("user_has_password");

src/test/java/app/fyreplace/api/testing/UserTestsBase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public class UserTestsBase extends TransactionalTestsBase {
1111
@Override
1212
public void beforeEach() {
1313
super.beforeEach();
14-
range(0, getActiveUserCount()).forEach(i -> dataSeeder.createUser("user_" + i, true, false));
15-
range(0, getInactiveUserCount()).forEach(i -> dataSeeder.createUser("user_inactive_" + i, false, false));
14+
range(0, getActiveUserCount()).forEach(i -> dataSeeder.createUser("user_" + i, true, false, false));
15+
range(0, getInactiveUserCount()).forEach(i -> dataSeeder.createUser("user_inactive_" + i, false, false, false));
1616
}
1717

1818
public int getActiveUserCount() {

src/test/java/app/fyreplace/api/testing/endpoints/tokens/CreateNewTokenTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static java.util.Objects.requireNonNull;
66
import static org.junit.jupiter.api.Assertions.assertEquals;
77

8+
import app.fyreplace.api.data.Email;
89
import app.fyreplace.api.data.NewTokenCreation;
910
import app.fyreplace.api.data.Password;
1011
import app.fyreplace.api.data.User;
@@ -51,9 +52,10 @@ public void createNewTokenWhileUserHasPassword() {
5152
password.user = user;
5253
password.password = BcryptUtil.bcryptHash("password");
5354
password.persist();
55+
Email.delete("user", user);
5456
});
5557
given().contentType(ContentType.JSON)
56-
.body(new NewTokenCreation(user.mainEmail.email))
58+
.body(new NewTokenCreation(user.username))
5759
.post("new")
5860
.then()
5961
.statusCode(403);

src/test/java/app/fyreplace/api/testing/endpoints/tokens/CreateTokenTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,13 @@ public void createTokenWithPassword() {
100100
final var randomCodeCount = RandomCode.count();
101101
assertFalse(password.user.mainEmail.verified);
102102
given().contentType(ContentType.JSON)
103-
.body(new TokenCreation(password.user.mainEmail.email, "password"))
103+
.body(new TokenCreation(password.user.username, "password"))
104104
.post()
105105
.then()
106106
.statusCode(201)
107107
.contentType(ContentType.TEXT)
108108
.body(isA(String.class));
109109
assertEquals(randomCodeCount, RandomCode.count());
110-
final var email = Email.<Email>find("id", password.user.mainEmail.id).firstResult();
111-
assertTrue(email.verified);
112110
}
113111

114112
@Test
@@ -165,6 +163,7 @@ public void beforeEach() {
165163
password.user = User.findByUsername("user_inactive_1");
166164
password.password = BcryptUtil.bcryptHash("password");
167165
password.persist();
166+
Email.delete("user", password.user);
168167
}
169168

170169
private RandomCode makeRandomCode(final String username, final String code) {

0 commit comments

Comments
 (0)