models

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApiRule

type ApiRule struct {
	// 路径
	Path []string `json:"path"`
	// 规则:true表示允许,false表示禁止
	Rule bool `json:"rule"`
}

ApiRule 定义API规则

type Config

type Config struct {
	// Language:错误信息语言类型,"zh"为中文,其他为英文
	Language string
	// MaxTokens:最大缓存Token数量,如果小于等于0则不限制数量
	MaxTokens int
	// Delimiter:分隔符,权限字符串分割符,默认是空格
	Delimiter string
	// TokenRenewTime:Token续期时间,单位秒,默认10分钟
	TokenRenewTime int64
}

Config 定义了Token管理器的配置

type ConfigRaw

type ConfigRaw struct {
	// Language:错误信息语言类型,"zh"为中文,其他为英文
	Language string `json:"language"`
	// Delimiter:分隔符,权限字符串分割符,默认是空格
	Delimiter string `json:"delimiter"`
	// MaxTokens:最大缓存Token数量,如果小于等于0则不限制数量
	MaxTokens int `json:"maxTokens"`
	// TokenRenewTime:Token续期时间,单位秒,默认10分钟
	TokenRenewTime string `json:"tokenRenewTime"`
}

type Group

type Group struct {
	// 名称
	Name string `json:"name"`
	// api权限规则
	ApiRules []ApiRule `json:"apiRules"`
	// Token过期时间(秒),0表示永不过期
	ExpireSeconds int64 `json:"tokenExpireSeconds"`
	// 允许多设备登录。为true时允许同一用户在多个设备上登录,不校验IP;为false时只允许在一个设备上登录,会校验IP
	AllowMultipleLogin bool `json:"allowMultipleLogin"`
}

Group 用户组配置

type GroupRaw

type GroupRaw struct {
	// 组ID
	ID uint `json:"id"`
	// 组名称
	Name string `json:"name"`
	// 允许访问的API列表
	AllowedAPIs string `json:"allowedApis"`
	// 禁止访问的API列表
	DeniedAPIs string `json:"deniedApis"`
	// Token过期时间(秒),0表示永不过期
	TokenExpire string `json:"tokenExpire"`
	// 允许多设备登录。为1时允许同一用户在多个设备上登录,不校验IP;为false时只允许在一个设备上登录,会校验IP
	AllowMultipleLogin int `json:"allowMultipleLogin"`
}

用户组原型

type IManager

type IManager[T any] interface {
	// token管理
	GetToken(key string) (*Token[T], error)
	AddToken(userID uint, groupID uint, clientIp string) (string, error)
	GenerateToken() (string, error)
	DelToken(key string) error
	DelTokensByUserID(userID uint) error
	DelTokensByGroupID(groupID uint) error
	UpdateToken(key string, token *Token[T]) error
	CleanExpiredTokens()

	// 批量操作
	BatchDeleteTokensByUserIDs(userIDs []uint) error
	BatchDeleteTokensByGroupIDs(groupIDs []uint) error
	BatchDeleteExpiredTokens() error
	GetTokensByUserID(userID uint) []*Token[T]
	GetTokensByGroupID(groupID uint) []*Token[T]

	// 身份验证
	Auth(key string, clientIp string, api string) error
	BatchAuth(key string, clientIp string, apis []string) []bool

	// 用户组管理
	GetGroup(groupID uint) (*Group, error)
	AddGroup(group *GroupRaw) error
	DelGroup(groupID uint) error
	UpdateGroup(groupID uint, group *GroupRaw) error
	UpdateAllGroup(groups []GroupRaw) error

	// 统计信息
	GetStats() Stats

	// 用户数据管理
	SetUserData(key string, data T) error
	GetUserData(key string) (T, error)
}

IManager token管理器接口

type Stats

type Stats struct {
	// TotalTokens token总数
	TotalTokens int `json:"total_tokens"`
	// ActiveTokens 活跃token数量
	ActiveTokens int `json:"active_tokens"`
	// ExpiredTokens 过期token数量
	ExpiredTokens int `json:"expired_tokens"`
	// LastUpdateTime 最后更新时间
	LastUpdateTime time.Time `json:"last_update_time"`
}

Stats token统计信息

type Token

type Token[T any] struct {
	// 用户ID
	UserID uint `json:"userId"`
	// 用户组ID
	GroupID uint `json:"groupId"`
	// 登录时间
	LoginTime time.Time `json:"loginTime"`
	// 过期秒数,为0表示永不过期,大于0表示从登录时间起多少秒后过期,它会在使用token时刷新
	ExpireSeconds int64 `json:"expireTime"`
	// 最后访问时间
	LastAccessTime time.Time `json:"lastAccessTime"`
	// 用户数据
	UserData T `json:"userData"`
	// Token所属用户的IP地址
	IP string `json:"ip"`
}

Token 用户Token信息

func (*Token[T]) IsExpired

func (ut *Token[T]) IsExpired() bool

IsExpired 检查token是否过期

Jump to

Keyboard shortcuts

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