Documentation ¶
Index ¶
- Constants
- type AuthFailed
- type CustomClaims
- type GToken
- func (m *GToken) AuthMiddleware(group *ghttp.RouterGroup) bool
- func (m *GToken) AuthPath(urlPath string) bool
- func (m *GToken) DecryptToken(token string) Resp
- func (m *GToken) DecryptUtilToken(ctx context.Context, token string) (DecryptStr, uuid string, err error)
- func (m *GToken) EncryptToken(userKey string, uuid string) Resp
- func (m *GToken) EncryptUtilToken(ctx context.Context, key string, randStr ...string) (encryptStr, uuid string, err error)
- func (m *GToken) GenerateToken(ctx context.Context, key string, data interface{}) (keys string, err error)
- func (m *GToken) GetRequestToken(r *ghttp.Request) (token string)
- func (m *GToken) GetToken(r *ghttp.Request) (tData *tokenData, err error)
- func (m *GToken) GetTokenData(r *ghttp.Request) Resp
- func (m *GToken) GetUtilToken(r *ghttp.Request) (tData *tokenData, err error)
- func (m *GToken) InitConfig() bool
- func (m *GToken) IsLogin(r *ghttp.Request) (b bool, failed *AuthFailed)
- func (m *GToken) IsUtilEffective(ctx context.Context, token string) bool
- func (m *GToken) IsUtilLogin(r *ghttp.Request) (b bool, failed *AuthFailed)
- func (m *GToken) IsUtilNotExpired(token string) (*CustomClaims, int)
- func (m *GToken) IsUtilRefresh(token string) bool
- func (m *GToken) Login(r *ghttp.Request)
- func (m *GToken) Logout(r *ghttp.Request)
- func (m *GToken) Middleware(group *ghttp.RouterGroup) error
- func (m *GToken) ParseToken(r *ghttp.Request) (*CustomClaims, error)
- func (m *GToken) ParseUtilToken(r *ghttp.Request) (*CustomClaims, error)
- func (m *GToken) RefreshUtilToken(oldToken string) (newToken string, err error)
- func (m *GToken) RemoveGeneralToken(token string) Resp
- func (m *GToken) RemoveToken(ctx context.Context, token string) (err error)
- func (m *GToken) Start() error
- func (m *GToken) Stop() error
- func (m *GToken) String() string
- type JwtSign
- type OptionFunc
- func WithCacheKey(value string) OptionFunc
- func WithEncryptKey(value []byte) OptionFunc
- func WithExcludePaths(value g.SliceStr) OptionFunc
- func WithGCache() OptionFunc
- func WithGRedis(redis ...*gredis.Redis) OptionFunc
- func WithGRedisConfig(redisConfig ...*gredis.Config) OptionFunc
- func WithMaxRefresh(value int64) OptionFunc
- func WithMultiLogin(b bool) OptionFunc
- func WithServerName(value string) OptionFunc
- func WithTimeout(value int64) OptionFunc
- func WithTimeoutAndMaxRefresh(timeout, maxRefresh int64) OptionFunc
- func WithUserJwt(key string) OptionFunc
- type Resp
Constants ¶
View Source
const ( CacheModeCache = 1 CacheModeRedis = 2 MiddlewareTypeGroup = 1 MiddlewareTypeBind = 2 MiddlewareTypeGlobal = 3 DefaultTimeout = 10 * 24 * 60 * 60 * 1000 DefaultCacheKey = "GToken:" DefaultTokenDelimiter = "_" DefaultEncryptKey = "12345678912345678912345678912345" DefaultAuthFailMsg = "请求错误或登录超时" )
View Source
const ( //token部分 ErrorsParseTokenFail string = "解析token失败" ErrorsTokenInvalid string = "无效的token" ErrorsTokenNotActiveYet string = "Token 尚未激活" ErrorsTokenMalFormed string = "Token 格式不正确" JwtTokenOK int = 200100 //token有效 JwtTokenInvalid int = -400100 //无效的token JwtTokenExpired int = -400101 //过期的token JwtTokenFormatErrCode int = -400102 //提交的 Token 格式错误 )
View Source
const ( SUCCESS = 0 FAIL = -1 ERROR = -99 UNAUTHORIZED = -401 //配置 TypeConfig = 1 // 字典 TypeDict = 2 )
View Source
const FailedAuthCode = 401
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuthFailed ¶
type CustomClaims ¶
type CustomClaims struct { Data interface{} jwt.StandardClaims }
type GToken ¶
type GToken struct { // GoFrame server name ServerName string // 缓存模式 1 gcache 2 gredis 默认1 CacheMode int8 // 缓存key CacheKey string // 超时时间 默认10天(毫秒) Timeout int64 // 缓存刷新时间 默认为超时时间的一半(毫秒) MaxRefresh int64 // Token分隔符 TokenDelimiter string // Token加密key EncryptKey []byte // 认证失败中文提示 AuthFailMsg string // 是否支持多端登录,默认false MultiLogin bool // 是否是全局认证,兼容历史版本,已废弃 GlobalMiddleware bool // 中间件类型 1 GroupMiddleware 2 BindMiddleware 3 GlobalMiddleware MiddlewareType uint // 登录路径 LoginPath string // 登录验证方法 return userKey 用户标识 如果userKey为空,结束执行 LoginBeforeFunc func(r *ghttp.Request) (string, interface{}) // 登录返回方法 LoginAfterFunc func(r *ghttp.Request, respData Resp) // 登出地址 LogoutPath string // 登出验证方法 return true 继续执行,否则结束执行 LogoutBeforeFunc func(r *ghttp.Request) bool // 登出返回方法 LogoutAfterFunc func(r *ghttp.Request, respData Resp) // 拦截地址 AuthPaths g.SliceStr // 拦截排除地址 AuthExcludePaths g.SliceStr // 认证验证方法 return true 继续执行,否则结束执行 AuthBeforeFunc func(r *ghttp.Request) bool // 认证返回方法 AuthAfterFunc func(r *ghttp.Request, respData Resp) // contains filtered or unexported fields }
GToken gtoken结构体
func NewGToken ¶
func NewGToken(opts ...OptionFunc) *GToken
func (*GToken) AuthMiddleware ¶
func (m *GToken) AuthMiddleware(group *ghttp.RouterGroup) bool
AuthMiddleware 绑定登录状态校验
func (*GToken) DecryptToken ¶
DecryptToken token解密方法
func (*GToken) DecryptUtilToken ¶
func (m *GToken) DecryptUtilToken(ctx context.Context, token string) (DecryptStr, uuid string, err error)
DecryptToken token解密方法
func (*GToken) EncryptToken ¶
EncryptToken token加密方法
func (*GToken) EncryptUtilToken ¶
func (m *GToken) EncryptUtilToken(ctx context.Context, key string, randStr ...string) (encryptStr, uuid string, err error)
EncryptToken token加密方法
func (*GToken) GenerateToken ¶
func (m *GToken) GenerateToken(ctx context.Context, key string, data interface{}) (keys string, err error)
生成token
func (*GToken) GetTokenData ¶
GetTokenData 通过token获取对象
func (*GToken) GetUtilToken ¶
func (*GToken) IsUtilEffective ¶
检查缓存的token是否有效且自动刷新缓存token
func (*GToken) IsUtilLogin ¶
func (m *GToken) IsUtilLogin(r *ghttp.Request) (b bool, failed *AuthFailed)
func (*GToken) IsUtilNotExpired ¶
func (m *GToken) IsUtilNotExpired(token string) (*CustomClaims, int)
检查token是否过期 (过期时间 = 超时时间 + 缓存刷新时间)
func (*GToken) Middleware ¶
func (m *GToken) Middleware(group *ghttp.RouterGroup) error
Middleware 绑定group
func (*GToken) ParseToken ¶
func (m *GToken) ParseToken(r *ghttp.Request) (*CustomClaims, error)
解析token (只验证格式并不验证过期)
func (*GToken) ParseUtilToken ¶
func (m *GToken) ParseUtilToken(r *ghttp.Request) (*CustomClaims, error)
解析token (只验证格式并不验证过期)
func (*GToken) RefreshUtilToken ¶
刷新token的缓存有效期
func (*GToken) RemoveGeneralToken ¶
RemoveGeneralToken 删除Token
func (*GToken) RemoveToken ¶
RemoveToken 删除token
type JwtSign ¶
type JwtSign struct {
SigningKey []byte
}
定义一个 JWT验签 结构体
func (*JwtSign) CreateToken ¶
func (j *JwtSign) CreateToken(claims CustomClaims) (string, error)
CreateToken 生成一个token
func (*JwtSign) ParseToken ¶
func (j *JwtSign) ParseToken(tokenString string) (*CustomClaims, error)
解析Token (只验证格式并不验证过期)
type OptionFunc ¶
type OptionFunc func(*GToken)
func WithCacheKey ¶
func WithCacheKey(value string) OptionFunc
func WithEncryptKey ¶
func WithEncryptKey(value []byte) OptionFunc
func WithExcludePaths ¶
func WithExcludePaths(value g.SliceStr) OptionFunc
func WithGCache ¶
func WithGCache() OptionFunc
func WithGRedis ¶
func WithGRedis(redis ...*gredis.Redis) OptionFunc
func WithGRedisConfig ¶
func WithGRedisConfig(redisConfig ...*gredis.Config) OptionFunc
func WithMaxRefresh ¶
func WithMaxRefresh(value int64) OptionFunc
func WithMultiLogin ¶
func WithMultiLogin(b bool) OptionFunc
func WithServerName ¶
func WithServerName(value string) OptionFunc
func WithTimeout ¶
func WithTimeout(value int64) OptionFunc
func WithTimeoutAndMaxRefresh ¶
func WithTimeoutAndMaxRefresh(timeout, maxRefresh int64) OptionFunc
func WithUserJwt ¶
func WithUserJwt(key string) OptionFunc
Click to show internal directories.
Click to hide internal directories.