Documentation
¶
Index ¶
- Variables
- func ConsumeResetToken(token string) bool
- func ConsumeTmpToken(token string) bool
- func FormatBackupCode(code string) string
- func GenerateQRCodeData(secret, username, issuer string) (string, error)
- func GenerateResetToken(userID, email string) (string, string, error)
- func GenerateTOTPSecret(username, issuer string) (string, error)
- func GenerateTOTPURL(secret, username, issuer string) (string, error)
- func GenerateToken(userID, username, role string) (string, error)
- func GetEmailConfig() *config.EmailConfig
- func HandleLogin(w http.ResponseWriter, r *http.Request)
- func HandleRefresh(w http.ResponseWriter, r *http.Request)
- func InitAdmin(db *storage.DB, username, password string) error
- func InitDB(db *storage.DB)
- func MaskEmail(email string) string
- func Middleware(next http.HandlerFunc) http.HandlerFunc
- func OptionalMiddleware(next http.HandlerFunc) http.HandlerFunc
- func RefreshToken(tokenString string) (string, error)
- func SecretFingerprint() string
- func SendPasswordResetEmail(email, username, code, token string) error
- func SetEmailConfig(cfg *config.EmailConfig)
- func SetExpiration(expiration time.Duration)
- func SetSecret(secret string)
- func ValidateTOTP(secret, code string) bool
- func ValidateTmpToken(token string) (string, bool)
- type Claims
- type ForgotPasswordRequest
- type ForgotPasswordResponse
- type LoginRequest
- type LoginResponse
- type ResetPasswordRequest
- type ResetTokenData
- type TmpTokenData
- type UserInfo
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrResetTokenInvalid 重置令牌无效 ErrResetTokenInvalid = errors.New("invalid or expired reset token") // ErrEmailNotConfigured 邮件未配置 ErrEmailNotConfigured = errors.New("email service not configured") )
View Source
var DB *storage.DB
DB 存储接口(在运行时注入)
Functions ¶
func ConsumeTmpToken ¶
ConsumeTmpToken 消耗临时令牌(验证后删除,导出供外部使用)
func GenerateQRCodeData ¶
GenerateQRCodeData 生成 QR 码数据 注意:实际 QR 码图片生成在前端进行,这里只返回 URL
func GenerateResetToken ¶
GenerateResetToken 生成密码重置令牌和验证码
func GenerateTOTPSecret ¶
GenerateTOTPSecret 生成 TOTP 密钥
func GenerateTOTPURL ¶
GenerateTOTPURL 生成 TOTP URL(用于 QR 码)
func GenerateToken ¶
GenerateToken 生成 JWT token
func GetEmailConfig ¶
func GetEmailConfig() *config.EmailConfig
GetEmailConfig 获取邮件配置(优先使用配置文件,否则回退到环境变量)
func HandleRefresh ¶
func HandleRefresh(w http.ResponseWriter, r *http.Request)
HandleRefresh 处理 token 刷新
func OptionalMiddleware ¶
func OptionalMiddleware(next http.HandlerFunc) http.HandlerFunc
OptionalMiddleware 可选认证中间件(不强制要求登录)
func SendPasswordResetEmail ¶
SendPasswordResetEmail 发送密码重置邮件
func ValidateTmpToken ¶
ValidateTmpToken 验证临时令牌(导出供外部使用)
Types ¶
type Claims ¶
type Claims struct {
UserID string `json:"user_id"`
Username string `json:"username"`
Role string `json:"role"`
jwt.RegisteredClaims
}
Claims JWT 声明
func ValidateToken ¶
ValidateToken 验证 JWT token
type ForgotPasswordRequest ¶
type ForgotPasswordRequest struct {
Username string `json:"username"`
}
ForgotPasswordRequest 忘记密码请求
type ForgotPasswordResponse ¶
type ForgotPasswordResponse struct {
Email string `json:"email"` // 脱敏邮箱
MaskedEmail string `json:"masked_email"` // 脱敏邮箱
Message string `json:"message"`
}
ForgotPasswordResponse 忘记密码响应
type LoginRequest ¶
LoginRequest 登录请求
type LoginResponse ¶
type LoginResponse struct {
Token string `json:"token,omitempty"`
ExpiresAt int64 `json:"expires_at,omitempty"`
UserID string `json:"user_id"`
Username string `json:"username"`
Role string `json:"role,omitempty"`
RequiresTOTP bool `json:"requires_totp"`
TmpToken string `json:"tmp_token,omitempty"`
}
LoginResponse 登录响应
type ResetPasswordRequest ¶
type ResetPasswordRequest struct {
Token string `json:"token"`
Code string `json:"code"` // 可选,用于验证码验证
NewPassword string `json:"new_password"`
}
ResetPasswordRequest 重置密码请求
type ResetTokenData ¶
ResetTokenData 重置令牌数据
func ValidateResetCode ¶
func ValidateResetCode(token, code string) (*ResetTokenData, error)
ValidateResetCode 验证重置验证码
func ValidateResetToken ¶
func ValidateResetToken(token string) (*ResetTokenData, error)
ValidateResetToken 验证重置令牌
type TmpTokenData ¶
TmpTokenData 临时令牌数据
Click to show internal directories.
Click to hide internal directories.