diff --git a/log/default_logger.go b/log/default_logger.go new file mode 100644 index 0000000..da9eaff --- /dev/null +++ b/log/default_logger.go @@ -0,0 +1,71 @@ +package log + +import ( + "github.com/weloe/token-go/model" + "log" +) + +var _ Logger = (*DefaultLogger)(nil) + +type DefaultLogger struct { + enable bool +} + +func (d *DefaultLogger) Enable(bool bool) { + d.enable = bool +} + +func (d *DefaultLogger) IsEnabled() bool { + return d.enable +} + +func (d *DefaultLogger) Login(loginType string, id interface{}, tokenValue string, loginModel *model.Login) { + if !d.enable { + return + } + log.Printf("LoginByModel: loginId = %v, loginType = %v, tokenValue = %v, "+ + "loginMode = %v", id, loginType, tokenValue, loginModel) + +} + +func (d *DefaultLogger) Logout(loginType string, id interface{}, tokenValue string) { + if !d.enable { + return + } + log.Printf("Logout: loginId = %v, loginType = %v, tokenValue = %v", id, loginType, tokenValue) +} + +func (d *DefaultLogger) Kickout(loginType string, id interface{}, tokenValue string) { + if !d.enable { + return + } + log.Printf("Kickout: loginId = %v, loginType = %v, tokenValue = %v", id, loginType, tokenValue) +} + +func (d *DefaultLogger) Replace(loginType string, id interface{}, tokenValue string) { + if !d.enable { + return + } + log.Printf("Replaced: loginId = %v, loginType = %v, tokenValue = %v", id, loginType, tokenValue) +} + +func (d *DefaultLogger) Ban(loginType string, id interface{}, service string) { + if !d.enable { + return + } + log.Printf("Banned: loginId = %v, loginType = %v, service = %v", id, loginType, service) +} + +func (d *DefaultLogger) UnBan(loginType string, id interface{}, service string) { + if !d.enable { + return + } + log.Printf("UnBanned: loginId = %v, loginType = %v, service = %v", id, loginType, service) +} + +func (d *DefaultLogger) RefreshToken(tokenValue string, id interface{}, timeout int64) { + if !d.enable { + return + } + log.Printf("RefreshToken: loginId = %v, tokenValue = %v, timeout = %v", id, tokenValue, timeout) +} diff --git a/log/logger.go b/log/logger.go new file mode 100644 index 0000000..9a2dfc3 --- /dev/null +++ b/log/logger.go @@ -0,0 +1,13 @@ +package log + +import "github.com/weloe/token-go/persist" + +type Logger interface { + persist.Watcher + + // Enable turn on or off + Enable(bool bool) + + // IsEnabled return if logger is enabled + IsEnabled() bool +} diff --git a/model/login.go b/model/login.go new file mode 100644 index 0000000..431ad0d --- /dev/null +++ b/model/login.go @@ -0,0 +1,21 @@ +package model + +type Login struct { + Device string + IsLastingCookie bool + Timeout int64 + JwtData map[string]interface{} + Token string + IsWriteHeader bool +} + +func DefaultLoginModel() *Login { + return &Login{ + Device: "default", + IsLastingCookie: true, + Timeout: 60 * 60 * 24 * 30, + JwtData: nil, + Token: "", + IsWriteHeader: true, + } +} diff --git a/persist/watcher.go b/persist/watcher.go new file mode 100644 index 0000000..02beb67 --- /dev/null +++ b/persist/watcher.go @@ -0,0 +1,21 @@ +package persist + +import "github.com/weloe/token-go/model" + +// Watcher event watcher +type Watcher interface { + // Login called after login + Login(loginType string, id interface{}, tokenValue string, loginModel *model.Login) + // Logout called after logout + Logout(loginType string, id interface{}, tokenValue string) + // Kickout called when being kicked out of the server + Kickout(loginType string, id interface{}, tokenValue string) + // 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) + // UnBan called when account has been unbanned. + UnBan(loginType string, id interface{}, service string) + // RefreshToken called when renew token timeout + RefreshToken(tokenValue string, id interface{}, timeout int64) +}