Documentation ¶
Index ¶
- Constants
- Variables
- func Base64Decode(src []byte) ([]byte, error)
- func Base64Encode(src []byte) []byte
- func GetToken() (string, error)
- func GetTokenExpire(loginType int) time.Duration
- func GetUserPrefixKey(authorityType int, id string) string
- func InitDriver(c *Config) error
- type Authentication
- type Config
- type JwtAuth
- func (ra *JwtAuth) CleanUserTokenCache(authorityType int, userId string) error
- func (ra *JwtAuth) Close()
- func (ra *JwtAuth) DelUserTokenCache(token string) error
- func (ra *JwtAuth) GenerateToken(claims *MultiClaims) (string, int64, error)
- func (ra *JwtAuth) GetMultiClaims(tokenString string) (*MultiClaims, error)
- func (ra *JwtAuth) GetTokenByClaims(cla *MultiClaims) (string, error)
- func (ra *JwtAuth) IsRole(token string, authorityType int) (bool, error)
- func (ra *JwtAuth) SetUserTokenMaxCount(tokenMaxCount int64) error
- func (ra *JwtAuth) UpdateUserTokenCacheExpire(token string) error
- type LocalAuth
- func (la *LocalAuth) CleanUserTokenCache(authorityType int, userId string) error
- func (la *LocalAuth) Close()
- func (la *LocalAuth) DelUserTokenCache(token string) error
- func (la *LocalAuth) GenerateToken(claims *MultiClaims) (string, int64, error)
- func (la *LocalAuth) GetMultiClaims(token string) (*MultiClaims, error)
- func (la *LocalAuth) GetTokenByClaims(cla *MultiClaims) (string, error)
- func (la *LocalAuth) IsRole(token string, authorityType int) (bool, error)
- func (la *LocalAuth) SetUserTokenMaxCount(tokenMaxCount int64) error
- func (la *LocalAuth) UpdateUserTokenCacheExpire(token string) error
- type Multi
- type MultiClaims
- func (c *MultiClaims) Valid() error
- func (c *MultiClaims) VerifyAuthType() bool
- func (c *MultiClaims) VerifyAuthorityId() bool
- func (c *MultiClaims) VerifyAuthorityType() bool
- func (c *MultiClaims) VerifyExpiresAt(cmp int64, req bool) bool
- func (c *MultiClaims) VerifyId() bool
- func (c *MultiClaims) VerifyLoginType() bool
- func (c *MultiClaims) VerifyUsername() bool
- type RedisAuth
- func (ra *RedisAuth) CleanUserTokenCache(authorityType int, userId string) error
- func (ra *RedisAuth) Close()
- func (ra *RedisAuth) DelUserTokenCache(token string) error
- func (ra *RedisAuth) GenerateToken(claims *MultiClaims) (string, int64, error)
- func (ra *RedisAuth) GetMultiClaims(token string) (*MultiClaims, error)
- func (ra *RedisAuth) GetTokenByClaims(cla *MultiClaims) (string, error)
- func (ra *RedisAuth) IsRole(token string, authorityType int) (bool, error)
- func (ra *RedisAuth) SetUserTokenMaxCount(tokenMaxCount int64) error
- func (ra *RedisAuth) UpdateUserTokenCacheExpire(token string) error
- type TokenValidator
- type TokenValidatorFunc
Constants ¶
const ( ValidationErrorMalformed uint32 = 1 << iota // Token is malformed ValidationErrorUnverifiable // Token could not be verified because of signing problems ValidationErrorSignatureInvalid // Signature validation failed ValidationErrorExpired // EXP validation failed ValidationErrorId ValidationErrorUsername ValidationErrorAuthorityId ValidationErrorAuthorityType ValidationErrorLoginType ValidationErrorAuthType )
const ( GtSessionTokenPrefix = "GST:" // token 缓存前缀 GtSessionBindUserPrefix = "GSBU:" // token 绑定用户前缀 GtSessionUserPrefix = "GSU:" // 用户前缀 GtSessionUserMaxTokenPrefix = "GTUserMaxToken" // 用户最大 token 数前缀 )
redis 前缀
const ( NoneAuthority int = iota // 空授权 AdminAuthority // 管理员 TenancyAuthority // 商户 GeneralAuthority //普通用户 )
授权角色类型
const ( NoAuth int = iota AuthPwd AuthCode AuthThirdParty )
授权类型
const ( LoginTypeWeb int = iota LoginTypeApp LoginTypeWx LoginTypeDevice )
登陆类型
Variables ¶
var ( AuthorityTypeSplit = "-" GtSessionUserMaxTokenDefault int64 = 10 )
var ( ErrTokenInvalid = errors.New("TOKEN不可用") ErrEmptyToken = errors.New("TOKEN为空") ErrOverMaxTokenCount = errors.New("已达到同时登录设备上限") )
错误类型
var ( RedisSessionTimeoutWeb = 4 * time.Hour // 4 小时 RedisSessionTimeoutApp = 7 * 24 * time.Hour // 7 天 RedisSessionTimeoutWx = 5 * 52 * 168 * time.Hour // 1年 RedisSessionTimeoutDevice = 5 * 52 * 168 * time.Hour // 1年 )
授权时长
Functions ¶
func Base64Decode ¶
Base64Decode decodes "src" to jwt base64 url format. We could use the base64.RawURLEncoding but the below is a bit faster.
func GetTokenExpire ¶ added in v0.0.2
GetTokenExpire 过期时间
func GetUserPrefixKey ¶ added in v0.0.2
GetUserPrefixKey
Types ¶
type Authentication ¶
type Authentication interface { GenerateToken(claims *MultiClaims) (string, int64, error) // 生成 token DelUserTokenCache(token string) error // 清除用户当前token信息 UpdateUserTokenCacheExpire(token string) error // 更新token 过期时间 GetMultiClaims(token string) (*MultiClaims, error) // 获取token用户信息 GetTokenByClaims(claims *MultiClaims) (string, error) // 通过用户信息获取token CleanUserTokenCache(authorityType int, userId string) error // 清除用户所有 token SetUserTokenMaxCount(tokenMaxCount int64) error // 设置最大登录限制 IsRole(token string, authorityType int) (bool, error) Close() }
Authentication 认证
var AuthDriver Authentication
type JwtAuth ¶ added in v0.0.2
type JwtAuth struct {
HmacSecret []byte
}
JwtAuth
func (*JwtAuth) CleanUserTokenCache ¶ added in v0.0.2
CleanUserTokenCache 清空token缓存
func (*JwtAuth) DelUserTokenCache ¶ added in v0.0.2
DelUserTokenCache 删除token缓存
func (*JwtAuth) GenerateToken ¶ added in v0.0.2
func (ra *JwtAuth) GenerateToken(claims *MultiClaims) (string, int64, error)
GenerateToken
func (*JwtAuth) GetMultiClaims ¶ added in v0.0.2
func (ra *JwtAuth) GetMultiClaims(tokenString string) (*MultiClaims, error)
GetMultiClaims 获取用户信息
func (*JwtAuth) GetTokenByClaims ¶ added in v0.0.2
func (ra *JwtAuth) GetTokenByClaims(cla *MultiClaims) (string, error)
GetTokenByClaims 获取用户信息
func (*JwtAuth) SetUserTokenMaxCount ¶ added in v0.0.2
SetUserTokenMaxCount 最大登录限制
func (*JwtAuth) UpdateUserTokenCacheExpire ¶ added in v0.0.2
UpdateUserTokenCacheExpire 更新过期时间
type LocalAuth ¶
type LocalAuth struct {
Cache *cache.Cache
}
func NewLocalAuth ¶
func NewLocalAuth() *LocalAuth
func (*LocalAuth) CleanUserTokenCache ¶
CleanUserTokenCache 清空token缓存
func (*LocalAuth) DelUserTokenCache ¶
func (*LocalAuth) GenerateToken ¶
func (la *LocalAuth) GenerateToken(claims *MultiClaims) (string, int64, error)
GenerateToken
func (*LocalAuth) GetMultiClaims ¶ added in v0.0.2
func (la *LocalAuth) GetMultiClaims(token string) (*MultiClaims, error)
func (*LocalAuth) GetTokenByClaims ¶
func (la *LocalAuth) GetTokenByClaims(cla *MultiClaims) (string, error)
GetTokenByClaims 获取用户信息
func (*LocalAuth) SetUserTokenMaxCount ¶
SetUserTokenMaxCount 最大登录限制
func (*LocalAuth) UpdateUserTokenCacheExpire ¶
type Multi ¶ added in v0.0.2
type Multi struct { Id uint `json:"id,omitempty"` Username string `json:"username,omitempty"` TenancyId uint `json:"tenancyId,omitempty"` TenancyName string `json:"tenancyName,omitempty"` AuthorityIds []string `json:"authorityIds,omitempty"` AuthorityType int `json:"authorityType,omitempty"` LoginType int `json:"loginType,omitempty"` AuthType int `json:"authType,omitempty"` CreationDate int64 `json:"creationData,omitempty"` ExpiresAt int64 `json:"expiresAt,omitempty"` }
Multi
type MultiClaims ¶ added in v0.0.2
type MultiClaims struct { Id string `json:"id,omitempty" redis:"id"` Username string `json:"username,omitempty" redis:"username"` TenancyId uint `json:"tenancyId,omitempty" redis:"tenancy_id"` TenancyName string `json:"tenancyName,omitempty" redis:"tenancy_name"` AuthorityId string `json:"authorityId,omitempty" redis:"authority_id"` AuthorityType int `json:"authorityType,omitempty" redis:"authority_type"` LoginType int `json:"loginType,omitempty" redis:"login_type"` AuthType int `json:"authType,omitempty" redis:"auth_type"` CreationDate int64 `json:"creationData,omitempty" redis:"creation_data"` ExpiresAt int64 `json:"expiresAt,omitempty" redis:"expires_at"` }
自定义结构 Id 用户id Username 用户名 TenancyId 商户id TenancyName 商户名称 AuthorityId 角色id AuthorityType 角色类型 LoginType 登录类型 web,app,wechat AuthType 授权类型 密码,验证码,第三方 CreationDate 登录时间 ExpiresIn 有效期
func New ¶ added in v0.0.2
func New(m *Multi) *MultiClaims
func (*MultiClaims) Valid ¶ added in v0.0.2
func (c *MultiClaims) Valid() error
func (*MultiClaims) VerifyAuthType ¶ added in v0.0.4
func (c *MultiClaims) VerifyAuthType() bool
func (*MultiClaims) VerifyAuthorityId ¶ added in v0.0.4
func (c *MultiClaims) VerifyAuthorityId() bool
func (*MultiClaims) VerifyAuthorityType ¶ added in v0.0.4
func (c *MultiClaims) VerifyAuthorityType() bool
func (*MultiClaims) VerifyExpiresAt ¶ added in v0.0.2
func (c *MultiClaims) VerifyExpiresAt(cmp int64, req bool) bool
Compares the exp claim against cmp. If required is false, this method will return true if the value matches or is unset
func (*MultiClaims) VerifyId ¶ added in v0.0.4
func (c *MultiClaims) VerifyId() bool
func (*MultiClaims) VerifyLoginType ¶ added in v0.0.4
func (c *MultiClaims) VerifyLoginType() bool
func (*MultiClaims) VerifyUsername ¶ added in v0.0.4
func (c *MultiClaims) VerifyUsername() bool
type RedisAuth ¶
type RedisAuth struct {
Client redis.UniversalClient
}
RedisAuth
func NewRedisAuth ¶
NewRedisAuth
func (*RedisAuth) CleanUserTokenCache ¶
CleanUserTokenCache 清空token缓存
func (*RedisAuth) DelUserTokenCache ¶
DelUserTokenCache 删除token缓存
func (*RedisAuth) GenerateToken ¶
func (ra *RedisAuth) GenerateToken(claims *MultiClaims) (string, int64, error)
GenerateToken
func (*RedisAuth) GetMultiClaims ¶ added in v0.0.2
func (ra *RedisAuth) GetMultiClaims(token string) (*MultiClaims, error)
GetMultiClaims 获取用户信息
func (*RedisAuth) GetTokenByClaims ¶
func (ra *RedisAuth) GetTokenByClaims(cla *MultiClaims) (string, error)
GetTokenByClaims 获取用户信息
func (*RedisAuth) SetUserTokenMaxCount ¶
SetUserTokenMaxCount 最大登录限制
func (*RedisAuth) UpdateUserTokenCacheExpire ¶
UpdateUserTokenCacheExpire 更新过期时间
type TokenValidator ¶
type TokenValidator interface { // ValidateToken accepts the token, the claims extracted from that // and any error that may caused by claims validation (e.g. ErrExpired) // or the previous validator. // A token validator can skip the builtin validation and return a nil error. // Usage: // func(v *myValidator) ValidateToken(token []byte, standardClaims Claims, err error) error { // if err!=nil { return err } <- to respect the previous error // // otherwise return nil or any custom error. // } // // Look `Blocklist`, `Expected` and `Leeway` for builtin implementations. ValidateToken(token []byte, err error) error }
TokenValidator provides further token and claims validation.
type TokenValidatorFunc ¶
TokenValidatorFunc is the interface-as-function shortcut for a TokenValidator.
func (TokenValidatorFunc) ValidateToken ¶
func (fn TokenValidatorFunc) ValidateToken(token []byte, err error) error
ValidateToken completes the ValidateToken interface. It calls itself.