manager

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 30, 2026 License: Apache-2.0 Imports: 13 Imported by: 10

Documentation

Overview

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

@Author daixk 2025/12/22 15:56:00

Index

Constants

View Source
const (
	// DisableKeyPrefix stores disable key prefix. DisableKeyPrefix 存储账号封禁键前缀。
	DisableKeyPrefix = "disable:"
	// DisableServiceKeyPrefix stores service disable key prefix. DisableServiceKeyPrefix 存储服务封禁键前缀。
	DisableServiceKeyPrefix = "disable:service:"
	// DisableDeviceKeyPrefix stores device disable key prefix. DisableDeviceKeyPrefix 存储设备封禁键前缀。
	DisableDeviceKeyPrefix = "disable:device:"
	// DisableDeviceIDKeyPrefix stores concrete device disable key prefix. DisableDeviceIDKeyPrefix 存储具体设备封禁键前缀。
	DisableDeviceIDKeyPrefix = "disable:device:id:"
	// SessionKeyPrefix stores session key prefix. SessionKeyPrefix 存储会话键前缀。
	SessionKeyPrefix = "session:"
	// RenewKeyPrefix stores renew key prefix. RenewKeyPrefix 存储 Token 续期键前缀。
	RenewKeyPrefix = "renew:"
	// ActivePrefix stores active key prefix. ActivePrefix 存储 Token 活跃时间键前缀。
	ActivePrefix = "active:"

	// SessionKeyLoginID stores session login id key. SessionKeyLoginID 存储登录 ID 字段名。
	SessionKeyLoginID = "loginId"
	// SessionKeyDevice stores session device key. SessionKeyDevice 存储设备类型字段名。
	SessionKeyDevice = "device"
	// SessionKeyLoginTime stores session login time key. SessionKeyLoginTime 存储登录时间字段名。
	SessionKeyLoginTime = "loginTime"
	// SessionKeyPermissions stores permissions key. SessionKeyPermissions 存储权限列表字段名。
	SessionKeyPermissions = "permissions"
	// SessionKeyRoles stores roles key. SessionKeyRoles 存储角色列表字段名。
	SessionKeyRoles = "roles"

	// PermissionWildcard stores permission wildcard. PermissionWildcard 存储权限通配符。
	PermissionWildcard = "*"
	// PermissionSeparator stores permission separator. PermissionSeparator 存储权限段分隔符。
	PermissionSeparator = ":"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessProvider

type AccessProvider interface {
	// Permissions resolves subject permissions. Permissions 解析主体权限。
	Permissions(ctx context.Context, subject AccessSubject) ([]string, error)
	// Roles resolves subject roles. Roles 解析主体角色。
	Roles(ctx context.Context, subject AccessSubject) ([]string, error)
}

AccessProvider resolves permissions and roles for a subject. AccessProvider 为主体解析权限和角色。

func NewLegacyAccessProvider

func NewLegacyAccessProvider(
	permissionFunc, roleFunc func(loginID, authType string) ([]string, error),
	permissionExtFunc, roleExtFunc func(loginID, device, deviceId, authType string) ([]string, error),
) AccessProvider

NewLegacyAccessProvider adapts custom callbacks into AccessProvider. NewLegacyAccessProvider 将自定义回调适配为 AccessProvider。

type AccessProviderFunc

type AccessProviderFunc struct {
	PermissionFunc func(ctx context.Context, subject AccessSubject) ([]string, error) // PermissionFunc resolves permissions. PermissionFunc 解析权限。
	RoleFunc       func(ctx context.Context, subject AccessSubject) ([]string, error) // RoleFunc resolves roles. RoleFunc 解析角色。
}

AccessProviderFunc adapts functions into an AccessProvider. AccessProviderFunc 将函数适配为 AccessProvider。

func (AccessProviderFunc) Permissions

func (f AccessProviderFunc) Permissions(ctx context.Context, subject AccessSubject) ([]string, error)

Permissions resolves permissions through PermissionFunc. Permissions 通过 PermissionFunc 解析权限。

func (AccessProviderFunc) Roles

func (f AccessProviderFunc) Roles(ctx context.Context, subject AccessSubject) ([]string, error)

Roles resolves roles through RoleFunc. Roles 通过 RoleFunc 解析角色。

type AccessSubject

type AccessSubject struct {
	AuthType string // AuthType stores auth namespace. AuthType 存储认证命名空间。
	LoginID  string // LoginID stores subject identifier. LoginID 存储主体标识。
	Device   string // Device stores device type. Device 存储设备类型。
	DeviceID string // DeviceID stores concrete device ID. DeviceID 存储具体设备 ID。
	Token    string // Token stores terminal token. Token 存储终端 Token。
}

AccessSubject describes the subject used to resolve permissions and roles. AccessSubject 描述用于解析权限和角色的主体。

type DeviceDisableInfo

type DeviceDisableInfo struct {
	Device        string `json:"device"`        // Device stores disabled device type. Device 存储被封禁设备类型。
	DeviceId      string `json:"deviceId"`      // DeviceId stores disabled concrete device ID. DeviceId 存储被封禁具体设备 ID。
	DisableTime   int64  `json:"disableTime"`   // DisableTime stores disable timestamp. DisableTime 存储封禁时间戳。
	DisableReason string `json:"disableReason"` // DisableReason stores disable reason. DisableReason 存储封禁原因。
}

DeviceDisableInfo defines device disable info. DeviceDisableInfo 定义设备封禁信息。

type DisableInfo

type DisableInfo struct {
	DisableTime   int64  `json:"disableTime"`   // DisableTime stores disable timestamp. DisableTime 存储封禁时间戳。
	DisableReason string `json:"disableReason"` // DisableReason stores disable reason. DisableReason 存储封禁原因。
}

DisableInfo defines account disable info. DisableInfo 定义账号封禁信息。

type Manager

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

Manager is the core auth facade. Manager 是鉴权核心门面。

func NewManager

func NewManager(
	cfg *config.Config,
	generator adapter.Generator,
	storage adapter.Storage,
	serializer adapter.Codec,
	logger adapter.Log,
	pool adapter.Pool,
	accessProvider AccessProvider,
	options ...Option,
) *Manager

NewManager creates a manager with the provided core components. NewManager 使用提供的核心组件创建管理器。

func (*Manager) AddPermissions

func (m *Manager) AddPermissions(ctx context.Context, loginID string, permissions []string) error

AddPermissions adds permissions to a user. AddPermissions 为用户添加权限。

func (*Manager) AddPermissionsByToken

func (m *Manager) AddPermissionsByToken(ctx context.Context, tokenValue string, permissions []string) error

AddPermissionsByToken adds permissions to a user by token. AddPermissionsByToken 根据 Token 为用户添加权限。

func (*Manager) AddRoles

func (m *Manager) AddRoles(ctx context.Context, loginID string, roles []string) error

AddRoles adds roles to a user. AddRoles 为用户添加角色。

func (*Manager) AddRolesByToken

func (m *Manager) AddRolesByToken(ctx context.Context, tokenValue string, roles []string) error

AddRolesByToken adds roles to a user by token. AddRolesByToken 根据 Token 为用户添加角色。

func (*Manager) CheckDisable

func (m *Manager) CheckDisable(ctx context.Context, loginID string) error

CheckDisable validates account disable state. CheckDisable 校验账号封禁状态。

func (*Manager) CheckDisableDevice

func (m *Manager) CheckDisableDevice(ctx context.Context, loginID, device string) error

CheckDisableDevice validates device type disable state. CheckDisableDevice 校验设备类型封禁状态。

func (*Manager) CheckDisableDeviceAndDeviceId

func (m *Manager) CheckDisableDeviceAndDeviceId(ctx context.Context, loginID, device, deviceId string) error

CheckDisableDeviceAndDeviceId validates concrete device disable state. CheckDisableDeviceAndDeviceId 校验具体设备封禁状态。

func (*Manager) CheckDisableService

func (m *Manager) CheckDisableService(ctx context.Context, loginID string, services ...string) error

CheckDisableService checks if any of the specified services are disabled, returns error if disabled. CheckDisableService 校验账号的指定服务是否被封禁,被封禁则返回 error。

func (*Manager) CheckDisableServiceLevel

func (m *Manager) CheckDisableServiceLevel(ctx context.Context, loginID, service string, level int) error

CheckDisableServiceLevel checks if a service is disabled at or above the given level, returns error if so. CheckDisableServiceLevel 校验账号的指定服务是否达到指定封禁等级,达到则返回 error。

func (*Manager) CheckLogin

func (m *Manager) CheckLogin(ctx context.Context, tokenValue string) error

CheckLogin checks if a user is logged in and returns an error if not. CheckLogin 检查用户是否登录,如果未登录则返回错误。

func (*Manager) CheckPermission

func (m *Manager) CheckPermission(ctx context.Context, loginID string, permission string) error

CheckPermission checks if a user has a specific permission. CheckPermission 校验用户是否拥有指定权限。

func (*Manager) CheckPermissionAnd

func (m *Manager) CheckPermissionAnd(ctx context.Context, loginID string, permissions []string) error

CheckPermissionAnd checks if a user has all specified permissions. CheckPermissionAnd 校验用户是否拥有全部权限。

func (*Manager) CheckPermissionAndByToken

func (m *Manager) CheckPermissionAndByToken(ctx context.Context, tokenValue string, permissions []string) error

CheckPermissionAndByToken checks if a token user has all specified permissions. CheckPermissionAndByToken 根据 Token 校验用户是否拥有全部权限。

func (*Manager) CheckPermissionByToken

func (m *Manager) CheckPermissionByToken(ctx context.Context, tokenValue string, permission string) error

CheckPermissionByToken checks if a token user has a specific permission. CheckPermissionByToken 根据 Token 校验用户是否拥有指定权限。

func (*Manager) CheckPermissionOr

func (m *Manager) CheckPermissionOr(ctx context.Context, loginID string, permissions []string) error

CheckPermissionOr checks if a user has any specified permission. CheckPermissionOr 校验用户是否拥有任一权限。

func (*Manager) CheckPermissionOrByToken

func (m *Manager) CheckPermissionOrByToken(ctx context.Context, tokenValue string, permissions []string) error

CheckPermissionOrByToken checks if a token user has any specified permission. CheckPermissionOrByToken 根据 Token 校验用户是否拥有任一权限。

func (*Manager) CheckRole

func (m *Manager) CheckRole(ctx context.Context, loginID string, role string) error

CheckRole checks if a user has a specific role. CheckRole 校验用户是否拥有指定角色。

func (*Manager) CheckRoleAnd

func (m *Manager) CheckRoleAnd(ctx context.Context, loginID string, roles []string) error

CheckRoleAnd checks if a user has all specified roles. CheckRoleAnd 校验用户是否拥有全部角色。

func (*Manager) CheckRoleAndByToken

func (m *Manager) CheckRoleAndByToken(ctx context.Context, tokenValue string, roles []string) error

CheckRoleAndByToken checks if a token user has all specified roles. CheckRoleAndByToken 根据 Token 校验用户是否拥有全部角色。

func (*Manager) CheckRoleByToken

func (m *Manager) CheckRoleByToken(ctx context.Context, tokenValue string, role string) error

CheckRoleByToken checks if a token user has a specific role. CheckRoleByToken 根据 Token 校验用户是否拥有指定角色。

func (*Manager) CheckRoleOr

func (m *Manager) CheckRoleOr(ctx context.Context, loginID string, roles []string) error

CheckRoleOr checks if a user has any specified role. CheckRoleOr 校验用户是否拥有任一角色。

func (*Manager) CheckRoleOrByToken

func (m *Manager) CheckRoleOrByToken(ctx context.Context, tokenValue string, roles []string) error

CheckRoleOrByToken checks if a token user has any specified role. CheckRoleOrByToken 根据 Token 校验用户是否拥有任一角色。

func (*Manager) CloseManager

func (m *Manager) CloseManager()

CloseManager closes the manager and releases all resources. CloseManager 关闭管理器并释放全部资源。

func (*Manager) Disable

func (m *Manager) Disable(ctx context.Context, loginID string, duration time.Duration, reason ...string) error

Disable disables an account for a specified duration. Disable 封禁账号指定时长。

func (*Manager) DisableDevice

func (m *Manager) DisableDevice(ctx context.Context, loginID, device string, duration time.Duration, reason ...string) error

DisableDevice disables a device type for an account. DisableDevice 封禁账号的指定设备类型。

func (*Manager) DisableDeviceAndDeviceId

func (m *Manager) DisableDeviceAndDeviceId(ctx context.Context, loginID, device, deviceId string, duration time.Duration, reason ...string) error

DisableDeviceAndDeviceId disables a concrete device for an account. DisableDeviceAndDeviceId 封禁账号的具体设备。

func (*Manager) DisableService

func (m *Manager) DisableService(ctx context.Context, loginID, service string, duration time.Duration, reason ...string) error

DisableService disables a specific service for an account. DisableService 封禁账号的指定服务。

func (*Manager) DisableServiceLevel

func (m *Manager) DisableServiceLevel(ctx context.Context, loginID, service string, level int, duration time.Duration, reason ...string) error

DisableServiceLevel disables a specific service for an account with a level. DisableServiceLevel 封禁账号的指定服务并设置封禁等级。

func (*Manager) ExchangeOAuth2CodeForToken

func (m *Manager) ExchangeOAuth2CodeForToken(ctx context.Context, code, clientID, clientSecret, redirectURI string) (*oauth2.AccessToken, error)

ExchangeOAuth2CodeForToken exchanges code for token. ExchangeOAuth2CodeForToken 使用授权码换取访问令牌。

func (*Manager) ForEachTerminal

func (m *Manager) ForEachTerminal(ctx context.Context, loginID string, visitor TerminalVisitor) error

ForEachTerminal iterates over all terminals for a login ID and calls the visitor function. ForEachTerminal 遍历指定登录 ID 的所有终端,对每个终端调用回调函数。

func (*Manager) ForEachTerminalByDevice

func (m *Manager) ForEachTerminalByDevice(ctx context.Context, loginID, device string, visitor TerminalVisitor) error

ForEachTerminalByDevice iterates over terminals filtered by device type. ForEachTerminalByDevice 遍历指定设备类型的终端。

func (*Manager) GenerateNonce

func (m *Manager) GenerateNonce(ctx context.Context) (string, error)

GenerateNonce generates nonce with default timeout. GenerateNonce 使用默认有效期生成 nonce。

func (*Manager) GenerateNonceWithTimeout

func (m *Manager) GenerateNonceWithTimeout(ctx context.Context, timeout time.Duration) (string, error)

GenerateNonceWithTimeout generates nonce with custom timeout. GenerateNonceWithTimeout 使用指定有效期生成 nonce。

func (*Manager) GenerateOAuth2AuthorizationCode

func (m *Manager) GenerateOAuth2AuthorizationCode(ctx context.Context, clientID, userID, redirectURI string, scopes []string) (*oauth2.AuthorizationCode, error)

GenerateOAuth2AuthorizationCode generates auth code. GenerateOAuth2AuthorizationCode 生成 OAuth2 授权码。

func (*Manager) GetAccessProvider

func (m *Manager) GetAccessProvider() AccessProvider

GetAccessProvider retrieves the access provider. GetAccessProvider 获取访问权限提供器。

func (*Manager) GetConfig

func (m *Manager) GetConfig() *config.Config

GetConfig retrieves the manager configuration. GetConfig 获取管理器配置。

func (*Manager) GetDevice

func (m *Manager) GetDevice(ctx context.Context, tokenValue string) (string, error)

GetDevice retrieves the device type for a token. GetDevice 获取 Token 的设备类型。

func (*Manager) GetDeviceId

func (m *Manager) GetDeviceId(ctx context.Context, tokenValue string) (string, error)

GetDeviceId retrieves the device ID for a token. GetDeviceId 获取 Token 的设备 ID。

func (*Manager) GetDisableDeviceAndDeviceIdInfo

func (m *Manager) GetDisableDeviceAndDeviceIdInfo(ctx context.Context, loginID, device, deviceId string) (*DeviceDisableInfo, error)

GetDisableDeviceAndDeviceIdInfo returns concrete device disable information. GetDisableDeviceAndDeviceIdInfo 获取具体设备封禁信息。

func (*Manager) GetDisableDeviceAndDeviceIdTTL

func (m *Manager) GetDisableDeviceAndDeviceIdTTL(ctx context.Context, loginID, device, deviceId string) (int64, error)

GetDisableDeviceAndDeviceIdTTL returns concrete device disable TTL in seconds. GetDisableDeviceAndDeviceIdTTL 获取具体设备封禁剩余秒数。

func (*Manager) GetDisableDeviceInfo

func (m *Manager) GetDisableDeviceInfo(ctx context.Context, loginID, device string) (*DeviceDisableInfo, error)

GetDisableDeviceInfo returns device type disable information. GetDisableDeviceInfo 获取设备类型封禁信息。

func (*Manager) GetDisableDeviceTTL

func (m *Manager) GetDisableDeviceTTL(ctx context.Context, loginID, device string) (int64, error)

GetDisableDeviceTTL returns device type disable TTL in seconds. GetDisableDeviceTTL 获取设备类型封禁剩余秒数。

func (*Manager) GetDisableInfo

func (m *Manager) GetDisableInfo(ctx context.Context, loginID string) (*DisableInfo, error)

GetDisableInfo retrieves disable information for an account. GetDisableInfo 获取账号的封禁信息。

func (*Manager) GetDisableServiceInfo

func (m *Manager) GetDisableServiceInfo(ctx context.Context, loginID, service string) (*ServiceDisableInfo, error)

GetDisableServiceInfo retrieves the disable info for a specific service. GetDisableServiceInfo 获取账号指定服务的封禁信息。

func (*Manager) GetDisableServiceTTL

func (m *Manager) GetDisableServiceTTL(ctx context.Context, loginID, service string) (int64, error)

GetDisableServiceTTL retrieves the remaining disable time for a specific service in seconds. GetDisableServiceTTL 获取账号指定服务的剩余封禁时间(秒)。

func (*Manager) GetDisableTTL

func (m *Manager) GetDisableTTL(ctx context.Context, loginID string) (int64, error)

GetDisableTTL retrieves the remaining disable time for an account in seconds. GetDisableTTL 获取账号剩余封禁时间(秒)。 Returns: -2: account is not disabled (未封禁) -1: account is permanently disabled (永久封禁) >0: remaining seconds until unban (剩余封禁秒数)

func (*Manager) GetEventManager

func (m *Manager) GetEventManager() *listener.Manager

GetEventManager retrieves the event manager. GetEventManager 获取事件监听管理器。

func (*Manager) GetGenerator

func (m *Manager) GetGenerator() adapter.Generator

GetGenerator retrieves the token generator. GetGenerator 获取 Token 生成器。

func (*Manager) GetLogger

func (m *Manager) GetLogger() adapter.Log

GetLogger retrieves the logger adapter. GetLogger 获取日志适配器。

func (*Manager) GetLoginID

func (m *Manager) GetLoginID(ctx context.Context, tokenValue string) (string, error)

GetLoginID retrieves the login ID from a token. GetLoginID 根据 Token 获取登录 ID。

func (*Manager) GetNonceManager

func (m *Manager) GetNonceManager() *nonce.NonceManager

GetNonceManager retrieves the nonce manager. GetNonceManager 获取 nonce 管理器。

func (*Manager) GetNonceTTL

func (m *Manager) GetNonceTTL(ctx context.Context, nonce string) (int64, error)

GetNonceTTL gets nonce ttl in seconds. GetNonceTTL 获取 nonce 剩余有效秒数。

func (*Manager) GetOAuth2Client

func (m *Manager) GetOAuth2Client(clientID string) (*oauth2.Client, error)

GetOAuth2Client gets oauth2 client. GetOAuth2Client 根据 ID 获取 OAuth2 客户端。

func (*Manager) GetOAuth2Manager

func (m *Manager) GetOAuth2Manager() *oauth2.OAuth2Server

GetOAuth2Manager retrieves the OAuth2 manager. GetOAuth2Manager 获取 OAuth2 管理器。

func (*Manager) GetOnlineTerminalCount

func (m *Manager) GetOnlineTerminalCount(ctx context.Context, loginID string) (int, error)

GetOnlineTerminalCount retrieves the count of online terminals for a user. GetOnlineTerminalCount 获取用户的在线终端数量。

func (*Manager) GetOnlineTerminalCountByDevice

func (m *Manager) GetOnlineTerminalCountByDevice(ctx context.Context, loginID, device string) (int, error)

GetOnlineTerminalCountByDevice retrieves the count of online terminals for a specific device type. GetOnlineTerminalCountByDevice 获取用户在指定设备类型的在线终端数量。

func (*Manager) GetOnlineTerminalCountByDeviceAndDeviceId

func (m *Manager) GetOnlineTerminalCountByDeviceAndDeviceId(ctx context.Context, loginID, device, deviceId string) (int, error)

GetOnlineTerminalCountByDeviceAndDeviceId retrieves the count of online terminals for a specific device type and device ID. GetOnlineTerminalCountByDeviceAndDeviceId 获取用户在指定设备类型和设备ID的在线终端数量。

func (*Manager) GetPermissions

func (m *Manager) GetPermissions(ctx context.Context, loginID string) ([]string, error)

GetPermissions retrieves the permission list for a user. GetPermissions 获取用户的权限列表。

func (*Manager) GetPermissionsByToken

func (m *Manager) GetPermissionsByToken(ctx context.Context, tokenValue string) ([]string, error)

GetPermissionsByToken retrieves the permission list by token. GetPermissionsByToken 根据 Token 获取权限列表。

func (*Manager) GetPool

func (m *Manager) GetPool() adapter.Pool

GetPool retrieves the goroutine pool. GetPool 获取协程池。

func (*Manager) GetRoles

func (m *Manager) GetRoles(ctx context.Context, loginID string) ([]string, error)

GetRoles retrieves the role list for a user. GetRoles 获取用户的角色列表。

func (*Manager) GetRolesByToken

func (m *Manager) GetRolesByToken(ctx context.Context, tokenValue string) ([]string, error)

GetRolesByToken retrieves the role list by token. GetRolesByToken 根据 Token 获取角色列表。

func (*Manager) GetSerializer

func (m *Manager) GetSerializer() adapter.Codec

GetSerializer retrieves the serializer adapter. GetSerializer 获取序列化适配器。

func (*Manager) GetSession

func (m *Manager) GetSession(ctx context.Context, loginID string) (*Session, error)

GetSession retrieves session information for a login ID. GetSession 获取指定登录 ID 的会话信息。

func (*Manager) GetSessionByToken

func (m *Manager) GetSessionByToken(ctx context.Context, tokenValue string) (*Session, error)

GetSessionByToken retrieves session information by token. GetSessionByToken 通过 Token 值获取会话信息。

func (*Manager) GetStorage

func (m *Manager) GetStorage() adapter.Storage

GetStorage retrieves the storage adapter. GetStorage 获取存储适配器。

func (*Manager) GetTerminalInfoByToken

func (m *Manager) GetTerminalInfoByToken(ctx context.Context, tokenValue string) (*TerminalInfo, error)

GetTerminalInfoByToken retrieves terminal info for a specific token. GetTerminalInfoByToken 根据 Token 获取终端详情。

func (*Manager) GetTerminalListByLoginID

func (m *Manager) GetTerminalListByLoginID(ctx context.Context, loginID string, device ...string) ([]TerminalInfo, error)

GetTerminalListByLoginID retrieves all terminal info for a login ID, optionally filtered by device. GetTerminalListByLoginID 获取指定登录 ID 的所有终端信息列表,可选按设备类型过滤。

func (*Manager) GetTokenCreateTime

func (m *Manager) GetTokenCreateTime(ctx context.Context, tokenValue string) (int64, error)

GetTokenCreateTime retrieves the creation time for a token. GetTokenCreateTime 获取 Token 的创建时间戳。

func (*Manager) GetTokenInfo

func (m *Manager) GetTokenInfo(ctx context.Context, tokenValue string) (*TokenInfo, error)

GetTokenInfo retrieves token information. GetTokenInfo 根据 Token 获取 TokenInfo 信息。

func (*Manager) GetTokenTTL

func (m *Manager) GetTokenTTL(ctx context.Context, tokenValue string) (int64, error)

GetTokenTTL retrieves the remaining time-to-live for a token in seconds. GetTokenTTL 获取 Token 的剩余有效时间(秒)。

func (*Manager) GetTokenValueByLoginID

func (m *Manager) GetTokenValueByLoginID(ctx context.Context, loginID string, device ...string) (string, error)

GetTokenValueByLoginID retrieves the latest token for a login ID, optionally filtered by device. GetTokenValueByLoginID 获取指定登录 ID 的最新 Token,可选按设备类型过滤。

func (*Manager) GetTokenValueListByDevice

func (m *Manager) GetTokenValueListByDevice(ctx context.Context, loginID, device string, checkAlive ...bool) ([]string, error)

GetTokenValueListByDevice retrieves all tokens for a specific device type. GetTokenValueListByDevice 获取指定设备类型的所有 Token。

func (*Manager) GetTokenValueListByDeviceAndDeviceId

func (m *Manager) GetTokenValueListByDeviceAndDeviceId(ctx context.Context, loginID, device, deviceId string, checkAlive ...bool) ([]string, error)

GetTokenValueListByDeviceAndDeviceId retrieves all tokens for a specific device type and device ID. GetTokenValueListByDeviceAndDeviceId 获取指定设备类型和设备 ID 的所有 Token。

func (*Manager) GetTokenValueListByLoginID

func (m *Manager) GetTokenValueListByLoginID(ctx context.Context, loginID string, checkAlive ...bool) ([]string, error)

GetTokenValueListByLoginID retrieves all tokens for a login ID. GetTokenValueListByLoginID 获取指定登录 ID 的所有 Token。

func (*Manager) HasPermission

func (m *Manager) HasPermission(ctx context.Context, loginID string, permission string) bool

HasPermission checks if a user has a specific permission. HasPermission 检查用户是否拥有指定权限。

func (*Manager) HasPermissionByToken

func (m *Manager) HasPermissionByToken(ctx context.Context, tokenValue string, permission string) bool

HasPermissionByToken checks if a user has a specific permission by token. HasPermissionByToken 根据 Token 检查用户是否拥有指定权限。

func (*Manager) HasPermissionsAnd

func (m *Manager) HasPermissionsAnd(ctx context.Context, loginID string, permissions []string) bool

HasPermissionsAnd checks if a user has all specified permissions. HasPermissionsAnd 检查用户是否拥有全部指定权限。

func (*Manager) HasPermissionsAndByToken

func (m *Manager) HasPermissionsAndByToken(ctx context.Context, tokenValue string, permissions []string) bool

HasPermissionsAndByToken checks if a token user has all specified permissions. HasPermissionsAndByToken 根据 Token 检查全部权限。

func (*Manager) HasPermissionsOr

func (m *Manager) HasPermissionsOr(ctx context.Context, loginID string, permissions []string) bool

HasPermissionsOr checks if a user has any specified permission. HasPermissionsOr 检查用户是否拥有任一指定权限。

func (*Manager) HasPermissionsOrByToken

func (m *Manager) HasPermissionsOrByToken(ctx context.Context, tokenValue string, permissions []string) bool

HasPermissionsOrByToken checks if a token user has any specified permission. HasPermissionsOrByToken 根据 Token 检查任一权限。

func (*Manager) HasRole

func (m *Manager) HasRole(ctx context.Context, loginID string, role string) bool

HasRole checks if a user has a specific role. HasRole 检查用户是否拥有指定角色。

func (*Manager) HasRoleByToken

func (m *Manager) HasRoleByToken(ctx context.Context, tokenValue string, role string) bool

HasRoleByToken checks if a user has a specific role by token. HasRoleByToken 根据 Token 检查用户是否拥有指定角色。

func (*Manager) HasRolesAnd

func (m *Manager) HasRolesAnd(ctx context.Context, loginID string, roles []string) bool

HasRolesAnd checks if a user has all specified roles. HasRolesAnd 检查用户是否拥有全部指定角色。

func (*Manager) HasRolesAndByToken

func (m *Manager) HasRolesAndByToken(ctx context.Context, tokenValue string, roles []string) bool

HasRolesAndByToken checks if a token user has all specified roles. HasRolesAndByToken 根据 Token 检查全部角色。

func (*Manager) HasRolesOr

func (m *Manager) HasRolesOr(ctx context.Context, loginID string, roles []string) bool

HasRolesOr checks if a user has any specified role. HasRolesOr 检查用户是否拥有任一指定角色。

func (*Manager) HasRolesOrByToken

func (m *Manager) HasRolesOrByToken(ctx context.Context, tokenValue string, roles []string) bool

HasRolesOrByToken checks if a token user has any specified role. HasRolesOrByToken 根据 Token 检查任一角色。

func (*Manager) IsDisable

func (m *Manager) IsDisable(ctx context.Context, loginID string) bool

IsDisable checks if an account is disabled. IsDisable 检查账号是否被封禁。

func (*Manager) IsDisableDevice

func (m *Manager) IsDisableDevice(ctx context.Context, loginID, device string) bool

IsDisableDevice checks device type disable state. IsDisableDevice 检查设备类型封禁状态。

func (*Manager) IsDisableDeviceAndDeviceId

func (m *Manager) IsDisableDeviceAndDeviceId(ctx context.Context, loginID, device, deviceId string) bool

IsDisableDeviceAndDeviceId checks concrete device disable state. IsDisableDeviceAndDeviceId 检查具体设备封禁状态。

func (*Manager) IsDisableService

func (m *Manager) IsDisableService(ctx context.Context, loginID, service string) bool

IsDisableService checks if a specific service is disabled for an account. IsDisableService 检查账号的指定服务是否被封禁。

func (*Manager) IsDisableServiceLevel

func (m *Manager) IsDisableServiceLevel(ctx context.Context, loginID, service string, level int) bool

IsDisableServiceLevel checks if a specific service is disabled at or above the given level. IsDisableServiceLevel 检查账号的指定服务是否达到指定封禁等级。

func (*Manager) IsLogin

func (m *Manager) IsLogin(ctx context.Context, tokenValue string) bool

IsLogin checks if a user is logged in. IsLogin 检查用户是否登录。

func (*Manager) IsNonceValid

func (m *Manager) IsNonceValid(ctx context.Context, nonce string) bool

IsNonceValid checks nonce validity without consuming it. IsNonceValid 检查 nonce 是否有效且不消费。

func (*Manager) Kickout

func (m *Manager) Kickout(ctx context.Context, tokenValue string) error

Kickout kicks out a user by token. Kickout 根据 Token 踢人下线。

func (*Manager) KickoutByDevice

func (m *Manager) KickoutByDevice(ctx context.Context, loginID string, device string) error

KickoutByDevice kicks out all terminals of a specific device type. KickoutByDevice 根据设备类型踢人下线(踢掉该设备类型的所有终端)。

func (*Manager) KickoutByDeviceAndDeviceId

func (m *Manager) KickoutByDeviceAndDeviceId(ctx context.Context, loginID string, deviceAndDeviceId ...string) error

KickoutByDeviceAndDeviceId kicks out a user by device type and device ID. KickoutByDeviceAndDeviceId 根据设备类型和设备ID踢人下线。

func (*Manager) KickoutByLoginID

func (m *Manager) KickoutByLoginID(ctx context.Context, loginID string) error

KickoutByLoginID kicks out all terminals for the specified loginID. KickoutByLoginID 踢出指定 loginID 的所有终端。

func (*Manager) Login

func (m *Manager) Login(ctx context.Context, loginID string, deviceAndDeviceId ...string) (string, error)

Login performs user login and returns a token. Login 执行用户登录并返回 token。

func (*Manager) LoginByToken

func (m *Manager) LoginByToken(ctx context.Context, tokenValue string) error

LoginByToken performs login renewal based on an existing token. LoginByToken 根据 Token 续期登录。

func (*Manager) LoginWithTimeout

func (m *Manager) LoginWithTimeout(ctx context.Context, loginID string, timeout time.Duration, deviceAndDeviceId ...string) (string, error)

LoginWithTimeout performs user login with a custom token timeout and returns a token. LoginWithTimeout 执行用户登录并返回 token,使用指定的过期时间(0 或负数则使用全局配置)。

func (*Manager) Logout

func (m *Manager) Logout(ctx context.Context, tokenValue string) error

Logout logs out a user by token. Logout 根据 Token 登出用户。

func (*Manager) LogoutByDevice

func (m *Manager) LogoutByDevice(ctx context.Context, loginID string, device string) error

LogoutByDevice logs out all terminals of a specific device type. LogoutByDevice 根据设备类型登出所有该设备的终端。

func (*Manager) LogoutByDeviceAndDeviceId

func (m *Manager) LogoutByDeviceAndDeviceId(ctx context.Context, loginID string, deviceAndDeviceId ...string) error

LogoutByDeviceAndDeviceId logs out a user by device type and device ID. LogoutByDeviceAndDeviceId 根据设备类型和设备ID登出用户。

func (*Manager) LogoutByLoginID

func (m *Manager) LogoutByLoginID(ctx context.Context, loginID string) error

LogoutByLoginID logs out all terminals for the specified loginID. LogoutByLoginID 登出指定 loginID 的所有终端。

func (*Manager) OAuth2ClientCredentialsToken

func (m *Manager) OAuth2ClientCredentialsToken(ctx context.Context, clientID, clientSecret string, scopes []string) (*oauth2.AccessToken, error)

OAuth2ClientCredentialsToken gets token by client credentials. OAuth2ClientCredentialsToken 使用客户端凭证模式获取访问令牌。

func (*Manager) OAuth2PasswordGrantToken

func (m *Manager) OAuth2PasswordGrantToken(ctx context.Context, clientID, clientSecret, username, password string, scopes []string, validateUser oauth2.UserValidator) (*oauth2.AccessToken, error)

OAuth2PasswordGrantToken gets token by password grant. OAuth2PasswordGrantToken 使用密码模式获取访问令牌。

func (*Manager) OAuth2Token

func (m *Manager) OAuth2Token(ctx context.Context, req *oauth2.TokenRequest, validateUser oauth2.UserValidator) (*oauth2.AccessToken, error)

OAuth2Token dispatches token request. OAuth2Token 统一处理不同授权类型的 OAuth2 令牌请求。

func (*Manager) RefreshOAuth2AccessToken

func (m *Manager) RefreshOAuth2AccessToken(ctx context.Context, clientID, refreshToken, clientSecret string) (*oauth2.AccessToken, error)

RefreshOAuth2AccessToken refreshes access token. RefreshOAuth2AccessToken 使用刷新令牌刷新访问令牌。

func (*Manager) RegisterOAuth2Client

func (m *Manager) RegisterOAuth2Client(client *oauth2.Client) error

RegisterOAuth2Client registers oauth2 client. RegisterOAuth2Client 注册 OAuth2 客户端。

func (*Manager) RemovePermissions

func (m *Manager) RemovePermissions(ctx context.Context, loginID string, permissions []string) error

RemovePermissions removes permissions from a user. RemovePermissions 删除用户的指定权限。

func (*Manager) RemovePermissionsByToken

func (m *Manager) RemovePermissionsByToken(ctx context.Context, tokenValue string, permissions []string) error

RemovePermissionsByToken removes permissions from a user by token. RemovePermissionsByToken 根据 Token 删除用户的指定权限。

func (*Manager) RemoveRoles

func (m *Manager) RemoveRoles(ctx context.Context, loginID string, roles []string) error

RemoveRoles removes roles from a user. RemoveRoles 删除用户的指定角色。

func (*Manager) RemoveRolesByToken

func (m *Manager) RemoveRolesByToken(ctx context.Context, tokenValue string, roles []string) error

RemoveRolesByToken removes roles from a user by token. RemoveRolesByToken 根据 Token 删除用户的指定角色。

func (*Manager) RenewTimeout

func (m *Manager) RenewTimeout(ctx context.Context, tokenValue string, timeout time.Duration) error

RenewTimeout manually renews the timeout of a token. RenewTimeout 手动续期指定 Token 的过期时间。

func (*Manager) Replace

func (m *Manager) Replace(ctx context.Context, tokenValue string) error

Replace replaces a user session by token. Replace 根据 Token 顶人下线。

func (*Manager) ReplaceByDevice

func (m *Manager) ReplaceByDevice(ctx context.Context, loginID string, device string) error

ReplaceByDevice replaces all terminals of a specific device type. ReplaceByDevice 根据设备类型顶人下线(顶掉该设备类型的所有终端)。

func (*Manager) ReplaceByDeviceAndDeviceId

func (m *Manager) ReplaceByDeviceAndDeviceId(ctx context.Context, loginID string, deviceAndDeviceId ...string) error

ReplaceByDeviceAndDeviceId replaces a user session by device type and device ID. ReplaceByDeviceAndDeviceId 根据设备类型和设备ID顶人下线。

func (*Manager) ReplaceByLoginID

func (m *Manager) ReplaceByLoginID(ctx context.Context, loginID string) error

ReplaceByLoginID replaces all terminals for the specified loginID. ReplaceByLoginID 顶替指定 loginID 的所有终端。

func (*Manager) RevokeOAuth2Token

func (m *Manager) RevokeOAuth2Token(ctx context.Context, accessToken string) error

RevokeOAuth2Token revokes oauth2 token. RevokeOAuth2Token 撤销访问令牌及其刷新令牌。

func (*Manager) SearchSessionId

func (m *Manager) SearchSessionId(ctx context.Context, keyword string, start, size int) ([]string, error)

SearchSessionId searches session IDs by keyword with pagination. SearchSessionId 根据关键词搜索 Session ID,支持分页。 keyword: 搜索关键词(模糊匹配),start: 起始索引,size: 返回数量(-1 返回全部)

func (*Manager) SearchTokenValue

func (m *Manager) SearchTokenValue(ctx context.Context, keyword string, start, size int) ([]string, error)

SearchTokenValue searches token values by keyword with pagination. SearchTokenValue 根据关键词搜索 Token 值,支持分页。 keyword: 搜索关键词(模糊匹配),start: 起始索引,size: 返回数量(-1 返回全部)

func (*Manager) StartRenewPoolStatusLogger

func (m *Manager) StartRenewPoolStatusLogger(interval time.Duration)

StartRenewPoolStatusLogger starts renew pool status logging. StartRenewPoolStatusLogger 启动续期池状态日志。

func (*Manager) UnregisterOAuth2Client

func (m *Manager) UnregisterOAuth2Client(clientID string) error

UnregisterOAuth2Client unregisters oauth2 client. UnregisterOAuth2Client 注销 OAuth2 客户端。

func (*Manager) Untie

func (m *Manager) Untie(ctx context.Context, loginID string) error

Untie removes the disable status from an account. Untie 解封账号。

func (*Manager) UntieDevice

func (m *Manager) UntieDevice(ctx context.Context, loginID, device string) error

UntieDevice removes device type disable state. UntieDevice 解除设备类型封禁状态。

func (*Manager) UntieDeviceAndDeviceId

func (m *Manager) UntieDeviceAndDeviceId(ctx context.Context, loginID, device, deviceId string) error

UntieDeviceAndDeviceId removes concrete device disable state. UntieDeviceAndDeviceId 解除具体设备封禁状态。

func (*Manager) UntieService

func (m *Manager) UntieService(ctx context.Context, loginID, service string) error

UntieService removes the disable status of a specific service for an account. UntieService 解封账号的指定服务。

func (*Manager) ValidateOAuth2AccessToken

func (m *Manager) ValidateOAuth2AccessToken(ctx context.Context, accessToken string) bool

ValidateOAuth2AccessToken validates access token. ValidateOAuth2AccessToken 验证访问令牌。

func (*Manager) ValidateOAuth2AccessTokenAndGetInfo

func (m *Manager) ValidateOAuth2AccessTokenAndGetInfo(ctx context.Context, accessToken string) (*oauth2.AccessToken, error)

ValidateOAuth2AccessTokenAndGetInfo validates token and gets info. ValidateOAuth2AccessTokenAndGetInfo 验证访问令牌并获取信息。

func (*Manager) VerifyAndConsumeNonce

func (m *Manager) VerifyAndConsumeNonce(ctx context.Context, nonce string) error

VerifyAndConsumeNonce verifies and consumes nonce with error detail. VerifyAndConsumeNonce 验证并消费 nonce,失败时返回错误。

func (*Manager) VerifyNonce

func (m *Manager) VerifyNonce(ctx context.Context, nonce string) bool

VerifyNonce verifies and consumes nonce. VerifyNonce 验证并消费一次性 nonce。

type Option

type Option func(m *Manager)

Option configures optional manager modules Option 配置 Manager 的可选模块

func WithNonceManager

func WithNonceManager(nonceManager *nonce.NonceManager) Option

WithNonceManager replaces the default nonce manager WithNonceManager 替换默认 Nonce 管理器

func WithOAuth2Manager

func WithOAuth2Manager(oauth2Manager *oauth2.OAuth2Server) Option

WithOAuth2Manager replaces the default OAuth2 server WithOAuth2Manager 替换默认 OAuth2 服务端

type ServiceDisableInfo

type ServiceDisableInfo struct {
	Service       string `json:"service"`       // Service stores disabled service name. Service 存储被封禁服务名。
	Level         int    `json:"level"`         // Level stores disable level. Level 存储封禁等级。
	DisableTime   int64  `json:"disableTime"`   // DisableTime stores disable timestamp. DisableTime 存储封禁时间戳。
	DisableReason string `json:"disableReason"` // DisableReason stores disable reason. DisableReason 存储封禁原因。
}

ServiceDisableInfo defines service disable info. ServiceDisableInfo 定义服务封禁信息。

type Session

type Session struct {
	AuthType             string         `json:"authType"`                       // AuthType stores auth namespace. AuthType 存储认证命名空间。
	LoginID              string         `json:"loginId"`                        // LoginID stores subject identifier. LoginID 存储主体标识。
	CreateTime           int64          `json:"createTime"`                     // CreateTime stores session creation time. CreateTime 存储会话创建时间。
	TerminalInfos        []TerminalInfo `json:"terminalInfos,omitempty"`        // TerminalInfos stores online terminals. TerminalInfos 存储在线终端。
	Permissions          []string       `json:"permissions,omitempty"`          // Permissions stores cached permissions. Permissions 存储缓存权限。
	Roles                []string       `json:"roles,omitempty"`                // Roles stores cached roles. Roles 存储缓存角色。
	HistoryTerminalCount int64          `json:"historyTerminalCount,omitempty"` // HistoryTerminalCount stores terminal sequence count. HistoryTerminalCount 存储终端历史序号。
}

Session defines session object. Session 定义会话对象。

type TerminalInfo

type TerminalInfo struct {
	Token      string `json:"token"`      // Token stores terminal token. Token 存储终端 Token。
	LoginID    string `json:"loginId"`    // LoginID stores subject identifier. LoginID 存储主体标识。
	Device     string `json:"device"`     // Device stores device type. Device 存储设备类型。
	DeviceId   string `json:"deviceId"`   // DeviceId stores concrete device ID. DeviceId 存储具体设备 ID。
	CreateTime int64  `json:"createTime"` // CreateTime stores terminal creation time. CreateTime 存储终端创建时间。
	Index      int64  `json:"index"`      // Index stores terminal sequence. Index 存储终端序号。
}

TerminalInfo defines terminal info. TerminalInfo 定义终端信息。

type TerminalRemovalFunc

type TerminalRemovalFunc func(sess *Session) []TerminalInfo

TerminalRemovalFunc defines how to remove terminals from a session. TerminalRemovalFunc 定义如何从 Session 中移除终端。

type TerminalVisitor

type TerminalVisitor func(terminal TerminalInfo) bool

TerminalVisitor is a callback function for terminal traversal. TerminalVisitor 终端遍历回调函数。 Return false to stop traversal. 返回 false 停止遍历。

type TokenInfo

type TokenInfo struct {
	AuthType   string `json:"authType"`   // AuthType stores auth namespace. AuthType 存储认证命名空间。
	LoginID    string `json:"loginId"`    // LoginID stores subject identifier. LoginID 存储主体标识。
	Device     string `json:"device"`     // Device stores device type. Device 存储设备类型。
	DeviceId   string `json:"deviceId"`   // DeviceId stores concrete device ID. DeviceId 存储具体设备 ID。
	CreateTime int64  `json:"createTime"` // CreateTime stores token creation time. CreateTime 存储 Token 创建时间。
	Timeout    int64  `json:"timeout"`    // Timeout stores token timeout seconds. Timeout 存储 Token 过期秒数。
}

TokenInfo defines token info. TokenInfo 定义 Token 信息。

type TokenState

type TokenState string

TokenState defines token logical state. TokenState 定义 Token 逻辑状态。

const (
	// TokenStateLogout indicates logout state. TokenStateLogout 表示主动登出状态。
	TokenStateLogout TokenState = "LOGOUT"
	// TokenStateKickOut indicates kickout state. TokenStateKickOut 表示被踢下线状态。
	TokenStateKickOut TokenState = "KICK_OUT"
	// TokenStateReplaced indicates replaced state. TokenStateReplaced 表示被顶下线状态。
	TokenStateReplaced TokenState = "REPLACED"
	// TokenStateActiveTimeout indicates inactive timeout state. TokenStateActiveTimeout 表示不活跃超时状态。
	TokenStateActiveTimeout TokenState = "ACTIVE_TIMEOUT"
)

Jump to

Keyboard shortcuts

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