Documentation
¶
Index ¶
- Constants
- Variables
- func ClearValidation(userID string, store ValidationStore) error
- func GenerateProvisioningURI(secret, accountName string, config TOTPConfig) string
- func GenerateSecret(size int) (string, error)
- func GenerateTOTPCode(secret string, config TOTPConfig) (string, error)
- func NewMiddleware(options ...func(*MiddlewareConfig)) mist.Middleware
- func Validate(ctx *mist.Context, userID, code string, totp *TOTP, store ValidationStore, ...) error
- func ValidateTOTPCode(secret, code string, config TOTPConfig, usedTokens *UsedTokenTracker) bool
- func WithGetUserID(fn func(*mist.Context) (string, error)) func(*MiddlewareConfig)
- func WithRedirectURL(url string) func(*MiddlewareConfig)
- func WithStore(store ValidationStore) func(*MiddlewareConfig)
- func WithUnauthorizedHandler(handler func(*mist.Context)) func(*MiddlewareConfig)
- func WithValidationDuration(duration time.Duration) func(*MiddlewareConfig)
- type BackupCode
- type MemoryStore
- type MiddlewareConfig
- type TOTP
- func (t *TOTP) Generate() (string, error)
- func (t *TOTP) GetUnusedBackupCodes() []string
- func (t *TOTP) ProvisioningURI(accountName string) string
- func (t *TOTP) RegenerateBackupCodes() error
- func (t *TOTP) UseBackupCode(code string) error
- func (t *TOTP) Validate(code string) bool
- func (t *TOTP) ValidateBackupCode(code string) bool
- type TOTPConfig
- type UsedTokenTracker
- type ValidationStore
Constants ¶
const ( // MFACookieName 用于标记MFA验证状态的Cookie名 MFACookieName = "_mfa_validated" // MFASessionKey 用于在Session中存储MFA状态的键 MFASessionKey = "_mfa_status" // DefaultValidationDuration MFA验证状态默认有效期 DefaultValidationDuration = 12 * time.Hour )
const ( // 默认TOTP参数 DefaultDigits = 6 DefaultPeriod = 30 DefaultAlgorithm = "SHA1" DefaultIssuer = "Mist" // 备份码长度 DefaultBackupCodeLength = 8 // 默认备份码数量 DefaultBackupCodeCount = 10 // 默认验证窗口大小(允许前后多少个时间单位) DefaultWindowSize = 1 )
const ( AlgorithmSHA1 = "SHA1" AlgorithmSHA256 = "SHA256" AlgorithmSHA512 = "SHA512" )
支持的算法
Variables ¶
var ( // ErrMFARequired 表示需要多因素验证 ErrMFARequired = errors.New("需要多因素验证") // ErrInvalidMFACode 表示MFA验证码无效 ErrInvalidMFACode = errors.New("无效的多因素验证码") )
var ( // ErrInvalidOTP 表示OTP无效 ErrInvalidOTP = errors.New("提供的OTP代码无效") // ErrInvalidSecret 表示密钥无效 ErrInvalidSecret = errors.New("提供的密钥格式无效") // ErrInvalidInput 表示输入参数无效 ErrInvalidInput = errors.New("提供的输入参数无效") // ErrInvalidAlgorithm 表示算法无效 ErrInvalidAlgorithm = errors.New("提供的哈希算法无效") // ErrAllBackupCodesUsed 表示所有备份码都已使用 ErrAllBackupCodesUsed = errors.New("所有备份码都已使用") // ErrBackupCodeInvalid 表示备份码无效 ErrBackupCodeInvalid = errors.New("提供的备份码无效") )
Functions ¶
func ClearValidation ¶
func ClearValidation(userID string, store ValidationStore) error
ClearValidation 清除MFA验证状态
func GenerateProvisioningURI ¶
func GenerateProvisioningURI(secret, accountName string, config TOTPConfig) string
GenerateProvisioningURI 生成TOTP配置URI 用于生成二维码,让用户扫码添加到验证器应用(如Google Authenticator)
func GenerateTOTPCode ¶
func GenerateTOTPCode(secret string, config TOTPConfig) (string, error)
GenerateTOTPCode 基于密钥和当前时间生成TOTP代码
func NewMiddleware ¶
func NewMiddleware(options ...func(*MiddlewareConfig)) mist.Middleware
New 创建新的MFA中间件
func Validate ¶
func Validate(ctx *mist.Context, userID, code string, totp *TOTP, store ValidationStore, duration time.Duration) error
Validate 验证MFA代码
func ValidateTOTPCode ¶
func ValidateTOTPCode(secret, code string, config TOTPConfig, usedTokens *UsedTokenTracker) bool
ValidateTOTPCode 验证TOTP代码 允许windowSize个时间周期的误差(默认前后1个)
func WithGetUserID ¶
func WithGetUserID(fn func(*mist.Context) (string, error)) func(*MiddlewareConfig)
WithGetUserID 设置获取用户ID的函数
func WithRedirectURL ¶
func WithRedirectURL(url string) func(*MiddlewareConfig)
WithRedirectURL 设置重定向URL
func WithUnauthorizedHandler ¶
func WithUnauthorizedHandler(handler func(*mist.Context)) func(*MiddlewareConfig)
WithUnauthorizedHandler 设置未授权处理函数
func WithValidationDuration ¶
func WithValidationDuration(duration time.Duration) func(*MiddlewareConfig)
WithValidationDuration 设置验证有效期
Types ¶
type BackupCode ¶ added in v0.1.24
BackupCode 备份码结构体
func GenerateBackupCodes ¶ added in v0.1.24
func GenerateBackupCodes(count, length int) ([]BackupCode, error)
GenerateBackupCodes 生成备份码
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore 内存实现的MFA验证状态存储
type MiddlewareConfig ¶
type MiddlewareConfig struct {
// Store MFA验证状态存储
Store ValidationStore
// GetUserID 从请求上下文中获取用户ID的函数
GetUserID func(*mist.Context) (string, error)
// ValidationDuration MFA验证有效期
ValidationDuration time.Duration
// RedirectURL 未验证时重定向的URL
RedirectURL string
OnUnauthorized func(*mist.Context)
}
Config MFA中间件配置
type TOTP ¶
type TOTP struct {
Secret string
Config TOTPConfig
BackupCodes []BackupCode
UsedTokens *UsedTokenTracker
}
TOTP 是一个简化使用的TOTP结构体
func NewTOTPWithSecret ¶
func NewTOTPWithSecret(secret string, config ...TOTPConfig) (*TOTP, error)
NewTOTPWithSecret 使用已有密钥创建TOTP实例
func (*TOTP) GetUnusedBackupCodes ¶ added in v0.1.24
GetUnusedBackupCodes 获取未使用的备份码
func (*TOTP) ProvisioningURI ¶
ProvisioningURI 生成配置URI
func (*TOTP) RegenerateBackupCodes ¶ added in v0.1.24
RegenerateBackupCodes 重新生成备份码
func (*TOTP) UseBackupCode ¶ added in v0.1.24
UseBackupCode 使用一个备份码
func (*TOTP) ValidateBackupCode ¶ added in v0.1.24
ValidateBackupCode 验证备份码
type TOTPConfig ¶
type TOTPConfig struct {
// Digits 代码位数,通常为6
Digits int
// Period 刷新周期,通常为30秒
Period int
// Algorithm 使用的哈希算法,通常为SHA1
Algorithm string
// Issuer 发行者名称,通常为应用名
Issuer string
// SecretSize 密钥大小(字节)
SecretSize int
// WindowSize 验证窗口大小,即允许前后多少个时间单位
WindowSize int
// SkipValidUsedTokens 是否跳过验证已使用过的令牌(防止重放攻击)
SkipValidUsedTokens bool
}
TOTPConfig TOTP配置结构体
type UsedTokenTracker ¶ added in v0.1.24
type UsedTokenTracker struct {
// contains filtered or unexported fields
}
UsedTokenTracker 用于跟踪已使用的令牌,防止重放攻击
func NewUsedTokenTracker ¶ added in v0.1.24
func NewUsedTokenTracker(expiry time.Duration) *UsedTokenTracker
NewUsedTokenTracker 创建新的令牌跟踪器
func (*UsedTokenTracker) IsTokenUsed ¶ added in v0.1.24
func (t *UsedTokenTracker) IsTokenUsed(tokenKey string) bool
IsTokenUsed 检查令牌是否已被使用
func (*UsedTokenTracker) MarkTokenAsUsed ¶ added in v0.1.24
func (t *UsedTokenTracker) MarkTokenAsUsed(tokenKey string)
MarkTokenAsUsed 标记令牌为已使用