token

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

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

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

Documentation

Index

Constants

View Source
const (
	NEVER_EXPIRE     int64 = -1 //常量,表示一个key永不过期 (在一个key被标注为永远不过期时返回此值)
	NOT_VALUE_EXPIRE int64 = -2 //常量,表示系统中不存在这个缓存 (在对不存在的key获取剩余存活时间时返回此值)
)
View Source
const (
	NOT_TOKEN             = "-1"
	NOT_TOKEN_MESSAGE     = "未提供Token"
	INVALID_TOKEN         = "-2"
	INVALID_TOKEN_MESSAGE = "Token无效"
	TOKEN_TIMEOUT         = "-3"
	TOKEN_TIMEOUT_MESSAGE = "Token已过期"
	BE_REPLACED           = "-4"
	BE_REPLACED_MESSAGE   = "Token已被顶下线"
	KICK_OUT              = "-5"
	KICK_OUT_MESSAGE      = "Token已被踢下线"
	DEFAULT_MESSAGE       = "当前会话未登录"
)

Variables

ABNORMAL_LIST 异常map

Functions

func InterfaceIsNil

func InterfaceIsNil(i interface{}) bool

func NewSession

func NewSession(id string, logic *Logic) *session

NewSession 根据sessionId创建session

Types

type Config

type Config struct {
	TokenName       string     `json:"token_name" yaml:"TokenName" toml:"token_name"`                   // token名称,提交的时候按照此名称获取token数据
	Timeout         int64      `json:"timeout" yaml:"Timeout" toml:"timeout"`                           // userToken 过期时间
	ActivityTimeout int64      `json:"activity_timeout" yaml:"ActivityTimeout" toml:"activity_timeout"` // 临时过期时间,用于(超过多少时间不能再操作)
	IsConcurrent    bool       `json:"is_concurrent" yaml:"IsConcurrent" toml:"is_concurrent"`          // 是否支持多账号登录
	IsShare         bool       `json:"is_share" yaml:"IsShare" toml:"is_share"`                         // 是否共享token
	TokenStyle      TokenStyle `json:"token_style" yaml:"TokenStyle" toml:"token_style"`                // token的样式
	AutoRenew       bool       `json:"auto_renew" yaml:"AutoRenew"  toml:"auto_renew"`                  // 自动续签
	TokenPrefix     string     `json:"token_prefix" yaml:"TokenPrefix" toml:"token_prefix"`             // token前缀
	IsLog           bool       `json:"is_log" yaml:"IsLog" toml:"is_log"`                               // 是否打印日志
	CheckLogin      bool       `json:"check_login" yaml:"CheckLogin" toml:"check_login"`                // 检查是否登录
	// contains filtered or unexported fields
}

Config 配置信息

func DefaultConfig

func DefaultConfig() *Config

func RawConfig

func RawConfig(key string) *Config

RawConfig 根据配置键扫描配置

func ScanConfig

func ScanConfig(name string) *Config

ScanConfig 根据配置名扫描配置

func (*Config) Build

func (c *Config) Build(loginType string) *Logic

Build 构建token

func (*Config) WithLogger

func (c *Config) WithLogger(log *logger.Logger) *Config

WithLogger 设置日志组件

type DisableLoginError

type DisableLoginError struct {
	LoginId     string `json:"login_id"`
	LogicType   string `json:"logic_type"`
	DisableTime int64  `json:"disable_time"`
}

func (*DisableLoginError) Error

func (d *DisableLoginError) Error() string

Error 详细的错误信息

func (*DisableLoginError) Message

func (d *DisableLoginError) Message() string

Message 错误提示信息

func (*DisableLoginError) Type

type Info

type Info struct {
	TokenName            string `json:"token_name"`             // token名称
	TokenValue           string `json:"token_value"`            // token值
	IsLogin              bool   `json:"is_login"`               // 是否登录
	LogicType            string `json:"logic_type"`             // 逻辑类型
	TokenTimeout         int64  `json:"token_timeout"`          // 当前token剩余时间
	SessionTimeout       int64  `json:"session_timeout"`        // 当前用户session有效时间
	TokenSessionTimeout  int64  `json:"token_session_timeout"`  // 当前token的有效时间
	TokenActivityTimeout int64  `json:"token_activity_timeout"` // 无操作时token剩余时间
	LoginDevice          string `json:"login_device"`           // 登录设备
}

