فهرست منبع

Add feature: handling other encrypted request.

ZRY 1 سال پیش
والد
کامیت
5e60f5ba7c
3فایلهای تغییر یافته به همراه95 افزوده شده و 0 حذف شده
  1. 27 0
      zllauth1/auth.go
  2. 1 0
      zllauth_gin/elh_int.go
  3. 67 0
      zllauth_gin/gin_handler.go

+ 27 - 0
zllauth1/auth.go

@@ -60,6 +60,33 @@ func (h *ZLLAuthHandler) GetJwtTTL() time.Duration {
 	return h.config.JWTTTL
 }
 
+func (h *ZLLAuthHandler) HandlingOtherEncryptedRequest(encData string, v interface{}) (visibleErr LoginFailureType, internalErr error) {
+	if !h.isSecureInit {
+		visibleErr = LFT_SECURE_SYSTEM_NOT_INIT
+		internalErr = fmt.Errorf("secure module not initialized")
+		return
+	}
+	hdec, err := hex.DecodeString(encData)
+	if err != nil {
+		visibleErr = LFT_DECODE_HEX_FAIL
+		internalErr = fmt.Errorf("decode hex data error: %s", err)
+		return
+	}
+	pt, err := sm2.Decrypt(h.sm2key, hdec)
+	if err != nil {
+		visibleErr = LFT_DECRYPT_SM2_FAIL
+		internalErr = fmt.Errorf("decode hex data error: %s", err)
+		return
+	}
+	err = json.Unmarshal(pt, v)
+	if err != nil {
+		visibleErr = LFT_UNMARSHAL_JSON_FAIL
+		internalErr = fmt.Errorf("unmarshal json error: %s", err)
+		return
+	}
+	return LFT_SUCCESS, nil
+}
+
 func (h *ZLLAuthHandler) HandlingLogin(data string, serverExternalInfo map[string]interface{}) (isSuccess bool, jwtdata JwtResult, visibleErr LoginFailureType, internalErr error) {
 	if !h.isSecureInit {
 		isSuccess = false

+ 1 - 0
zllauth_gin/elh_int.go

@@ -13,4 +13,5 @@ type EventLogHandler interface {
 	JsonDecodeError(module string, req *http.Request, rawerr error)
 	LoginFailed(req *http.Request, visibleError zllauth1.LoginFailureType, rawerr error)
 	RenewJWTFailed(req *http.Request, visibleError zllauth1.LoginFailureType, rawerr error)
+	HandlingOtherEncryptedRequestFailed(req *http.Request, visibleError zllauth1.LoginFailureType, rawerr error)
 }

+ 67 - 0
zllauth_gin/gin_handler.go

@@ -192,6 +192,73 @@ func (h *ZLLAuthGinHandler) wh_Login(ctx *gin.Context) {
 	}
 }
 
+type HandlingOtherEncryptedRequestNextFunc func(returnData interface{})
+
+func (h *ZLLAuthGinHandler) HandlingOtherEncryptedRequest(ctx *gin.Context, v interface{}) (isOK bool, next HandlingOtherEncryptedRequestNextFunc) {
+	isOK = false
+	next = nil
+	var jdata JsonDef_LoginArugument
+	err := ctx.BindJSON(&jdata)
+	if err != nil {
+		if h.elh != nil {
+			h.elh.JsonDecodeError("oerutil", ctx.Request, err)
+		}
+		ctx.JSON(200, gin.H{
+			"suc":       false,
+			"err_hcode": 400,
+			"err_ecode": 2,
+			"err_msg":   "invalid arguments",
+		})
+		return
+	}
+	ve, ie := h.zllhdl.HandlingOtherEncryptedRequest(jdata.LoginData, v)
+	if h.elh != nil {
+		h.elh.HandlingOtherEncryptedRequestFailed(ctx.Request, ve, ie)
+	}
+	if ve == zllauth1.LFT_SUCCESS {
+		isOK = true
+		next = func(returnData interface{}) {
+			ctx.JSON(200, gin.H{
+				"suc":  true,
+				"data": returnData,
+			})
+		}
+		return
+	}
+	switch ve {
+	case zllauth1.LFT_DECODE_HEX_FAIL:
+		{
+			ctx.JSON(200, gin.H{
+				"suc":           true,
+				"login_suc":     false,
+				"login_ecode":   "argument_transport_error",
+				"external_info": "decode hex failed",
+			})
+			return
+		}
+	case zllauth1.LFT_DECRYPT_SM2_FAIL:
+		{
+			ctx.JSON(200, gin.H{
+				"suc":           true,
+				"login_suc":     false,
+				"login_ecode":   "argument_transport_error",
+				"external_info": "decrypt sm2 failed",
+			})
+			return
+		}
+	default:
+		{
+			ctx.JSON(200, gin.H{
+				"suc":       false,
+				"err_hcode": 502,
+				"err_ecode": 3,
+				"err_msg":   "internal server error",
+			})
+			return
+		}
+	}
+}
+
 func (h *ZLLAuthGinHandler) CheckJWT(jwtstr string) (ok bool, extData map[string]interface{}) {
 	se, exi, _, _ := h.zllhdl.CheckJWT(jwtstr)
 	if se {