auth

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: May 25, 2026 License: MIT Imports: 37 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrUnableToCreateDiscriminator      = "unable to create discriminator"
	ErrUnableToCreateUser               = "unable to create user"
	ErrUnableToGetUserById              = "unable to get user by ID"
	ErrUnableToRemoveRegistration       = "unable to remove registration"
	ErrDiscriminatorIsNotUnique         = "discriminator is not unique"
	ErrTokenIsIncorrect                 = "token is incorrect"
	ErrUnableToGetRegistrationById      = "unable to get registration by id"
	ErrUnableToGetRecoveryByUserId      = "unable to get recovery by user id"
	ErrUnableToCreateRegistration       = "unable to create registration"
	ErrUnableToGetRegistrationByEmail   = "unable to get registration by email"
	ErrUnableToGetPasswordHash          = "unable to get password hash"
	ErrPasswordIsTooShort               = "password is too short"
	ErrUnableToParseBody                = "unable to parse body"
	ErrUnableToCheckIsRefreshToken      = "unable to check is refresh token"
	ErrUnableToGetUserFromToken         = "unable to get user from token"
	ErrUnableToSendEmail                = "unable to send email"
	ErrEmailAlreadySent                 = "email already sent"
	ErrUnableToGenerateToken            = "unable to generate token"
	ErrUnableToGetAuthenticationByEmail = "unable to get authentication by email"
	ErrUnableToSignAuthenticationToken  = "unable to sign authentication token"
	ErrUnableToSignRefreshToken         = "unable to sign refresh token"
	ErrUserIsBanned                     = "user is banned"
	ErrUnableToCompareHash              = "unable to compare hash"
	ErrEmailNotFound                    = "email not found"
	ErrUnableToSetPasswordHash          = "unable to set password hash"
	ErrRecoveryEmailAlreadySent         = "recovery email already sent"
	ErrRecoveryExpired                  = "recovery is expired or invalid"

	// Validation error messages
	ErrNameRequired               = "name is required"
	ErrNameTooShort               = "name must be at least 4 characters"
	ErrNameTooLong                = "name must be less than 20 characters"
	ErrDiscriminatorRequired      = "discriminator is required"
	ErrDiscriminatorTooShort      = "discriminator must be at least 4 characters"
	ErrDiscriminatorTooLong       = "discriminator must be less than 20 characters"
	ErrDiscriminatorInvalidFormat = "discriminator can only contain lowercase letters, numbers, underscore, hyphen, and dot"
	ErrTokenRequired              = "token is required"
	ErrTokenInvalidLength         = "token must be exactly 40 characters"
	ErrIdRequired                 = "id is required"
	ErrIdInvalid                  = "id must be a positive number"
	ErrPasswordTooLong            = "password must be less than 50 characters"
	ErrEmailRequired              = "email is required"
	ErrEmailInvalidFormat         = "email format is invalid"
	ErrPasswordRequired           = "password is required"
)
View Source
const (
	ErrUnableToGetAuthenticationByUserId = "unable to get authentication by user id"
	ErrUnableToGetActiveFactor           = "unable to get active factor"
	ErrUnableToGetTOTPFactor             = "unable to get TOTP factor"
	ErrUnableToListRecoveryCodes         = "unable to list recovery codes"
	ErrUnableToReplaceRecoveryCodes      = "unable to replace recovery codes"
	ErrUnableToCreateFactor              = "unable to create factor"
	ErrUnableToDeleteFactor              = "unable to delete factor"
	ErrUnableToPersistSetup              = "unable to persist TOTP setup"
	ErrUnableToLoadSetup                 = "unable to load TOTP setup"
	ErrUnableToDeleteSetup               = "unable to delete TOTP setup"
	ErrUnableToCreateLoginChallenge      = "unable to create login challenge"
	ErrUnableToLoadLoginChallenge        = "unable to load login challenge"
	ErrUnableToDeleteLoginChallenge      = "unable to delete login challenge"
	ErrUnableToValidateSession           = "unable to validate session"
	ErrUnableToEncryptSecret             = "unable to encrypt secret"
	ErrUnableToDecryptSecret             = "unable to decrypt secret"
	ErrUnableToGenerateRecoveryCodes     = "unable to generate recovery codes"
	ErrUnableToGenerateEmailCode         = "unable to generate email code"
	ErrUnableToSendRecoveryCodeEmail     = "unable to send recovery code email"
	ErrUnableToConsumeRecoveryCode       = "unable to consume recovery code"
	ErrUnableToBumpSessionVersion        = "unable to bump session version"
	ErrTwoFactorAlreadyEnabled           = "two-factor auth is already enabled"
	ErrTwoFactorNotEnabled               = "two-factor auth is not enabled"
	ErrSetupExpired                      = "setup is expired or invalid"
	ErrLoginChallengeExpired             = "login challenge is expired or invalid"
	ErrInvalidTwoFactorCode              = "two-factor code is invalid"
	ErrInvalidRecoveryCode               = "recovery code is invalid"
	ErrTooManyAuthAttempts               = "too many authentication attempts"
	ErrTooManyRecoveryEmails             = "too many recovery emails"

	ErrCurrentPasswordRequired = "current password is required"
	ErrNewPasswordRequired     = "new password is required"
	ErrCodeRequired            = "code is required"
	ErrCodeTypeInvalid         = "code type must be totp or recovery_code"
	ErrChallengeIDRequired     = "challenge_id is required"
	ErrSetupIDRequired         = "setup_id is required"
	ErrOTPInvalidFormat        = "OTP must be exactly 6 digits"
)

