|
|
@ -1,33 +1,13 @@
|
|
|
|
package kr.xit.ens.nice.cmm;
|
|
|
|
package kr.xit.ens.nice.cmm;
|
|
|
|
|
|
|
|
|
|
|
|
import egovframework.com.cmm.EgovMessageSource;
|
|
|
|
import egovframework.com.cmm.EgovMessageSource;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
|
|
|
|
|
|
import java.security.InvalidKeyException;
|
|
|
|
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
|
|
import java.util.Base64;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import javax.crypto.BadPaddingException;
|
|
|
|
|
|
|
|
import javax.crypto.Cipher;
|
|
|
|
|
|
|
|
import javax.crypto.IllegalBlockSizeException;
|
|
|
|
|
|
|
|
import javax.crypto.Mac;
|
|
|
|
|
|
|
|
import javax.crypto.NoSuchPaddingException;
|
|
|
|
|
|
|
|
import javax.crypto.SecretKey;
|
|
|
|
|
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
|
|
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
|
|
|
|
|
import javax.validation.ConstraintViolation;
|
|
|
|
|
|
|
|
import javax.validation.Validation;
|
|
|
|
import javax.validation.Validation;
|
|
|
|
import javax.validation.Validator;
|
|
|
|
import javax.validation.Validator;
|
|
|
|
import kr.xit.biz.cmm.service.CmmEnsCacheService;
|
|
|
|
import kr.xit.biz.cmm.service.ICmmEnsCacheService;
|
|
|
|
import kr.xit.biz.ens.model.nice.NiceCiDTO.NiceCiInfo;
|
|
|
|
import kr.xit.biz.ens.model.nice.NiceCiDTO.NiceCiInfo;
|
|
|
|
import kr.xit.biz.ens.model.nice.NiceCiDTO.PublickeyResponse;
|
|
|
|
import kr.xit.biz.ens.model.nice.NiceCiDTO.PublickeyResponse;
|
|
|
|
import kr.xit.biz.ens.model.nice.NiceCiDTO.ResponseDataHeader;
|
|
|
|
import kr.xit.biz.ens.model.nice.NiceCiDTO.ResponseDataHeader;
|
|
|
@ -38,11 +18,11 @@ import kr.xit.core.spring.config.properties.PropertiesUtils;
|
|
|
|
import kr.xit.core.spring.util.ApiSpringUtils;
|
|
|
|
import kr.xit.core.spring.util.ApiSpringUtils;
|
|
|
|
import kr.xit.core.support.utils.DateUtils;
|
|
|
|
import kr.xit.core.support.utils.DateUtils;
|
|
|
|
import kr.xit.core.support.utils.JsonUtils;
|
|
|
|
import kr.xit.core.support.utils.JsonUtils;
|
|
|
|
|
|
|
|
import kr.xit.ens.cmm.CmmEnsUtils;
|
|
|
|
|
|
|
|
import kr.xit.ens.nice.service.INiceCiService;
|
|
|
|
import kr.xit.ens.nice.service.NiceCiDummyTestService;
|
|
|
|
import kr.xit.ens.nice.service.NiceCiDummyTestService;
|
|
|
|
import kr.xit.ens.nice.service.NiceCiService;
|
|
|
|
|
|
|
|
import lombok.AccessLevel;
|
|
|
|
import lombok.AccessLevel;
|
|
|
|
import lombok.NoArgsConstructor;
|
|
|
|
import lombok.NoArgsConstructor;
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
import org.springframework.util.Base64Utils;
|
|
|
|
import org.springframework.util.Base64Utils;
|
|
|
@ -67,14 +47,12 @@ import org.springframework.util.Base64Utils;
|
|
|
|
public class CmmNiceCiUtils {
|
|
|
|
public class CmmNiceCiUtils {
|
|
|
|
private static final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
|
|
|
private static final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
|
|
|
private static final EgovMessageSource messageSource = ApiSpringUtils.getMessageSource();
|
|
|
|
private static final EgovMessageSource messageSource = ApiSpringUtils.getMessageSource();
|
|
|
|
private static final CmmEnsCacheService cacheService = ApiSpringUtils.getCmmEnsCacheService();
|
|
|
|
private static final ICmmEnsCacheService cacheService = ApiSpringUtils.getCmmEnsCacheService();
|
|
|
|
private static final NiceCiService niceCiService = ApiSpringUtils.getNiceCiService();
|
|
|
|
private static final INiceCiService niceCiService = ApiSpringUtils.getNiceCiService();
|
|
|
|
private static final NiceCiDummyTestService dummyTestService = ApiSpringUtils.getDummyNiceCiService();
|
|
|
|
private static final NiceCiDummyTestService dummyTestService = ApiSpringUtils.getDummyNiceCiService();
|
|
|
|
|
|
|
|
|
|
|
|
private static final boolean IS_DUMMY = Boolean.valueOf(PropertiesUtils.getProperty("contract.nice.dummy"));
|
|
|
|
private static final boolean IS_DUMMY = Boolean.valueOf(PropertiesUtils.getProperty("contract.nice.dummy"));
|
|
|
|
|
|
|
|
|
|
|
|
private static final int EXPIRED_CHECK_DAYS = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//private static final String FFNLG_CODE = PropertiesUtils.getProperty("contract.nice.ffnlg-code");
|
|
|
|
//private static final String FFNLG_CODE = PropertiesUtils.getProperty("contract.nice.ffnlg-code");
|
|
|
|
//private static final String CLIENT_ID = PropertiesUtils.getProperty("contract.nice.client-id");
|
|
|
|
//private static final String CLIENT_ID = PropertiesUtils.getProperty("contract.nice.client-id");
|
|
|
|
|
|
|
|
|
|
|
@ -117,7 +95,7 @@ public class CmmNiceCiUtils {
|
|
|
|
// 2. 공개키 발급 요청
|
|
|
|
// 2. 공개키 발급 요청
|
|
|
|
PublickeyResponse pubResDTO = null;
|
|
|
|
PublickeyResponse pubResDTO = null;
|
|
|
|
if(IS_DUMMY) pubResDTO = dummyTestService.requestPublickey();
|
|
|
|
if(IS_DUMMY) pubResDTO = dummyTestService.requestPublickey();
|
|
|
|
else pubResDTO = niceCiService.requestPublickey(CmmNiceCiUtils.generateLengthUuid(24), niceDTO);
|
|
|
|
else pubResDTO = niceCiService.requestPublickey(CmmEnsUtils.generateLengthUuid(24), niceDTO);
|
|
|
|
return cacheService.getNiceCiInfoCache(signguCode, ffnlgCode);
|
|
|
|
return cacheService.getNiceCiInfoCache(signguCode, ffnlgCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -137,7 +115,7 @@ public class CmmNiceCiUtils {
|
|
|
|
|
|
|
|
|
|
|
|
// 1. 토큰 폐기
|
|
|
|
// 1. 토큰 폐기
|
|
|
|
if(IS_DUMMY) dummyTestService.requestRegSymmetrickey(signguCode, ffnlgCode);
|
|
|
|
if(IS_DUMMY) dummyTestService.requestRegSymmetrickey(signguCode, ffnlgCode);
|
|
|
|
else niceCiService.requestRegSymmetrickey(CmmNiceCiUtils.generateLengthUuid(24), null, niceDTO);
|
|
|
|
else niceCiService.requestRegSymmetrickey(CmmEnsUtils.generateLengthUuid(24), null, niceDTO);
|
|
|
|
|
|
|
|
|
|
|
|
return cacheService.getNiceCiInfoCache(signguCode, ffnlgCode);
|
|
|
|
return cacheService.getNiceCiInfoCache(signguCode, ffnlgCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -152,10 +130,10 @@ public class CmmNiceCiUtils {
|
|
|
|
public static SymkeyRegInfo getSymkeyRegInfo(String siteCode) {
|
|
|
|
public static SymkeyRegInfo getSymkeyRegInfo(String siteCode) {
|
|
|
|
return SymkeyRegInfo.builder()
|
|
|
|
return SymkeyRegInfo.builder()
|
|
|
|
.siteCode(siteCode)
|
|
|
|
.siteCode(siteCode)
|
|
|
|
.requestNo(generateLengthUuid(30))
|
|
|
|
.requestNo(CmmEnsUtils.generateLengthUuid(30))
|
|
|
|
.key(generateLengthUuid(32))
|
|
|
|
.key(CmmEnsUtils.generateLengthUuid(32))
|
|
|
|
.iv(generateLengthUuid(16))
|
|
|
|
.iv(CmmEnsUtils.generateLengthUuid(16))
|
|
|
|
.hmacKey(generateLengthUuid(32))
|
|
|
|
.hmacKey(CmmEnsUtils.generateLengthUuid(32))
|
|
|
|
.build();
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -177,22 +155,6 @@ public class CmmNiceCiUtils {
|
|
|
|
return map;
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static <T> void validate(T t) {
|
|
|
|
|
|
|
|
//final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
|
|
|
|
|
|
|
final Set<ConstraintViolation<T>> list = validator.validate(t);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (list.size() > 0) {
|
|
|
|
|
|
|
|
List<String> errors = list.stream()
|
|
|
|
|
|
|
|
.map(row -> String.format("%s=%s", row.getPropertyPath(), row.getMessageTemplate()))
|
|
|
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throw BizRuntimeException.create(
|
|
|
|
|
|
|
|
list.stream()
|
|
|
|
|
|
|
|
.map(row -> String.format("%s=%s", row.getPropertyPath(), row.getMessageTemplate()))
|
|
|
|
|
|
|
|
.collect(Collectors.toList()).toString());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* <pre>
|
|
|
|
* <pre>
|
|
|
|
* GW_RSLT_CD : "1200" 성공인 경우 TRAN_ID 체크
|
|
|
|
* GW_RSLT_CD : "1200" 성공인 경우 TRAN_ID 체크
|
|
|
@ -209,114 +171,4 @@ public class CmmNiceCiUtils {
|
|
|
|
if(!tranId.equals(resHeader.getTranId())) throw BizRuntimeException.create(messageSource.getMessage("fail.api.nice.invalid.tranId"));
|
|
|
|
if(!tranId.equals(resHeader.getTranId())) throw BizRuntimeException.create(messageSource.getMessage("fail.api.nice.invalid.tranId"));
|
|
|
|
return t;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String generateLengthUuid(int length) {
|
|
|
|
|
|
|
|
final String allChars = UUID.randomUUID().toString().replace("-", "");
|
|
|
|
|
|
|
|
final Random random = new Random();
|
|
|
|
|
|
|
|
final char[] otp = new char[length];
|
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
|
|
|
|
otp[i] = allChars.charAt(random.nextInt(allChars.length()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return String.valueOf(otp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* sha256 암호화
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param text
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
* @throws IOException
|
|
|
|
|
|
|
|
* @throws NoSuchAlgorithmException
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static String hexSha256(final String text) {
|
|
|
|
|
|
|
|
final StringBuffer sbuf = new StringBuffer();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
final MessageDigest mDigest = MessageDigest.getInstance("SHA-256");
|
|
|
|
|
|
|
|
mDigest.update(text.getBytes());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final byte[] msgStr = mDigest.digest();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < msgStr.length; i++) {
|
|
|
|
|
|
|
|
final byte tmpStrByte = msgStr[i];
|
|
|
|
|
|
|
|
final String tmpEncTxt = Integer.toString((tmpStrByte & 0xff) + 0x100, 16).substring(1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sbuf.append(tmpEncTxt);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (NoSuchAlgorithmException nae){
|
|
|
|
|
|
|
|
throw BizRuntimeException.create(nae.getMessage());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return sbuf.toString();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* <pre>
|
|
|
|
|
|
|
|
* Nice ci 데이타 암호화
|
|
|
|
|
|
|
|
* Base64 encoding return
|
|
|
|
|
|
|
|
* @param key
|
|
|
|
|
|
|
|
* @param iv
|
|
|
|
|
|
|
|
* @param planText
|
|
|
|
|
|
|
|
* @return String Base64 encoding data
|
|
|
|
|
|
|
|
* </pre>
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static String encodeAesData(final String key, final String iv, final String planText) {
|
|
|
|
|
|
|
|
final SecretKey secureKey = new SecretKeySpec(key.getBytes(), "AES");
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
|
|
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, secureKey, new IvParameterSpec(iv.getBytes()));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final byte[] encData = cipher.doFinal(planText.trim().getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
|
|
|
return Base64.getEncoder().encodeToString(encData);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}catch (NoSuchPaddingException | NoSuchAlgorithmException e){
|
|
|
|
|
|
|
|
throw BizRuntimeException.create(e.getMessage());
|
|
|
|
|
|
|
|
} catch (InvalidAlgorithmParameterException | InvalidKeyException e) {
|
|
|
|
|
|
|
|
throw BizRuntimeException.create(e.getMessage());
|
|
|
|
|
|
|
|
} catch (IllegalBlockSizeException | BadPaddingException e) {
|
|
|
|
|
|
|
|
throw BizRuntimeException.create(e.getMessage());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* <pre>
|
|
|
|
|
|
|
|
* Hmac 무결성체크값(integrity_value) 생성
|
|
|
|
|
|
|
|
* @param hmacKey
|
|
|
|
|
|
|
|
* @param message
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return String
|
|
|
|
|
|
|
|
* </pre>
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static String encodeHmacSha256(final String hmacKey, final String message) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
final Mac mac = Mac.getInstance("HmacSHA256");
|
|
|
|
|
|
|
|
final SecretKeySpec sks = new SecretKeySpec(hmacKey.getBytes(), "HmacSHA256");
|
|
|
|
|
|
|
|
mac.init(sks);
|
|
|
|
|
|
|
|
final byte[] hmac256 = mac.doFinal(message.getBytes());
|
|
|
|
|
|
|
|
return Base64.getEncoder().encodeToString(hmac256);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}catch (NoSuchAlgorithmException|InvalidKeyException e){
|
|
|
|
|
|
|
|
throw BizRuntimeException.create(e.getMessage());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String decodeAesData(String encData, String key, String iv) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final byte[] respDataEnc = Base64.getDecoder().decode(encData.getBytes());
|
|
|
|
|
|
|
|
final SecretKey secureKey = new SecretKeySpec(key.getBytes(), "AES");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
|
|
|
|
|
|
cipher.init(Cipher.DECRYPT_MODE, secureKey, new IvParameterSpec(iv.getBytes()));
|
|
|
|
|
|
|
|
final byte[] decrypted = cipher.doFinal(respDataEnc);
|
|
|
|
|
|
|
|
return new String(decrypted);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}catch (NoSuchPaddingException|NoSuchAlgorithmException e){
|
|
|
|
|
|
|
|
throw BizRuntimeException.create(e.getMessage());
|
|
|
|
|
|
|
|
} catch (InvalidAlgorithmParameterException|InvalidKeyException e) {
|
|
|
|
|
|
|
|
throw BizRuntimeException.create(e.getMessage());
|
|
|
|
|
|
|
|
} catch (IllegalBlockSizeException|BadPaddingException e) {
|
|
|
|
|
|
|
|
throw BizRuntimeException.create(e.getMessage());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|