Documentation
¶
Index ¶
- Constants
- func AddExcludePaths(urls ...string)
- func ClearPaths()
- func GenerateToken(userID string) (string, error)
- func GetRequestToken(r *ghttp.Request) (string, error)
- func GetTokenByUserKey(ctx context.Context, userID string) (string, error)
- func GetUserIDByToken(ctx context.Context, token string) (*gvar.Var, error)
- func IsLogin(ctx context.Context, token string) (userID *gvar.Var, isLoggedIn bool, err error)
- func Login(ctx context.Context, userID string) (string, error)
- func Logout(ctx context.Context, userID string) error
- func LogoutByToken(ctx context.Context, token string) error
- func RemovePaths(urls ...string)
- func SearchPath(url string) bool
- type Config
- type Manager
- func (m *Manager) AddExcludePaths(urls ...string)
- func (m *Manager) ClearPaths()
- func (m *Manager) GenerateToken(userID string) (string, error)
- func (m *Manager) GetTokenByUserKey(ctx context.Context, userID string) (string, error)
- func (m *Manager) GetUserIDByToken(ctx context.Context, token string) (*gvar.Var, error)
- func (m *Manager) IsLogin(ctx context.Context, token string) (userID *gvar.Var, isLoggedIn bool, err error)
- func (m *Manager) Login(ctx context.Context, userID string) (string, error)
- func (m *Manager) Logout(ctx context.Context, userID string) error
- func (m *Manager) LogoutByToken(ctx context.Context, token string) error
- func (m *Manager) RemovePaths(url ...string)
- func (m *Manager) SearchPath(url string) bool
- type Middleware
- type TokenStyle
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" MsgErrInstanceNotInit = "GToken instance not initialized" )
错误信息
Variables ¶
This section is empty.
Functions ¶
func AddExcludePaths ¶ added in v0.0.2
func AddExcludePaths(urls ...string)
AddExcludePaths 添加认证排除路径,这些路径不需要 token 验证 urls: 需要排除的路径列表
func GenerateToken ¶ added in v0.0.2
GenerateToken 生成新的 token userID: 用户唯一标识 return: token 和错误信息
func GetTokenByUserKey ¶ added in v0.0.2
GetTokenByUserKey 通过用户 ID 获取 token ctx: 上下文 userID: 用户唯一标识 return: token 和错误信息
func GetUserIDByToken ¶ added in v0.0.2
GetUserIDByToken 通过 token 获取用户 ID ctx: 上下文 token: 用户 token return: 用户 ID 和错误信息
func IsLogin ¶ added in v0.0.2
IsLogin 验证 token 是否有效,判断用户是否已登录 ctx: 上下文 token: 用户 token return: 用户 ID、是否登录、错误信息
func LogoutByToken ¶ added in v0.0.2
LogoutByToken 通过 token 退出登录,清除该 token 及关联的用户缓存 ctx: 上下文 token: 用户 token return: 错误信息
func RemovePaths ¶ added in v0.0.2
func RemovePaths(urls ...string)
RemovePaths 移除排除路径 urls: 要移除的路径列表
func SearchPath ¶ added in v0.0.2
SearchPath 检查路径是否在排除列表中 url: 要检查的路径 return: 是否在排除列表中
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 `
}
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
var (
Instance *Manager
)
全局实例
func (*Manager) GenerateToken ¶
生成token
func (*Manager) GetTokenByUserKey ¶
通过userID获取token, token过期或不存在error
func (*Manager) GetUserIDByToken ¶
通过token获取userID, UserID过期或不存在error
func (*Manager) LogoutByToken ¶
通过token退出登录
type Middleware ¶
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" )
Click to show internal directories.
Click to hide internal directories.