Info token详细信息

type Listener

type Listener interface {
	// DoLogin 每次登录触发
	// 形参:
	//	loginType – 账号类别
	//	loginId – 账号id
	//	loginOptions – 登录参数
	DoLogin(loginType string, loginId string, options loginOptions)
	// DoLogout 每次注销时触发
	//形参:
	//	loginType – 账号类别
	//	loginId – 账号id
	//	tokenValue – token值
	DoLogout(loginType string, loginId string, tokenValue string)
	// DoKickout 每次被踢下线时触发
	//形参:
	//	loginType – 账号类别
	//	loginId – 账号id
	//	tokenValue – token值
	DoKickout(loginType string, loginId string, tokenValue string)
	// DoReplaced 每次被顶下线时触发
	//形参:
	//	loginType – 账号类别
	//	loginId – 账号id
	//	tokenValue – token值
	DoReplaced(loginType string, loginId string, tokenValue string)
	// DoDisable 账号被封禁时触发
	//形参:
	//	loginType – 账号类别
	//	loginId – 账号id
	//	disableTime – 封禁的时长
	DoDisable(loginType string, loginId string, disableTime int64)
	// DoUntieDisable 每次账号被解封时触发
	//形参:
	//	loginType – 账号类别
	//	loginId – 账号id
	DoUntieDisable(loginType string, loginId string)
	// DoCreateSession 每次创建Session时触发
	//形参:
	//	id – SessionId
	DoCreateSession(id string)
	//DoLogoutSession 每次注销Session时触发
	//形参:
	//	id – SessionId
	DoLogoutSession(id string)
}

Listener 监听器

type Logger

type Logger interface {
	Debug(args ...interface{})
	Info(args ...interface{})
	Warn(args ...interface{})
	Error(args ...interface{})
	DPanic(args ...interface{})
	Panic(args ...interface{})

	Panicd(message string, fields ...logger.Field)

	Debugf(template string, args ...interface{})
	Infof(template string, args ...interface{})
	Warnf(template string, args ...interface{})
	Errorf(template string, args ...interface{})
	DPanicf(template string, args ...interface{})
	Panicf(template string, args ...interface{})
}

type Logic

type Logic struct {
	// contains filtered or unexported fields
}

func NewLogic

func NewLogic(logicType string, conf *Config) *Logic

NewLogic 创建登录逻辑

func (*Logic) CheckActivityTimeout

func (l *Logic) CheckActivityTimeout(tokenValue string) error

CheckActivityTimeout 检查指定token 是否已经[临时过期],如果已经过期则返回错误 形参: tokenValue - token值

func (*Logic) CheckLogin

func (l *Logic) CheckLogin(tokenValue string) bool

CheckLogin 查询指定token是否登录 形参 tokenValue -- token值

func (*Logic) CheckRoleOr

func (l *Logic) CheckRoleOr(loginId string, roleArray ...string) bool

CheckRoleOr 校验:当前账号是否含有指定角色标识 [指定多个,只要其一验证通过即可] 形参:

loginId - 指定用户
roleArray – 角色标识数组

func (*Logic) CreateSession

func (l *Logic) CreateSession(sessionId string) *session

CreateSession 创建一个session

func (*Logic) CreateTokenValue

func (l *Logic) CreateTokenValue(loginId string, device string, timeout int64) string

CreateTokenValue 创建token

func (*Logic) DeleteTokenSession

func (l *Logic) DeleteTokenSession(tokenValue string)

DeleteTokenSession 删除Token-Session 形参: tokenValue – token值

func (*Logic) DeleteTokenToIdMapping

func (l *Logic) DeleteTokenToIdMapping(tokenValue string)

DeleteTokenToIdMapping 删除 Token-Id 映射 形参:

tokenValue – token值

func (*Logic) Disable

func (l *Logic) Disable(loginId string, disableTime int64) bool

Disable 封禁指定账号到指定时间 形参: loginId - 账号id disableTime - 封禁时长 (-1=永久封禁)

func (*Logic) GetDisableTime

func (l *Logic) GetDisableTime(loginId string) int64

GetDisableTime 获取封禁时间

func (*Logic) GetListener

func (l *Logic) GetListener() Listener

GetListener 获取监听器

func (*Logic) GetLogicType

func (l *Logic) GetLogicType() string

GetLogicType 获取登录逻辑

