Documentation
¶
Overview ¶
Package authutil provides utilities for authentication, including Google Authenticator (TOTP) functionality.
Index ¶
- func QuickGenerate(issuer, accountName string) (secret, qrCodeURL string, err error)
- func QuickGenerateCode(secret string) (string, error)
- func QuickVerify(secret, code string) bool
- type BackupCodes
- type GoogleAuthenticator
- func (ga *GoogleAuthenticator) GenerateCode(secret string) (string, error)
- func (ga *GoogleAuthenticator) GenerateCodeAtTime(secret string, timestamp time.Time) (string, error)
- func (ga *GoogleAuthenticator) GenerateQRCodeImageURL(secret, accountName string) string
- func (ga *GoogleAuthenticator) GenerateQRCodeImageURLCN(secret, accountName string) string
- func (ga *GoogleAuthenticator) GenerateQRCodeURL(secret, accountName string) string
- func (ga *GoogleAuthenticator) GenerateSecret() (string, error)
- func (ga *GoogleAuthenticator) GetOtpauthURL(secret, accountName string) string
- func (ga *GoogleAuthenticator) GetRemainingTime() int
- func (ga *GoogleAuthenticator) VerifyCode(secret, code string) bool
- func (ga *GoogleAuthenticator) VerifyCodeWithTolerance(secret, code string, tolerance int) bool
- type TOTPConfig
- type TOTPManager
- func (tm *TOTPManager) GetUserSecret(userID string) (string, bool)
- func (tm *TOTPManager) RemoveUser(userID string)
- func (tm *TOTPManager) SetupUser(userID, accountName string) (string, string, error)
- func (tm *TOTPManager) SetupUserWithPwd(userID, accountName, secret string) (string, string, error)
- func (tm *TOTPManager) VerifyUserCode(userID, code string) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func QuickGenerate ¶
QuickGenerate generates a secret and QR code URL with default settings 默认使用国内可访问的QR Server API生成二维码
func QuickGenerateCode ¶
QuickGenerateCode generates a TOTP code with default settings
func QuickVerify ¶
QuickVerify verifies a TOTP code with default settings
Types ¶
type BackupCodes ¶
type BackupCodes struct {
Codes []string `json:"codes"`
Generated time.Time `json:"generated"`
Used []bool `json:"used"`
}
BackupCodes represents backup recovery codes
func GenerateBackupCodes ¶
func GenerateBackupCodes(count int) (*BackupCodes, error)
GenerateBackupCodes generates backup recovery codes
func (*BackupCodes) GetUnusedCodes ¶
func (bc *BackupCodes) GetUnusedCodes() []string
GetUnusedCodes returns all unused backup codes
func (*BackupCodes) UseBackupCode ¶
func (bc *BackupCodes) UseBackupCode(code string) bool
UseBackupCode marks a backup code as used
type GoogleAuthenticator ¶
type GoogleAuthenticator struct {
// contains filtered or unexported fields
}
GoogleAuthenticator represents a Google Authenticator helper
func NewGoogleAuthenticator ¶
func NewGoogleAuthenticator(config *TOTPConfig) *GoogleAuthenticator
NewGoogleAuthenticator creates a new Google Authenticator instance
func (*GoogleAuthenticator) GenerateCode ¶
func (ga *GoogleAuthenticator) GenerateCode(secret string) (string, error)
GenerateCode generates a TOTP code for the current time
func (*GoogleAuthenticator) GenerateCodeAtTime ¶
func (ga *GoogleAuthenticator) GenerateCodeAtTime(secret string, timestamp time.Time) (string, error)
GenerateCodeAtTime generates a TOTP code for a specific time
func (*GoogleAuthenticator) GenerateQRCodeImageURL ¶
func (ga *GoogleAuthenticator) GenerateQRCodeImageURL(secret, accountName string) string
GenerateQRCodeImageURL generates a URL to display QR code image via Google Charts API Note: Google Charts API requires VPN access in China
func (*GoogleAuthenticator) GenerateQRCodeImageURLCN ¶
func (ga *GoogleAuthenticator) GenerateQRCodeImageURLCN(secret, accountName string) string
GenerateQRCodeImageURLCN generates a QR code image URL using Chinese accessible APIs 使用国内可访问的二维码生成API(多个备选方案)
func (*GoogleAuthenticator) GenerateQRCodeURL ¶
func (ga *GoogleAuthenticator) GenerateQRCodeURL(secret, accountName string) string
GenerateQRCodeURL generates a QR code URL for Google Authenticator
func (*GoogleAuthenticator) GenerateSecret ¶
func (ga *GoogleAuthenticator) GenerateSecret() (string, error)
GenerateSecret generates a new secret key for TOTP
func (*GoogleAuthenticator) GetOtpauthURL ¶
func (ga *GoogleAuthenticator) GetOtpauthURL(secret, accountName string) string
GetOtpauthURL returns the raw otpauth:// URL for manual QR code generation 返回原始的 otpauth:// URL,可用于本地生成二维码
func (*GoogleAuthenticator) GetRemainingTime ¶
func (ga *GoogleAuthenticator) GetRemainingTime() int
GetRemainingTime returns the remaining time in seconds until the next code change
func (*GoogleAuthenticator) VerifyCode ¶
func (ga *GoogleAuthenticator) VerifyCode(secret, code string) bool
VerifyCode verifies a TOTP code against the current time (with tolerance)
func (*GoogleAuthenticator) VerifyCodeWithTolerance ¶
func (ga *GoogleAuthenticator) VerifyCodeWithTolerance(secret, code string, tolerance int) bool
VerifyCodeWithTolerance verifies a TOTP code with time tolerance tolerance: number of time steps to check before and after current time
type TOTPConfig ¶
type TOTPConfig struct {
// Secret key (base32 encoded)
Secret string
// Issuer name (e.g., "MyCompany")
Issuer string
// Number of digits in the code (usually 6)
Digits int
// Time step in seconds (usually 30)
Period int
// Hash algorithm (sha1, sha256, sha512)
Algorithm string
}
TOTPConfig holds configuration for TOTP generation
func DefaultTOTPConfig ¶
func DefaultTOTPConfig() *TOTPConfig
DefaultTOTPConfig returns default TOTP configuration
type TOTPManager ¶
type TOTPManager struct {
// contains filtered or unexported fields
}
TOTPManager manages TOTP for multiple users
func NewTOTPManager ¶
func NewTOTPManager(issuer string) *TOTPManager
NewTOTPManager creates a new TOTP manager
func (*TOTPManager) GetUserSecret ¶
func (tm *TOTPManager) GetUserSecret(userID string) (string, bool)
GetUserSecret gets the secret for a user (for backup purposes)
func (*TOTPManager) RemoveUser ¶
func (tm *TOTPManager) RemoveUser(userID string)
RemoveUser removes TOTP setup for a user
func (*TOTPManager) SetupUser ¶
func (tm *TOTPManager) SetupUser(userID, accountName string) (string, string, error)
SetupUser sets up TOTP for a user
func (*TOTPManager) SetupUserWithPwd ¶
func (tm *TOTPManager) SetupUserWithPwd(userID, accountName, secret string) (string, string, error)
SetupUserWithPwd sets up TOTP for a user with external provided secret
func (*TOTPManager) VerifyUserCode ¶
func (tm *TOTPManager) VerifyUserCode(userID, code string) bool
VerifyUserCode verifies TOTP code for a user