core

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Standard HTTP status codes | 标准 HTTP 状态码
	CodeSuccess          = 200 // Request successful | 请求成功
	CodeBadRequest       = 400 // Bad request | 错误的请求
	CodeNotLogin         = 401 // Not authenticated | 未认证
	CodePermissionDenied = 403 // Permission denied | 权限不足
	CodeNotFound         = 404 // Resource not found | 资源未找到
	CodeServerError      = 500 // Internal server error | 服务器内部错误

	// Sa-Token specific error codes (10000-19999) | Sa-Token 特定错误码 (10000-19999)
	CodeTokenInvalid     = 10001 // Token is invalid or malformed | Token无效或格式错误
	CodeTokenExpired     = 10002 // Token has expired | Token已过期
	CodeAccountDisabled  = 10003 // Account is disabled | 账号已被禁用
	CodeKickedOut        = 10004 // User has been kicked out | 用户已被踢下线
	CodeActiveTimeout    = 10005 // Session inactive timeout | Session活跃超时
	CodeMaxLoginCount    = 10006 // Maximum login count reached | 达到最大登录数量
	CodeStorageError     = 10007 // Storage backend error | 存储后端错误
	CodeInvalidParameter = 10008 // Invalid parameter | 无效参数
	CodeSessionError     = 10009 // Session operation error | Session操作错误
)
View Source
const (
	TokenStyleUUID      = config.TokenStyleUUID
	TokenStyleSimple    = config.TokenStyleSimple
	TokenStyleRandom32  = config.TokenStyleRandom32
	TokenStyleRandom64  = config.TokenStyleRandom64
	TokenStyleRandom128 = config.TokenStyleRandom128
	TokenStyleJWT       = config.TokenStyleJWT
	TokenStyleHash      = config.TokenStyleHash
	TokenStyleTimestamp = config.TokenStyleTimestamp
	TokenStyleTik       = config.TokenStyleTik
)

Token style constants | Token风格常量

View Source
const (
	EventLogin           = listener.EventLogin
	EventLogout          = listener.EventLogout
	EventKickout         = listener.EventKickout
	EventDisable         = listener.EventDisable
	EventUntie           = listener.EventUntie
	EventRenew           = listener.EventRenew
	EventCreateSession   = listener.EventCreateSession
	EventDestroySession  = listener.EventDestroySession
	EventPermissionCheck = listener.EventPermissionCheck
	EventRoleCheck       = listener.EventRoleCheck
	EventAll             = listener.EventAll
)

Event constants | 事件常量

View Source
const (
	GrantTypeAuthorizationCode = oauth2.GrantTypeAuthorizationCode
	GrantTypeRefreshToken      = oauth2.GrantTypeRefreshToken
	GrantTypeClientCredentials = oauth2.GrantTypeClientCredentials
	GrantTypePassword          = oauth2.GrantTypePassword
)
View Source
const Version = "0.1.3"

Version Sa-Token-Go version | Sa-Token-Go版本

Variables

