diff --git a/server/jccommon/constant.go b/server/jccommon/constant.go index ace0c6fa..0d4ee765 100644 --- a/server/jccommon/constant.go +++ b/server/jccommon/constant.go @@ -76,3 +76,13 @@ const ( EVENT_MAIL_UPDATE = "mail.update" EVENT_UPSER_GROUP_UPDATE = "user_group.update" ) + +const ( + IMTBL_CHANNEL = 1 + GUEST_CHANNEL = 2 + BC_CHANNEL = 3 +) + +const ( + BC_POLY_POLY_METAKASK = 13 +) diff --git a/server/marketserver/middleware/jwtauth.go b/server/marketserver/middleware/jwtauth.go index 1a15c00c..608539cf 100644 --- a/server/marketserver/middleware/jwtauth.go +++ b/server/marketserver/middleware/jwtauth.go @@ -5,6 +5,8 @@ import ( "f5" "mt" "fmt" + "jccommon" + "strings" "github.com/gin-gonic/gin" ) @@ -32,6 +34,15 @@ func internalJwtAuth(c *gin.Context, maybe bool) { } } + tmpStrings := q5.StrSplit(jwtToken, ".") + if len(tmpStrings) > 3 { + internalMetaMaskJwtAuth(c, jwtToken) + } else { + internalImmutJwtAuth(c, jwtToken) + } +} + +func internalImmutJwtAuth(c *gin.Context, jwtToken string) { params := map[string]string{ "c": "Jwt", "a": "verify", @@ -83,7 +94,73 @@ func internalJwtAuth(c *gin.Context, maybe bool) { c.Abort() return } - c.Set("open_id", jsonRspObj.Decoded.Sub) + openId := fmt.Sprintf("%d_2006_%s", jccommon.IMTBL_CHANNEL, jsonRspObj.Decoded.Sub) + c.Set("open_id", openId) + c.Next() + }) +} + +func internalMetaMaskJwtAuth(c *gin.Context, jwtToken string) { + params := map[string]string{ + "c": "MetaMaskJwt", + "a": "verify", + } + jsonReqObj := &struct { + Data string `json:"data"` + }{ + Data: jwtToken, + } + jsonRspObj := &struct { + ErrCode interface{} `json:"errcode"` + ErrMsg string `json:"errmsg"` + Decoded struct { + Id string `json:"id"` + OpenId string `json:"openid"` + Plat int32 `json:"plat"` + Version int32 `json:"version"` + } `json:"decoded"` + }{} + rspObj := &struct { + ErrCode interface{} `json:"errcode"` + ErrMsg string `json:"errmsg"` + }{} + paramsStr := q5.EncodeJson(jsonReqObj) + url := fmt.Sprintf("%s/webapp/index.php", mt.Table.Web3ServiceCluster.RandElement().GetUrl()) + f5.GetHttpCliMgr().SendGoStyleJsonRspPost( + url, + params, + jsonRspObj, + q5.HTTP_HEADER_JSON, + paramsStr, + func(rsp f5.HttpCliResponse) { + if rsp.GetErr() != nil || + !rsp.JsonParseOk() { + rspObj.ErrCode = 500 + rspObj.ErrMsg = "server internal error" + c.JSON(200, rspObj) + c.Abort() + return + } + rspObj.ErrCode = q5.SafeToInt32(jsonRspObj.ErrCode) + rspObj.ErrMsg = jsonRspObj.ErrMsg + if q5.SafeToInt32(rspObj.ErrCode) != 0 { + rspObj.ErrCode = 501 + rspObj.ErrMsg = "jwt error" + c.JSON(200, rspObj) + c.Abort() + return + } + if jsonRspObj.Decoded.Plat != jccommon.BC_POLY_POLY_METAKASK { + rspObj.ErrCode = 501 + rspObj.ErrMsg = "not summport platform" + c.JSON(200, rspObj) + c.Abort() + return + } + openId := fmt.Sprintf("%d_2006_%s", + jccommon.BC_CHANNEL, + strings.ToLower(jsonRspObj.Decoded.OpenId)) + c.Set("open_id", openId) c.Next() }) }