diff --git a/config/token.go b/config/token.go index 63342df..d1906cd 100644 --- a/config/token.go +++ b/config/token.go @@ -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. diff --git a/enforcer.go b/enforcer.go index 65c4489..bf7fadf 100644 --- a/enforcer.go +++ b/enforcer.go @@ -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 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 } diff --git a/enforcer_internal_api.go b/enforcer_internal_api.go index 882de6a..ef8cd82 100644 --- a/enforcer_internal_api.go +++ b/enforcer_internal_api.go @@ -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 } diff --git a/enforcer_test.go b/enforcer_test.go index 4ed4225..a8be12a 100644 --- a/enforcer_test.go +++ b/enforcer_test.go @@ -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) } } diff --git a/errors/id_error.go b/errors/id_error.go new file mode 100644 index 0000000..c8be58e --- /dev/null +++ b/errors/id_error.go @@ -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") +)