libGtoken

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2022 License: Apache-2.0 Imports: 22 Imported by: 0

README

说明:本文件夹参考 github.com/tiger1103/gfast-cache 项目进行实现

Documentation

Index

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

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

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

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

func (*GToken) DecryptToken

func (m *GToken) DecryptToken(token string) Resp

DecryptToken token解密方法

func (*GToken) DecryptUtilToken

func (m *GToken) DecryptUtilToken(ctx context.Context, token string) (DecryptStr, uuid string, err error)

DecryptToken token解密方法

func (*GToken) EncryptToken

func (m *GToken) EncryptToken(userKey string, uuid string) Resp

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

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

func (*GToken) GetToken

func (m *GToken) GetToken(r *ghttp.Request) (tData *tokenData, err error)

func (*GToken) GetTokenData

func (m *GToken) GetTokenData(r *ghttp.Request) Resp

GetTokenData 通过token获取对象

func (*GToken) GetUtilToken

func (m *GToken) GetUtilToken(r *ghttp.Request) (tData *tokenData, err error)

func (*GToken) InitConfig

func (m *GToken) InitConfig() bool

InitConfig 初始化配置信息

func (*GToken) IsLogin

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

func (*GToken) IsUtilEffective

func (m *GToken) IsUtilEffective(ctx context.Context, token string) bool

检查缓存的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) IsUtilRefresh

func (m *GToken) IsUtilRefresh(token string) bool

token是否处于刷新期

func (*GToken) Login

func (m *GToken) Login(r *ghttp.Request)

Login 登录

func (*GToken) Logout

func (m *GToken) Logout(r *ghttp.Request)

Logout 登出

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

func (m *GToken) RefreshUtilToken(oldToken string) (newToken string, err error)

刷新token的缓存有效期

func (*GToken) RemoveGeneralToken

func (m *GToken) RemoveGeneralToken(token string) Resp

RemoveGeneralToken 删除Token

func (*GToken) RemoveToken

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

RemoveToken 删除token

func (*GToken) Start

func (m *GToken) Start() error

Start 启动

func (*GToken) Stop

func (m *GToken) Stop() error

Stop 结束

func (*GToken) String

func (m *GToken) String() string

String 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(*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

type Resp

type Resp struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func Error

func Error(msg string) Resp

错误

func ErrorData

func ErrorData(msg string, data interface{}) Resp

错误设置Data

func Fail

func Fail(msg string) Resp

失败

func FailData

func FailData(msg string, data interface{}) Resp

失败设置Data

func Succ

func Succ(data interface{}) Resp

成功

func Unauthorized

func Unauthorized(msg string, data interface{}) Resp

认证失败

func (Resp) DataInt

func (resp Resp) DataInt() int

获取Data转Int

func (Resp) DataString

func (resp Resp) DataString() string

获取Data转字符串

func (Resp) Get

func (resp Resp) Get(key string) interface{}

获取Data值

func (Resp) GetInt

func (resp Resp) GetInt(key string) int

获取Data值转Int

func (Resp) GetString

func (resp Resp) GetString(key string) string

获取Data值转字符串

func (Resp) Json

func (resp Resp) Json() string

func (Resp) Success

func (resp Resp) Success() bool

获取Data值转字符串

Jump to

Keyboard shortcuts

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