|
@@ -0,0 +1,140 @@
|
|
|
+package com.dragoninfo.dcuc.auth.business.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.dragoninfo.dcuc.app.entity.ApplyInfo;
|
|
|
+import com.dragoninfo.dcuc.app.facade.IApplyInfoFacade;
|
|
|
+import com.dragoninfo.dcuc.auth.api.enums.zerotrust.ZeroTrustBusinessRespEnum;
|
|
|
+import com.dragoninfo.dcuc.auth.api.vo.zerotrust.ZeroTrustMessageRespVO;
|
|
|
+import com.dragoninfo.dcuc.auth.api.vo.zerotrust.ZeroTrustSignReqVO;
|
|
|
+import com.dragoninfo.dcuc.auth.business.IApiCommonBusiness;
|
|
|
+import com.dragoninfo.dcuc.auth.config.DcucAuthConfig;
|
|
|
+import com.dragoninfo.dcuc.auth.constance.AuthRedisConstant;
|
|
|
+import com.dragonsoft.duceap.commons.util.date.DateConst;
|
|
|
+import com.dragonsoft.duceap.commons.util.date.DateUtils;
|
|
|
+import com.dragonsoft.smtools.loader.SMFactory;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author mazq
|
|
|
+ * @date 2023/7/12
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class ApiCommonBusiness implements IApiCommonBusiness {
|
|
|
+
|
|
|
+ private DcucAuthConfig dcucAuthConfig;
|
|
|
+
|
|
|
+ private IApplyInfoFacade applyInfoFacade;
|
|
|
+
|
|
|
+ private SMFactory smFactory;
|
|
|
+
|
|
|
+ private StringRedisTemplate stringRedisTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
|
|
|
+ this.stringRedisTemplate = stringRedisTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setDcucAuthConfig(DcucAuthConfig dcucAuthConfig) {
|
|
|
+ this.dcucAuthConfig = dcucAuthConfig;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setApplyInfoFacade(IApplyInfoFacade applyInfoFacade) {
|
|
|
+ this.applyInfoFacade = applyInfoFacade;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public void setSmFactory(SMFactory smFactory) {
|
|
|
+ this.smFactory = smFactory;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ZeroTrustMessageRespVO checkSecret(ZeroTrustSignReqVO signReqVO) {
|
|
|
+ Boolean checkCallerSign = dcucAuthConfig.getCheckCallerSign();
|
|
|
+ log.info("checkCallerSign:{} ", checkCallerSign);
|
|
|
+ if (!checkCallerSign) {
|
|
|
+ return ZeroTrustMessageRespVO.messageEnumMessage(ZeroTrustBusinessRespEnum.SUCCESS);
|
|
|
+ }
|
|
|
+
|
|
|
+ String callerId = signReqVO.getCallerId();
|
|
|
+ String callerSign = signReqVO.getCallerSign();
|
|
|
+ String callerNounce = signReqVO.getCallerNounce();
|
|
|
+ String callerTimestamp = signReqVO.getCallerTimestamp();
|
|
|
+
|
|
|
+ int minCallerLength = 32;
|
|
|
+ int maxCallerLength = 40;
|
|
|
+ if (callerNounce.length() < minCallerLength) {
|
|
|
+ return ZeroTrustMessageRespVO.requestErrorMessage("callerNounce 不合法");
|
|
|
+ }
|
|
|
+ if (callerNounce.length() > maxCallerLength) {
|
|
|
+ return ZeroTrustMessageRespVO.requestErrorMessage("callerNounce 不合法");
|
|
|
+ }
|
|
|
+
|
|
|
+ Date parseLocalDate;
|
|
|
+ try {
|
|
|
+ parseLocalDate = DateUtils.getDate(callerTimestamp, DateConst.HYPHEN_DISPLAY_TIME);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.debug("TimeStamp:{} pattern error.", callerTimestamp);
|
|
|
+ log.debug("parse error", e);
|
|
|
+ return ZeroTrustMessageRespVO.requestErrorMessage("callerTimestamp 不合法");
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer timeStampCheckSeconds = dcucAuthConfig.getTimeStampCheckSeconds();
|
|
|
+ Date date = new Date();
|
|
|
+ Date minLocalDate = DateUtil.offsetSecond(date, -timeStampCheckSeconds);
|
|
|
+ Date maxLocalDate = DateUtil.offsetSecond(date, timeStampCheckSeconds);
|
|
|
+
|
|
|
+ if (parseLocalDate.before(minLocalDate)
|
|
|
+ || parseLocalDate.after(maxLocalDate)) {
|
|
|
+ log.error("AppCode :{},SystemTimeStamp:{} TimeStamp:{} is not in the check time range.", callerId,
|
|
|
+ System.currentTimeMillis(), timeStampCheckSeconds);
|
|
|
+ return ZeroTrustMessageRespVO.requestErrorMessage("callerTimestamp 不合法");
|
|
|
+ }
|
|
|
+
|
|
|
+ ApplyInfo applyInfo = applyInfoFacade.getAppByCode(callerId);
|
|
|
+
|
|
|
+ if (applyInfo == null) {
|
|
|
+ log.info("AppCode:{} isn't exits", callerId);
|
|
|
+ return ZeroTrustMessageRespVO.requestErrorMessage("callerId不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ String secretKey = applyInfo.getSecretKey();
|
|
|
+
|
|
|
+ String nonceValue = stringRedisTemplate.opsForValue().get(AuthRedisConstant.REDIS_TOKEN_NONCE_NAMESPACE + callerNounce);
|
|
|
+ boolean exitsNonce = StrUtil.isNotBlank(nonceValue);
|
|
|
+
|
|
|
+ if (exitsNonce) {
|
|
|
+ log.info("nonce:{} is used", callerNounce);
|
|
|
+ return ZeroTrustMessageRespVO.requestErrorMessage("callerNounce已被使用");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 应用资源标识&访问秘钥&随机&时间戳
|
|
|
+ String origin = "callerId=" + callerId + "&appSecret=" + secretKey + "&nonce=" + callerNounce + "&callerTimestamp=" + callerTimestamp;
|
|
|
+
|
|
|
+
|
|
|
+ log.info("origin:{}", origin);
|
|
|
+ log.info("callerSign:{}", callerSign);
|
|
|
+
|
|
|
+ String serverSign = (String) smFactory.getSM3().summary(origin);
|
|
|
+ log.info("serverSign:{}", serverSign);
|
|
|
+
|
|
|
+ Boolean signEquals = callerSign.equalsIgnoreCase(serverSign);
|
|
|
+
|
|
|
+ log.info("signEquals:{}", signEquals);
|
|
|
+
|
|
|
+ if (signEquals) {
|
|
|
+ stringRedisTemplate.opsForValue().set(AuthRedisConstant.REDIS_TOKEN_NONCE_NAMESPACE + callerNounce, "1", 30, TimeUnit.MINUTES);
|
|
|
+ return ZeroTrustMessageRespVO.messageEnumMessage(ZeroTrustBusinessRespEnum.SUCCESS);
|
|
|
+ }
|
|
|
+ return ZeroTrustMessageRespVO.requestErrorMessage("验签不一致");
|
|
|
+ }
|
|
|
+}
|