func (*Logic) GetLoginDevice

func (l *Logic) GetLoginDevice(tokenValue string) string

GetLoginDevice 根据指定token值查询登录设备 形参:

tokenValue -- token值

func (*Logic) GetLoginId

func (l *Logic) GetLoginId(tokenValue string) (string, error)

GetLoginId 根据指定token获取用户,不存在则返回错误信息 形参: tokenValue - 指定的token

func (*Logic) GetLoginIdByToken

func (l *Logic) GetLoginIdByToken(tokenValue string) string

GetLoginIdByToken 获取指定Token对应的账号id,如果未登录,则返回 "" 形参:

tokenValue – token

func (*Logic) GetLoginIdDefaultNull

func (l *Logic) GetLoginIdDefaultNull(tokenValue string) string

GetLoginIdDefaultNull 获取当前会话账号id, 如果未登录,则返回"",并返回错误

func (*Logic) GetLoginIdNotHandle

func (l *Logic) GetLoginIdNotHandle(tokenValue string) string

GetLoginIdNotHandle 获取指定Token对应的账号id (不做任何特殊处理) 形参: tokenValue – token值

func (*Logic) GetPermission

func (l *Logic) GetPermission() Permission

GetPermission 获取权限获取器

func (*Logic) GetPermissionList

func (l *Logic) GetPermissionList(loginId string) []string

GetPermissionList 获取:指定账号的权限码集合 形参: loginId – 指定账号id

func (*Logic) GetRoleList

func (l *Logic) GetRoleList(loginId string) ([]string, error)

GetRoleList 获取:指定账号的角色集合 形参:

loginId – 指定账号id

func (*Logic) GetSessionByLoginId

func (l *Logic) GetSessionByLoginId(loginId string, isCreate bool) *session

GetSessionByLoginId 获取指定loginId的session

func (*Logic) GetSessionBySessionId

func (l *Logic) GetSessionBySessionId(sessionId string, isCreate bool) *session

GetSessionBySessionId 根据SessionId获取session对象

func (*Logic) GetSessionTimeoutByLoginId

func (l *Logic) GetSessionTimeoutByLoginId(loginId string) int64

GetSessionTimeoutByLoginId 获取指定 loginId 的 User-Session 剩余有效时间 (单位: 秒) 形参: loginId - 指定用户

func (*Logic) GetStorage

func (l *Logic) GetStorage() Storage

GetStorage 获取持久化

func (*Logic) GetTokenAction

func (l *Logic) GetTokenAction() TokenAction

GetTokenAction 设置token操作接口

func (*Logic) GetTokenActivityTimeoutByToken

func (l *Logic) GetTokenActivityTimeoutByToken(tokenValue string) int64

GetTokenActivityTimeoutByToken 获取指定token的临时有效期 形参: tokenValue – 指定token

func (*Logic) GetTokenInfo

func (l *Logic) GetTokenInfo(tokenValue string) *Info

GetTokenInfo 获取指定token的登录信息

func (*Logic) GetTokenName

func (l *Logic) GetTokenName() string

GetTokenName 获取当前logic的token名

func (*Logic) GetTokenSessionTimeoutByTokenValue

func (l *Logic) GetTokenSessionTimeoutByTokenValue(tokenValue string) int64

GetTokenSessionTimeoutByTokenValue 获取指定 Token-Session 剩余有效时间 (单位: 秒) 形参: tokenValue – 指定token

func (*Logic) GetTokenTimeout

func (l *Logic) GetTokenTimeout(tokenValue string) int64

GetTokenTimeout 获取指定token的过期时间 形参: tokenValue - 指定token

func (*Logic) GetTokenValueByLoginId

func (l *Logic) GetTokenValueByLoginId(loginId string, device string) string

GetTokenValueByLoginId 获取指定id指定设备端的tokenValue 形参 loginId - 登录用户 device - 登录设备

func (*Logic) GetTokenValueListByLoginId

func (l *Logic) GetTokenValueListByLoginId(loginId string, device string) []string

GetTokenValueListByLoginId 获取指定id指定设备端的token切片

func (*Logic) HasPathPermission

func (l *Logic) HasPathPermission(loginId string, path string, unescape bool) bool

HasPathPermission 判断:指定账号是否含有指定路由 形参: loginId - 指定用户 path – 指定路由路径

func (*Logic) HasPermission

