This commit is contained in:
aozhiwei 2024-06-26 15:46:59 +08:00
parent 791d3cabf6
commit 1d2a5c140e
2 changed files with 88 additions and 1 deletions

View File

@ -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
)

View File

@ -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()
})
}