mirror of
https://github.com/weloe/token-go.git
synced 2025-10-05 15:36:50 +08:00
feat: add banned feature and api IsLoginByToken()
This commit is contained in:
@@ -49,10 +49,6 @@ type TokenConfig struct {
|
||||
CookieConfig *CookieConfig
|
||||
}
|
||||
|
||||
func (t *TokenConfig) SetJwtSecretKey(secretKey string) {
|
||||
t.JwtSecretKey = secretKey
|
||||
}
|
||||
|
||||
func DefaultTokenConfig() *TokenConfig {
|
||||
return &TokenConfig{
|
||||
TokenStyle: "uuid",
|
||||
|
74
enforcer.go
74
enforcer.go
@@ -342,6 +342,10 @@ func (e *Enforcer) GetIdByToken(token string) string {
|
||||
// IsLogin check if user logged in by token.
|
||||
func (e *Enforcer) IsLogin(ctx ctx.Context) (bool, error) {
|
||||
tokenValue := e.GetRequestToken(ctx)
|
||||
return e.IsLoginByToken(tokenValue)
|
||||
}
|
||||
|
||||
func (e *Enforcer) IsLoginByToken(tokenValue string) (bool, error) {
|
||||
if tokenValue == "" {
|
||||
return false, nil
|
||||
}
|
||||
@@ -382,8 +386,74 @@ func (e *Enforcer) GetLoginCount(id string) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (e *Enforcer) Banned(id string, service string) error {
|
||||
panic("implement me ...")
|
||||
// Banned ban user, if time == 0,the timeout is not set
|
||||
func (e *Enforcer) Banned(id string, service string, level int, time int64) error {
|
||||
if id == "" || service == "" {
|
||||
return fmt.Errorf("parameter cannot be nil")
|
||||
}
|
||||
if level < 1 {
|
||||
return fmt.Errorf("unexpected level = %v, level must large or equal 1", level)
|
||||
}
|
||||
err := e.adapter.SetStr(e.spliceBannedKey(id, service), strconv.Itoa(level), time)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// callback
|
||||
e.logger.Ban(e.loginType, id, service, level, time)
|
||||
if e.watcher != nil {
|
||||
e.watcher.Ban(e.loginType, id, service, level, time)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnBanned Unblock user account
|
||||
func (e *Enforcer) UnBanned(id string, services ...string) error {
|
||||
if id == "" {
|
||||
return fmt.Errorf("parmeter id can not be nil")
|
||||
}
|
||||
if len(services) == 0 {
|
||||
return fmt.Errorf("parmeter services length can not be 0")
|
||||
}
|
||||
|
||||
for _, service := range services {
|
||||
err := e.adapter.DeleteStr(e.spliceBannedKey(id, service))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e.logger.UnBan(e.loginType, id, service)
|
||||
if e.watcher != nil {
|
||||
e.watcher.UnBan(e.loginType, id, service)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsBanned if banned return true, else return false
|
||||
func (e *Enforcer) IsBanned(id string, services string) bool {
|
||||
s := e.adapter.GetStr(e.spliceBannedKey(id, services))
|
||||
|
||||
return s != ""
|
||||
}
|
||||
|
||||
// GetBannedLevel get banned level
|
||||
func (e *Enforcer) GetBannedLevel(id string, service string) (int64, error) {
|
||||
str := e.adapter.GetStr(e.spliceBannedKey(id, service))
|
||||
if str == "" {
|
||||
return 0, fmt.Errorf("loginId = %v, service = %v is not banned", id, service)
|
||||
}
|
||||
time, err := strconv.ParseInt(str, 10, 64)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return time, nil
|
||||
}
|
||||
|
||||
// GetBannedTime get banned time
|
||||
func (e *Enforcer) GetBannedTime(id string, service string) int64 {
|
||||
timeout := e.adapter.GetStrTimeout(e.spliceBannedKey(id, service))
|
||||
return timeout
|
||||
}
|
||||
|
||||
// Kickout kickout user
|
||||
|
@@ -16,14 +16,20 @@ type IEnforcer interface {
|
||||
Logout(ctx ctx.Context) error
|
||||
LogoutByToken(token string) error
|
||||
IsLogin(ctx ctx.Context) (bool, error)
|
||||
IsLoginByToken(token string) (bool, error)
|
||||
IsLoginById(id string) (bool, error)
|
||||
GetLoginId(ctx ctx.Context) (string, error)
|
||||
GetIdByToken(token string) string
|
||||
GetLoginCount(id string) int
|
||||
|
||||
Replaced(id string, device string) error
|
||||
// Banned TODO
|
||||
Banned(id string, service string) error
|
||||
// Banned banned api
|
||||
Banned(id string, service string, level int, time int64) error
|
||||
UnBanned(id string, services ...string) error
|
||||
IsBanned(id string, service string) bool
|
||||
GetBannedLevel(id string, service string) (int64, error)
|
||||
GetBannedTime(id string, service string) int64
|
||||
|
||||
Kickout(id string, device string) error
|
||||
|
||||
GetRequestToken(ctx ctx.Context) string
|
||||
|
@@ -144,6 +144,10 @@ func (e *Enforcer) spliceTokenKey(id string) string {
|
||||
return e.config.TokenName + ":" + e.loginType + ":token:" + id
|
||||
}
|
||||
|
||||
func (e *Enforcer) spliceBannedKey(id string, service string) string {
|
||||
return e.config.TokenName + ":" + e.loginType + ":ban:" + service + ":" + id
|
||||
}
|
||||
|
||||
func (e *Enforcer) SetJwtSecretKey(key string) {
|
||||
e.config.JwtSecretKey = key
|
||||
}
|
||||
|
@@ -419,3 +419,55 @@ func TestEnforcer_JsonAdapter(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestEnforcer_Banned(t *testing.T) {
|
||||
err, enforcer, _ := NewTestEnforcer(t)
|
||||
if err != nil {
|
||||
t.Fatalf("NewTestEnforcer() failed: %v", err)
|
||||
}
|
||||
err = enforcer.Banned("1", "comment", 1, 100)
|
||||
if err != nil {
|
||||
t.Fatalf("Banned() failed: %v", err)
|
||||
}
|
||||
isBanned := enforcer.IsBanned("1", "comment")
|
||||
if !isBanned {
|
||||
t.Errorf("unexpected isBanned is false")
|
||||
}
|
||||
level, err := enforcer.GetBannedLevel("1", "comment")
|
||||
if err != nil {
|
||||
t.Errorf("GetBannedLevel() failed: %v", err)
|
||||
}
|
||||
if level != 1 {
|
||||
t.Errorf("unexpected banned level = %v", level)
|
||||
}
|
||||
|
||||
err = enforcer.UnBanned("1", "comment")
|
||||
if err != nil {
|
||||
t.Fatalf("UnBanned() failed: %v", err)
|
||||
}
|
||||
isBanned = enforcer.IsBanned("1", "comment")
|
||||
if isBanned {
|
||||
t.Errorf("unexpected isBanned is false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnforcer_GetBannedTime(t *testing.T) {
|
||||
err, enforcer, _ := NewTestEnforcer(t)
|
||||
if err != nil {
|
||||
t.Fatalf("NewTestEnforcer() failed: %v", err)
|
||||
}
|
||||
err = enforcer.Banned("1", "comment", 1, 100)
|
||||
if err != nil {
|
||||
t.Fatalf("Banned() failed: %v", err)
|
||||
}
|
||||
|
||||
t.Logf("banned time = %v", enforcer.GetBannedTime("1", "comment"))
|
||||
|
||||
err = enforcer.Banned("1", "comment", 1, -1)
|
||||
if err != nil {
|
||||
t.Fatalf("Banned() failed: %v", err)
|
||||
}
|
||||
|
||||
t.Logf("banned time = %v", enforcer.GetBannedTime("1", "comment"))
|
||||
|
||||
}
|
||||
|
@@ -53,11 +53,11 @@ func (d *DefaultLogger) Replace(loginType string, id interface{}, tokenValue str
|
||||
log.Printf("Replaced: loginId = %v, loginType = %v, tokenValue = %v", id, loginType, tokenValue)
|
||||
}
|
||||
|
||||
func (d *DefaultLogger) Ban(loginType string, id interface{}, service string) {
|
||||
func (d *DefaultLogger) Ban(loginType string, id interface{}, service string, level int, time int64) {
|
||||
if !d.enable {
|
||||
return
|
||||
}
|
||||
log.Printf("Banned: loginId = %v, loginType = %v, service = %v", id, loginType, service)
|
||||
log.Printf("Banned: loginId = %v, loginType = %v, service = %v, level = %v, time = %v", id, loginType, service, level, time)
|
||||
}
|
||||
|
||||
func (d *DefaultLogger) UnBan(loginType string, id interface{}, service string) {
|
||||
|
@@ -13,7 +13,7 @@ type Watcher interface {
|
||||
// Replace called when Someone else has taken over your account
|
||||
Replace(loginType string, id interface{}, tokenValue string)
|
||||
// Ban called when account banned
|
||||
Ban(loginType string, id interface{}, service string)
|
||||
Ban(loginType string, id interface{}, service string, level int, time int64)
|
||||
// UnBan called when account has been unbanned.
|
||||
UnBan(loginType string, id interface{}, service string)
|
||||
// RefreshToken called when renew token timeout
|
||||
|
Reference in New Issue
Block a user