Documentation ¶
Index ¶
- Constants
- Variables
- func CheckDisable(id int64) error
- func CheckDisableWithLevel(id, level int64) error
- func CheckDisableWithLevelAndService(id, level int64, service string) error
- func CheckDisableWithService(id int64, service string) error
- func CheckLogin(a Adaptor) error
- func CheckLoginFromCtx(ctx context.Context) error
- func Disable(id, exTime int64)
- func DisableExTime(id int64) int64
- func DisableExTimeWithService(id int64, services string) int64
- func DisableWithLevel(id, level, exTime int64)
- func DisableWithLevelAndService(id, level, exTime int64, service string)
- func DisableWithService(id, exTime int64, service string)
- func GetLoginId(a Adaptor) int64
- func GetLoginIdFromCtx(ctx context.Context) int64
- func GetLoginToken(a Adaptor) string
- func GetLoginTokenFromCtx(ctx context.Context) string
- func HasPermission(a Adaptor, p string) bool
- func HasPermissionAnd(a Adaptor, ps ...string) bool
- func HasPermissionAndFromCtx(ctx context.Context, ps ...string) bool
- func HasPermissionFromCtx(ctx context.Context, p string) bool
- func HasPermissionOr(a Adaptor, ps ...string) bool
- func HasPermissionOrFromCtx(ctx context.Context, ps ...string) bool
- func HasRole(a Adaptor, role string) bool
- func HasRoleAnd(a Adaptor, roles ...string) bool
- func HasRoleAndFromCtx(ctx context.Context, roles ...string) bool
- func HasRoleFromCtx(ctx context.Context, role string) bool
- func HasRoleOr(a Adaptor, roles ...string) bool
- func HasRoleOrFromCtx(ctx context.Context, roles ...string) bool
- func IsDisable(id int64) bool
- func IsDisableWithLevel(id, level int64) bool
- func IsDisableWithLevelAndService(id, level int64, service string) bool
- func IsDisableWithService(id int64, service string) bool
- func IsLogin(a Adaptor) bool
- func IsLoginFromCtx(ctx context.Context) bool
- func KeyMatch(key1 string, key2 string) bool
- func KeyMatch2(key1 string, key2 string) bool
- func Kick(id int64)
- func KickWithDevice(id int64, device string)
- func KickWithToken(token string)
- func Login(id int64) string
- func LoginAndSet(id int64, a Adaptor) string
- func LoginAndSetFromCtx(id int64, ctx context.Context) string
- func Logout(a Adaptor)
- func LogoutById(id int64)
- func LogoutByIdAndDevice(id int64, device string)
- func LogoutFromCtx(ctx context.Context)
- func NewErrDisable(level int64, service string) error
- func RmDisable(id int64)
- func RmDisableWithServices(id int64, services ...string)
- func SetDefaultSecurity(l *Logic)
- func SetLogger(l *zap.Logger)
- func SetLoggerEnable(enable bool)
- type Adaptor
- type Authorization
- type Config
- type Cookie
- type ErrDisable
- type GenerateToken
- type Logic
- func (l *Logic) CheckDisableWithLevelAndService(id, level int64, service string) error
- func (l *Logic) CheckLoginByToken(token string) error
- func (l *Logic) DisableExTime(id int64, service string) int64
- func (l *Logic) DisableWithLevelAndService(id, level, exTime int64, service string)
- func (l *Logic) GetConfig() Config
- func (l *Logic) GetIdByToken(token string) int64
- func (l *Logic) GetPermissionList(id int64) []string
- func (l *Logic) GetRoleList(id int64) []string
- func (l *Logic) GetSessionById(id int64) *Session
- func (l *Logic) GetSessionByIdOrCreate(id int64) *Session
- func (l *Logic) GetSessionByToken(token string) *Session
- func (l *Logic) GetStore() Store
- func (l *Logic) GetTokenByIdAndDevice(id int64, device string) string
- func (l *Logic) GetTokenTimeout(token string) int64
- func (l *Logic) IsDisableWithLevelAndService(id, level int64, service string) bool
- func (l *Logic) IsLoginByToken(token string) bool
- func (l *Logic) Kick(id int64)
- func (l *Logic) KickWithToken(token string)
- func (l *Logic) Login(id int64) string
- func (l *Logic) LoginWithDevice(id int64, device string) string
- func (l *Logic) Logout(id int64)
- func (l *Logic) LogoutByIdAndDevice(id int64, device string)
- func (l *Logic) LogoutByToken(token string)
- func (l *Logic) RmDisableWithServices(id int64, services ...string)
- func (l *Logic) SetAuthorization(a Authorization)
- type Security
- func (s *Security) CheckDisable(id int64) error
- func (s *Security) CheckDisableWithLevel(id, level int64) error
- func (s *Security) CheckDisableWithLevelAndService(id, level int64, service string) error
- func (s *Security) CheckDisableWithService(id int64, service string) error
- func (s *Security) CheckLogin(a Adaptor) error
- func (s *Security) Disable(id, exTime int64)
- func (s *Security) DisableExTime(id int64) int64
- func (s *Security) DisableExTimeWithService(id int64, service string) int64
- func (s *Security) DisableWithLevel(id, level, exTime int64)
- func (s *Security) DisableWithLevelAndService(id, level, exTime int64, service string)
- func (s *Security) DisableWithService(id, exTime int64, service string)
- func (s *Security) GetLoginId(a Adaptor) int64
- func (s *Security) GetPermissionList(a Adaptor) []string
- func (s *Security) GetRoleList(a Adaptor) []string
- func (s *Security) GetToken(a Adaptor) string
- func (s *Security) HasPermission(a Adaptor, p string) bool
- func (s *Security) HasPermissionAnd(a Adaptor, ps ...string) bool
- func (s *Security) HasPermissionOr(a Adaptor, ps ...string) bool
- func (s *Security) HasRole(a Adaptor, role string) bool
- func (s *Security) HasRoleAnd(a Adaptor, roles ...string) bool
- func (s *Security) HasRoleOr(a Adaptor, roles ...string) bool
- func (s *Security) IsDisable(id int64) bool
- func (s *Security) IsDisableWithLevel(id, level int64) bool
- func (s *Security) IsDisableWithLevelAndService(id, level int64, service string) bool
- func (s *Security) IsDisableWithService(id int64, service string) bool
- func (s *Security) IsLogin(a Adaptor) bool
- func (s *Security) Login(id int64) string
- func (s *Security) LoginAndSet(id int64, a Adaptor) string
- func (s *Security) LoginWithDevice(id int64, device string) string
- func (s *Security) LoginWithDeviceAndSet(id int64, device string, a Adaptor) string
- func (s *Security) Logout(a Adaptor)
- func (s *Security) LogoutById(id int64)
- func (s *Security) LogoutByIdAndDevice(id int64, device string)
- func (s *Security) RmDisable(id int64)
- func (s *Security) RmDisableWithServices(id int64, services ...string)
- func (s *Security) Session(a Adaptor) *Session
- func (s *Security) SetAuthorization(a Authorization)
- type Session
- func (s *Session) AddTokenSign(ts TokenSign)
- func (s *Session) DelTokenSignByToken(token string)
- func (s *Session) Get(key string) (interface{}, bool)
- func (s *Session) GetTokenSignByDevice(device string) (TokenSign, bool)
- func (s *Session) GetTokenSignByToken(token string) (TokenSign, bool)
- func (s *Session) MarshalBinary() (data []byte, err error)
- func (s *Session) Set(key string, val interface{})
- func (s *Session) UnmarshalBinary(data []byte) error
- type Store
- type TokenSign
Constants ¶
const ( // NeverExpire token never expire NeverExpire = -1 // NotValueExist Store getExTime if value not exist return this value NotValueExist = -2 // BeReplace being knocked off the line BeReplace = -4 BeKick = -5 LoginIdReqCtx = "LoginIdReqCtx" LoginTokenReqCtx = "LoginTokenReqCtx" )
const ContextKey = "GSecurityContextKey"
const (
DefaultDevice = "default-device"
)
Variables ¶
var ( ErrBeReplace = errors.New("has been replaced") ErrNotLogin = errors.New("not login") ErrBeKick = errors.New("has been kicked") )
var ( DefaultConfig = Config{ TokenName: "G-Security", Timeout: 2592000, IsConcurrent: true, IsShare: true, TokenStyle: UUID, ReadFromQuery: false, ReadFromCookie: true, ReadFromHeader: false, WriteToHeader: false, WriteToCookie: true, Cookie: Cookie{ Path: "/", Secure: false, HttpOnly: false, SameSite: "Lax", }, } )
var ErrNoAdaptorInContext = errors.New("no adaptor in context")
Functions ¶
func CheckDisable ¶
func CheckDisableWithLevel ¶
func CheckDisableWithLevelAndService ¶
CheckDisableWithLevelAndService check disable with id, level and service, if it's disabled, return ErrDisable
func CheckDisableWithService ¶
func CheckLogin ¶
func CheckLoginFromCtx ¶
CheckLoginFromCtx The method at the end of FromCtx can be used only when Adaptor is the value and ContextKey is the Key in the Context
func DisableExTime ¶
func DisableExTimeWithService ¶
DisableExTimeWithService get disabled expire time, if never expire return NeverExpire, if not disable return NotValueExist
func DisableWithLevel ¶
func DisableWithLevel(id, level, exTime int64)
func DisableWithLevelAndService ¶
DisableWithLevelAndService disable with the id, level, expire time of seconds and service
func DisableWithService ¶
func GetLoginId ¶
func GetLoginIdFromCtx ¶
GetLoginIdFromCtx The method at the end of FromCtx can be used only when Adaptor is the value and ContextKey is the Key in the Context
func GetLoginToken ¶
func GetLoginTokenFromCtx ¶
GetLoginTokenFromCtx The method at the end of FromCtx can be used only when Adaptor is the value and ContextKey is the Key in the Context
func HasPermission ¶
func HasPermissionAnd ¶
func HasPermissionAndFromCtx ¶
HasPermissionAndFromCtx The method at the end of FromCtx can be used only when Adaptor is the value and ContextKey is the Key in the Context
func HasPermissionFromCtx ¶
HasPermissionFromCtx The method at the end of FromCtx can be used only when Adaptor is the value and ContextKey is the Key in the Context
func HasPermissionOr ¶
func HasPermissionOrFromCtx ¶
HasPermissionOrFromCtx The method at the end of FromCtx can be used only when Adaptor is the value and ContextKey is the Key in the Context
func HasRoleAnd ¶
func HasRoleAndFromCtx ¶
HasRoleAndFromCtx The method at the end of FromCtx can be used only when Adaptor is the value and ContextKey is the Key in the Context
func HasRoleFromCtx ¶
HasRoleFromCtx The method at the end of FromCtx can be used only when Adaptor is the value and ContextKey is the Key in the Context
func HasRoleOrFromCtx ¶
HasRoleOrFromCtx The method at the end of FromCtx can be used only when Adaptor is the value and ContextKey is the Key in the Context
func IsDisableWithLevel ¶
func IsDisableWithLevelAndService ¶
IsDisableWithLevelAndService check is disable with id, level and service
func IsDisableWithService ¶
func IsLoginFromCtx ¶
IsLoginFromCtx The method at the end of FromCtx can be used only when Adaptor is the value and ContextKey is the Key in the Context
func KickWithDevice ¶
KickWithDevice kicking user offline by id and device
func LoginAndSet ¶
func LoginAndSetFromCtx ¶
LoginAndSetFromCtx The method at the end of FromCtx can be used only when Adaptor is the value and ContextKey is the Key in the Context
func LogoutById ¶
func LogoutById(id int64)
func LogoutByIdAndDevice ¶
func LogoutFromCtx ¶
LogoutFromCtx The method at the end of FromCtx can be used only when Adaptor is the value and ContextKey is the Key in the Context
func NewErrDisable ¶
func RmDisableWithServices ¶
RmDisableWithServices remove disable with id and services
func SetDefaultSecurity ¶
func SetDefaultSecurity(l *Logic)
func SetLoggerEnable ¶
func SetLoggerEnable(enable bool)
Types ¶
type Authorization ¶
type Config ¶
type Config struct { // token name (also Cookie name and data persistence prefix) TokenName string // token validity period Timeout int64 // whether to allow concurrent logins with the same account (if true, allow concurrent logins, if false, // new logins will crowd out old logins) IsConcurrent bool // if false, create a token for each login) IsShare bool // generated token's style, can be customized TokenStyle GenerateToken // read token from query, default true ReadFromQuery bool // read token from header, default true ReadFromHeader bool // read token from cookie, default true ReadFromCookie bool // set token to header, default false WriteToHeader bool // set token to cookie, default true WriteToCookie bool Cookie Cookie }
type ErrDisable ¶
type ErrDisable struct {
// contains filtered or unexported fields
}
func (ErrDisable) Error ¶
func (e ErrDisable) Error() string
type GenerateToken ¶
type GenerateToken func() string
var ( UUID GenerateToken = uuid.NewString Rand32 GenerateToken = rand32 Rand64 GenerateToken = rand64 Rand128 GenerateToken = rand128 )
type Logic ¶
func NewLogicWithLoginType ¶
func (*Logic) CheckDisableWithLevelAndService ¶
func (*Logic) CheckLoginByToken ¶
CheckLoginByToken check token is login, if it's login, return nil, else return error
func (*Logic) DisableWithLevelAndService ¶
func (*Logic) GetIdByToken ¶
GetIdByToken if not login or token is invalid, return 0
func (*Logic) GetPermissionList ¶
func (*Logic) GetRoleList ¶
func (*Logic) GetSessionById ¶
GetSessionById get session by id, if id not exist, return nil
func (*Logic) GetSessionByIdOrCreate ¶
GetSessionByIdOrCreate get session by id, if not exist, create and return
func (*Logic) GetSessionByToken ¶
GetSessionByToken get session by token, if token not exist, return nil
func (*Logic) GetTokenByIdAndDevice ¶
GetTokenByIdAndDevice get token by id and device, if not exist, return ""
func (*Logic) GetTokenTimeout ¶
GetTokenTimeout get expire time by token, return value is the number of seconds
func (*Logic) IsDisableWithLevelAndService ¶
func (*Logic) IsLoginByToken ¶
IsLoginByToken validate token is login
func (*Logic) KickWithToken ¶
func (*Logic) LoginWithDevice ¶
LoginWithDevice login with id and device
func (*Logic) LogoutByIdAndDevice ¶
LogoutByIdAndDevice logout with id and device
func (*Logic) LogoutByToken ¶
LogoutByToken logout with token
func (*Logic) RmDisableWithServices ¶
func (*Logic) SetAuthorization ¶
func (l *Logic) SetAuthorization(a Authorization)
type Security ¶
type Security struct {
*Logic
}
func NewSecurity ¶
func (*Security) CheckDisable ¶
func (*Security) CheckDisableWithLevel ¶
func (*Security) CheckDisableWithLevelAndService ¶
CheckDisableWithLevelAndService check disable with id, level and service, if it's disabled, return ErrDisable
func (*Security) CheckDisableWithService ¶
func (*Security) CheckLogin ¶
CheckLogin get token from adaptor.Adaptor and check token login or not, if not login, return ErrNotLogin
func (*Security) DisableExTime ¶
func (*Security) DisableExTimeWithService ¶
DisableExTimeWithService get disabled expire time, if never expire return NeverExpire, if not disable return NotValueExist
func (*Security) DisableWithLevel ¶
func (*Security) DisableWithLevelAndService ¶
DisableWithLevelAndService disable with the id, level, expire time of seconds and service
func (*Security) DisableWithService ¶
func (*Security) GetLoginId ¶
func (*Security) GetPermissionList ¶
func (*Security) GetRoleList ¶
func (*Security) HasPermissionAnd ¶
func (*Security) IsDisableWithLevel ¶
func (*Security) IsDisableWithLevelAndService ¶
IsDisableWithLevelAndService check is disable with id, level and service
func (*Security) IsDisableWithService ¶
func (*Security) LoginAndSet ¶
LoginAndSet use id to login, and set token to cookie, return token
func (*Security) LoginWithDevice ¶
LoginWithDevice use id and device to login, return token
func (*Security) LoginWithDeviceAndSet ¶
func (*Security) LogoutByIdAndDevice ¶
LogoutByIdAndDevice logout of the id and device
func (*Security) RmDisableWithServices ¶
RmDisableWithServices remove disable with id and services
func (*Security) SetAuthorization ¶
func (s *Security) SetAuthorization(a Authorization)
type Session ¶
type Session struct { Id string `json:"id,omitempty"` CreateTime int64 `json:"createTime,omitempty"` Data map[string]interface{} `json:"data,omitempty"` TokenSignList []TokenSign `json:"tokenSignList,omitempty"` // contains filtered or unexported fields }
func SessionsFromCtx ¶
SessionsFromCtx The method at the end of FromCtx can be used only when Adaptor is the value and ContextKey is the Key in the Context
func (*Session) AddTokenSign ¶
func (*Session) DelTokenSignByToken ¶
func (*Session) GetTokenSignByDevice ¶
func (*Session) GetTokenSignByToken ¶
func (*Session) MarshalBinary ¶
func (*Session) UnmarshalBinary ¶
type Store ¶
type Store interface { Set(key string, val interface{}, exTime int64) Get(key string) (interface{}, bool) GetSession(key string) (interface{}, bool) Update(key string, val interface{}) Delete(key string) GetExTime(key string) int64 UpdateSessionTimeout(key string, exTime int64) }
func NewDefaultStore ¶
NewDefaultStore clearSleepTime is the number of seconds between each check when using default storage
func NewRedisStore ¶
func NewRedisStore(r *redis.Client) Store