feat: improve code, add errors

This commit is contained in:
weloe
2023-10-12 16:31:44 +08:00
parent 2d802fd47b
commit 05d6f38f64
5 changed files with 37 additions and 27 deletions

View File

@@ -30,6 +30,7 @@ type TokenConfig struct {
// Set to true to read token from these method before login. // Set to true to read token from these method before login.
IsReadBody bool IsReadBody bool
IsReadHeader bool IsReadHeader bool
// If IsReadCookie is set to true, a cookie will be set after successful login
IsReadCookie bool IsReadCookie bool
// Write token to response header. // Write token to response header.

View File

@@ -333,7 +333,7 @@ func (e *Enforcer) LogoutByToken(token string) error {
// delete token-id // delete token-id
id := e.GetIdByToken(token) id := e.GetIdByToken(token)
if id == "" { if id == "" {
return errors.New("not logged in") return errors.New("user not logged in")
} }
// delete token-id // delete token-id
err = e.adapter.Delete(e.spliceTokenKey(token)) err = e.adapter.Delete(e.spliceTokenKey(token))
@@ -369,35 +369,32 @@ func (e *Enforcer) LogoutByToken(token string) error {
// IsLoginById check if user logged in by loginId. // IsLoginById check if user logged in by loginId.
// check all tokenValue and if one is validated return true // check all tokenValue and if one is validated return true
func (e *Enforcer) IsLoginById(id string) (bool, error) { func (e *Enforcer) IsLoginById(id string) (bool, error) {
var error error var err error
session := e.GetSession(id) session := e.GetSession(id)
if session != nil { if session != nil {
l := session.TokenSignList l := session.TokenSignList
for _, tokenSign := range l { for _, tokenSign := range l {
str := e.GetIdByToken(tokenSign.Value) err = e.CheckLoginByToken(tokenSign.Value)
if str == "" {
continue
}
value, err := e.validateValue(str)
if err != nil { if err != nil {
error = err
continue continue
} }
if value { return true, nil
return true, nil
}
} }
} }
return false, error return false, err
} }
// GetId get id // GetId get the id from the Adapter, do not check the value
// if GetId()= -4, it means that user be replaced
// if GetId()= -5, it means that user be kicked
// if GetId()= -6, it means that user be banned
func (e *Enforcer) GetId(ctx ctx.Context) string { func (e *Enforcer) GetId(ctx ctx.Context) string {
token := e.GetRequestToken(ctx) token := e.GetRequestToken(ctx)
return e.GetIdByToken(token) return e.GetIdByToken(token)
} }
// GetIdByToken get the id from the Adapter
func (e *Enforcer) GetIdByToken(token string) string { func (e *Enforcer) GetIdByToken(token string) string {
if token == "" { if token == "" {
return "" return ""
@@ -415,12 +412,13 @@ func (e *Enforcer) IsLoginByToken(tokenValue string) (bool, error) {
if tokenValue == "" { if tokenValue == "" {
return false, nil return false, nil
} }
str := e.GetIdByToken(tokenValue)
if str == "" { err := e.CheckLoginByToken(tokenValue)
return false, nil if err != nil {
return false, err
} }
return e.validateValue(str) return true, nil
} }
func (e *Enforcer) CheckLogin(ctx ctx.Context) error { func (e *Enforcer) CheckLogin(ctx ctx.Context) error {
@@ -439,7 +437,7 @@ func (e *Enforcer) CheckLoginByToken(token string) error {
return nil return nil
} }
// GetLoginId get id and validate it // GetLoginId get id and check it
func (e *Enforcer) GetLoginId(ctx ctx.Context) (string, error) { func (e *Enforcer) GetLoginId(ctx ctx.Context) (string, error) {
tokenValue := e.GetRequestToken(ctx) tokenValue := e.GetRequestToken(ctx)
return e.GetLoginIdByToken(tokenValue) return e.GetLoginIdByToken(tokenValue)
@@ -450,7 +448,7 @@ func (e *Enforcer) GetLoginIdByToken(token string) (string, error) {
if str == "" { if str == "" {
return "", errors.New("GetLoginId() failed: not logged in") return "", errors.New("GetLoginId() failed: not logged in")
} }
validate, err := e.validateValue(str) validate, err := e.checkId(str)
if !validate { if !validate {
return "", err return "", err
} }

View File

@@ -1,9 +1,9 @@
package token_go package token_go
import ( import (
"errors"
"github.com/weloe/token-go/constant" "github.com/weloe/token-go/constant"
"github.com/weloe/token-go/ctx" "github.com/weloe/token-go/ctx"
"github.com/weloe/token-go/errors"
"github.com/weloe/token-go/model" "github.com/weloe/token-go/model"
"math" "math"
"strconv" "strconv"
@@ -92,21 +92,21 @@ func (e *Enforcer) responseToken(tokenValue string, loginModel *model.Login, ctx
return nil return nil
} }
// validateValue validate if value is proper // checkId check id
func (e *Enforcer) validateValue(str string) (bool, error) { func (e *Enforcer) checkId(str string) (bool, error) {
i, err := strconv.Atoi(str) i, err := strconv.Atoi(str)
// if convert err return true // if convert err return true
if err != nil { if err != nil {
return true, nil return true, nil
} }
if i == constant.BeReplaced { if i == constant.BeReplaced {
return false, errors.New("this account is replaced") return false, errors.BeReplaced
} }
if i == constant.BeKicked { if i == constant.BeKicked {
return false, errors.New("this account is kicked out") return false, errors.BeKicked
} }
if i == constant.BeBanned { if i == constant.BeBanned {
return false, errors.New("this account is banned") return false, errors.BeBanned
} }
return true, nil return true, nil
} }

View File

@@ -254,8 +254,8 @@ func TestEnforcer_Logout(t *testing.T) {
if login { if login {
t.Errorf("IsLogin() failed: unexpected value %v", login) t.Errorf("IsLogin() failed: unexpected value %v", login)
} }
if err != nil { if login && err != nil {
t.Errorf("err: %v", err) t.Errorf("IsLogin() returns unexpected error: %v", err)
} }
} }

11
errors/id_error.go Normal file
View File

@@ -0,0 +1,11 @@
package errors
import (
"errors"
)
var (
BeReplaced = errors.New("this account is replaced")
BeKicked = errors.New("this account is kicked out")
BeBanned = errors.New("this account is banned")
)