View Source
var (
	// ErrNotLogin indicates the user is not logged in | 用户未登录错误
	ErrNotLogin = fmt.Errorf("not logged in: authentication required")

	// ErrTokenInvalid indicates the provided token is invalid or malformed | Token无效或格式错误
	ErrTokenInvalid = fmt.Errorf("invalid token: the token is malformed or corrupted")

	// ErrTokenExpired indicates the token has expired | Token已过期
	ErrTokenExpired = fmt.Errorf("token expired: please login again to get a new token")

	// ErrInvalidLoginID indicates the login ID is invalid | 登录ID无效
	ErrInvalidLoginID = fmt.Errorf("invalid login ID: the login identifier cannot be empty")

	// ErrInvalidDevice indicates the device identifier is invalid | 设备标识无效
	ErrInvalidDevice = fmt.Errorf("invalid device: the device identifier is not valid")
)
View Source
var (
	// ErrPermissionDenied indicates insufficient permissions | 权限不足
	ErrPermissionDenied = fmt.Errorf("permission denied: insufficient permissions")

	// ErrRoleDenied indicates insufficient role | 角色权限不足
	ErrRoleDenied = fmt.Errorf("insufficient role: required role not found")
)
View Source
var (
	// ErrAccountDisabled indicates the account has been disabled or banned | 账号已被禁用
	ErrAccountDisabled = fmt.Errorf("account disabled: this account has been suspended or banned")

	// ErrAccountNotFound indicates the account doesn't exist | 账号不存在
	ErrAccountNotFound = fmt.Errorf("account not found: no account associated with this identifier")
)
View Source
var (
	// ErrSessionNotFound indicates the session doesn't exist | Session不存在
	ErrSessionNotFound = fmt.Errorf("session not found: the session may have expired or been deleted")

	// ErrKickedOut indicates the user has been kicked out | 用户已被踢下线
	ErrKickedOut = fmt.Errorf("kicked out: this session has been forcibly terminated")

	// ErrActiveTimeout indicates the session has been inactive for too long | Session活跃超时
	ErrActiveTimeout = fmt.Errorf("session inactive: the session has exceeded the inactivity timeout")

	// ErrMaxLoginCount indicates maximum concurrent login limit reached | 达到最大登录数量限制
	ErrMaxLoginCount = fmt.Errorf("max login limit: maximum number of concurrent logins reached")
)
View Source
var (
	// String utilities | 字符串工具
	RandomString        = utils.RandomString
	RandomNumericString = utils.RandomNumericString
	RandomAlphanumeric  = utils.RandomAlphanumeric
	IsEmpty             = utils.IsEmpty
	IsNotEmpty          = utils.IsNotEmpty
	DefaultString       = utils.DefaultString

	// Slice utilities | 切片工具
	ContainsString = utils.ContainsString
	RemoveString   = utils.RemoveString
	UniqueStrings  = utils.UniqueStrings
	MergeStrings   = utils.MergeStrings
	FilterStrings  = utils.FilterStrings
	MapStrings     = utils.MapStrings

	// Pattern matching | 模式匹配
	MatchPattern = utils.MatchPattern

	// Duration utilities | 时长工具
	FormatDuration = utils.FormatDuration
	ParseDuration  = utils.ParseDuration

	// Hash & Encoding | 哈希和编码
	SHA256Hash   = utils.SHA256Hash
	Base64Encode = utils.Base64Encode
	Base64Decode = utils.Base64Decode
)
View Source
var ErrStorageUnavailable = fmt.Errorf("storage unavailable: unable to connect to storage backend")

ErrStorageUnavailable indicates the storage backend is unavailable | 存储后端不可用

Functions

func GetErrorCode

func GetErrorCode(err error) int

GetErrorCode Extracts error code from SaTokenError | 从SaTokenError中提取错误码

func IsAccountDisabledError

func IsAccountDisabledError(err error) bool

IsAccountDisabledError Checks if error is an account disabled error | 检查是否为账号禁用错误

func IsNotLoginError

func IsNotLoginError(err error) bool

IsNotLoginError Checks if error is a not login error | 检查是否为未登录错误

func IsPermissionDeniedError

func IsPermissionDeniedError(err error) bool

IsPermissionDeniedError Checks if error is a permission denied error | 检查是否为权限拒绝错误

func IsTokenError

func IsTokenError(err error) bool

IsTokenError Checks if error is a token-related error | 检查是否为Token相关错误

Types

type Builder

type Builder = builder.Builder

Core types | 核心类型

func NewBuilder

func NewBuilder() *Builder

NewBuilder Creates a new builder for fluent configuration | 创建新的Builder构建器(用于流式配置)

type Config

type Config = config.Config

Configuration related types | 配置相关类型

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig Returns default configuration | 返回默认配置

type CookieConfig

type CookieConfig = config.CookieConfig

Configuration related types | 配置相关类型

type Event

type Event = listener.Event

Event related types | 事件相关类型

type EventData

type EventData = listener.EventData

Event related types | 事件相关类型

type EventListener

type EventListener = listener.Listener

Event related types | 事件相关类型

type EventManager

type EventManager = listener.Manager

Event related types | 事件相关类型

func NewEventManager

func NewEventManager() *EventManager

NewEventManager Creates a new event manager | 创建新的事件管理器

type ListenerConfig

type ListenerConfig = listener.ListenerConfig

Event related types | 事件相关类型

type ListenerFunc

type ListenerFunc = listener.ListenerFunc

Event related types | 事件相关类型

type Manager

type Manager = manager.Manager

Core types | 核心类型

func NewManager

func NewManager(storage Storage, cfg *Config) *Manager

NewManager Creates a new authentication manager | 创建新的认证管理器

type NonceManager

type NonceManager = security.NonceManager

Core types | 核心类型

func NewNonceManager

func NewNonceManager(storage Storage, prefix string, ttl ...int64) *NonceManager

NewNonceManager Creates a new nonce manager | 创建新的Nonce管理器

type OAuth2AccessToken

type OAuth2AccessToken = oauth2.AccessToken

Core types | 核心类型

type OAuth2Client

type OAuth2Client = oauth2.Client

