Versions in this module Expand all Collapse all v1 v1.0.0 Feb 3, 2026 Changes in this version + const EmailTypeAccount + const EmailTypeNotification + const EmailVerifyDuration + const PasswordResetDuration + const RoleAdmin + const RoleModerator + const RoleUser + const TokenTypeEmailVerify + const TokenTypePasswordReset + var BlockedUsernames = map[string]bool + var ErrCannotRemovePrimary = errors.New("cannot remove primary email address") + var ErrDecryptionFailed = errors.New("decryption failed") + var ErrEmailAlreadyPrimary = errors.New("email is already the primary address") + var ErrEmailExists = errors.New("email address already registered") + var ErrEmailInvalid = errors.New("invalid email address") + var ErrEmailNotFoundEM = errors.New("email address not found") + var ErrEmailNotVerified = errors.New("email not verified") + var ErrEmailRequired = errors.New("email is required") + var ErrEmailTaken = errors.New("email is already registered") + var ErrEncryptionFailed = errors.New("encryption failed") + var ErrInvalidCredentials = errors.New("invalid username or password") + var ErrInvalidRecoveryKey = errors.New("invalid recovery key") + var ErrInvalidVerification = errors.New("invalid verification token") + var ErrMinimumOneEmail = errors.New("user must have at least one email address") + var ErrNoRecoveryKeys = errors.New("no recovery keys available") + var ErrPasskeyAlreadyExists = errors.New("passkey already registered") + var ErrPasskeyChallengeExpired = errors.New("passkey challenge has expired") + var ErrPasskeyChallengeFailed = errors.New("passkey challenge verification failed") + var ErrPasskeyInvalidResponse = errors.New("invalid passkey response") + var ErrPasskeyNotEnabled = errors.New("passkeys are not enabled for this user") + var ErrPasskeyNotFound = errors.New("passkey not found") + var ErrPasskeySignCountInvalid = errors.New("passkey sign count invalid (possible cloned authenticator)") + var ErrPasswordRequired = errors.New("password is required") + var ErrPasswordTooShort = errors.New("password must be at least 8 characters") + var ErrPasswordTooWeak = errors.New(...) + var ErrPasswordWhitespace = errors.New("password cannot start or end with whitespace") + var ErrPreferencesNotFound = errors.New("preferences not found") + var ErrRecoveryKeyNotFound = errors.New("recovery key not found") + var ErrRecoveryKeyUsed = errors.New("recovery key already used") + var ErrRegistrationDisabled = errors.New("registration is currently disabled") + var ErrSessionExpired = errors.New("session expired") + var ErrSessionNotFound = errors.New("session not found") + var ErrTOTPAlreadySetup = errors.New("2FA is already set up") + var ErrTOTPInvalidCode = errors.New("invalid 2FA code") + var ErrTOTPNotEnabled = errors.New("2FA is not enabled for this user") + var ErrTOTPNotVerified = errors.New("2FA setup not verified") + var ErrTokenExpired = errors.New("token expired") + var ErrTokenInvalid = errors.New("invalid token") + var ErrTokenNameEmpty = errors.New("token name is required") + var ErrTokenNotFound = errors.New("token not found") + var ErrUserInactive = errors.New("user account is inactive") + var ErrUserNotFound = errors.New("user not found") + var ErrUsernameInvalid = errors.New("username can only contain lowercase letters, numbers, underscore, and hyphen") + var ErrUsernameRequired = errors.New("username is required") + var ErrUsernameReserved = errors.New("this username is reserved") + var ErrUsernameTaken = errors.New("username is already taken") + var ErrUsernameTooLong = errors.New("username must be at most 32 characters") + var ErrUsernameTooShort = errors.New("username must be at least 3 characters") + var ErrVerificationExpired = errors.New("verification link has expired") + var ErrVerificationTokenExpired = errors.New("verification token expired") + var ErrVerificationTokenInvalid = errors.New("invalid verification token") + var ErrVerificationTokenNotFound = errors.New("verification token not found") + func CheckPassword(password, encodedHash string) bool + func FormatRecoveryKey(key string) string + func GenerateSessionToken() (string, error) + func GenerateToken(length int) (string, error) + func HashPassword(password string) (string, error) + func IsBlockedUsername(username string) bool + func MaskEmail(email string) string + func NormalizeEmail(email string) string + func NormalizeUsername(username string) string + func SerializeOptions(options interface{}) (string, error) + func ValidateEmail(email string) error + func ValidatePassword(password string, minLength int) error + func ValidateUsername(username string) error + type AuthConfig struct + CookieDomain string + CookieName string + CookieSecure bool + SessionDurationDays int + type AuthManager struct + func NewAuthManager(db *sql.DB, config AuthConfig) *AuthManager + func (am *AuthManager) CleanupExpiredSessions(ctx context.Context) (int64, error) + func (am *AuthManager) ClearSessionCookie(w http.ResponseWriter) + func (am *AuthManager) GetSessionToken(r *http.Request) string + func (am *AuthManager) GetUserByEmail(ctx context.Context, email string) (*User, error) + func (am *AuthManager) GetUserByID(ctx context.Context, id int64) (*User, error) + func (am *AuthManager) GetUserByUsername(ctx context.Context, username string) (*User, error) + func (am *AuthManager) GetUserSessions(ctx context.Context, userID int64) ([]UserSession, error) + func (am *AuthManager) Login(ctx context.Context, usernameOrEmail, password, ipAddress, userAgent string) (*User, *UserSession, error) + func (am *AuthManager) Logout(ctx context.Context, token string) error + func (am *AuthManager) LogoutAll(ctx context.Context, userID int64, exceptToken string) error + func (am *AuthManager) Register(ctx context.Context, username, email, password string, minPasswordLength int) (*User, error) + func (am *AuthManager) RevokeSession(ctx context.Context, userID, sessionID int64) error + func (am *AuthManager) SetSessionCookie(w http.ResponseWriter, token string) + func (am *AuthManager) SetUserActive(ctx context.Context, userID int64, active bool) error + func (am *AuthManager) SetUserRole(ctx context.Context, userID int64, role string) error + func (am *AuthManager) UpdatePassword(ctx context.Context, userID int64, newPassword string, minPasswordLength int) error + func (am *AuthManager) UpdateProfile(ctx context.Context, userID int64, displayName, bio, avatarURL string) error + func (am *AuthManager) ValidateSession(ctx context.Context, token string) (*User, *UserSession, error) + func (am *AuthManager) VerifyEmail(ctx context.Context, userID int64) error + type AuthenticationOptions struct + AllowCredentials []PublicKeyCredentialDescriptor + Challenge string + RpId string + Timeout int + UserVerification string + type AuthenticationResponse struct + ID string + RawID string + Response struct{ ... } + Type string + type AuthenticatorSelection struct + AuthenticatorAttachment string + RequireResidentKey bool + ResidentKey string + UserVerification string + type CreateTokenRequest struct + ExpiresIn time.Duration + Name string + Permissions []string + type CreateTokenResponse struct + Expiry *time.Time + ID int64 + Name string + Prefix string + Token string + type EmailInfo struct + AccountEmail string + AccountEmailVerified bool + NotificationEmail string + NotificationEmailVerified bool + UsingSeparateNotification bool + type EmailManager struct + func NewEmailManager(db *sql.DB) *EmailManager + func (em *EmailManager) AddEmail(ctx context.Context, userID, email string) (*UserEmail, string, error) + func (em *EmailManager) ClearNotificationEmail(ctx context.Context, userID string) error + func (em *EmailManager) GetAccountEmail(ctx context.Context, userID string) (string, error) + func (em *EmailManager) GetEmailForType(ctx context.Context, userID string, emailType string) (string, error) + func (em *EmailManager) GetEmails(ctx context.Context, userID string) ([]*UserEmail, error) + func (em *EmailManager) GetNotificationEmail(ctx context.Context, userID string) (string, error) + func (em *EmailManager) RemoveEmail(ctx context.Context, userID, emailID string) error + func (em *EmailManager) ResendVerification(ctx context.Context, emailID string) (string, error) + func (em *EmailManager) SetAsNotificationEmail(ctx context.Context, userID, emailID string) error + func (em *EmailManager) VerifyEmail(ctx context.Context, token string) (*UserEmail, error) + type Passkey struct + AAGUID string + AttestationType string + CreatedAt time.Time + CredentialID string + ID string + LastUsedAt *time.Time + Name string + PublicKey string + SignCount uint32 + Transport string + UserID string + func (pk *Passkey) ToInfo() PasskeyInfo + type PasskeyChallenge struct + Challenge string + CreatedAt time.Time + ExpiresAt time.Time + ID string + Type string + UserID string + type PasskeyInfo struct + CreatedAt time.Time + ID string + LastUsedAt *time.Time + Name string + type PasskeyListResponse struct + Count int + Passkeys []PasskeyInfo + type PasskeyManager struct + func NewPasskeyManager(db *sql.DB, rpID, rpOrigin, rpName string) *PasskeyManager + func (pm *PasskeyManager) BeginAuthentication(ctx context.Context, userID *int64) (*AuthenticationOptions, error) + func (pm *PasskeyManager) BeginRegistration(ctx context.Context, user *User) (*RegistrationOptions, error) + func (pm *PasskeyManager) CleanupExpiredChallenges(ctx context.Context) error + func (pm *PasskeyManager) DeletePasskey(ctx context.Context, userID int64, passkeyID string) error + func (pm *PasskeyManager) FinishAuthentication(ctx context.Context, response *AuthenticationResponse) (*Passkey, error) + func (pm *PasskeyManager) FinishRegistration(ctx context.Context, userID int64, response *RegistrationResponse, name string) (*Passkey, error) + func (pm *PasskeyManager) GetPasskey(ctx context.Context, userID int64, passkeyID string) (*Passkey, error) + func (pm *PasskeyManager) GetPasskeyCount(ctx context.Context, userID int64) (int, error) + func (pm *PasskeyManager) GetPasskeys(ctx context.Context, userID int64) ([]*Passkey, error) + func (pm *PasskeyManager) GetStatus(ctx context.Context, userID int64) PasskeyStatus + func (pm *PasskeyManager) HasPasskeys(ctx context.Context, userID int64) bool + func (pm *PasskeyManager) ListPasskeysForDisplay(ctx context.Context, userID int64) (*PasskeyListResponse, error) + func (pm *PasskeyManager) RenamePasskey(ctx context.Context, userID int64, passkeyID, name string) error + type PasskeyStatus struct + Count int + Enabled bool + type PreferencesManager struct + func NewPreferencesManager(db *sql.DB) *PreferencesManager + func (pm *PreferencesManager) ClearCookie(w http.ResponseWriter) + func (pm *PreferencesManager) GetForUser(ctx context.Context, userID int64) (*UserPreferences, error) + func (pm *PreferencesManager) GetFromCookie(r *http.Request) *UserPreferences + func (pm *PreferencesManager) SaveForUser(ctx context.Context, userID int64, prefs *UserPreferences) error + func (pm *PreferencesManager) SetCookie(w http.ResponseWriter, prefs *UserPreferences) error + type PublicKeyCredentialDescriptor struct + ID string + Transports []string + Type string + type PublicKeyCredentialParam struct + Alg int + Type string + type PublicKeyCredentialUser struct + DisplayName string + ID string + Name string + type PublicProfile struct + AvatarURL string + Bio string + CreatedAt time.Time + DisplayName string + ID int64 + Username string + type RecoveryKey struct + CreatedAt time.Time + ID int64 + KeyHash string + Used bool + UsedAt *time.Time + UserID int64 + type RecoveryKeyStats struct + Remaining int + Total int + Used int + type RecoveryManager struct + func NewRecoveryManager(db *sql.DB, keyCount int) *RecoveryManager + func (rm *RecoveryManager) Generate(ctx context.Context, userID int64) ([]string, error) + func (rm *RecoveryManager) GetRemainingCount(ctx context.Context, userID int64) (int, error) + func (rm *RecoveryManager) GetUsageStats(ctx context.Context, userID int64) (*RecoveryKeyStats, error) + func (rm *RecoveryManager) HasRecoveryKeys(ctx context.Context, userID int64) bool + func (rm *RecoveryManager) RevokeAll(ctx context.Context, userID int64) error + func (rm *RecoveryManager) Validate(ctx context.Context, userID int64, key string) error + type RegistrationOptions struct + Attestation string + AuthenticatorSelection AuthenticatorSelection + Challenge string + ExcludeCredentials []PublicKeyCredentialDescriptor + PubKeyCredParams []PublicKeyCredentialParam + RelyingParty RelyingPartyEntity + Timeout int + User PublicKeyCredentialUser + type RegistrationResponse struct + AuthenticatorAttachment string + ID string + RawID string + Response struct{ ... } + Type string + type RelyingPartyEntity struct + ID string + Name string + type TOTPManager struct + func NewTOTPManager(db *sql.DB, issuer string, encryptionKey []byte) (*TOTPManager, error) + func (tm *TOTPManager) Disable(ctx context.Context, userID int64) error + func (tm *TOTPManager) GenerateBackupCodes(count int) ([]string, error) + func (tm *TOTPManager) Get2FA(ctx context.Context, userID int64) (*User2FA, error) + func (tm *TOTPManager) GetStatus(ctx context.Context, userID int64) TOTPStatus + func (tm *TOTPManager) Is2FAEnabled(ctx context.Context, userID int64) bool + func (tm *TOTPManager) Setup(ctx context.Context, user *User) (*TOTPSetupResponse, error) + func (tm *TOTPManager) Verify(ctx context.Context, userID int64, code string) error + func (tm *TOTPManager) VerifySetup(ctx context.Context, userID int64, code string) error + type TOTPSetupResponse struct + Account string + Issuer string + QRCodeURL string + Secret string + type TOTPStatus struct + Enabled bool + EnabledAt *time.Time + Verified bool + type TokenInfo struct + CreatedAt time.Time + Expired bool + ExpiresAt *time.Time + ID int64 + LastUsed *time.Time + Name string + Permissions []string + Prefix string + type TokenManager struct + func NewTokenManager(db *sql.DB) *TokenManager + func (tm *TokenManager) CleanupExpired(ctx context.Context) (int64, error) + func (tm *TokenManager) Create(ctx context.Context, userID int64, req CreateTokenRequest) (*CreateTokenResponse, error) + func (tm *TokenManager) List(ctx context.Context, userID int64) ([]UserToken, error) + func (tm *TokenManager) Revoke(ctx context.Context, userID, tokenID int64) error + func (tm *TokenManager) RevokeAll(ctx context.Context, userID int64) (int64, error) + func (tm *TokenManager) Validate(ctx context.Context, token string) (*User, *UserToken, error) + type User struct + Active bool + AvatarURL string + Bio string + CreatedAt time.Time + DisplayName string + Email string + EmailVerified bool + ID int64 + LastLogin *time.Time + NotificationEmail string + NotificationEmailVerified bool + PasswordHash string + Role string + UpdatedAt time.Time + Username string + func NewUser(username, email, password string, minPasswordLength int) (*User, error) + func (u *User) CanLogin() bool + func (u *User) ClearNotificationEmail() + func (u *User) GetAccountEmail() string + func (u *User) GetEmailForType(emailType string) string + func (u *User) GetEmailInfo() EmailInfo + func (u *User) GetNotificationEmail() string + func (u *User) HasSeparateNotificationEmail() bool + func (u *User) IsAdmin() bool + func (u *User) IsModerator() bool + func (u *User) SetNotificationEmail(email string) error + func (u *User) ToPublicProfile() PublicProfile + func (u *User) VerifyNotificationEmail() + type User2FA struct + CreatedAt time.Time + Enabled bool + EnabledAt *time.Time + ID int64 + SecretEncrypted string + UserID int64 + Verified bool + type UserEmail struct + CreatedAt time.Time + Email string + ID string + IsNotification bool + IsPrimary bool + UserID string + VerificationExpires *time.Time + VerificationToken string + Verified bool + VerifiedAt *time.Time + func (ue *UserEmail) ToInfo() UserEmailInfo + type UserEmailInfo struct + CreatedAt time.Time + Email string + ID string + IsNotification bool + IsPrimary bool + MaskedEmail string + Verified bool + VerifiedAt *time.Time + type UserPreferences struct + AnonymizeResults bool + AutocompleteOn bool + DefaultCategory string + DefaultLanguage string + DefaultRegion string + DefaultSort string + DisabledEngines []string + EnabledEngines []string + HighContrast bool + InfiniteScroll bool + LargeFont bool + OpenInNewTab bool + ReduceMotion bool + ResultsPerPage int + SafeSearch int + SaveSearchHistory bool + ShowEngineIcons bool + ShowThumbnails bool + Theme string + func DefaultPreferences() *UserPreferences + func FromJSON(data []byte) (*UserPreferences, error) + func ParsePreferenceString(s string) *UserPreferences + func (prefs *UserPreferences) GetShareableURL(baseURL string) string + func (prefs *UserPreferences) Merge(updates *UserPreferences) + func (prefs *UserPreferences) ToJSON() ([]byte, error) + func (prefs *UserPreferences) ToPreferenceString() string + func (prefs *UserPreferences) Validate() error + type UserSession struct + CreatedAt time.Time + DeviceName string + ExpiresAt time.Time + ID int64 + IPAddress string + LastUsed time.Time + Token string + UserAgent string + UserID int64 + type UserToken struct + CreatedAt time.Time + ExpiresAt *time.Time + ID int64 + LastUsed *time.Time + Name string + Permissions string + TokenHash string + TokenPrefix string + UserID int64 + func (ut *UserToken) GetPermissions() []string + func (ut *UserToken) HasPermission(permission string) bool + func (ut *UserToken) IsExpired() bool + func (ut *UserToken) ToInfo() TokenInfo + type VerificationManager struct + func NewVerificationManager(db *sql.DB) *VerificationManager + func (vm *VerificationManager) CleanupExpired(ctx context.Context) (int64, error) + func (vm *VerificationManager) ConsumePasswordReset(ctx context.Context, token string) (*User, error) + func (vm *VerificationManager) CreateEmailVerification(ctx context.Context, userID int64) (string, error) + func (vm *VerificationManager) CreatePasswordReset(ctx context.Context, userID int64) (string, error) + func (vm *VerificationManager) GetPendingVerification(ctx context.Context, userID int64) (*VerificationToken, error) + func (vm *VerificationManager) ValidatePasswordReset(ctx context.Context, token string) (*User, error) + func (vm *VerificationManager) VerifyEmail(ctx context.Context, token string) (*User, error) + type VerificationToken struct + CreatedAt time.Time + ExpiresAt time.Time + ID int64 + Token string + Type string + UserID int64 + type WebAuthnAuthenticator struct + AAGUID []byte + CloneWarning bool + SignCount uint32 + type WebAuthnCredential struct + AttestationType string + Authenticator WebAuthnAuthenticator + Flags WebAuthnCredentialFlags + ID []byte + PublicKey []byte + Transport []string + type WebAuthnCredentialFlags struct + BackupEligible bool + BackupState bool + UserPresent bool + UserVerified bool + type WebAuthnUser struct + Credentials []WebAuthnCredential + DisplayName string + ID []byte + Name string