From 96f8979b209cb28cd7782eb531dd3d97e45ecd0a Mon Sep 17 00:00:00 2001 From: Vladik Date: Fri, 25 Oct 2024 12:57:11 +0300 Subject: [PATCH] task: updated ActivateAccountService.sendVerifyEmail for sendMessage of gmail and delete UUID - version beta --- .../security/SecurityJwtProperties.java | 1 + .../dto/common/EmailMessageInfoDto.java | 3 +- .../com/chat/yourway/security/JwtService.java | 9 +++++- .../service/ActivateAccountService.java | 31 +++++-------------- .../service/EmailMessageFactoryService.java | 19 +++++++----- src/main/resources/application.properties | 1 + 6 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/chat/yourway/config/security/SecurityJwtProperties.java b/src/main/java/com/chat/yourway/config/security/SecurityJwtProperties.java index 7bbd2f9..032d3e8 100644 --- a/src/main/java/com/chat/yourway/config/security/SecurityJwtProperties.java +++ b/src/main/java/com/chat/yourway/config/security/SecurityJwtProperties.java @@ -15,4 +15,5 @@ public class SecurityJwtProperties { private String secretKey; private Duration accessExpiration; private Duration refreshExpiration; + private Duration emailExpiration; } \ No newline at end of file diff --git a/src/main/java/com/chat/yourway/dto/common/EmailMessageInfoDto.java b/src/main/java/com/chat/yourway/dto/common/EmailMessageInfoDto.java index 2619b91..59c439f 100644 --- a/src/main/java/com/chat/yourway/dto/common/EmailMessageInfoDto.java +++ b/src/main/java/com/chat/yourway/dto/common/EmailMessageInfoDto.java @@ -1,11 +1,12 @@ package com.chat.yourway.dto.common; import com.chat.yourway.model.enums.EmailMessageType; +import lombok.Builder; import java.util.UUID; +@Builder public record EmailMessageInfoDto(String username, String email, - UUID uuidToken, String path, EmailMessageType emailMessageType) { } \ No newline at end of file diff --git a/src/main/java/com/chat/yourway/security/JwtService.java b/src/main/java/com/chat/yourway/security/JwtService.java index 9a99b29..82b7d01 100644 --- a/src/main/java/com/chat/yourway/security/JwtService.java +++ b/src/main/java/com/chat/yourway/security/JwtService.java @@ -7,7 +7,6 @@ import com.chat.yourway.exception.InvalidTokenException; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; -import io.jsonwebtoken.SignatureAlgorithm; import io.jsonwebtoken.io.Decoders; import io.jsonwebtoken.security.Keys; import jakarta.servlet.http.HttpServletRequest; @@ -40,6 +39,10 @@ public String generateRefreshToken(UserDetails userDetails) { return generateRefreshTokenBuild(new HashMap<>(), userDetails); } + public String generateEmailToken(UserDetails userDetails) { + return generateEmailTokenBuild(new HashMap<>(), userDetails); + } + public String extractToken(HttpServletRequest request) { var token = request.getHeader(AUTHORIZATION); @@ -70,6 +73,10 @@ private String generateRefreshTokenBuild(Map extraClaims, UserDe return buildToken(extraClaims, userDetails, jwtProperties.getRefreshExpiration().toMillis()); } + private String generateEmailTokenBuild(Map extraClaims, UserDetails userDetails) { + return buildToken(extraClaims, userDetails, jwtProperties.getEmailExpiration().toMillis()); + } + private boolean isNotValidTokenType(String token) { return token == null || !token.startsWith(BEARER); } diff --git a/src/main/java/com/chat/yourway/service/ActivateAccountService.java b/src/main/java/com/chat/yourway/service/ActivateAccountService.java index 4dca734..02dfe8d 100644 --- a/src/main/java/com/chat/yourway/service/ActivateAccountService.java +++ b/src/main/java/com/chat/yourway/service/ActivateAccountService.java @@ -1,17 +1,12 @@ package com.chat.yourway.service; import com.chat.yourway.dto.common.EmailMessageInfoDto; -import com.chat.yourway.exception.EmailTokenNotFoundException; import com.chat.yourway.model.Contact; -import com.chat.yourway.model.EmailToken; -import com.chat.yourway.repository.jpa.EmailTokenRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.UUID; - import static com.chat.yourway.model.enums.EmailMessageType.ACTIVATE; @Service @@ -20,7 +15,6 @@ public class ActivateAccountService { private final EmailSenderService emailSenderService; - private final EmailTokenRepository emailTokenRepository; private final EmailMessageFactoryService emailMessageFactoryService; private final ContactService contactService; @@ -36,28 +30,19 @@ public void activateAccount() { } public void sendVerifyEmail(Contact contact, String clientHost) { - log.trace("Started sendVerifyEmail by contact email [{}], and client host [{}]", - contact.getEmail(), clientHost); + log.trace("Started sendVerifyEmail by contact email [{}], and client host [{}]", contact.getEmail(), clientHost); - EmailToken emailToken = saveEmailToken(contact); + final var emailMessageInfoDto = EmailMessageInfoDto.builder() + .username(contact.getNickname()) + .email(contact.getEmail()) + .path(clientHost) + .emailMessageType(ACTIVATE) + .build(); - var emailMessageInfoDto = new EmailMessageInfoDto(contact.getNickname(), contact.getEmail(), - emailToken.getToken(), clientHost, ACTIVATE); - var emailMessage = emailMessageFactoryService.generateEmailMessage(emailMessageInfoDto); + final var emailMessage = emailMessageFactoryService.generateEmailMessage(emailMessageInfoDto); emailSenderService.sendEmail(emailMessage); log.info("Verifying account email was sent to contact email [{}]", contact.getEmail()); } - - private EmailToken saveEmailToken(Contact contact) { - EmailToken emailToken = EmailToken.builder() - .contact(contact) - .messageType(ACTIVATE) - .build(); - - emailTokenRepository.save(emailToken); - return emailToken; - } - } diff --git a/src/main/java/com/chat/yourway/service/EmailMessageFactoryService.java b/src/main/java/com/chat/yourway/service/EmailMessageFactoryService.java index 7757a3f..e42023c 100644 --- a/src/main/java/com/chat/yourway/service/EmailMessageFactoryService.java +++ b/src/main/java/com/chat/yourway/service/EmailMessageFactoryService.java @@ -3,21 +3,26 @@ import com.chat.yourway.dto.common.EmailMessageDto; import com.chat.yourway.dto.common.EmailMessageInfoDto; import com.chat.yourway.model.enums.EmailMessageType; +import com.chat.yourway.security.JwtService; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; -import java.util.UUID; - import static com.chat.yourway.utils.Constants.TOKEN_PARAMETER; -@Service @Slf4j +@Service +@RequiredArgsConstructor public class EmailMessageFactoryService { + + private final JwtService jwtService; + private final ContactService contactService; + public EmailMessageDto generateEmailMessage(EmailMessageInfoDto emailMessageInfoDto) { final var path = emailMessageInfoDto.path(); - final var uuidToken = emailMessageInfoDto.uuidToken(); final var emailMessageType = emailMessageInfoDto.emailMessageType(); - final var link = generateLink(path, uuidToken, emailMessageType); + final var contact = contactService.findByEmail(emailMessageInfoDto.email()); + final var link = generateLink(path, jwtService.generateEmailToken(contact), emailMessageType); log.info("Generated link: {}", link); @@ -28,7 +33,7 @@ public EmailMessageDto generateEmailMessage(EmailMessageInfoDto emailMessageInfo return new EmailMessageDto(emailMessageInfoDto.email(), emailMessageType.getSubject(), messageBody); } - private String generateLink(String path, UUID uuidToken, EmailMessageType emailMessageType) { - return path + emailMessageType.getEmailType() + TOKEN_PARAMETER + uuidToken; + private String generateLink(String path, String token, EmailMessageType emailMessageType) { + return path + emailMessageType.getEmailType() + TOKEN_PARAMETER + token; } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index a231126..f9e2968 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -37,6 +37,7 @@ security.jwt.secret-key=${SECRET_KEY} security.jwt.access-expiration=60m # 7 days security.jwt.refresh-expiration=10080m +security.jwt.email-expiration=120m #CORS cors.allowed-origins=${CORS_ORIGINS:http://localhost:8080,http://localhost:5557,http://localhost:3000,\