Variables

This section is empty.

Functions

func CompareHashAndPassword

func CompareHashAndPassword(hashedPassword, password string) error

CompareHashAndPassword checks if the provided password matches the hashed password.

func HashPassword

func HashPassword(password string) (string, error)

HashPassword hashes the given password using bcrypt.

func New

func New(pg *pgdb.DB, cache authCache, m *mailer.Mailer, transporter mq.SendTransporter, appName, secret string, secretBox *helper.SecretBox, sessionChecker *helper.SessionVersionChecker, idGenerator func() int64, log *slog.Logger, accessMiddleware, refreshMiddleware fiber.Handler) server.Entity

Types

type ConfirmationRequest

type ConfirmationRequest struct {
	Id            int64  `json:"id" example:"2230469276416868352"`               // User ID
	Token         string `json:"token" example:"just_a_randomly_generated_text"` // Registration token
	Name          string `json:"name" example:"FancyUserName"`                   // User name
	Discriminator string `json:"discriminator" example:"uniquename"`             // Unique discriminator
	Password      string `json:"password" example:"VerYstR0NgP@66WoR6"`          // User password
}

func (*ConfirmationRequest) UnmarshalJSON added in v1.10.0

func (r *ConfirmationRequest) UnmarshalJSON(data []byte) error

func (ConfirmationRequest) Validate

func (r ConfirmationRequest) Validate() error

type DisableTwoFactorRequest added in v1.11.0

type DisableTwoFactorRequest struct {
	CurrentPassword string `json:"current_password" example:"VerYstR0NgP@66WoR6"`
	CodeType        string `json:"code_type" example:"totp"`
	Code            string `json:"code" example:"123456"`
}

func (DisableTwoFactorRequest) Validate added in v1.11.0

func (r DisableTwoFactorRequest) Validate() error

type LoginChallengeResponse added in v1.11.0

type LoginChallengeResponse struct {
	ChallengeID string    `json:"challenge_id" example:"e2ec6cc32bcc5b37d2d3b99d6f450c086f8b54c9"`
	Methods     []string  `json:"methods" example:"totp,recovery_code,email_recovery"`
	ExpiresAt   time.Time `json:"expires_at"`
}

type LoginEmailStartRequest added in v1.11.0

type LoginEmailStartRequest struct {
	ChallengeID string `json:"challenge_id" example:"e2ec6cc32bcc5b37d2d3b99d6f450c086f8b54c9"`
}

func (LoginEmailStartRequest) Validate added in v1.11.0

func (r LoginEmailStartRequest) Validate() error

type LoginEmailVerifyRequest added in v1.11.0

type LoginEmailVerifyRequest struct {
	ChallengeID string `json:"challenge_id" example:"e2ec6cc32bcc5b37d2d3b99d6f450c086f8b54c9"`
	Code        string `json:"code" example:"123456"`
}

func (LoginEmailVerifyRequest) Validate added in v1.11.0

func (r LoginEmailVerifyRequest) Validate() error

type LoginRecoveryCodeRequest added in v1.11.0

type LoginRecoveryCodeRequest struct {
	ChallengeID string `json:"challenge_id" example:"e2ec6cc32bcc5b37d2d3b99d6f450c086f8b54c9"`
	Code        string `json:"code" example:"AB12CD34EF56"`
}

func (LoginRecoveryCodeRequest) Validate added in v1.11.0

func (r LoginRecoveryCodeRequest) Validate() error

type LoginRequest

type LoginRequest struct {
	Email    string `json:"email" example:"user@example.com"`      // User Email
	Password string `json:"password" example:"VerYstR0NgP@66WoR6"` // User password
}

func (LoginRequest) Validate

func (r LoginRequest) Validate() error

type LoginResponse

