-
Notifications
You must be signed in to change notification settings - Fork 1
feat : Add Email Authentication Code Sender Api #44
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
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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/ftm/server/adapter/controller/user/EmailAuthenticationController.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,30 @@ | ||
| package com.ftm.server.adapter.controller.user; | ||
|
|
||
| import com.ftm.server.adapter.dto.request.EmailAuthenticationRequest; | ||
| import com.ftm.server.common.response.ApiResponse; | ||
| import com.ftm.server.common.response.enums.SuccessResponseCode; | ||
| import com.ftm.server.domain.dto.command.EmailAuthenticationCommand; | ||
| import com.ftm.server.domain.usecase.user.EmailAuthenticationUseCase; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @RestController | ||
| public class EmailAuthenticationController { | ||
|
|
||
| private final EmailAuthenticationUseCase emailAuthenticationUseCase; | ||
|
|
||
| @PostMapping("/api/users/email/authentication") | ||
| public ResponseEntity<ApiResponse<Object>> emailAuthenticationCodeSender( | ||
| @Valid @RequestBody EmailAuthenticationRequest request) { | ||
| emailAuthenticationUseCase.sendEmailAuthenticationCode( | ||
| EmailAuthenticationCommand.from(request)); // command κ°μ²΄ μ λ¬ | ||
| return ResponseEntity.status(HttpStatus.OK) | ||
| .body(ApiResponse.success(SuccessResponseCode.OK)); | ||
| } | ||
| } |
Empty file.
13 changes: 13 additions & 0 deletions
13
src/main/java/com/ftm/server/adapter/dto/request/EmailAuthenticationRequest.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,13 @@ | ||
| package com.ftm.server.adapter.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.Pattern; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class EmailAuthenticationRequest { | ||
|
|
||
| @Pattern( | ||
| regexp = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$", | ||
| message = "μ΄λ©μΌ νμμ΄ μ¬λ°λ₯΄μ§ μμ΅λλ€.") | ||
| private final String email; | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/ftm/server/adapter/gateway/MailSenderGateway.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,6 @@ | ||
| package com.ftm.server.adapter.gateway; | ||
|
|
||
| public interface MailSenderGateway { | ||
|
|
||
| void sendEmail(String to, String code); | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/ftm/server/adapter/gateway/repository/EmailVerificationLogsRepository.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,13 @@ | ||
| package com.ftm.server.adapter.gateway.repository; | ||
|
|
||
| import com.ftm.server.entity.entities.EmailVerificationLogs; | ||
| import java.util.Optional; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| public interface EmailVerificationLogsRepository | ||
| extends JpaRepository<EmailVerificationLogs, Long> { | ||
|
|
||
| Optional<EmailVerificationLogs> findByEmail(String email); | ||
| } |
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
Empty file.
22 changes: 22 additions & 0 deletions
22
src/main/java/com/ftm/server/common/utils/RandomCodeCreator.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,22 @@ | ||
| package com.ftm.server.common.utils; | ||
|
|
||
| import java.security.SecureRandom; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| public class RandomCodeCreator { | ||
|
|
||
| private static final int CODE_LENGTH = 6; | ||
|
|
||
| private final SecureRandom random = new SecureRandom(); | ||
|
|
||
| public String generateAuthCode() { | ||
|
|
||
| StringBuilder sb = new StringBuilder(CODE_LENGTH); | ||
| for (int i = 0; i < CODE_LENGTH; i++) { | ||
| int digit = random.nextInt(10); | ||
| sb.append(digit); | ||
| } | ||
| return sb.toString(); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/ftm/server/domain/dto/command/EmailAuthenticationCommand.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,13 @@ | ||
| package com.ftm.server.domain.dto.command; | ||
|
|
||
| import com.ftm.server.adapter.dto.request.EmailAuthenticationRequest; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class EmailAuthenticationCommand { | ||
| private final String email; | ||
|
|
||
| public static EmailAuthenticationCommand from(EmailAuthenticationRequest request) { | ||
| return new EmailAuthenticationCommand(request.getEmail()); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/ftm/server/domain/dto/command/EmailVerificationLogCreationCommand.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,13 @@ | ||
| package com.ftm.server.domain.dto.command; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class EmailVerificationLogCreationCommand { | ||
| private final String email; | ||
| private final String verificationCode; | ||
|
|
||
| public static EmailVerificationLogCreationCommand of(String email, String verificationCode) { | ||
| return new EmailVerificationLogCreationCommand(email, verificationCode); | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/ftm/server/domain/service/EmailVerificationLogsService.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,24 @@ | ||
| package com.ftm.server.domain.service; | ||
|
|
||
| import com.ftm.server.adapter.gateway.repository.EmailVerificationLogsRepository; | ||
| import com.ftm.server.domain.dto.command.EmailVerificationLogCreationCommand; | ||
| import com.ftm.server.domain.dto.query.FindByEmailQuery; | ||
| import com.ftm.server.entity.entities.EmailVerificationLogs; | ||
| import java.util.Optional; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class EmailVerificationLogsService { | ||
| private final EmailVerificationLogsRepository emailVerificationLogsRepository; | ||
|
|
||
| public Optional<EmailVerificationLogs> findEmailVerificationLogsByEmail( | ||
| FindByEmailQuery query) { | ||
| return emailVerificationLogsRepository.findByEmail(query.getEmail()); | ||
| } | ||
|
|
||
| public void saveEmailVerificationLogs(EmailVerificationLogCreationCommand command) { | ||
| emailVerificationLogsRepository.save(EmailVerificationLogs.from(command)); | ||
| } | ||
| } |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/ftm/server/domain/usecase/user/EmailAuthenticationUseCase.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,57 @@ | ||
| package com.ftm.server.domain.usecase.user; | ||
|
|
||
| import com.ftm.server.adapter.gateway.MailSenderGateway; | ||
| import com.ftm.server.common.annotation.UseCase; | ||
| import com.ftm.server.common.exception.CustomException; | ||
| import com.ftm.server.common.response.enums.ErrorResponseCode; | ||
| import com.ftm.server.common.utils.RandomCodeCreator; | ||
| import com.ftm.server.domain.dto.command.EmailAuthenticationCommand; | ||
| import com.ftm.server.domain.dto.command.EmailVerificationLogCreationCommand; | ||
| import com.ftm.server.domain.dto.query.FindByEmailQuery; | ||
| import com.ftm.server.domain.service.EmailVerificationLogsService; | ||
| import com.ftm.server.entity.entities.EmailVerificationLogs; | ||
| import jakarta.transaction.Transactional; | ||
| import java.time.LocalDateTime; | ||
| import java.util.Optional; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @UseCase | ||
| @RequiredArgsConstructor | ||
| public class EmailAuthenticationUseCase { | ||
|
|
||
| private final MailSenderGateway mailSenderGateway; | ||
|
|
||
| private final RandomCodeCreator randomCodeCreator; | ||
|
|
||
| private final EmailVerificationLogsService emailVerificationLogsService; | ||
|
|
||
| @Transactional | ||
| public void sendEmailAuthenticationCode(EmailAuthenticationCommand command) { | ||
|
|
||
| String authCode = randomCodeCreator.generateAuthCode(); | ||
| String email = command.getEmail(); | ||
|
|
||
| Optional<EmailVerificationLogs> emailVerificationLogs = | ||
| emailVerificationLogsService.findEmailVerificationLogsByEmail( | ||
| FindByEmailQuery.of(email)); | ||
|
|
||
| int MAX_TRIALS = 5; | ||
| int BLOCK_MINUTES = 15; | ||
|
|
||
| if (emailVerificationLogs.isEmpty()) { // μ΄λ©μΌ μΈμ¦μ νλ²λ μλνμ§ μμ κ²½μ° log μλ‘ μμ± | ||
| emailVerificationLogsService.saveEmailVerificationLogs( | ||
| EmailVerificationLogCreationCommand.of(email, authCode)); | ||
| } else if (emailVerificationLogs.get().getTrialNum() | ||
| < MAX_TRIALS) { // μ΄λ©μΌ μΈμ¦ μλλ₯Ό νμ μμΌλ μλ νμλ₯Ό μ΄κ³Όνμ§ μμ κ²½μ° log update | ||
| emailVerificationLogs.get().updateVerificationStatus(authCode); | ||
| } else if (emailVerificationLogs // μ΄λ©μΌ μΈμ¦ μλ νμλ₯Ό μ΄κ³ΌνμΌλ λ§μ§λ§ μλ νμλ‘λΆν° 15λΆ μ΄μμ΄ μ§λ κ²½μ° log μ΄κΈ°ν | ||
| .get() | ||
| .getTokenIssuanceTime() | ||
| .isBefore(LocalDateTime.now().minusMinutes(BLOCK_MINUTES))) { | ||
| emailVerificationLogs.get().initializeVerificationStatus(authCode); | ||
| } else { // μ΄λ©μΌ μΈμ¦ μλ νμλ₯Ό λ¨μν μ΄κ³Όν κ²½μ° | ||
| throw new CustomException(ErrorResponseCode.EXCEED_NUMBER_OF_TRIAL); | ||
| } | ||
| mailSenderGateway.sendEmail(email, authCode); | ||
| } | ||
| } |
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
38 changes: 38 additions & 0 deletions
38
src/main/java/com/ftm/server/infrastructure/smtp/MailSenderConfig.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,38 @@ | ||
| package com.ftm.server.infrastructure.smtp; | ||
|
|
||
| import java.util.Properties; | ||
| import org.springframework.boot.autoconfigure.mail.MailProperties; | ||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.mail.javamail.JavaMailSender; | ||
| import org.springframework.mail.javamail.JavaMailSenderImpl; | ||
|
|
||
| @Configuration | ||
| @EnableConfigurationProperties(MailProperties.class) | ||
| public class MailSenderConfig { | ||
|
|
||
| private final MailProperties mailProperties; | ||
|
|
||
| public MailSenderConfig(MailProperties mailProperties) { | ||
| this.mailProperties = mailProperties; | ||
| } | ||
|
|
||
| @Bean | ||
| public JavaMailSender javaMailService() { | ||
| JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); | ||
| javaMailSender.setHost(mailProperties.getHost()); | ||
| javaMailSender.setUsername(mailProperties.getUsername()); | ||
| javaMailSender.setPassword(mailProperties.getPassword()); | ||
| javaMailSender.setPort(mailProperties.getPort()); | ||
|
|
||
| Properties properties = javaMailSender.getJavaMailProperties(); | ||
| properties.put("mail.smtp.auth", "true"); | ||
| properties.put("mail.smtp.starttls.enable", "true"); | ||
|
|
||
| javaMailSender.setJavaMailProperties(properties); | ||
| javaMailSender.setDefaultEncoding("UTF-8"); | ||
|
|
||
| return javaMailSender; | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
src/main/java/com/ftm/server/infrastructure/smtp/MailSenderService.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,49 @@ | ||
| package com.ftm.server.infrastructure.smtp; | ||
|
|
||
| import com.ftm.server.adapter.gateway.MailSenderGateway; | ||
| import com.ftm.server.common.annotation.InfraService; | ||
| import com.ftm.server.common.exception.CustomException; | ||
| import com.ftm.server.common.response.enums.ErrorResponseCode; | ||
| import jakarta.mail.MessagingException; | ||
| import jakarta.mail.internet.MimeMessage; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.mail.MailException; | ||
| import org.springframework.mail.javamail.JavaMailSender; | ||
| import org.springframework.mail.javamail.MimeMessageHelper; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @InfraService | ||
| public class MailSenderService implements MailSenderGateway { | ||
|
|
||
| private final JavaMailSender mailSender; | ||
|
|
||
| @Override | ||
| public void sendEmail(String to, String code) { | ||
| MimeMessage message = mailSender.createMimeMessage(); | ||
|
|
||
| String mailContent = | ||
| """ | ||
| <html> | ||
| <body style="text-align:center; font-family:Arial, sans-serif;"> | ||
| <h1>νλ맨 μ΄λ©μΌ μΈμ¦ μ½λμ λλ€</h1> | ||
| <div style="font-size:24px; margin:20px 0; color:#4CAF50;"><strong>%s</strong></div> | ||
| <p>μ μΈμ¦ μ½λλ₯Ό μ λ ₯νμ¬ μ΄λ©μΌ μΈμ¦μ μλ£ν΄ μ£ΌμΈμ.</p> | ||
| <br/> | ||
| <hr/> | ||
| <p style="font-size:12px; color:#888;">μ΄ λ©μΌμ νλ맨 μμ€ν μ μν΄ μλ λ°μ‘λμμ΅λλ€.</p> | ||
| <p style="font-size:12px; color:#888;">λ¬Έμ:ftmanserver@gmail.com</p> | ||
| </body> | ||
| </html> | ||
| """ | ||
| .formatted(code); | ||
| try { | ||
| MimeMessageHelper helper = new MimeMessageHelper(message, true); | ||
| helper.setTo(to); | ||
| helper.setSubject("[νλ맨] μ΄λ©μΌ μΈμ¦μ μλ£ν΄μ£ΌμΈμ!"); | ||
| helper.setText(mailContent, true); | ||
| mailSender.send(message); | ||
| } catch (MailException | MessagingException e) { | ||
| throw new CustomException((ErrorResponseCode.FAIL_TO_SEND_EMAIL), e.getMessage()); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Controller -> UseCase κ³μΈ΅μΌλ‘ μ λ¬λ λ Command or Query κ°μ²΄λ‘ μ λ¬ν΄μ£ΌκΈ°λ‘ νλλ° EmailAuthenticationUseCase sendEmailAuthenticationCode() λ©μλμμ Request DTOλ₯Ό λ°κ³ μμ΅λλ€ !