gtoken

package
v1.6.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	CacheModeCache   = 1
	CacheModeRedis   = 2
	CacheModeFile    = 3
	CacheModeFileDat = "gtoken.dat"

	MiddlewareTypeGroup  = 1
	MiddlewareTypeBind   = 2
	MiddlewareTypeGlobal = 3

	DefaultTimeout        = 10 * 24 * 60 * 60 * 1000
	DefaultCacheKey       = "GToken:"
	DefaultTokenDelimiter = "_"
	DefaultEncryptKey     = "12345678912345678912345678912345"
	DefaultAuthFailMsg    = "请求错误或登录超时"

	TraceId = "d5dfce77cdff812161134e55de3c5207"

	KeyUserKey     = "userKey"
	KeyRefreshTime = "refreshTime"
	KeyCreateTime  = "createTime"
	KeyUuid        = "uuid"
	KeyData        = "data"
	KeyToken       = "token"
)
View Source
const (
	DefaultLogPrefix   = "[GToken]" // 日志前缀
	MsgLogoutSucc      = "Logout success"
	MsgErrInitFail     = "InitConfig fail"
	MsgErrNotSet       = "%s not set, error"
	MsgErrUserKeyEmpty = "userKey is empty"
	MsgErrReqMethod    = "request method is error! "
	MsgErrAuthHeader   = "Authorization : %s get token key fail"
	MsgErrTokenEmpty   = "token is empty"
	MsgErrTokenEncrypt = "token encrypt error"
	MsgErrTokenDecode  = "token decode error"
	MsgErrTokenLen     = "token len error"
	MsgErrAuthUuid     = "user auth uuid error"
)
View Source
const (
	SUCCESS      = 0
	FAIL         = -1
	ERROR        = -99
	UNAUTHORIZED = -401
)

Variables

This section is empty.

Functions

This section is empty.

Types

type GfToken

type GfToken struct {
	// GoFrame server name
	ServerName string
	// 缓存模式 1 gcache 2 gredis 默认1
	CacheMode int8
	// 缓存key
	CacheKey string
	// 超时时间 默认10天(毫秒)
	Timeout int
	// 缓存刷新时间 默认为超时时间的一半(毫秒)
	MaxRefresh int
	// 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)
}

GfToken gtoken结构体

func (*GfToken) AuthPath

func (m *GfToken) AuthPath(ctx context.Context, urlPath string) bool

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

func (*GfToken) DecryptToken

func (m *GfToken) DecryptToken(ctx context.Context, token string) Resp

DecryptToken token解密方法

func (*GfToken) EncryptToken

func (m *GfToken) EncryptToken(ctx context.Context, userKey string, uuid string) Resp

EncryptToken token加密方法

func (*GfToken) GetTokenData

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

GetTokenData 通过token获取对象

func (*GfToken) InitConfig

func (m *GfToken) InitConfig() bool

InitConfig 初始化配置信息

func (*GfToken) Login

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

Login 登录

func (*GfToken) Logout

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

Logout 登出

func (*GfToken) Middleware

func (m *GfToken) Middleware(ctx context.Context, group *ghttp.RouterGroup) error

Middleware 绑定group

func (*GfToken) RemoveToken

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

RemoveToken 删除Token

func (*GfToken) Start

func (m *GfToken) Start() error

Start 启动

func (*GfToken) Stop

func (m *GfToken) Stop(ctx context.Context) error

Stop 结束

func (*GfToken) String

func (m *GfToken) String() string

String token解密方法

type Resp

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

func Error

func Error(msg string) Resp

Error 错误

func ErrorData

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

ErrorData 错误设置Data

func Fail

func Fail(msg string) Resp

Fail 失败

func FailData

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

FailData 失败设置Data

func Succ

func Succ(data interface{}) Resp

Succ 成功

func Unauthorized

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

Unauthorized 认证失败

func (Resp) DataInt

func (resp Resp) DataInt() int

DataInt 获取Data转Int

func (Resp) DataString

func (resp Resp) DataString() string

DataString 获取Data转字符串

func (Resp) Get

func (resp Resp) Get(key string) *gvar.Var

Get 获取Data值

func (Resp) GetInt

func (resp Resp) GetInt(key string) int

GetInt 获取Data值转Int

func (Resp) GetString

func (resp Resp) GetString(key string) string

GetString 获取Data值转字符串

func (Resp) Json

func (resp Resp) Json() string

func (Resp) Success

func (resp Resp) Success() bool

Success 获取Data值转字符串

Jump to

Keyboard shortcuts

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