gtoken

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 16 Imported by: 0

README

gtoken

基于GoFrame框架的token插件

Documentation

Index

Constants

View Source
const (
	CacheModeCache = 1
	CacheModeRedis = 2
)

缓存模式

View Source
const (
	UserKeyPrefix  = "account:"
	TokenKeyPrefix = "token:"
)

常量

View Source
const (
	MsgErrUserIDEmpty       = "userID empty"
	MsgErrDataEmpty         = "cache value is nil"
	MsgErrTokenEmpty        = "token empty"
	MsgErrExpiredOrNotExist = "Data Expired or not Exist"
)

错误信息

Variables

This section is empty.

Functions

func GetRequestToken

func GetRequestToken(r *ghttp.Request) (string, error)

Types

type Config

type Config struct {
	CacheMode        int8          `c:"cacheMode"`        // 缓存模式 1 gcache 2 gredis 默认1
	CachePreKey      string        `c:"cachePreKey"`      // 缓存key前缀
	TokenStyle       TokenStyle    `c:"tokenStyle"`       // Token风格 默认 uuid
	TokenExpire      time.Duration `c:"tokenExpire"`      // Token过期时间 默认 2小时
	MultiLogin       bool          `c:"multiLogin"`       // 是否支持多端登录,默认false, (为true时支持多端登录,为false时仅允许一个端点登录)
	EnableRenew      bool          `c:"enableRenew"`      // 是否开启续期模式,默认false, (为true时在token过期前续期,为false时不续期)
	RenewThreshold   time.Duration `c:"renewThreshold"`   // 续期阈值,默认30分钟, 续签时间必须小于TokenExpire(距离过期时间多少开始续签)
	RenewTimeout     time.Duration `c:"renewTimeout"`     // 续期超时时间(默认3秒)
	AuthExcludePaths []string      `c:"authExcludePaths"` // 拦截排除地址 如: /login
	AuthHeaderName   string        `c:"authHeaderName"`   // 验证header的名称 默认 Authorization
	TokenPrefix      string        `c:"tokenPrefix"`      // token前缀 如`Bearer `
}

func NewConfigFromCtx

func NewConfigFromCtx(ctx context.Context) *Config

从配置文件中读取

type Manager

type Manager struct {
	// contains filtered or unexported fields
}
var (
	Instance *Manager
)

全局实例

func Init

func Init(config *Config) *Manager

初始化GToken实例

func (*Manager) AddExcludePaths

func (m *Manager) AddExcludePaths(urls ...string)

添加排除路径

func (*Manager) ClearPaths

func (m *Manager) ClearPaths()

清空排除路径

func (*Manager) GenerateToken

func (m *Manager) GenerateToken(userID string) (string, error)

生成token

func (*Manager) GetTokenByUserKey

func (m *Manager) GetTokenByUserKey(ctx context.Context, userID string) (string, error)

通过userID获取token, token过期或不存在error

func (*Manager) GetUserIDByToken

func (m *Manager) GetUserIDByToken(ctx context.Context, token string) (*gvar.Var, error)

通过token获取userID, UserID过期或不存在error

func (*Manager) IsLogin

func (m *Manager) IsLogin(ctx context.Context, token string) (userID *gvar.Var, isLoggedIn bool, err error)

func (*Manager) Login

func (m *Manager) Login(ctx context.Context, userID string) (string, error)

登录

func (*Manager) Logout

func (m *Manager) Logout(ctx context.Context, userID string) error

退出登录

func (*Manager) LogoutByToken

func (m *Manager) LogoutByToken(ctx context.Context, token string) error

通过token退出登录

func (*Manager) RemovePaths

func (m *Manager) RemovePaths(url ...string)

移除排除路径

func (*Manager) SearchPath

func (m *Manager) SearchPath(url string) bool

检查是否包含该路径

type Middleware

type Middleware struct {
	ResFunc func(r *ghttp.Request, err error)
}

func NewMiddleware

func NewMiddleware(resFunc ...func(r *ghttp.Request, err error)) *Middleware

func (*Middleware) Auth

func (m *Middleware) Auth(r *ghttp.Request)

type TokenStyle

type TokenStyle string
const (
	TokenStyleUUID    TokenStyle = "uuid"
	TokenStyleRand64  TokenStyle = "rand64"
	TokenStyleRand128 TokenStyle = "rand128"
)

Jump to

Keyboard shortcuts

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