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.
IsReadBody bool
IsReadHeader bool
// If IsReadCookie is set to true, a cookie will be set after successful login
IsReadCookie bool
// Write token to response header.

View File

@@ -333,7 +333,7 @@ func (e *Enforcer) LogoutByToken(token string) error {
// delete token-id
id := e.GetIdByToken(token)
if id == "" {
return errors.New("not logged in")
return errors.New("user not logged in")
}
// delete token-id
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.
// check all tokenValue and if one is validated return true
func (e *Enforcer) IsLoginById(id string) (bool, error) {
var error error
var err error
session := e.GetSession(id)
if session != nil {
l := session.TokenSignList
for _, tokenSign := range l {
str := e.GetIdByToken(tokenSign.Value)
if str == "" {
continue
}
value, err := e.validateValue(str)
err = e.CheckLoginByToken(tokenSign.Value)
if err != nil {
error = err
continue
}
if value {
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 {
token := e.GetRequestToken(ctx)
return e.GetIdByToken(token)
}
// GetIdByToken get the id from the Adapter
func (e *Enforcer) GetIdByToken(token string) string {
if token == "" {
return ""
@@ -415,12 +412,13 @@ func (e *Enforcer) IsLoginByToken(tokenValue string) (bool, error) {
if tokenValue == "" {
return false, nil
}
str := e.GetIdByToken(tokenValue)
if str == "" {
return false, nil
err := e.CheckLoginByToken(tokenValue)
if err != nil {
return false, err
}
return e.validateValue(str)
return true, nil
}
func (e *Enforcer) CheckLogin(ctx ctx.Context) error {
@@ -439,7 +437,7 @@ func (e *Enforcer) CheckLoginByToken(token string) error {
return nil
}
// GetLoginId get id and validate it
// GetLoginId get id and check it
func (e *Enforcer) GetLoginId(ctx ctx.Context) (string, error) {
tokenValue := e.GetRequestToken(ctx)
return e.GetLoginIdByToken(tokenValue)
@@ -450,7 +448,7 @@ func (e *Enforcer) GetLoginIdByToken(token string) (string, error) {
if str == "" {
return "", errors.New("GetLoginId() failed: not logged in")
}
validate, err := e.validateValue(str)
validate, err := e.checkId(str)
if !validate {
return "", err
}

View File

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

View File

@@ -254,8 +254,8 @@ func TestEnforcer_Logout(t *testing.T) {
if login {
t.Errorf("IsLogin() failed: unexpected value %v", login)
}
if err != nil {
t.Errorf("err: %v", err)
if login && err != nil {
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")
)