Documentation ¶
Index ¶
- Constants
- type AuthFailed
- type CustomClaims
- type GfToken
- func (m *GfToken) AuthPath(urlPath string) bool
- func (m *GfToken) DecryptToken(ctx context.Context, token string) (DecryptStr, uuid string, err error)
- func (m *GfToken) EncryptToken(ctx context.Context, key string, randStr ...string) (encryptStr, uuid string, err error)
- func (m *GfToken) GenerateToken(ctx context.Context, key string, data interface{}) (keys string, err error)
- func (m *GfToken) GetRequestToken(r *ghttp.Request) (token string)
- func (m *GfToken) GetToken(r *ghttp.Request) (tData *TokenData, err error)
- func (m *GfToken) GetTokenData(ctx context.Context, token string) (tData *TokenData, key string, err error)
- func (m *GfToken) IsEffective(ctx context.Context, token string) bool
- func (m *GfToken) IsLogin(r *ghttp.Request) (b bool, failed *AuthFailed)
- func (m *GfToken) IsNotExpired(token string) (*CustomClaims, int)
- func (m *GfToken) IsRefresh(token string) bool
- func (m *GfToken) Middleware(group *ghttp.RouterGroup) error
- func (m *GfToken) ParseToken(r *ghttp.Request) (*CustomClaims, error)
- func (m *GfToken) RefreshToken(oldToken string) (newToken string, err error)
- func (m *GfToken) RemoveToken(ctx context.Context, token string) (err error)
- 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 TokenData
Constants ¶
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 FailedAuthCode = 401
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuthFailed ¶
type CustomClaims ¶
type CustomClaims struct { Data interface{} jwt.RegisteredClaims }
type GfToken ¶
type GfToken struct { // server name ServerName string // 缓存key (每创建一个实例CacheKey必须不相同) CacheKey string // 超时时间 默认10天(秒) Timeout int64 // 缓存刷新时间 默认5天(秒) // 处理携带token的请求时当前时间大于超时时间并小于缓存刷新时间时token将自动刷新即重置token存活时间 // MaxRefresh值为0时,token将不会自动刷新 MaxRefresh int64 // 是否允许多点登录 MultiLogin bool // Token加密key 32位 EncryptKey []byte // 拦截排除地址 ExcludePaths g.SliceStr // contains filtered or unexported fields }
func NewGfToken ¶
func NewGfToken(opts ...OptionFunc) *GfToken
func (*GfToken) DecryptToken ¶
func (m *GfToken) DecryptToken(ctx context.Context, token string) (DecryptStr, uuid string, err error)
DecryptToken token解密方法
func (*GfToken) EncryptToken ¶
func (m *GfToken) EncryptToken(ctx context.Context, key string, randStr ...string) (encryptStr, uuid string, err error)
EncryptToken token加密方法
func (*GfToken) GenerateToken ¶
func (m *GfToken) GenerateToken(ctx context.Context, key string, data interface{}) (keys string, err error)
GenerateToken 生成token
func (*GfToken) GetRequestToken ¶
func (*GfToken) GetTokenData ¶
func (*GfToken) IsEffective ¶
IsEffective 检查缓存的token是否有效且自动刷新缓存token
func (*GfToken) IsNotExpired ¶
func (m *GfToken) IsNotExpired(token string) (*CustomClaims, int)
IsNotExpired 检查token是否过期 (过期时间 = 超时时间 + 缓存刷新时间)
func (*GfToken) Middleware ¶
func (m *GfToken) Middleware(group *ghttp.RouterGroup) error
Middleware 绑定group
func (*GfToken) ParseToken ¶
func (m *GfToken) ParseToken(r *ghttp.Request) (*CustomClaims, error)
ParseToken 解析token (只验证格式并不验证过期)
func (*GfToken) RefreshToken ¶
RefreshToken 刷新token的缓存有效期
type JwtSign ¶
type JwtSign struct {
SigningKey []byte
}
JwtSign 定义一个 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)
ParseToken 解析Token (只验证格式并不验证过期)
type OptionFunc ¶
type OptionFunc func(*GfToken)
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
Source Files ¶
Click to show internal directories.
Click to hide internal directories.