mfa

package
v0.1.27 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MFACookieName 用于标记MFA验证状态的Cookie名
	MFACookieName = "_mfa_validated"

	// MFASessionKey 用于在Session中存储MFA状态的键
	MFASessionKey = "_mfa_status"

	// DefaultValidationDuration MFA验证状态默认有效期
	DefaultValidationDuration = 12 * time.Hour
)
View Source
const (
	// 默认TOTP参数
	DefaultDigits    = 6
	DefaultPeriod    = 30
	DefaultAlgorithm = "SHA1"
	DefaultIssuer    = "Mist"
	// 备份码长度
	DefaultBackupCodeLength = 8
	// 默认备份码数量
	DefaultBackupCodeCount = 10
	// 默认验证窗口大小(允许前后多少个时间单位)
	DefaultWindowSize = 1
)
View Source
const (
	AlgorithmSHA1   = "SHA1"
	AlgorithmSHA256 = "SHA256"
	AlgorithmSHA512 = "SHA512"
)

支持的算法

Variables

View Source
var (
	// ErrMFARequired 表示需要多因素验证
	ErrMFARequired = errors.New("需要多因素验证")

	// ErrInvalidMFACode 表示MFA验证码无效
	ErrInvalidMFACode = errors.New("无效的多因素验证码")
)
View Source
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 GenerateSecret

func GenerateSecret(size int) (string, error)

GenerateSecret 生成随机TOTP密钥

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 WithStore

func WithStore(store ValidationStore) func(*MiddlewareConfig)

WithStore 设置验证状态存储

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

type BackupCode struct {
	// Code 备份码
	Code string
	// Used 是否已使用
	Used bool
}

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验证状态存储

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore 创建新的内存验证状态存储

func (*MemoryStore) Clear

func (s *MemoryStore) Clear(userID string) error

Clear 清除用户MFA验证状态

func (*MemoryStore) Set

func (s *MemoryStore) Set(userID string, expiry time.Duration) error

Set 设置用户MFA验证状态

func (*MemoryStore) Validate

func (s *MemoryStore) Validate(userID string) (bool, error)

Validate 验证用户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 未验证时的处理函数
	OnUnauthorized func(*mist.Context)
}

Config MFA中间件配置

type TOTP

type TOTP struct {
	Secret      string
	Config      TOTPConfig
	BackupCodes []BackupCode
	UsedTokens  *UsedTokenTracker
}

TOTP 是一个简化使用的TOTP结构体

func NewTOTP

func NewTOTP(config ...TOTPConfig) (*TOTP, error)

NewTOTP 创建一个新的TOTP实例

func NewTOTPWithSecret

func NewTOTPWithSecret(secret string, config ...TOTPConfig) (*TOTP, error)

NewTOTPWithSecret 使用已有密钥创建TOTP实例

func (*TOTP) Generate

func (t *TOTP) Generate() (string, error)

Generate 生成当前TOTP代码

func (*TOTP) GetUnusedBackupCodes added in v0.1.24

func (t *TOTP) GetUnusedBackupCodes() []string

GetUnusedBackupCodes 获取未使用的备份码

func (*TOTP) ProvisioningURI

func (t *TOTP) ProvisioningURI(accountName string) string

ProvisioningURI 生成配置URI

func (*TOTP) RegenerateBackupCodes added in v0.1.24

func (t *TOTP) RegenerateBackupCodes() error

RegenerateBackupCodes 重新生成备份码

func (*TOTP) UseBackupCode added in v0.1.24

func (t *TOTP) UseBackupCode(code string) error

UseBackupCode 使用一个备份码

func (*TOTP) Validate

func (t *TOTP) Validate(code string) bool

Validate 验证TOTP代码

func (*TOTP) ValidateBackupCode added in v0.1.24

func (t *TOTP) ValidateBackupCode(code string) bool

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配置结构体

func DefaultTOTPConfig

func DefaultTOTPConfig() TOTPConfig

DefaultTOTPConfig 返回默认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 标记令牌为已使用

type ValidationStore

type ValidationStore interface {
	// Validate 验证指定用户ID是否已完成MFA验证
	Validate(userID string) (bool, error)

	// Set 设置用户MFA验证状态
	Set(userID string, expiry time.Duration) error

	// Clear 清除用户MFA验证状态
	Clear(userID string) error
}

ValidationStore 存储MFA验证状态的接口

Jump to

Keyboard shortcuts

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