func (l *Logic) HasPermission(loginId string, permission string) bool

HasPermission 判断:当前账号是否含有指定权限, 返回true或false 形参:

loginId - 指定用户
permission – 权限码

func (*Logic) HasPermissionAnd

func (l *Logic) HasPermissionAnd(loginId string, permissionArray ...string) bool

HasPermissionAnd 判断:指定账号是否含有指定权限, [指定多个,必须全部具有] 形参: loginId - 指定用户 permissionArray – 权限码数组

func (*Logic) HasPermissionOr

func (l *Logic) HasPermissionOr(loginId string, permissionArray ...string) bool

HasPermissionOr 判断:指定账号是否含有指定权限, [指定多个,任意一个有] 形参: loginId - 指定用户 permissionArray – 权限码数组

func (*Logic) HasRole

func (l *Logic) HasRole(loginId string, role string) bool

HasRole 判断:指定账号是否含有指定角色标识, 返回true或false 形参:

loginId – 账号id
role – 角色标识

func (*Logic) HasRoleAnd

func (l *Logic) HasRoleAnd(loginId string, roleArray ...string) bool

HasRoleAnd 判断:当前账号是否含有指定角色标识 [指定多个,必须全部验证通过] 形参:

loginId - 指定用户
roleArray – 角色标识数组

func (*Logic) IsDisable

func (l *Logic) IsDisable(loginId string) bool

IsDisable 判断指定账号是否被封禁停用

func (*Logic) IsLogin

func (l *Logic) IsLogin(tokenValue string) bool

IsLogin 查询指定token是否登录 形参 tokenValue -- token值

func (*Logic) IsValidLoginId

func (l *Logic) IsValidLoginId(loginId string) bool

IsValidLoginId 判断指定用户id是否有效 形参 loginId - 用户id

func (*Logic) Login

func (l *Logic) Login(loginId string, opts ...LoginOption) (string, error)

Login 登录 形参:

loginId – 账号id
opts – 设备和超时时间

func (*Logic) Logout

func (l *Logic) Logout(loginId string, device string)

Logout 会话注销,根据账号id 和 设备标识 形参:

loginId – 账号id
device – 设备标识 (填""代表所有注销设备)

func (*Logic) LogoutByTokenValue

func (l *Logic) LogoutByTokenValue(tokenValue string)

LogoutByTokenValue 会话注销,根据token

func (*Logic) Replaced

func (l *Logic) Replaced(loginId string, device string) TokenError

Replaced 顶人下线,根据账号id 和 设备标识 当对方再次访问系统时,会抛出错误,场景值=-4 形参:

loginId – 账号id
device – 设备标识 (填null代表顶替所有设备)

func (*Logic) SaveTokenToIdMapping

func (l *Logic) SaveTokenToIdMapping(tokenValue string, loginId string, timeout int64)

SaveTokenToIdMapping 存储 Token-Id 映射 形参: tokenValue – token值 loginId – 账号id timeout – 会话有效期 (单位: 秒)

func (*Logic) SetListener

func (l *Logic) SetListener(listener Listener) *Logic

SetListener 设置监听器

func (*Logic) SetPermission

func (l *Logic) SetPermission(permission Permission) *Logic

SetPermission 设置权限获取器

func (*Logic) SetStorage

func (l *Logic) SetStorage(storage Storage) *Logic

SetStorage 设置持久化存储

func (*Logic) SetTokenAction

func (l *Logic) SetTokenAction(action TokenAction) *Logic

SetTokenAction 设置token操作接口

func (*Logic) UpdateLastActivityToNow

func (l *Logic) UpdateLastActivityToNow(tokenValue string)

UpdateLastActivityToNow 续签指定token:(将 [最后操作时间] 更新为当前时间戳) 形参: tokenValue – 指定token

func (*Logic) UpdateTokenToIdMapping

func (l *Logic) UpdateTokenToIdMapping(tokenValue string, loginId string)

UpdateTokenToIdMapping 更改 Token 指向的 账号Id 值 形参:

tokenValue – token值
loginId – 新的账号Id值

type LoginOption

type LoginOption func(o *loginOptions)

func LoginDevice

func LoginDevice(device string) LoginOption

LoginDevice 客户端

func LoginTimeout

func LoginTimeout(timeout int64) LoginOption

LoginTimeout 当前此次登录的过期时间

type NoLoginErrorValue

