From a685c53dfc144959ef3556f135b59350af2978b9 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 13 Jul 2024 15:02:41 +0800 Subject: [PATCH] 1 --- doc/admin/User.js | 33 ++++++ server/adminserver/api/v1/system/sys_user.go | 112 ++++++++++++------- server/adminserver/app/app.go | 47 ++------ server/adminserver/middleware/auth.go | 12 +- server/adminserver/router/system/sys_user.go | 1 + third_party/f5 | 2 +- 6 files changed, 118 insertions(+), 89 deletions(-) diff --git a/doc/admin/User.js b/doc/admin/User.js index 1bf3ad9c..bc25b02b 100644 --- a/doc/admin/User.js +++ b/doc/admin/User.js @@ -34,6 +34,39 @@ module.exports = class { new common.RspHead(), ] }, + { + 'method': 'GET', + 'name': 'api/v1/user/getNonce', + 'desc': '获取nonce', + 'group': 'User', + 'url': 'api/v1/user/getNonce', + 'uri_params': [ + ['account', '', 'account'], + ], + 'response': [ + new common.RspHead(), + ['data', '', 'nonce'] + ] + }, + { + 'method': 'POST', + 'name': 'metamask_login', + 'desc': 'metamask用户登录', + 'group': 'User', + 'url': 'api/v1/user/metamask_login', + 'header': [ + ], + 'is_json_params': true, + 'params': [ + ['account', '', 'account'], + ['nonce', '', 'nonce'], + ['signature', '', 'signature'], + ['tips', '', 'tips'], + ], + 'response': [ + new common.RspHead(), + ] + }, ]; } diff --git a/server/adminserver/api/v1/system/sys_user.go b/server/adminserver/api/v1/system/sys_user.go index bf54f539..5c4aa32f 100644 --- a/server/adminserver/api/v1/system/sys_user.go +++ b/server/adminserver/api/v1/system/sys_user.go @@ -1,9 +1,10 @@ package system import ( + "q5" "f5" "github.com/gin-gonic/gin" - "main/constant" + "mt" . "main/global" "main/model/system" "net/http" @@ -15,50 +16,11 @@ type UserApi struct { } func (this *UserApi) Login(c *gin.Context) { - //username := c.PostForm("username") - //password := c.PostForm("password") - //if username == "" || password == "" { - // c.JSON(http.StatusOK, gin.H{ - // "errcode": 1, - // "errmsg": "请求参数不正确", - // }) - // return - //} - type loginForm struct { - Username string `binding:"required" json:"username"` - Password string `binding:"required" json:"password"` - } - reqJson := loginForm{} - if err := c.ShouldBindJSON(&reqJson); err != nil { - c.JSON(http.StatusOK, gin.H{ - "code": 1, - "message": err.Error(), - }) - return - } - user := system.SysUser{} - //err := f5.GetApp().GetOrmDb(constant.ADMIN_DB).Where("username = ?", username).Where("password = ?", password).First(&user).Error - err := f5.GetApp().GetOrmDb(constant.ADMIN_DB).Where("username = ?", reqJson.Username).Where("password = ?", reqJson.Password).First(&user).Error - if err != nil { - c.JSON(http.StatusOK, gin.H{ - "code": 1, - "message": "用户名或密码错误", - }) - return - } - //token := GetApp().AddSession(username) - token := GetApp().AddSession(reqJson.Username) - c.JSON(http.StatusOK, gin.H{ - "code": 0, - "message": "登录成功", - "data": user, - "token": token, - }) } func (this *UserApi) Info(c *gin.Context) { - token := c.Request.Header.Get("Authorization") - strArr := strings.Split(token, "|") + //token := c.Request.Header.Get("Authorization") + //strArr := strings.Split(token, "|") //username := c.Query("username") //if username == "" { // c.JSON(http.StatusOK, gin.H{ @@ -68,6 +30,19 @@ func (this *UserApi) Info(c *gin.Context) { // return //} + user := new(system.SysUser) + user.Idx = 1000 + user.Username = "" + user.Password = "" + q5.NewSlice(&user.Roles, 0, 10) + q5.AppendSlice(&user.Roles, "admin") + c.JSON(http.StatusOK, gin.H{ + "code": 0, + "message": "success", + "data": user, + }) + + /* user := system.SysUser{} err := f5.GetApp().GetOrmDb(constant.ADMIN_DB).Where("username = ?", strArr[0]).First(&user).Error if err != nil { @@ -83,7 +58,7 @@ func (this *UserApi) Info(c *gin.Context) { "code": 0, "message": "success", "data": user, - }) + })*/ } func (this *UserApi) Logout(c *gin.Context) { @@ -95,3 +70,54 @@ func (this *UserApi) Logout(c *gin.Context) { "message": "success", }) } + +func (this *UserApi) MetaMaskLogin(c *gin.Context) { + reqJson := struct { + AccountAddress string `json:"account"` + Nonce string `json:"nonce"` + Signature string `json:"signature"` + Tips string `json:"tips"` + }{} + if err := c.ShouldBindJSON(&reqJson); err != nil { + f5.RspErr2(c, 1, err.Error()) + return + } + if len(reqJson.Nonce) > 1024 || len(reqJson.Signature) > 1024 || len(reqJson.Tips) > 1024 { + f5.RspErr2(c, 2, "param error") + return + } + + f5.GetHttpCliMgr().SendGoStyleRequest( + mt.Table.Web3ServiceCluster.RandElement().GetUrl() + "/webapp/index.php", + map[string]string { + "c": "BcService", + "a": "authVerifySignature", + "tips": reqJson.Tips, + "nonce": reqJson.Nonce, + "signature": reqJson.Signature, + }, + func (rsp f5.HttpCliResponse) { + if rsp.GetErr() != nil { + f5.RspErr2(c, 500, rsp.GetErr().Error()) + return + } + rspJson := struct { + ErrCode int32 `json:"errcode"` + ErrMsg string `json:"errmsg"` + Recovered string `json:"recovered"` + }{} + if q5.DecodeJson(rsp.GetRawData(), &rspJson) != nil { + f5.RspErr2(c, 500, "server internal error") + return + } + if strings.ToLower(rspJson.Recovered) != strings.ToLower(reqJson.AccountAddress) { + f5.RspErr2(c, 500, "server internal error") + return + } + c.JSON(http.StatusOK, gin.H{ + "code": 0, + "message": "success", + "token": "312424", + }) + }) +} diff --git a/server/adminserver/app/app.go b/server/adminserver/app/app.go index 7ad71210..9f09909e 100644 --- a/server/adminserver/app/app.go +++ b/server/adminserver/app/app.go @@ -1,24 +1,20 @@ package app import ( - "crypto/md5" - "encoding/hex" + //"crypto/md5" + //"encoding/hex" "f5" - "fmt" + //"fmt" "main/constant" "main/task" - "math/rand" "mt" - "sync" - "time" + //"sync" + //"time" ) type app struct { initCb func() unInitCb func() - sessionLock sync.Mutex - sessionHash map[string]string - accountIdHash map[string]string } func (this *app) GetPkgName() string { @@ -38,8 +34,6 @@ func (this *app) Run(initCb func(), unInitCb func()) { func (this *app) Init() { f5.LoadMetaTable(mt.Table) this.registerDataSources() - this.sessionHash = make(map[string]string) - this.accountIdHash = make(map[string]string) task.TaskMgr.Init() this.initCb() } @@ -130,6 +124,7 @@ func (this *app) registerDataSources() { } func (this *app) AddSession(accountId string) string { + /* this.sessionLock.Lock() defer this.sessionLock.Unlock() uuid := f5.GetApp().NewGlobalUuid() @@ -141,34 +136,8 @@ func (this *app) AddSession(accountId string) string { token := accountId + "|" + md5String this.sessionHash[accountId] = token return token -} - -const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" - -func randStringBytes(n int) string { - b := make([]byte, n) - for i := range b { - b[i] = letterBytes[rand.Intn(len(letterBytes))] - } - return string(b) -} - -func (this *app) GetSessionAccountId(accountId string) string { - this.sessionLock.Lock() - defer this.sessionLock.Unlock() - if session, ok := this.sessionHash[accountId]; ok { - return session - } else { - return "nil" - } -} - -func (this *app) RemoveSession(accountId string) { - this.sessionLock.Lock() - defer this.sessionLock.Unlock() - if _, ok := this.sessionHash[accountId]; ok { - delete(this.sessionHash, accountId) - } + */ + return "" } func (this *app) HasTask() bool { diff --git a/server/adminserver/middleware/auth.go b/server/adminserver/middleware/auth.go index 2995b601..82a8b340 100644 --- a/server/adminserver/middleware/auth.go +++ b/server/adminserver/middleware/auth.go @@ -2,13 +2,14 @@ package middleware import ( "github.com/gin-gonic/gin" - . "main/global" - "net/http" - "strings" + //. "main/global" + //"net/http" + //"strings" ) func Auth() gin.HandlerFunc { return func(c *gin.Context) { + /* token := c.Request.Header.Get("Authorization") strArr := strings.Split(token, "|") authToken := GetApp().GetSessionAccountId(strArr[0]) @@ -17,11 +18,10 @@ func Auth() gin.HandlerFunc { "code": 50014, "message": "未登录或非法访问", }) - /* - response.FailWithDetailed(gin.H{"reload": true}, "未登录或非法访问", c)*/ + response.FailWithDetailed(gin.H{"reload": true}, "未登录或非法访问", c) c.Abort() return - } + }*/ c.Next() } } diff --git a/server/adminserver/router/system/sys_user.go b/server/adminserver/router/system/sys_user.go index 964d0101..18ce2535 100644 --- a/server/adminserver/router/system/sys_user.go +++ b/server/adminserver/router/system/sys_user.go @@ -15,6 +15,7 @@ func (this *UserRouter) InitUserRouter(priRouter *gin.RouterGroup, { pubUserRouter.POST("login", userApi.Login) pubUserRouter.GET("getNonce", userApi.GetNonce) + pubUserRouter.POST("metamask_login", userApi.MetaMaskLogin) } { priUserRouter.GET("info", userApi.Info) diff --git a/third_party/f5 b/third_party/f5 index ac6d1f71..5610bc1f 160000 --- a/third_party/f5 +++ b/third_party/f5 @@ -1 +1 @@ -Subproject commit ac6d1f71f5281b1a10c7fadce50ef65e48a9f41f +Subproject commit 5610bc1f758e6b7b7738e4ac4ee492206b26b3e3