Skip to content

Commit

Permalink
task: updated ActivateAccountService.sendVerifyEmail for sendMessage …
Browse files Browse the repository at this point in the history
…of gmail and delete UUID - version beta
  • Loading branch information
Vladik-gif committed Oct 25, 2024
1 parent 8f934f3 commit 96f8979
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public class SecurityJwtProperties {
private String secretKey;
private Duration accessExpiration;
private Duration refreshExpiration;
private Duration emailExpiration;
}
Original file line number Diff line number Diff line change
@@ -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) { }
9 changes: 8 additions & 1 deletion src/main/java/com/chat/yourway/security/JwtService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -70,6 +73,10 @@ private String generateRefreshTokenBuild(Map<String, Object> extraClaims, UserDe
return buildToken(extraClaims, userDetails, jwtProperties.getRefreshExpiration().toMillis());
}

private String generateEmailTokenBuild(Map<String, Object> extraClaims, UserDetails userDetails) {
return buildToken(extraClaims, userDetails, jwtProperties.getEmailExpiration().toMillis());
}

private boolean isNotValidTokenType(String token) {
return token == null || !token.startsWith(BEARER);
}
Expand Down
31 changes: 8 additions & 23 deletions src/main/java/com/chat/yourway/service/ActivateAccountService.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,7 +15,6 @@
public class ActivateAccountService {

private final EmailSenderService emailSenderService;
private final EmailTokenRepository emailTokenRepository;
private final EmailMessageFactoryService emailMessageFactoryService;
private final ContactService contactService;

Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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,\
Expand Down

0 comments on commit 96f8979

Please sign in to comment.