调整
This commit is contained in:
parent
338fe67a0e
commit
87dd2018b5
@ -3,7 +3,7 @@ package vip
|
||||
import (
|
||||
"f5"
|
||||
"main/constant"
|
||||
"main/middleware"
|
||||
"main/service"
|
||||
"q5"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@ -69,10 +69,10 @@ func (this *VIPApi) Bind(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
passportContext := c.Copy()
|
||||
passportContext.Request.Header.Set("Authorization", reqObj.Passport_jwt)
|
||||
middleware.JwtAuthOnly(passportContext)
|
||||
passport_addr := passportContext.GetString("account_address")
|
||||
info := map[string]interface{}{}
|
||||
info["account_address"] = ""
|
||||
service.JwtAuth.JwtAuthHandle(reqObj.Passport_jwt, &info)
|
||||
passport_addr := info["account_address"].(string)
|
||||
if passport_addr == "" {
|
||||
f5.RspErr(c, 1, "bad request passport")
|
||||
return
|
||||
|
@ -19,29 +19,14 @@ func JwtAuth(c *gin.Context) {
|
||||
internalJwtAuth(c, false)
|
||||
}
|
||||
|
||||
func JwtAuthOnly(c *gin.Context) {
|
||||
tokenHeader := c.Request.Header.Get("Authorization")
|
||||
jwtToken := ""
|
||||
if len(tokenHeader) > 8 {
|
||||
jwtToken = tokenHeader[7:len(tokenHeader)]
|
||||
}
|
||||
|
||||
tmpStrings := q5.StrSplit(jwtToken, ".")
|
||||
if len(tmpStrings) > 3 {
|
||||
internalMetaMaskJwtAuth(c, jwtToken, false)
|
||||
} else {
|
||||
internalImmutJwtAuth(c, jwtToken, false)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
'Authorization Bearer {JwtToken}'
|
||||
'Authorization Bearer {JwtToken}'
|
||||
*/
|
||||
func internalJwtAuth(c *gin.Context, maybe bool) {
|
||||
tokenHeader := c.Request.Header.Get("Authorization")
|
||||
jwtToken := ""
|
||||
if len(tokenHeader) > 8 {
|
||||
jwtToken = tokenHeader[7:len(tokenHeader)]
|
||||
jwtToken = tokenHeader[7:]
|
||||
} else {
|
||||
if maybe {
|
||||
c.Set("open_id", "")
|
||||
@ -54,40 +39,40 @@ func internalJwtAuth(c *gin.Context, maybe bool) {
|
||||
|
||||
tmpStrings := q5.StrSplit(jwtToken, ".")
|
||||
if len(tmpStrings) > 3 {
|
||||
internalMetaMaskJwtAuth(c, jwtToken, true)
|
||||
internalMetaMaskJwtAuth(c, jwtToken)
|
||||
} else {
|
||||
internalImmutJwtAuth(c, jwtToken, true)
|
||||
internalImmutJwtAuth(c, jwtToken)
|
||||
}
|
||||
}
|
||||
|
||||
func internalImmutJwtAuth(c *gin.Context, jwtToken string, rsperr bool) {
|
||||
func internalImmutJwtAuth(c *gin.Context, jwtToken string) {
|
||||
params := map[string]string{
|
||||
"c": "Jwt",
|
||||
"a": "verify",
|
||||
}
|
||||
jsonReqObj := &struct {
|
||||
JwksUri string `json:"jwksUri"`
|
||||
Data string `json:"data"`
|
||||
Data string `json:"data"`
|
||||
}{
|
||||
JwksUri: mt.Table.Config.GetJwksUri(),
|
||||
Data: jwtToken,
|
||||
Data: jwtToken,
|
||||
}
|
||||
jsonRspObj := &struct {
|
||||
ErrCode interface{} `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
Decoded struct {
|
||||
Sub string `json:"sub"`
|
||||
Email string `json:"email"`
|
||||
EmailVerified bool `json:"email_verified"`
|
||||
Sid string `json:"sid"`
|
||||
Passport struct {
|
||||
Sub string `json:"sub"`
|
||||
Email string `json:"email"`
|
||||
EmailVerified bool `json:"email_verified"`
|
||||
Sid string `json:"sid"`
|
||||
Passport struct {
|
||||
ZkevmEthAddress string `json:"zkevm_eth_address"`
|
||||
} `json:"passport"`
|
||||
} `json:"decoded"`
|
||||
}{}
|
||||
rspObj := &struct {
|
||||
ErrCode interface{} `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
}{}
|
||||
paramsStr := q5.EncodeJson(jsonReqObj)
|
||||
url := fmt.Sprintf("%s/webapp/index.php", mt.Table.Web3ServiceCluster.RandElement().GetUrl())
|
||||
@ -100,23 +85,19 @@ func internalImmutJwtAuth(c *gin.Context, jwtToken string, rsperr bool) {
|
||||
func(rsp f5.HttpCliResponse) {
|
||||
if rsp.GetErr() != nil ||
|
||||
!rsp.JsonParseOk() {
|
||||
if rsperr {
|
||||
rspObj.ErrCode = 500
|
||||
rspObj.ErrMsg = "server internal error"
|
||||
c.JSON(200, rspObj)
|
||||
c.Abort()
|
||||
}
|
||||
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 {
|
||||
if rsperr {
|
||||
rspObj.ErrCode = 501
|
||||
rspObj.ErrMsg = "jwt expired"
|
||||
c.JSON(200, rspObj)
|
||||
c.Abort()
|
||||
}
|
||||
rspObj.ErrCode = 501
|
||||
rspObj.ErrMsg = "jwt expired"
|
||||
c.JSON(200, rspObj)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
openId := fmt.Sprintf("%d_2006_%s", jccommon.IMTBL_CHANNEL, jsonRspObj.Decoded.Sub)
|
||||
@ -127,7 +108,7 @@ func internalImmutJwtAuth(c *gin.Context, jwtToken string, rsperr bool) {
|
||||
})
|
||||
}
|
||||
|
||||
func internalMetaMaskJwtAuth(c *gin.Context, jwtToken string, rsperr bool) {
|
||||
func internalMetaMaskJwtAuth(c *gin.Context, jwtToken string) {
|
||||
params := map[string]string{
|
||||
"c": "MetaMaskJwt",
|
||||
"a": "verify",
|
||||
@ -139,18 +120,18 @@ func internalMetaMaskJwtAuth(c *gin.Context, jwtToken string, rsperr bool) {
|
||||
}
|
||||
jsonRspObj := &struct {
|
||||
ErrCode interface{} `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
Decoded struct {
|
||||
Id string `json:"id"`
|
||||
OpenId string `json:"openid"`
|
||||
Plat int32 `json:"plat"`
|
||||
Version int32 `json:"version"`
|
||||
Email string `json:"email"`
|
||||
Id string `json:"id"`
|
||||
OpenId string `json:"openid"`
|
||||
Plat int32 `json:"plat"`
|
||||
Version int32 `json:"version"`
|
||||
Email string `json:"email"`
|
||||
} `json:"decoded"`
|
||||
}{}
|
||||
rspObj := &struct {
|
||||
ErrCode interface{} `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
}{}
|
||||
paramsStr := q5.EncodeJson(jsonReqObj)
|
||||
url := fmt.Sprintf("%s/webapp/index.php", mt.Table.Web3ServiceCluster.RandElement().GetUrl())
|
||||
@ -163,32 +144,26 @@ func internalMetaMaskJwtAuth(c *gin.Context, jwtToken string, rsperr bool) {
|
||||
func(rsp f5.HttpCliResponse) {
|
||||
if rsp.GetErr() != nil ||
|
||||
!rsp.JsonParseOk() {
|
||||
if rsperr {
|
||||
rspObj.ErrCode = 500
|
||||
rspObj.ErrMsg = "server internal error"
|
||||
c.JSON(200, rspObj)
|
||||
c.Abort()
|
||||
}
|
||||
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 {
|
||||
if rsperr {
|
||||
rspObj.ErrCode = 501
|
||||
rspObj.ErrMsg = "jwt error"
|
||||
c.JSON(200, rspObj)
|
||||
c.Abort()
|
||||
}
|
||||
rspObj.ErrCode = 501
|
||||
rspObj.ErrMsg = "jwt error"
|
||||
c.JSON(200, rspObj)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if jsonRspObj.Decoded.Plat != jccommon.BC_POLY_POLY_METAKASK {
|
||||
if rsperr {
|
||||
rspObj.ErrCode = 501
|
||||
rspObj.ErrMsg = "not support this platform"
|
||||
c.JSON(200, rspObj)
|
||||
c.Abort()
|
||||
}
|
||||
rspObj.ErrCode = 501
|
||||
rspObj.ErrMsg = "not support this platform"
|
||||
c.JSON(200, rspObj)
|
||||
c.Abort()
|
||||
f5.GetSysLog().Info("not support this platform:%s", rsp.GetRawData())
|
||||
return
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ var Contribution = new(contribution)
|
||||
var _serviceMgr = new(serviceMgr)
|
||||
var GameSwitches = new(gameSwitch)
|
||||
var User = new(user)
|
||||
var JwtAuth = new(jwtauth)
|
||||
|
||||
func init() {
|
||||
global.RegModule(constant.SERVICE_MGR_MODULE_IDX, _serviceMgr)
|
||||
|
144
server/marketserver/service/jwtauth.go
Normal file
144
server/marketserver/service/jwtauth.go
Normal file
@ -0,0 +1,144 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"f5"
|
||||
"fmt"
|
||||
"jccommon"
|
||||
"main/mt"
|
||||
"q5"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type jwtauth struct {
|
||||
}
|
||||
|
||||
func (this *jwtauth) init() {
|
||||
}
|
||||
|
||||
func (this *jwtauth) unInit() {
|
||||
}
|
||||
|
||||
func (this *jwtauth) JwtAuthHandle(srcToken string, rspinfo *map[string]interface{}) {
|
||||
jwtToken := ""
|
||||
if len(srcToken) > 8 {
|
||||
jwtToken = srcToken[7:]
|
||||
}
|
||||
|
||||
tmpStrings := q5.StrSplit(jwtToken, ".")
|
||||
if len(tmpStrings) > 3 {
|
||||
this.internalMetaMaskJwtAuth(jwtToken, rspinfo)
|
||||
} else {
|
||||
this.internalImmutJwtAuth(jwtToken, rspinfo)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *jwtauth) internalImmutJwtAuth(jwtToken string, rspinfo *map[string]interface{}) {
|
||||
params := map[string]string{
|
||||
"c": "Jwt",
|
||||
"a": "verify",
|
||||
}
|
||||
jsonReqObj := &struct {
|
||||
JwksUri string `json:"jwksUri"`
|
||||
Data string `json:"data"`
|
||||
}{
|
||||
JwksUri: mt.Table.Config.GetJwksUri(),
|
||||
Data: jwtToken,
|
||||
}
|
||||
jsonRspObj := &struct {
|
||||
ErrCode interface{} `json:"errcode"`
|
||||
ErrMsg string `json:"errmsg"`
|
||||
Decoded struct {
|
||||
Sub string `json:"sub"`
|
||||
Email string `json:"email"`
|
||||
EmailVerified bool `json:"email_verified"`
|
||||
Sid string `json:"sid"`
|
||||
Passport struct {
|
||||
ZkevmEthAddress string `json:"zkevm_eth_address"`
|
||||
} `json:"passport"`
|
||||
} `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() {
|
||||
return
|
||||
}
|
||||
rspObj.ErrCode = q5.SafeToInt32(jsonRspObj.ErrCode)
|
||||
rspObj.ErrMsg = jsonRspObj.ErrMsg
|
||||
if q5.SafeToInt32(rspObj.ErrCode) != 0 {
|
||||
return
|
||||
}
|
||||
openId := fmt.Sprintf("%d_2006_%s", jccommon.IMTBL_CHANNEL, jsonRspObj.Decoded.Sub)
|
||||
(*rspinfo)["open_id"] = openId
|
||||
(*rspinfo)["account_address"] = jsonRspObj.Decoded.Passport.ZkevmEthAddress
|
||||
(*rspinfo)["email"] = jsonRspObj.Decoded.Email
|
||||
})
|
||||
}
|
||||
|
||||
func (this *jwtauth) internalMetaMaskJwtAuth(jwtToken string, rspinfo *map[string]interface{}) {
|
||||
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"`
|
||||
Email string `json:"email"`
|
||||
} `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() {
|
||||
return
|
||||
}
|
||||
rspObj.ErrCode = q5.SafeToInt32(jsonRspObj.ErrCode)
|
||||
rspObj.ErrMsg = jsonRspObj.ErrMsg
|
||||
if q5.SafeToInt32(rspObj.ErrCode) != 0 {
|
||||
|
||||
return
|
||||
}
|
||||
if jsonRspObj.Decoded.Plat != jccommon.BC_POLY_POLY_METAKASK {
|
||||
f5.GetSysLog().Info("not support this platform:%s", rsp.GetRawData())
|
||||
return
|
||||
}
|
||||
openId := fmt.Sprintf("%d_2006_%s",
|
||||
jccommon.BC_CHANNEL,
|
||||
strings.ToLower(jsonRspObj.Decoded.OpenId))
|
||||
(*rspinfo)["open_id"] = openId
|
||||
(*rspinfo)["account_address"] = strings.ToLower(jsonRspObj.Decoded.OpenId)
|
||||
(*rspinfo)["email"] = strings.ToLower(jsonRspObj.Decoded.Email)
|
||||
})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user