-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[KAN-34] 유저 - 이메일인증 메일 전송 API 개발 (#19)
* [KAN-34] 유저 - 이메일인증 메일 전송 API 개발 * [KAN-34] 유저 - 이메일인증 메일 전송 API 개발 - test * [KAN-34] 유저 - 이메일인증 메일 전송 API 개발 (패스워드 재설정 기능 추가) * [KAN-34] 유저 - 이메일인증 메일 전송 API 개발 (패스워드 재설정 기능 추가) * [KAN-34] 유저 - 이메일인증 메일 전송 API 개발 (네이밍 변경 * [KAN-34] 유저 - 이메일인증 메일 전송 API 개발 (네이밍 변경 * [KAN-34] 유저 - 이메일인증 메일 전송 API 개발 (네이밍 변경 * [KAN-34] 유저 - 이메일인증 메일 전송 API 개발 (네이밍 변경 (ktlint)
- Loading branch information
1 parent
fa78f1d
commit fd532e2
Showing
16 changed files
with
226 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -45,3 +45,5 @@ docker/redis/** | |
|
||
### IDEA run ### | ||
/.run | ||
.env | ||
|
This file contains 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 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
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/com/restaurant/be/common/config/AwsSESConfig.kt
This file contains 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,23 @@ | ||
package com.restaurant.be.common.config | ||
|
||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials | ||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider | ||
import software.amazon.awssdk.regions.Region | ||
import software.amazon.awssdk.services.ses.SesClient | ||
|
||
@Configuration | ||
class AwsSESConfig( | ||
@Value("\${aws.accessKey}") val accessKey: String, | ||
@Value("\${aws.secretKey}") val secretKey: String | ||
) { | ||
private val awsCred = AwsBasicCredentials.create(accessKey, secretKey) | ||
|
||
@Bean | ||
fun sesClient(): SesClient = SesClient.builder() | ||
.region(Region.AP_NORTHEAST_2) | ||
.credentialsProvider(StaticCredentialsProvider.create(awsCred)) | ||
.build() | ||
} |
This file contains 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
81 changes: 81 additions & 0 deletions
81
src/main/kotlin/com/restaurant/be/user/domain/service/ValidateEmailService.kt
This file contains 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,81 @@ | ||
package com.restaurant.be.user.domain.service | ||
|
||
import com.restaurant.be.common.exception.SendEmailException | ||
import com.restaurant.be.common.exception.SkkuEmailException | ||
import com.restaurant.be.common.redis.RedisRepository | ||
import com.restaurant.be.user.presentation.dto.SendEmailRequest | ||
import com.restaurant.be.user.presentation.dto.common.EmailSendType | ||
import com.restaurant.be.user.repository.EmailRepository | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Service | ||
import java.util.concurrent.TimeUnit | ||
|
||
@Service | ||
class ValidateEmailService( | ||
private val emailRepository: EmailRepository, | ||
@Value("\${aws.sender-email}") private val emailSource: String, | ||
private val redisRepository: RedisRepository | ||
) { | ||
private val signUpTemplate = | ||
EmailRepository::class.java.classLoader | ||
.getResource("sign-up-template.html")!! | ||
.readText() | ||
private val resetPasswordTemplate = | ||
EmailRepository::class.java.classLoader | ||
.getResource("reset-password-template.html")!! | ||
.readText() | ||
|
||
fun sendValidateCode(request: SendEmailRequest) { | ||
if (request.email.split("@")[1] != "g.skku.edu") { | ||
throw SkkuEmailException() | ||
} | ||
|
||
try { | ||
val code = emailRepository.generateRandomCode() | ||
val email = request.email | ||
if (request.sendType == EmailSendType.SIGN_UP) { | ||
val message = software.amazon.awssdk.services.ses.model.SendEmailRequest | ||
.builder() | ||
.source(emailSource) | ||
.destination { | ||
it.toAddresses(email) | ||
} | ||
.message { | ||
it.subject { | ||
it.data("먹꾸스꾸 회원가입 인증번호입니다.") | ||
} | ||
it.body { | ||
it.html { | ||
it.data(signUpTemplate.replace("AUTHENTICATION_CODE", code)) | ||
} | ||
} | ||
}.build() | ||
|
||
redisRepository.setValue("user:$email:signUpCode", code, 3, TimeUnit.MINUTES) | ||
emailRepository.sendEmail(message) | ||
} else { | ||
val message = software.amazon.awssdk.services.ses.model.SendEmailRequest | ||
.builder() | ||
.source(emailSource) | ||
.destination { | ||
it.toAddresses(email) | ||
} | ||
.message { | ||
it.subject { | ||
it.data("먹꾸스꾸 비밀번호 재설정 코드입니다.") | ||
} | ||
it.body { | ||
it.html { | ||
it.data(resetPasswordTemplate.replace("AUTHENTICATION_CODE", code)) | ||
} | ||
} | ||
}.build() | ||
|
||
redisRepository.setValue("user:$email:resetPasswordCode", code, 3, TimeUnit.MINUTES) | ||
emailRepository.sendEmail(message) | ||
} | ||
} catch (e: Exception) { | ||
throw SendEmailException() | ||
} | ||
} | ||
} |
This file contains 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 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 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
4 changes: 2 additions & 2 deletions
4
src/main/kotlin/com/restaurant/be/user/presentation/dto/common/EmailSendType.kt
This file contains 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package com.restaurant.be.user.presentation.dto.common | ||
|
||
enum class EmailSendType { | ||
EMAIL_VALIDATION, | ||
UPDATE_PASSWORD | ||
SIGN_UP, | ||
RESET_PASSWORD | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/com/restaurant/be/user/repository/EmailRepository.kt
This file contains 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,27 @@ | ||
package com.restaurant.be.user.repository | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import org.springframework.stereotype.Repository | ||
import software.amazon.awssdk.services.ses.SesClient | ||
import software.amazon.awssdk.services.ses.model.SendEmailRequest | ||
import java.time.LocalDateTime | ||
import java.time.ZoneOffset | ||
import kotlin.random.Random | ||
|
||
@Repository | ||
class EmailRepository( | ||
private val sesClient: SesClient | ||
|
||
) { | ||
private val rnd = Random(LocalDateTime.now().toEpochSecond(ZoneOffset.UTC)) | ||
|
||
fun sendEmail(sendEmailRequest: SendEmailRequest) { | ||
runBlocking { | ||
sesClient.sendEmail(sendEmailRequest) | ||
} | ||
} | ||
|
||
fun generateRandomCode(): String { | ||
return String.format("%06d", rnd.nextInt(0, 1000000)) | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -34,4 +34,9 @@ jwt: | |
secret: c2lsdmVybmluZS10ZWNoLXNwcmluZy1ib290LWp3dC10dXRvcmlhbC1zZWNyZXQtc2lsdmVybmluZS10ZWNoLXNwcmluZy1ib290LWp3dC10dXRvcmlhbC1zZWNyZXQK | ||
token-validity-in-seconds: 86400 | ||
access-token-validity-in-seconds: 7200 | ||
refresh-token-validity-in-seconds: 1209600 | ||
refresh-token-validity-in-seconds: 1209600 | ||
|
||
aws: | ||
accessKey: ${AWS_ACCESS_KEY:test} | ||
secretKey: ${AWS_SECRET_KEY:test} | ||
sender-email: ${SENDER_EMAIL:[email protected]} |
This file contains 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 @@ | ||
<!DOCTYPE html> | ||
<html lang="kr"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Password Reset Template</title></head> | ||
<body style="font-family: Arial, sans-serif; background-color: #f4f4f4; margin: 0; padding: 0;"> | ||
<table style="background-color: #ffffff; width: 100%; max-width: 600px; margin: 0 auto; padding: 20px; text-align: center;"> | ||
<tr> | ||
<td><p style="font-size: 32px; font-weight: bold; color: #000000; margin-bottom: 10px;">비밀번호 재설정 인증번호</p> | ||
<p style="margin-bottom: 30px; color: #555555; line-height: 1.5;"> 안녕하세요. 먹꾸스꾸입니다. <br> | ||
비밀번호 재설정을 위한 인증번호를 보내드립니다. </p> | ||
<p style="margin-bottom: 30px; color: #555555; line-height: 1.5;"> 이메일 인증번호를 통해 인증을 완료해주세요. </p> | ||
<p style="font-size: 40px; color: #db0d0d; font-weight: bold; padding: 20px 0;">AUTHENTICATION_CODE</p> | ||
<p style="font-size: 14px; color: #aaaaaa; margin-top: 30px;"> 이 메일은 발신 전용 메일로 회신되지 않습니다.<br> | ||
문의사항은 앱 내 문의하기를 이용해 주세요. </p> | ||
<p style="font-size: 14px; color: #aaaaaa; margin-top: 30px;"> 혹시 직접 요청 하지 않은 인증 메일을 받으 셨을 경우 무시 해주시길 바랍니다. </p> | ||
<p style="font-size: 14px; color: #aaaaaa; margin-top: 30px;"> 먹꾸스꾸 </p></td> | ||
</tr> | ||
</table> | ||
</body> | ||
</html> |
This file contains 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 @@ | ||
<!DOCTYPE html> | ||
<html lang="kr"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>SignUp Template</title></head> | ||
<body style="font-family: Arial, sans-serif; background-color: #f4f4f4; margin: 0; padding: 0;"> | ||
<table style="background-color: #ffffff; width: 100%; max-width: 600px; margin: 0 auto; padding: 20px; text-align: center;"> | ||
<tr> | ||
<td><p style="font-size: 32px; font-weight: bold; color: #000000; margin-bottom: 10px;">회원가입 인증번호</p> | ||
<p style="margin-bottom: 30px; color: #555555; line-height: 1.5;"> 안녕하세요. 먹꾸스꾸입니다. <br> | ||
회원가입 위한 인증번호를 보내드립니다. </p> | ||
<p style="margin-bottom: 30px; color: #555555; line-height: 1.5;"> 이메일 인증번호를 통해 인증을 완료해주세요. </p> | ||
<p style="font-size: 40px; color: #db0d0d; font-weight: bold; padding: 20px 0;">AUTHENTICATION_CODE</p> | ||
<p style="font-size: 14px; color: #aaaaaa; margin-top: 30px;"> 이 메일은 발신 전용 메일로 회신되지 않습니다.<br> | ||
문의사항은 앱 내 문의하기를 이용해 주세요. </p> | ||
<p style="font-size: 14px; color: #aaaaaa; margin-top: 30px;"> 혹시 직접 요청 하지 않은 인증 메일을 받으 셨을 경우 무시 해주시길 바랍니다. </p> | ||
<p style="font-size: 14px; color: #aaaaaa; margin-top: 30px;"> 먹꾸스꾸 </p></td> | ||
</tr> | ||
</table> | ||
</body> | ||
</html> |
This file contains 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 |
---|---|---|
|
@@ -34,4 +34,9 @@ jwt: | |
secret: c2lsdmVybmluZS10ZWNoLXNwcmluZy1ib290LWp3dC10dXRvcmlhbC1zZWNyZXQtc2lsdmVybmluZS10ZWNoLXNwcmluZy1ib290LWp3dC10dXRvcmlhbC1zZWNyZXQK | ||
token-validity-in-seconds: 86400 | ||
access-token-validity-in-seconds: 7200 | ||
refresh-token-validity-in-seconds: 1209600 | ||
refresh-token-validity-in-seconds: 1209600 | ||
|
||
aws: | ||
accessKey: ${AWS_ACCESS_KEY:test} | ||
secretKey: ${AWS_SECRET_KEY:test} | ||
sender-email: ${SENDER_EMAIL:[email protected]} |