type NoLoginErrorValue int8

NoLoginErrorValue 没有登录的错误

type NotLoginError

type NotLoginError struct {
	LogicType string
	Code      string
	Message   string
}

func NewNotLoginError

func NewNotLoginError(logicType string, code string, token string) *NotLoginError

NewNotLoginError 创建一个未登录错误

func (*NotLoginError) Error

func (n *NotLoginError) Error() string

Error 获取错误信息

func (*NotLoginError) Type

func (n *NotLoginError) Type() TokenErrorType

Type 获取错误类型

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single URL parameter, consisting of a key and a value.

type Params

type Params []Param

Params is a Param-slice, as returned by the router. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.

func (Params) ByName

func (ps Params) ByName(name string) (va string)

ByName returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.

func (Params) Get

func (ps Params) Get(name string) (string, bool)

Get returns the value of the first Param which key matches the given name and a boolean true. If no matching Param is found, an empty string is returned and a boolean false .

type Permission

type Permission interface {
	// GetRouterInfoWithPath 获取指定路径的路由信息
	GetRouterInfoWithPath(path string, unescape bool) (*RouterInfo, error)
	// GetPermissionSlice 获取指定账号指定设备的权限列表
	GetPermissionSlice(loginId string, logicType string) ([]string, error)
	// GetRoleListSlice 获取指定
	GetRoleListSlice(loginId string, logicType string) ([]string, error)
}

func NewDefaultPermission

func NewDefaultPermission() Permission

NewDefaultPermission 创建一个默认权限

type Router

type Router struct {
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter() *Router

func (*Router) AddRouter

func (r *Router) AddRouter(path string, permissions permissions)

func (*Router) GetRouter

func (r *Router) GetRouter(path string, unescape bool) *RouterInfo

type RouterInfo

type RouterInfo struct {
	Permissions permissions
	Params      *Params
	Tsr         bool
	FullPath    string
}

type Storage

type Storage interface {
	// Has 查询是否包含缓存
	Has(key string) bool
	// SetObject 设置对象
	SetObject(key string, value interface{}, timeout int64) bool
	// GetObject 获取obj
	GetObject(key string, obj interface{}) bool
	// Get 获取缓存
	Get(key string, def ...string) string
	// Set 写入缓存
	Set(key string, value string, timeout int64) bool
	// Update 修改缓存,不修改时间
	Update(key string, value string)
	// UpdateObject 修改持久化数据
	UpdateObject(key string, value interface{}) bool
	// UpdateObjectTTl 修改持久化时间
	UpdateObjectTTl(key string, timeout int64)
	// Del 删除缓存
	Del(key string) bool
	// TTl 获取缓存剩余过期时间
	TTl(key string) int64
	// Clear 清空缓存
	Clear() bool
}

func NewDefaultStorage

func NewDefaultStorage(size int) Storage

NewDefaultStorage 创建一个默认的存储器

type TokenAction

type TokenAction interface {
	// contains filtered or unexported methods
}

TokenAction token相关操作

type TokenError

type TokenError interface {
	// Type 错误类型
	Type() TokenErrorType
	// Error 错误信息
	Error() string
}

func NewDisableLoginError

func NewDisableLoginError(LoginId string, LogicType string, DisableTime int64) TokenError

NewDisableLoginError 创建封禁错误

func NewTokenError

func NewTokenError(message string) TokenError

NewTokenError 创建一个token

type TokenErrorType

type TokenErrorType string

TokenErrorType 定义token错误类型

const (
	// DisableLoginErrorType 封禁账号错误
	DisableLoginErrorType TokenErrorType = "disable-login-error"
	NotLoginErrorType     TokenErrorType = "not-login-error"
)

定义所有的错误类型

type TokenStyle

type TokenStyle string

TokenStyle token样式

const (
	TOKEN_STYLE_UUID        TokenStyle = "uuid"        // uuid样式
	TOKEN_STYLE_SIMPLE_UUID TokenStyle = "simple-uuid" // uuid不带下划线
	TOKEN_STYLE_RANDOM_32   TokenStyle = "random-32"   // 随机32位字符串
	TOKEN_STYLE_RANDOM_64   TokenStyle = "random-64"   // 随机64位字符串
	TOKEN_STYLE_JWT         TokenStyle = "jwt"         // jwt格式
)

Jump to

Keyboard shortcuts

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