type LoginResponse struct {
	Token        string `json:"token"`         // Authentication token
	RefreshToken string `json:"refresh_token"` // Refresh token. Used to refresh authentication token.
}

type LoginTOTPRequest added in v1.11.0

type LoginTOTPRequest struct {
	ChallengeID string `json:"challenge_id" example:"e2ec6cc32bcc5b37d2d3b99d6f450c086f8b54c9"`
	Code        string `json:"code" example:"123456"`
}

func (LoginTOTPRequest) Validate added in v1.11.0

func (r LoginTOTPRequest) Validate() error

type PasswordChangeRequest added in v1.11.0

type PasswordChangeRequest struct {
	CurrentPassword string `json:"current_password" example:"OldP@ssw0rd123"`
	NewPassword     string `json:"new_password" example:"NewP@ssw0rd123"`
	CodeType        string `json:"code_type,omitempty" example:"totp"`
	Code            string `json:"code,omitempty" example:"123456"`
}

func (PasswordChangeRequest) Validate added in v1.11.0

func (r PasswordChangeRequest) Validate() error

type PasswordRecoveryRequest

type PasswordRecoveryRequest struct {
	Email string `json:"email" example:"user@example.com"` // User Email to receive recovery email
}

func (PasswordRecoveryRequest) Validate

func (r PasswordRecoveryRequest) Validate() error

type PasswordResetRequest

type PasswordResetRequest struct {
	Id       int64  `json:"id" example:"2230469276416868352"`              // User ID
	Token    string `json:"token" example:"just_a_random_text_from_email"` // Reset token
	Password string `json:"password" example:"N3wVerYstR0NgP@66WoR6"`      // New password
}

func (*PasswordResetRequest) UnmarshalJSON added in v1.10.0

func (r *PasswordResetRequest) UnmarshalJSON(data []byte) error

func (PasswordResetRequest) Validate

func (r PasswordResetRequest) Validate() error

type RecoveryCodesRegenerateRequest added in v1.11.0

type RecoveryCodesRegenerateRequest struct {
	CurrentPassword string `json:"current_password" example:"VerYstR0NgP@66WoR6"`
	CodeType        string `json:"code_type" example:"totp"`
	Code            string `json:"code" example:"123456"`
}

func (RecoveryCodesRegenerateRequest) Validate added in v1.11.0

type RecoveryCodesResponse added in v1.11.0

type RecoveryCodesResponse struct {
	Token         string   `json:"token" example:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"`
	RefreshToken  string   `json:"refresh_token" example:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"`
	RecoveryCodes []string `json:"recovery_codes" example:"AB12CD34EF56,GH78JK90LM12"`
}

type RefreshTokenResponse

type RefreshTokenResponse struct {
	Token        string `json:"token"`         // Authentication token
	RefreshToken string `json:"refresh_token"` // Refresh token. Used to refresh authentication token.
}

type RegisterRequest

type RegisterRequest struct {
	Email string `json:"email" example:"user@example.com"` // User Email
}

func (RegisterRequest) Validate

func (r RegisterRequest) Validate() error

type TOTPConfirmRequest added in v1.11.0

type TOTPConfirmRequest struct {
	SetupID string `json:"setup_id" example:"b7af1b0bf9f0d78d913cb1fa746785a86f98b1ad"`
	Code    string `json:"code" example:"123456"`
}

func (TOTPConfirmRequest) Validate added in v1.11.0

func (r TOTPConfirmRequest) Validate() error

type TOTPSetupRequest added in v1.11.0

type TOTPSetupRequest struct {
	CurrentPassword string `json:"current_password" example:"VerYstR0NgP@66WoR6"`
}

func (TOTPSetupRequest) Validate added in v1.11.0

func (r TOTPSetupRequest) Validate() error

type TOTPSetupResponse added in v1.11.0

type TOTPSetupResponse struct {
	SetupID     string    `json:"setup_id" example:"b7af1b0bf9f0d78d913cb1fa746785a86f98b1ad"`
	OtpauthURI  string    `` /* 139-byte string literal not displayed */
	ManualKey   string    `json:"manual_key" example:"JBSWY3DPEHPK3PXP"`
	Issuer      string    `json:"issuer" example:"GoChat"`
	AccountName string    `json:"account_name" example:"user@example.com"`
	ExpiresAt   time.Time `json:"expires_at"`
}

type TwoFactorStatusResponse added in v1.11.0

type TwoFactorStatusResponse struct {
	Enabled                bool    `json:"enabled" example:"true"`
	FactorType             *string `json:"factor_type,omitempty" example:"totp"`
	RecoveryCodesRemaining int     `json:"recovery_codes_remaining" example:"10"`
}

Jump to

Keyboard shortcuts

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