Documentation
¶
Index ¶
- Variables
- func TokenFromContext(ctx context.Context) (string, bool)
- func WithProvider(ctx context.Context, provider AuthProvider) context.Context
- func WithToken(ctx context.Context, token string) context.Context
- func WithUser(ctx context.Context, user Authenticatable) context.Context
- type AuthContext
- type AuthProvider
- type Authenticatable
- type AuthenticatableUser
- func (u *AuthenticatableUser) GetAuthID() string
- func (u *AuthenticatableUser) GetAuthIdentifier() string
- func (u *AuthenticatableUser) GetAuthPassword() string
- func (u *AuthenticatableUser) GetAuthPermissions() []string
- func (u *AuthenticatableUser) GetAuthRoles() []string
- func (u *AuthenticatableUser) IsActive() bool
- type BasePolicy
- func (p *BasePolicy) Check(ctx context.Context, user Authenticatable, resource interface{}, action string) bool
- func (p *BasePolicy) Name() string
- func (p *BasePolicy) RegisterAction(action string, handler PolicyHandler)
- func (p *BasePolicy) RegisterActionAlias(action, alias string)
- func (p *BasePolicy) Resource() string
- type Config
- type Manager
- func (m *Manager) AuthMiddleware() flow.HandlerFunc
- func (m *Manager) Authenticate(ctx context.Context, credentials map[string]string) (Authenticatable, error)
- func (m *Manager) Check(ctx context.Context, user Authenticatable, permission string) bool
- func (m *Manager) DefaultProvider() (AuthProvider, error)
- func (m *Manager) GenerateToken(ctx context.Context, user Authenticatable) (string, error)
- func (m *Manager) GetUser(c *flow.Context) (Authenticatable, bool)
- func (m *Manager) GetUserByID(ctx context.Context, id string) (Authenticatable, error)
- func (m *Manager) HasRole(ctx context.Context, user Authenticatable, role string) bool
- func (m *Manager) InvalidateToken(ctx context.Context, token string) error
- func (m *Manager) Provider(name string) (AuthProvider, error)
- func (m *Manager) RefreshToken(ctx context.Context, token string) (string, error)
- func (m *Manager) RegisterProvider(name string, provider AuthProvider)
- func (m *Manager) RequireAuth() flow.HandlerFunc
- func (m *Manager) RequirePermission(permission string) flow.HandlerFunc
- func (m *Manager) RequireRole(role string) flow.HandlerFunc
- func (m *Manager) SetDefaultProvider(name string)
- func (m *Manager) ValidateToken(ctx context.Context, token string) (Authenticatable, error)
- type PolicyHandler
- type PolicyManager
- func (m *PolicyManager) Authorize(ctx context.Context, user Authenticatable, action string, resource interface{}) error
- func (m *PolicyManager) Can(ctx context.Context, user Authenticatable, action string, resource interface{}) bool
- func (m *PolicyManager) Cannot(ctx context.Context, user Authenticatable, action string, resource interface{}) bool
- func (m *PolicyManager) Check(ctx context.Context, user Authenticatable, resource interface{}, action string) bool
- func (m *PolicyManager) GetPoliciesForResource(resourceType string) []ResourcePolicy
- func (m *PolicyManager) GetPolicy(name string) (ResourcePolicy, error)
- func (m *PolicyManager) PolicyMiddleware(action string, resourceProvider func(*flow.Context) interface{}) flow.HandlerFunc
- func (m *PolicyManager) RegisterPolicy(policy ResourcePolicy)
- type Provider
- type ResourcePolicy
- func CreateOwnerPolicy(policyName, resourceType string, getOwnerID func(resource interface{}) string) ResourcePolicy
- func CreatePermissionBasedPolicy(policyName, resourceType string, permissionActionMap map[string][]string) ResourcePolicy
- func CreateRoleBasedPolicy(policyName, resourceType string, roleActionMap map[string][]string) ResourcePolicy
- type TokenClaims
- type UserProvider
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidCredentials 表示提供的凭证无效 ErrInvalidCredentials = errors.New("提供的凭证无效") // ErrInvalidToken 表示提供的令牌无效 ErrInvalidToken = errors.New("提供的令牌无效或已过期") // ErrUserNotFound 表示未找到请求的用户 ErrUserNotFound = errors.New("未找到用户") ErrUnauthorized = errors.New("未经授权访问") )
定义错误常量
var ( // ErrPolicyNotFound 表示找不到请求的策略 ErrPolicyNotFound = errors.New("找不到策略") // ErrInvalidPolicyHandler 表示策略处理器无效 ErrInvalidPolicyHandler = errors.New("无效的策略处理器") )
策略相关错误
var ( // ErrPermissionDenied 表示用户没有足够的权限访问资源 ErrPermissionDenied = errors.New("权限不足") )
添加缺失的错误常量
Functions ¶
func TokenFromContext ¶
TokenFromContext 从上下文中获取认证令牌
func WithProvider ¶
func WithProvider(ctx context.Context, provider AuthProvider) context.Context
WithProvider 将认证提供者添加到上下文中
Types ¶
type AuthContext ¶
type AuthContext string
AuthContext 是认证上下文的键类型
const ( // AuthUserKey 是存储在上下文中的已认证用户的键 AuthUserKey AuthContext = "auth_user" // AuthTokenKey 是存储在上下文中的认证令牌的键 AuthTokenKey AuthContext = "auth_token" // AuthProviderKey 是存储在上下文中的认证提供者的键 AuthProviderKey AuthContext = "auth_provider" )
认证上下文中的键名常量
type AuthProvider ¶
type AuthProvider interface {
// Authenticate 验证用户凭证并返回认证实体
Authenticate(ctx context.Context, credentials map[string]string) (Authenticatable, error)
// GetUserByID 根据ID获取认证实体
GetUserByID(ctx context.Context, id string) (Authenticatable, error)
// GetUserByIdentifier 根据标识符获取认证实体
GetUserByIdentifier(ctx context.Context, identifier string) (Authenticatable, error)
// GenerateToken 为认证实体生成令牌
GenerateToken(ctx context.Context, user Authenticatable, expiry time.Duration) (string, error)
// ValidateToken 验证令牌并返回认证实体
ValidateToken(ctx context.Context, token string) (Authenticatable, error)
// RefreshToken 刷新令牌
RefreshToken(ctx context.Context, token string) (string, error)
// InvalidateToken 使令牌失效
InvalidateToken(ctx context.Context, token string) error
// CheckPermission 检查认证实体是否拥有指定权限
CheckPermission(ctx context.Context, user Authenticatable, permission string) bool
// CheckRole 检查认证实体是否拥有指定角色
CheckRole(ctx context.Context, user Authenticatable, role string) bool
}
AuthProvider 定义了认证提供者的接口
func ProviderFromContext ¶
func ProviderFromContext(ctx context.Context) (AuthProvider, bool)
ProviderFromContext 从上下文中获取认证提供者
type Authenticatable ¶
type Authenticatable interface {
// GetAuthIdentifier 返回实体的唯一标识符
GetAuthIdentifier() string
// GetAuthUsername 返回实体的用户名
GetAuthUsername() string
// GetPermissions 返回实体拥有的权限列表
GetPermissions() []string
// GetRoles 返回实体拥有的角色列表
GetRoles() []string
}
Authenticatable 表示可以被认证的实体
func UserFromContext ¶
func UserFromContext(ctx context.Context) (Authenticatable, bool)
UserFromContext 从上下文中获取认证用户
type AuthenticatableUser ¶
type AuthenticatableUser struct {
ID string `json:"id"`
Identifier string `json:"identifier"`
Password string `json:"password"`
Roles []string `json:"roles"`
Permissions []string `json:"permissions"`
Status bool `json:"status"`
}
AuthenticatableUser 是Authenticatable接口的基本实现,可以嵌入到用户模型中
func (*AuthenticatableUser) GetAuthID ¶
func (u *AuthenticatableUser) GetAuthID() string
GetAuthID 实现Authenticatable接口
func (*AuthenticatableUser) GetAuthIdentifier ¶
func (u *AuthenticatableUser) GetAuthIdentifier() string
GetAuthIdentifier 实现Authenticatable接口
func (*AuthenticatableUser) GetAuthPassword ¶
func (u *AuthenticatableUser) GetAuthPassword() string
GetAuthPassword 实现Authenticatable接口
func (*AuthenticatableUser) GetAuthPermissions ¶
func (u *AuthenticatableUser) GetAuthPermissions() []string
GetAuthPermissions 实现Authenticatable接口
func (*AuthenticatableUser) GetAuthRoles ¶
func (u *AuthenticatableUser) GetAuthRoles() []string
GetAuthRoles 实现Authenticatable接口
func (*AuthenticatableUser) IsActive ¶
func (u *AuthenticatableUser) IsActive() bool
IsActive 实现Authenticatable接口
type BasePolicy ¶
type BasePolicy struct {
// contains filtered or unexported fields
}
BasePolicy 提供策略的基本实现
func (*BasePolicy) Check ¶
func (p *BasePolicy) Check(ctx context.Context, user Authenticatable, resource interface{}, action string) bool
Check 检查用户是否对资源有执行操作的权限
func (*BasePolicy) RegisterAction ¶
func (p *BasePolicy) RegisterAction(action string, handler PolicyHandler)
RegisterAction 注册可对资源执行的操作及其处理函数
func (*BasePolicy) RegisterActionAlias ¶
func (p *BasePolicy) RegisterActionAlias(action, alias string)
RegisterActionAlias 注册操作的别名
type Config ¶
type Config struct {
// DefaultProvider 默认认证提供者名称
DefaultProvider string
// TokenExpiry 令牌默认过期时间
TokenExpiry time.Duration
// ContextKey 存储在Flow上下文中的已认证用户键名
ContextKey string
// LoginURL 登录页面URL
LoginURL string
// LogoutURL 登出页面URL
LogoutURL string
// RedirectKey 重定向URL参数键名
RedirectKey string
// CookieName 认证Cookie名称
CookieName string
// CookiePath Cookie路径
CookiePath string
// CookieDomain Cookie域
CookieDomain string
// CookieSecure Cookie是否仅通过HTTPS发送
CookieSecure bool
// CookieHTTPOnly Cookie是否仅可通过HTTP访问
CookieHTTPOnly bool
// SessionDriver 会话驱动类型
SessionDriver string
// UserProvider 用户提供者实现
UserProvider UserProvider
}
Config 是认证系统的配置
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager 是认证管理器,负责协调多个认证提供者
func (*Manager) AuthMiddleware ¶
func (m *Manager) AuthMiddleware() flow.HandlerFunc
AuthMiddleware 创建认证中间件
func (*Manager) Authenticate ¶
func (m *Manager) Authenticate(ctx context.Context, credentials map[string]string) (Authenticatable, error)
Authenticate 使用默认提供者进行认证
func (*Manager) DefaultProvider ¶
func (m *Manager) DefaultProvider() (AuthProvider, error)
DefaultProvider 获取默认认证提供者
func (*Manager) GenerateToken ¶
GenerateToken 使用默认提供者为用户生成令牌
func (*Manager) GetUser ¶
func (m *Manager) GetUser(c *flow.Context) (Authenticatable, bool)
GetUser 从Flow上下文中获取已认证用户
func (*Manager) GetUserByID ¶
GetUserByID 使用默认提供者根据ID获取用户
func (*Manager) InvalidateToken ¶
InvalidateToken 使用默认提供者使令牌失效
func (*Manager) Provider ¶
func (m *Manager) Provider(name string) (AuthProvider, error)
Provider 获取指定名称的认证提供者
func (*Manager) RefreshToken ¶
RefreshToken 使用默认提供者刷新令牌
func (*Manager) RegisterProvider ¶
func (m *Manager) RegisterProvider(name string, provider AuthProvider)
RegisterProvider 注册认证提供者
func (*Manager) RequireAuth ¶
func (m *Manager) RequireAuth() flow.HandlerFunc
RequireAuth 创建要求认证的中间件
func (*Manager) RequirePermission ¶
func (m *Manager) RequirePermission(permission string) flow.HandlerFunc
RequirePermission 创建要求特定权限的中间件
func (*Manager) RequireRole ¶
func (m *Manager) RequireRole(role string) flow.HandlerFunc
RequireRole 创建要求特定角色的中间件
func (*Manager) SetDefaultProvider ¶
SetDefaultProvider 设置默认认证提供者
func (*Manager) ValidateToken ¶
ValidateToken 使用默认提供者验证令牌
type PolicyHandler ¶
type PolicyHandler func(ctx context.Context, user Authenticatable, resource interface{}, action string) bool
PolicyHandler 是策略处理函数类型
type PolicyManager ¶
type PolicyManager struct {
// contains filtered or unexported fields
}
PolicyManager 管理注册的策略
func (*PolicyManager) Authorize ¶
func (m *PolicyManager) Authorize(ctx context.Context, user Authenticatable, action string, resource interface{}) error
Authorize 检查授权并返回错误
func (*PolicyManager) Can ¶
func (m *PolicyManager) Can(ctx context.Context, user Authenticatable, action string, resource interface{}) bool
Can 检查用户是否能执行特定操作
func (*PolicyManager) Cannot ¶
func (m *PolicyManager) Cannot(ctx context.Context, user Authenticatable, action string, resource interface{}) bool
Cannot 检查用户是否不能执行特定操作
func (*PolicyManager) Check ¶
func (m *PolicyManager) Check(ctx context.Context, user Authenticatable, resource interface{}, action string) bool
Check 检查用户是否有权限对资源执行操作
func (*PolicyManager) GetPoliciesForResource ¶
func (m *PolicyManager) GetPoliciesForResource(resourceType string) []ResourcePolicy
GetPoliciesForResource 获取适用于资源类型的所有策略
func (*PolicyManager) GetPolicy ¶
func (m *PolicyManager) GetPolicy(name string) (ResourcePolicy, error)
GetPolicy 获取指定名称的策略
func (*PolicyManager) PolicyMiddleware ¶
func (m *PolicyManager) PolicyMiddleware(action string, resourceProvider func(*flow.Context) interface{}) flow.HandlerFunc
PolicyMiddleware 创建策略授权中间件
func (*PolicyManager) RegisterPolicy ¶
func (m *PolicyManager) RegisterPolicy(policy ResourcePolicy)
RegisterPolicy 注册新策略
type Provider ¶
type Provider interface {
// Authenticate 验证用户凭证并返回已认证用户
Authenticate(ctx context.Context, credentials map[string]string) (Authenticatable, error)
// GetUserByID 通过ID获取用户
GetUserByID(ctx context.Context, id string) (Authenticatable, error)
// GenerateToken 为用户生成认证令牌
GenerateToken(ctx context.Context, user Authenticatable, expiry time.Duration) (string, error)
// ValidateToken 验证令牌并返回关联的用户
ValidateToken(ctx context.Context, token string) (Authenticatable, error)
// RefreshToken 刷新认证令牌
RefreshToken(ctx context.Context, token string) (string, error)
// InvalidateToken 使令牌失效
InvalidateToken(ctx context.Context, token string) error
// CheckPermission 检查用户是否拥有指定权限
CheckPermission(ctx context.Context, user Authenticatable, permission string) bool
// CheckRole 检查用户是否拥有指定角色
CheckRole(ctx context.Context, user Authenticatable, role string) bool
}
Provider 表示认证服务提供者
type ResourcePolicy ¶
type ResourcePolicy interface {
// Name 返回策略名称
Name() string
// Resource 返回策略适用的资源类型名称
Resource() string
// Check 检查用户是否对资源有执行操作的权限
Check(ctx context.Context, user Authenticatable, resource interface{}, action string) bool
}
ResourcePolicy 定义资源策略接口
func CreateOwnerPolicy ¶
func CreateOwnerPolicy(policyName, resourceType string, getOwnerID func(resource interface{}) string) ResourcePolicy
CreateOwnerPolicy 创建基于所有者的策略 当用户是资源的所有者时,允许执行操作
func CreatePermissionBasedPolicy ¶
func CreatePermissionBasedPolicy(policyName, resourceType string, permissionActionMap map[string][]string) ResourcePolicy
CreatePermissionBasedPolicy 创建基于权限的策略 根据用户权限和操作的映射关系进行授权
func CreateRoleBasedPolicy ¶
func CreateRoleBasedPolicy(policyName, resourceType string, roleActionMap map[string][]string) ResourcePolicy
CreateRoleBasedPolicy 创建基于角色的策略 根据用户角色和操作的映射关系进行授权
type TokenClaims ¶
type TokenClaims struct {
jwt.RegisteredClaims
// UserID 是用户的唯一标识符
UserID string `json:"user_id"`
// Username 是用户的用户名
Username string `json:"username"`
// Permissions 是用户的权限列表
Permissions []string `json:"permissions,omitempty"`
// Roles 是用户的角色列表
Roles []string `json:"roles,omitempty"`
}
TokenClaims 表示JWT令牌声明
type UserProvider ¶
type UserProvider interface {
// FindByID 通过ID查找用户
FindByID(ctx context.Context, id string) (Authenticatable, error)
// FindByCredentials 通过凭证查找用户
FindByCredentials(ctx context.Context, credentials map[string]string) (Authenticatable, error)
// ValidateCredentials 验证用户的凭证
ValidateCredentials(ctx context.Context, user Authenticatable, credentials map[string]string) (bool, error)
}
UserProvider 定义用户数据访问接口