Core types | 核心类型

type OAuth2GrantType

type OAuth2GrantType = oauth2.GrantType

Core types | 核心类型

type OAuth2Server

type OAuth2Server = oauth2.OAuth2Server

Core types | 核心类型

func NewOAuth2Server

func NewOAuth2Server(storage Storage, prefix string) *OAuth2Server

NewOAuth2Server Creates a new OAuth2 server | 创建新的OAuth2服务器

type RefreshTokenInfo

type RefreshTokenInfo = security.RefreshTokenInfo

Core types | 核心类型

type RefreshTokenManager

type RefreshTokenManager = security.RefreshTokenManager

Core types | 核心类型

func NewRefreshTokenManager

func NewRefreshTokenManager(storage Storage, prefix string, cfg *Config) *RefreshTokenManager

NewRefreshTokenManager Creates a new refresh token manager | 创建新的刷新令牌管理器

type RequestContext

type RequestContext = adapter.RequestContext

Adapter interfaces | 适配器接口

type SaTokenContext

type SaTokenContext = context.SaTokenContext

Core types | 核心类型

func NewContext

func NewContext(ctx RequestContext, mgr *Manager) *SaTokenContext

NewContext Creates a new Sa-Token context | 创建新的Sa-Token上下文

type SaTokenError

type SaTokenError struct {
	Code    int            // Error code for programmatic handling | 错误码,用于程序化处理
	Message string         // Human-readable error message | 可读的错误消息
	Err     error          // Underlying error (if any) | 底层错误(如果有)
	Context map[string]any // Additional context information | 额外的上下文信息
}

SaTokenError Represents a custom error with error code and context | 自定义错误类型,包含错误码和上下文信息

func NewAccountDisabledError

func NewAccountDisabledError(loginID string) *SaTokenError

NewAccountDisabledError Creates an account disabled error | 创建账号禁用错误

func NewError

func NewError(code int, message string, err error) *SaTokenError

NewError Creates a new Sa-Token error | 创建新的 Sa-Token 错误

func NewErrorWithContext

func NewErrorWithContext(code int, message string, err error, context map[string]any) *SaTokenError

NewErrorWithContext Creates a new Sa-Token error with context | 创建带上下文的 Sa-Token 错误

func NewNotLoginError

func NewNotLoginError() *SaTokenError

NewNotLoginError Creates a not login error | 创建未登录错误

func NewPermissionDeniedError

func NewPermissionDeniedError(permission string) *SaTokenError

NewPermissionDeniedError Creates a permission denied error | 创建权限拒绝错误

func NewRoleDeniedError

func NewRoleDeniedError(role string) *SaTokenError

NewRoleDeniedError Creates a role denied error | 创建角色拒绝错误

func (*SaTokenError) Error

func (e *SaTokenError) Error() string

Error Implements the error interface | 实现 error 接口

func (*SaTokenError) GetContext

func (e *SaTokenError) GetContext(key string) (any, bool)

GetContext Gets context value | 获取上下文值

func (*SaTokenError) Is

func (e *SaTokenError) Is(target error) bool

Is Implements errors.Is for error comparison | 实现 errors.Is 进行错误比较

func (*SaTokenError) Unwrap

func (e *SaTokenError) Unwrap() error

Unwrap Implements the unwrap interface for error chains | 实现 unwrap 接口,支持错误链

func (*SaTokenError) WithContext

func (e *SaTokenError) WithContext(key string, value any) *SaTokenError

WithContext Adds context information to the error | 为错误添加上下文信息

type Session

type Session = session.Session

Core types | 核心类型

func LoadSession

func LoadSession(id string, storage Storage, prefix string) (*Session, error)

LoadSession Loads an existing session | 加载已存在的Session

func NewSession

func NewSession(id string, storage Storage, prefix string) *Session

NewSession Creates a new session | 创建新的Session

type Storage

type Storage = adapter.Storage

Adapter interfaces | 适配器接口

type TokenGenerator

type TokenGenerator = token.Generator

Core types | 核心类型

func NewTokenGenerator

func NewTokenGenerator(cfg *Config) *TokenGenerator

NewTokenGenerator Creates a new token generator | 创建新的Token生成器

type TokenInfo

type TokenInfo = manager.TokenInfo

Core types | 核心类型

type TokenStyle

type TokenStyle = config.TokenStyle

Configuration related types | 配置相关类型

Directories

Path Synopsis
@Author daixk 2025-10-28 22:00:20
@Author daixk 2025-10-28 22:00:20

Jump to

Keyboard shortcuts

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