auth

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAccountLocked       = errors.New("account locked due to too many failed login attempts")
	ErrEmailNotVerified    = errors.New("email address not verified")
	ErrAccountSuspended    = errors.New("account has been suspended")
	ErrAccountDeleted      = errors.New("account has been deleted")
	ErrInvalidCredentials  = errors.New("invalid username or password")
	MaxFailedLoginAttempts = 3
	AccountLockoutDuration = 15 * time.Minute
)
View Source
var (
	// ErrResetTokenInvalid is returned when password reset token is invalid
	ErrResetTokenInvalid = errors.New("invalid password reset token")

	// ErrResetTokenExpired is returned when password reset token has expired
	ErrResetTokenExpired = errors.New("password reset token has expired")

	// ErrResetFailed is returned when password reset fails
	ErrResetFailed = errors.New("password reset failed")
)
View Source
var (
	// ErrUsernameInvalid is returned when username format is invalid
	ErrUsernameInvalid = errors.New("username must be 1-50 characters and contain only letters, numbers, underscores, and hyphens")

	// ErrUsernameExists is returned when username already exists
	ErrUsernameExists = errors.New("username already exists")

	// ErrEmailExists is returned when email already exists
	ErrEmailExists = errors.New("email address already registered")

	// ErrRegistrationFailed is returned when registration fails
	ErrRegistrationFailed = errors.New("registration failed")
)
View Source
var (
	// ErrTokenInvalid is returned when verification token is invalid
	ErrTokenInvalid = errors.New("invalid verification token")

	// ErrTokenExpired is returned when verification token has expired
	ErrTokenExpired = errors.New("verification token has expired")

	// ErrVerificationFailed is returned when email verification fails
	ErrVerificationFailed = errors.New("email verification failed")
)

Functions

func ValidateUsername

func ValidateUsername(username string) error

ValidateUsername checks if a username has a valid format

Types

type AuthService

type AuthService struct {
	// contains filtered or unexported fields
}

AuthService handles authentication business logic

func NewAuthService

func NewAuthService(passwordHash string) *AuthService

NewAuthService creates a new auth service with default admin credentials

func (*AuthService) Login

func (a *AuthService) Login(username, password string) (*UserInfo, error)

Login validates credentials and returns user info if valid

type CreateTokenResult

type CreateTokenResult struct {
	Token  string
	UserID int
}

CreateTokenResult contains the result of token creation

type FirstLoginService

type FirstLoginService struct {
	// contains filtered or unexported fields
}

FirstLoginService manages first-login setup state by deriving it from the database. Setup is considered complete when the admin password differs from the default.

func NewFirstLoginService

func NewFirstLoginService(defaultPasswordHash string) *FirstLoginService

NewFirstLoginService creates a new first-login service

func (*FirstLoginService) IsSetupComplete

func (f *FirstLoginService) IsSetupComplete(currentAdminHash string) bool

IsSetupComplete returns whether first-login setup is complete by comparing the current admin password hash against the default password hash.

type LoginRequest

type LoginRequest struct {
	Username string
	Password string
}

LoginRequest represents a login request

type LoginResult

type LoginResult struct {
	UserID   string
	Username string
	Email    string
	Role     string
}

LoginResult represents the result of a successful login

type LoginService

type LoginService struct {
	// contains filtered or unexported fields
}

LoginService handles login business logic with account lockout

func NewLoginService

func NewLoginService(
	userRepo repository.UserRepo,
	failedLoginRepo repository.FailedLoginAttemptRepo,
	logger *util.Logger,
) *LoginService

NewLoginService creates a new login service

func (*LoginService) GetFailedLoginAttempts

func (s *LoginService) GetFailedLoginAttempts(ctx context.Context, username string) (int, error)

GetFailedLoginAttempts returns the number of failed login attempts for a user

func (*LoginService) IsAccountLocked

func (s *LoginService) IsAccountLocked(ctx context.Context, username string) (bool, *time.Time, error)

IsAccountLocked checks if an account is currently locked

func (*LoginService) Login

func (s *LoginService) Login(ctx context.Context, req LoginRequest) (*LoginResult, error)

Login performs login with account lockout mechanism

type PasswordResetResult

type PasswordResetResult struct {
	Success bool
	Message string
	UserID  int
}

PasswordResetResult contains the result of a successful password reset

type PasswordResetService

type PasswordResetService struct {
	// contains filtered or unexported fields
}

PasswordResetService handles password reset business logic

func NewPasswordResetService

func NewPasswordResetService(
	userRepo repository.UserRepo,
	tokenRepo repository.PasswordResetTokenRepo,
	expirationHours int,
) *PasswordResetService

NewPasswordResetService creates a new password reset service

func (*PasswordResetService) RequestPasswordReset

func (s *PasswordResetService) RequestPasswordReset(ctx context.Context, email string) (*RequestPasswordResetResult, error)

RequestPasswordReset creates a password reset token for a user

func (*PasswordResetService) ResetPassword

func (s *PasswordResetService) ResetPassword(ctx context.Context, token, newPassword string) (*PasswordResetResult, error)

ResetPassword verifies the token and updates the user's password

type RegisterRequest

type RegisterRequest struct {
	Username string `json:"username"`
	Name     string `json:"name"`
	Email    string `json:"email"`
	Password string `json:"password"`
}

RegisterRequest represents a user registration request

type RegisterResult

type RegisterResult struct {
	UserID  int
	Message string
}

RegisterResult contains the result of a successful registration

type RegistrationService

type RegistrationService struct {
	// contains filtered or unexported fields
}

RegistrationService handles user registration business logic

func NewRegistrationService

func NewRegistrationService(userRepo repository.UserRepo) *RegistrationService

NewRegistrationService creates a new registration service

func (*RegistrationService) RegisterUser

RegisterUser registers a new user with validation

type RequestPasswordResetResult

type RequestPasswordResetResult struct {
	Token  string
	UserID int
	Email  string
	Name   string
}

RequestPasswordResetResult contains the result of a password reset request

type UserInfo

type UserInfo struct {
	ID       string
	Username string
	Role     string
}

UserInfo contains user information for JWT token generation

func (*UserInfo) String

func (u *UserInfo) String() string

String returns a string representation of user info

type VerificationResult

type VerificationResult struct {
	Success bool
	Message string
	UserID  int
}

VerificationResult contains the result of a successful verification

type VerificationService

type VerificationService struct {
	// contains filtered or unexported fields
}

VerificationService handles email verification business logic

func NewVerificationService

func NewVerificationService(
	userRepo repository.UserRepo,
	tokenRepo repository.VerificationTokenRepo,
	expirationHours int,
) *VerificationService

NewVerificationService creates a new verification service

func (*VerificationService) CleanupExpiredTokens

func (s *VerificationService) CleanupExpiredTokens(ctx context.Context) error

CleanupExpiredTokens removes expired tokens from the database

func (*VerificationService) CreateVerificationToken

func (s *VerificationService) CreateVerificationToken(ctx context.Context, userID int) (*CreateTokenResult, error)

CreateVerificationToken creates a new verification token for a user

func (*VerificationService) VerifyEmail

func (s *VerificationService) VerifyEmail(ctx context.Context, token string) (*VerificationResult, error)

VerifyEmail verifies a user's email using a token

Jump to

Keyboard shortcuts

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