gftoken

package
v0.0.0-...-04dccba Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2023 License: MIT Imports: 18 Imported by: 0

Documentation

Index

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 AuthFailed struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

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) AuthPath

func (m *GfToken) AuthPath(urlPath string) bool

AuthPath 判断路径是否需要进行认证拦截 return true 需要认证

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)

生成token

func (*GfToken) GetRequestToken

func (m *GfToken) GetRequestToken(r *ghttp.Request) (token string)

func (*GfToken) GetToken

func (m *GfToken) GetToken(r *ghttp.Request) (tData *TokenData, err error)

func (*GfToken) GetTokenData

func (m *GfToken) GetTokenData(ctx context.Context, token string) (tData *TokenData, key string, err error)

func (*GfToken) IsEffective

func (m *GfToken) IsEffective(ctx context.Context, token string) bool

检查缓存的token是否有效且自动刷新缓存token

func (*GfToken) IsLogin

func (m *GfToken) IsLogin(r *ghttp.Request) (b bool, failed *AuthFailed)

func (*GfToken) IsNotExpired

func (m *GfToken) IsNotExpired(token string) (*CustomClaims, int)

检查token是否过期 (过期时间 = 超时时间 + 缓存刷新时间)

func (*GfToken) IsRefresh

func (m *GfToken) IsRefresh(token string) bool

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)

解析token (只验证格式并不验证过期)

func (*GfToken) RefreshToken

func (m *GfToken) RefreshToken(oldToken string) (newToken string, err error)

刷新token的缓存有效期

func (*GfToken) RemoveToken

func (m *GfToken) RemoveToken(ctx context.Context, token string) (err error)

RemoveToken 删除token

type JwtSign

type JwtSign struct {
	SigningKey []byte
}

定义一个 JWT验签 结构体

func CreateMyJWT

func CreateMyJWT(JwtTokenSignKey string) *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)

解析Token (只验证格式并不验证过期)

func (*JwtSign) RefreshToken

func (j *JwtSign) RefreshToken(tokenString string, extraAddSeconds int64) (string, error)

更新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

type TokenData

type TokenData struct {
	JwtToken string `json:"jwtToken"`
	UuId     string `json:"uuId"`
}

TokenData Token 数据

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL