|
2 | 2 |
|
3 | 3 | import dev.matheuscruz.domain.User; |
4 | 4 | import dev.matheuscruz.domain.UserRepository; |
| 5 | +import dev.matheuscruz.infra.security.AESAdapter; |
5 | 6 | import dev.matheuscruz.infra.security.BCryptAdapter; |
6 | 7 | import dev.matheuscruz.infra.security.Groups; |
7 | 8 | import io.quarkus.panache.common.Parameters; |
8 | 9 | import io.smallrye.jwt.build.Jwt; |
| 10 | +import jakarta.persistence.EntityManager; |
| 11 | +import jakarta.persistence.NoResultException; |
| 12 | +import jakarta.transaction.Transactional; |
9 | 13 | import jakarta.validation.Valid; |
10 | 14 | import jakarta.validation.constraints.Email; |
11 | 15 | import jakarta.validation.constraints.NotBlank; |
|
15 | 19 | import jakarta.ws.rs.Path; |
16 | 20 | import jakarta.ws.rs.core.Response; |
17 | 21 | import java.time.Duration; |
| 22 | +import java.util.Optional; |
18 | 23 | import java.util.Set; |
19 | 24 |
|
20 | 25 | @Path("/api/sign-in") |
21 | 26 | public class SignInResource { |
22 | 27 |
|
23 | 28 | UserRepository userRepository; |
| 29 | + AESAdapter aesAdapter; |
| 30 | + EntityManager entityManager; |
24 | 31 |
|
25 | | - public SignInResource(UserRepository userRepository) { |
| 32 | + public SignInResource(UserRepository userRepository, AESAdapter aesAdapter, EntityManager entityManager) { |
26 | 33 | this.userRepository = userRepository; |
| 34 | + this.aesAdapter = aesAdapter; |
| 35 | + this.entityManager = entityManager; |
27 | 36 | } |
28 | 37 |
|
29 | 38 | @POST |
| 39 | + @Transactional |
30 | 40 | public Response signIn(@Valid SignInRequest req) { |
31 | 41 |
|
32 | | - User user = userRepository.find("email = :email", Parameters.with("email", req.email())).firstResultOptional() |
33 | | - .orElseThrow(ForbiddenException::new); |
| 42 | + Optional<User> userOptional = userRepository.find("email = :email", Parameters.with("email", req.email())) |
| 43 | + .firstResultOptional(); |
| 44 | + |
| 45 | + User user = null; |
| 46 | + |
| 47 | + if (userOptional.isPresent()) { |
| 48 | + user = userOptional.get(); |
| 49 | + } else { |
| 50 | + // try to find by plain text |
| 51 | + try { |
| 52 | + user = (User) entityManager.createNativeQuery("SELECT * FROM users WHERE email = :email", User.class) |
| 53 | + .setParameter("email", req.email()).getSingleResult(); |
| 54 | + |
| 55 | + // if found, we need to migrate the user to the new encryption format |
| 56 | + // but first we need to check the password |
| 57 | + Boolean checked = BCryptAdapter.checkPassword(req.password(), user.getPassword()); |
| 58 | + |
| 59 | + if (!checked) { |
| 60 | + return Response.status(Response.Status.UNAUTHORIZED).build(); |
| 61 | + } |
| 62 | + |
| 63 | + String encryptedEmail = aesAdapter.encrypt(req.email()); |
| 64 | + String encryptedPhone = user.getPhoneNumber() != null ? aesAdapter.encrypt(user.getPhoneNumber()) |
| 65 | + : null; |
| 66 | + |
| 67 | + entityManager.createNativeQuery("UPDATE users SET email = :email, phone_number = :phone WHERE id = :id") |
| 68 | + .setParameter("email", encryptedEmail).setParameter("phone", encryptedPhone) |
| 69 | + .setParameter("id", user.getId()).executeUpdate(); |
| 70 | + |
| 71 | + } catch (NoResultException e) { |
| 72 | + throw new ForbiddenException(); |
| 73 | + } catch (Exception e) { |
| 74 | + throw new RuntimeException(e); |
| 75 | + } |
| 76 | + } |
34 | 77 |
|
35 | 78 | Boolean checked = BCryptAdapter.checkPassword(req.password(), user.getPassword()); |
36 | 79 |
|
|
0 commit comments