-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: SMS 인증 토큰 생성 API, 토큰으로 해당 계정 SMS 인증 API 완성
- Loading branch information
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
yogiyo/src/main/java/com/server/yogiyo/account/authentication/SmsAuthController.java
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,32 @@ | ||
package com.server.yogiyo.account.authentication; | ||
|
||
import com.server.yogiyo.configure.response.CommonResponse; | ||
import com.server.yogiyo.configure.response.DataResponse; | ||
import com.server.yogiyo.configure.response.ResponseService; | ||
import com.server.yogiyo.configure.security.authentication.CustomUserDetails; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping(value = "/app") | ||
public class SmsAuthController { | ||
|
||
private final SmsAuthService smsAuthService; | ||
private final ResponseService responseService; | ||
|
||
@PatchMapping(value = "/accounts/sms-token") | ||
public DataResponse<Integer> updateAccountSmsToken(@AuthenticationPrincipal CustomUserDetails customUserDetails, | ||
@RequestBody PhoneNumberDto phoneNumberDto) { | ||
Integer token = smsAuthService.updateAccountSmsToken(customUserDetails, phoneNumberDto.getPhoneNumber()); | ||
return responseService.getDataResponse(token); | ||
} | ||
|
||
@PatchMapping(value = "/accounts/sms-certification") | ||
public CommonResponse updateAccountSmsCertification(@AuthenticationPrincipal CustomUserDetails customUserDetails, | ||
@RequestBody TokenDto tokenDto) { | ||
smsAuthService.updateAccountSmsCertification(customUserDetails, tokenDto.getSmsToken()); | ||
return responseService.getSuccessResponse(); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
yogiyo/src/main/java/com/server/yogiyo/account/authentication/SmsAuthService.java
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,96 @@ | ||
package com.server.yogiyo.account.authentication; | ||
|
||
import com.server.yogiyo.account.AccountRepository; | ||
import com.server.yogiyo.account.entity.Account; | ||
import com.server.yogiyo.configure.response.exception.CustomException; | ||
import com.server.yogiyo.configure.response.exception.CustomExceptionStatus; | ||
import com.server.yogiyo.configure.security.authentication.CustomUserDetails; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.nurigo.java_sdk.api.Message; | ||
import net.nurigo.java_sdk.exceptions.CoolsmsException; | ||
import org.json.simple.JSONObject; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.beans.factory.annotation.Value; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.HashMap; | ||
import java.util.Random; | ||
|
||
@Transactional | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Service | ||
public class SmsAuthService { | ||
|
||
private final AccountRepository accountRepository; | ||
|
||
@Value("${coolsms.apikey}") | ||
private String apiKey; | ||
|
||
@Value("${coolsms.apiSecret}") | ||
private String apiSecret; | ||
|
||
@Value("${coolsms.fromNumber}") | ||
private String fromNumber; | ||
|
||
@PostConstruct | ||
public void init() { | ||
this.fromNumber = fromNumber.replaceAll("-", ""); | ||
} | ||
|
||
private Integer createRandNum() { | ||
Random rand = new Random(); | ||
String str = ""; | ||
for (int i = 0; i < 4; i++) { | ||
String ran = Integer.toString(rand.nextInt(10)); | ||
str += ran; | ||
} | ||
return Integer.parseInt(str); | ||
} | ||
|
||
|
||
public Integer updateAccountSmsToken(CustomUserDetails customUserDetails, String phoneNumber) { | ||
Account account = accountRepository.findById(customUserDetails.getAccount().getAccountId()) | ||
.orElseThrow(()-> new CustomException(CustomExceptionStatus.ACCOUNT_NOT_VALID)); | ||
|
||
if (account.isSmsCertified()) throw new CustomException(CustomExceptionStatus.ALREADY_CERTIFICATION_ACCOUNT); | ||
|
||
Integer randNum = createRandNum(); | ||
|
||
Message coolsms = new Message(apiKey, apiSecret); | ||
|
||
HashMap<String, String> params = new HashMap<String, String>(); | ||
params.put("to", phoneNumber); | ||
params.put("from", fromNumber); | ||
params.put("type", "SMS"); | ||
params.put("text", "[yogiyo] 인증번호 " + randNum.toString() + " 를 입력하세요."); | ||
params.put("app_version", "1.0"); | ||
|
||
|
||
try { | ||
JSONObject send = coolsms.send(params); | ||
if(Integer.parseInt(send.get("error_count").toString()) > 0) throw new CustomException(CustomExceptionStatus.FAILED_TO_RECEPTION); | ||
log.info(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"))+" : send SMS Authentication Token to "+phoneNumber); | ||
} catch (CoolsmsException e) { | ||
log.warn(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"))+" : "+e.getMessage()); | ||
throw new CustomException(CustomExceptionStatus.FAILED_TO_RECEPTION); | ||
} | ||
|
||
|
||
account.setSmsAuthToken(randNum); | ||
account.setPhoneNumber(phoneNumber); | ||
|
||
return randNum; | ||
} | ||
|
||
public void updateAccountSmsCertification(CustomUserDetails customUserDetails, Integer smsToken) { | ||
Account account = accountRepository.findById(customUserDetails.getAccount().getAccountId()) | ||
.orElseThrow(()-> new CustomException(CustomExceptionStatus.ACCOUNT_NOT_VALID)); | ||
if (account.getSmsAuthToken().equals(smsToken)) account.setSmsCertified(true); | ||
else throw new CustomException(CustomExceptionStatus.FAILED_TO_CERTIFICATION); | ||
} | ||
} |