Skip to content

Commit

Permalink
update resp path
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Sep 14, 2019
1 parent 8c0e6a6 commit 542d901
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 61 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Change Log 更新说明
------------------------------
## 2019-09-12 v1.2.4
1. gf升级为V2.0.0
2. 优化resp目录

## 2019-09-12 v1.2.3
1. gf升级为V1.8.3
Expand Down
7 changes: 3 additions & 4 deletions example/boot/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/os/glog"
"gtoken/gtoken"
"gtoken/utils/resp"
)

/*
Expand All @@ -16,10 +15,10 @@ func bindRouter() {
s := g.Server()
// 调试路由
s.BindHandler("/hello", func(r *ghttp.Request) {
r.Response.WriteJson(resp.Succ("hello"))
r.Response.WriteJson(gtoken.Succ("hello"))
})
s.BindHandler("/system/user", func(r *ghttp.Request) {
r.Response.WriteJson(resp.Succ("system user"))
r.Response.WriteJson(gtoken.Succ("system user"))
})

loginFunc := Login
Expand Down Expand Up @@ -70,7 +69,7 @@ func Login(r *ghttp.Request) (string, interface{}) {
passwd := r.GetPostString("passwd")

if username == "" || passwd == "" {
r.Response.WriteJson(resp.Fail("账号或密码错误."))
r.Response.WriteJson(gtoken.Fail("账号或密码错误."))
r.ExitAll()
}

Expand Down
12 changes: 6 additions & 6 deletions example/test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
"gtoken/utils/resp"
"gtoken/gtoken"
"testing"
)

Expand All @@ -27,7 +27,7 @@ func TestHello(t *testing.T) {
content := string(r.ReadAll())
t.Log(content)

var respData resp.Resp
var respData gtoken.Resp
err := json.Unmarshal([]byte(content), &respData)
if err != nil {
t.Error(err)
Expand All @@ -49,7 +49,7 @@ func TestSystemUser(t *testing.T) {
content := string(r.ReadAll())
t.Log(content)

var respData resp.Resp
var respData gtoken.Resp
err := json.Unmarshal([]byte(content), &respData)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -137,11 +137,11 @@ func TestLogout(t *testing.T) {
Username = "flyfox"
}

func Post(t *testing.T, urlPath string, data ...interface{}) resp.Resp {
func Post(t *testing.T, urlPath string, data ...interface{}) gtoken.Resp {
client := ghttp.NewClient()
client.SetHeader("Authorization", "Bearer "+getToken(t))
content := client.RequestContent("POST", TestURL+urlPath, data...)
var respData resp.Resp
var respData gtoken.Resp
err := json.Unmarshal([]byte(content), &respData)
if err != nil {
t.Error(err)
Expand All @@ -161,7 +161,7 @@ func getToken(t *testing.T) string {

content := string(r.ReadAll())

var respData resp.Resp
var respData gtoken.Resp
err := json.Unmarshal([]byte(content), &respData)
if err != nil {
t.Error(err)
Expand Down
65 changes: 32 additions & 33 deletions gtoken/gtoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/grand"
"gtoken/utils/resp"
"strings"
)

Expand Down Expand Up @@ -40,23 +39,23 @@ type GfToken struct {
// return userKey 用户标识 如果userKey为空,结束执行
LoginBeforeFunc func(r *ghttp.Request) (string, interface{})
// 登录返回方法
LoginAfterFunc func(r *ghttp.Request, respData resp.Resp)
LoginAfterFunc func(r *ghttp.Request, respData Resp)

// 登出地址
LogoutPath string
// 登出验证方法
// return true 继续执行,否则结束执行
LogoutBeforeFunc func(r *ghttp.Request) bool
// 登出返回方法
LogoutAfterFunc func(r *ghttp.Request, respData resp.Resp)
LogoutAfterFunc func(r *ghttp.Request, respData Resp)

// 拦截地址
AuthPaths g.SliceStr
// 认证验证方法
// return true 继续执行,否则结束执行
AuthBeforeFunc func(r *ghttp.Request) bool
// 认证返回方法
AuthAfterFunc func(r *ghttp.Request, respData resp.Resp)
AuthAfterFunc func(r *ghttp.Request, respData Resp)
}

// Init 初始化
Expand Down Expand Up @@ -86,11 +85,11 @@ func (m *GfToken) Init() bool {
}

if m.LoginAfterFunc == nil {
m.LoginAfterFunc = func(r *ghttp.Request, respData resp.Resp) {
m.LoginAfterFunc = func(r *ghttp.Request, respData Resp) {
if !respData.Success() {
r.Response.WriteJson(respData)
} else {
r.Response.WriteJson(resp.Succ(g.Map{
r.Response.WriteJson(Succ(g.Map{
"token": respData.GetString("token"),
}))
}
Expand All @@ -104,9 +103,9 @@ func (m *GfToken) Init() bool {
}

if m.LogoutAfterFunc == nil {
m.LogoutAfterFunc = func(r *ghttp.Request, respData resp.Resp) {
m.LogoutAfterFunc = func(r *ghttp.Request, respData Resp) {
if respData.Success() {
r.Response.WriteJson(resp.Succ("logout success"))
r.Response.WriteJson(Succ("logout success"))
} else {
r.Response.WriteJson(respData)
}
Expand All @@ -119,7 +118,7 @@ func (m *GfToken) Init() bool {
}
}
if m.AuthAfterFunc == nil {
m.AuthAfterFunc = func(r *ghttp.Request, respData resp.Resp) {
m.AuthAfterFunc = func(r *ghttp.Request, respData Resp) {
if !respData.Success() {
r.Response.WriteJson(respData)
r.ExitAll()
Expand Down Expand Up @@ -178,7 +177,7 @@ func (m *GfToken) Stop() bool {
}

// GetTokenData 通过token获取对象
func (m *GfToken) GetTokenData(r *ghttp.Request) resp.Resp {
func (m *GfToken) GetTokenData(r *ghttp.Request) Resp {
respData := m.getRequestToken(r)
if respData.Success() {
// 验证token
Expand Down Expand Up @@ -228,31 +227,31 @@ func (m *GfToken) authHook(r *ghttp.Request) {
}

// getRequestToken 返回请求Token
func (m *GfToken) getRequestToken(r *ghttp.Request) resp.Resp {
func (m *GfToken) getRequestToken(r *ghttp.Request) Resp {
authHeader := r.Header.Get("Authorization")
if authHeader != "" {
parts := strings.SplitN(authHeader, " ", 2)
if !(len(parts) == 2 && parts[0] == "Bearer") {
glog.Warning("[GToken]authHeader:" + authHeader + " get token key fail")
return resp.Unauthorized("get token key fail", "")
return Unauthorized("get token key fail", "")
} else if parts[1] == "" {
glog.Warning("[GToken]authHeader:" + authHeader + " get token fail")
return resp.Unauthorized("get token fail", "")
return Unauthorized("get token fail", "")
}

return resp.Succ(parts[1])
return Succ(parts[1])
}

authHeader = r.GetPostString("token")
if authHeader == "" {
return resp.Unauthorized("query token fail", "")
return Unauthorized("query token fail", "")
}
return resp.Succ(authHeader)
return Succ(authHeader)

}

// genToken 生成Token
func (m *GfToken) genToken(userKey string, data interface{}) resp.Resp {
func (m *GfToken) genToken(userKey string, data interface{}) Resp {
token := m.EncryptToken(userKey)
if !token.Success() {
return token
Expand All @@ -276,9 +275,9 @@ func (m *GfToken) genToken(userKey string, data interface{}) resp.Resp {
}

// validToken 验证Token
func (m *GfToken) validToken(token string) resp.Resp {
func (m *GfToken) validToken(token string) Resp {
if token == "" {
return resp.Unauthorized("valid token empty", "")
return Unauthorized("valid token empty", "")
}

decryptToken := m.DecryptToken(token)
Expand All @@ -298,7 +297,7 @@ func (m *GfToken) validToken(token string) resp.Resp {

if uuid != userCache["uuid"] {
glog.Error("[GToken]user auth error, decryptToken:" + decryptToken.Json() + " cacheValue:" + gconv.String(userCache))
return resp.Unauthorized("user auth error", "")
return Unauthorized("user auth error", "")
}

nowTime := gtime.Now().Millisecond()
Expand All @@ -312,11 +311,11 @@ func (m *GfToken) validToken(token string) resp.Resp {
return m.setCache(cacheKey, userCache)
}

return resp.Succ(userCache)
return Succ(userCache)
}

// removeToken 删除Token
func (m *GfToken) removeToken(token string) resp.Resp {
func (m *GfToken) removeToken(token string) Resp {
decryptToken := m.DecryptToken(token)
if !decryptToken.Success() {
return decryptToken
Expand All @@ -327,54 +326,54 @@ func (m *GfToken) removeToken(token string) resp.Resp {
}

// EncryptToken token加密方法
func (m *GfToken) EncryptToken(userKey string) resp.Resp {
func (m *GfToken) EncryptToken(userKey string) Resp {
if userKey == "" {
return resp.Fail("encrypt userKey empty")
return Fail("encrypt userKey empty")
}

uuid, err := gmd5.Encrypt(grand.Str(10))
if err != nil {
glog.Error("[GToken]uuid error", err)
return resp.Error("uuid error")
return Error("uuid error")
}
tokenStr := userKey + m.TokenDelimiter + uuid

token, err := gaes.Encrypt([]byte(tokenStr), m.EncryptKey)
if err != nil {
glog.Error("[GToken]encrypt error", err)
return resp.Error("encrypt error")
return Error("encrypt error")
}

return resp.Succ(g.Map{
return Succ(g.Map{
"userKey": userKey,
"uuid": uuid,
"token": gbase64.Encode(token),
})
}

// DecryptToken token解密方法
func (m *GfToken) DecryptToken(token string) resp.Resp {
func (m *GfToken) DecryptToken(token string) Resp {
if token == "" {
return resp.Fail("decrypt token empty")
return Fail("decrypt token empty")
}

token64, err := gbase64.Decode([]byte(token))
if err != nil {
glog.Error("[GToken]decode error", err)
return resp.Error("decode error")
return Error("decode error")
}
decryptToken, err2 := gaes.Decrypt([]byte(token64), m.EncryptKey)
if err2 != nil {
glog.Error("[GToken]decrypt error", err2)
return resp.Error("decrypt error")
return Error("decrypt error")
}
tokenArray := gstr.Split(string(decryptToken), m.TokenDelimiter)
if len(tokenArray) < 2 {
glog.Error("[GToken]token len error")
return resp.Error("token len error")
return Error("token len error")
}

return resp.Succ(g.Map{
return Succ(g.Map{
"userKey": tokenArray[0],
"uuid": tokenArray[1],
})
Expand Down
Loading

0 comments on commit 542d901

Please sign in to comment.