user

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: Apache-2.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUserNotFound       = errors.New("user not found")
	ErrEmailAlreadyExists = errors.New("email already registered")
	ErrInvalidCredentials = errors.New("invalid email or password")
	ErrInvalidToken       = errors.New("invalid or expired token")
	ErrInternalServer     = errors.New("internal server error")
)
View Source
var (
	ErrBiometricNotFound      = errors.New("biometric credential not found")
	ErrInvalidBiometricParams = errors.New("invalid biometric parameters")
	ErrBiometricVerification  = errors.New("biometric verification failed")
	ErrBiometricOwnership     = errors.New("biometric credential does not belong to user")
)

Common errors for biometric operations.

Functions

This section is empty.

Types

type AuthService

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

AuthService handles business logic for user authentication.

func NewAuthService

func NewAuthService(
	userRepo UserRepository,
	tokenService TokenService,
	emailService EmailService,
	idGen IDGenerator,
) *AuthService

NewAuthService creates a new instance of AuthService.

func (*AuthService) ForgotPassword

func (s *AuthService) ForgotPassword(ctx context.Context, email string) error

ForgotPassword initiates the password reset flow by sending an email with a reset token.

func (*AuthService) Login

func (s *AuthService) Login(ctx context.Context, req LoginRequest) (*LoginResponse, error)

Login authenticates a user and returns JWT tokens.

func (*AuthService) Register

Register creates a new user account, hashes the password, and sends a welcome email.

func (*AuthService) ResetPassword

func (s *AuthService) ResetPassword(ctx context.Context, req ResetPasswordRequest) error

ResetPassword validates the token and updates the user's password.

type BiometricCredential

type BiometricCredential struct {
	ID          string        `json:"id"`
	UserID      string        `json:"user_id"`
	Type        BiometricType `json:"type"`
	Label       string        `json:"label"`       // User-friendly name (e.g., "John's iPhone")
	PublicKey   []byte        `json:"public_key"`  // Raw public key bytes
	Attestation []byte        `json:"attestation"` // Attestation object if available
	DeviceID    string        `json:"device_id"`
	Counter     uint32        `json:"counter"` // Sign count for replay protection
	CreatedAt   time.Time     `json:"created_at"`
	LastUsedAt  time.Time     `json:"last_used_at"`
}

BiometricCredential represents the stored metadata and public key for a user's biometric authenticator.

type BiometricRepository

type BiometricRepository interface {
	Create(ctx context.Context, cred *BiometricCredential) error
	GetByID(ctx context.Context, id string) (*BiometricCredential, error)
	GetByUserID(ctx context.Context, userID string) ([]*BiometricCredential, error)
	Delete(ctx context.Context, id string) error
	UpdateCounterAndUsage(ctx context.Context, id string, newCounter uint32, lastUsed time.Time) error
}

BiometricRepository defines the data access layer requirements for biometric credentials.

type BiometricService

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

BiometricService manages the lifecycle of user biometric credentials.

func NewBiometricService

func NewBiometricService(repo BiometricRepository, verifier SignatureVerifier, logger *slog.Logger) *BiometricService

NewBiometricService creates a new instance of BiometricService.

func (*BiometricService) Deregister

func (s *BiometricService) Deregister(ctx context.Context, userID string, credentialID string) error

Deregister removes a specific biometric credential. It enforces ownership checks to ensure a user can only delete their own credentials.

func (*BiometricService) Enroll

Enroll registers a new biometric credential for a user.

func (*BiometricService) ListCredentials

func (s *BiometricService) ListCredentials(ctx context.Context, userID string) ([]*BiometricCredential, error)

ListCredentials retrieves all enrolled biometrics for a specific user.

func (*BiometricService) VerifyAuthenticate

func (s *BiometricService) VerifyAuthenticate(ctx context.Context, params VerifyParams) (bool, error)

VerifyAuthenticate checks if the provided signature matches the stored credential. It updates the usage timestamp and counter upon success.

type BiometricType

type BiometricType string

BiometricType defines the category of biometric data.

const (
	BiometricTypeFingerprint BiometricType = "FINGERPRINT"
	BiometricTypeFaceID      BiometricType = "FACE_ID"
	BiometricTypeIris        BiometricType = "IRIS"
	BiometricTypeFIDO2       BiometricType = "FIDO2"
)

type EmailService

type EmailService interface {
	SendWelcomeEmail(ctx context.Context, toEmail, name string) error
	SendPasswordResetEmail(ctx context.Context, toEmail, token string) error
}

EmailService defines operations for sending notifications.

type EnrollParams

type EnrollParams struct {
	UserID      string
	Type        BiometricType
	Label       string
	PublicKey   []byte
	Attestation []byte
	DeviceID    string
}

EnrollParams contains the necessary data to register a new biometric credential.

type IDGenerator

type IDGenerator interface {
	NewID() string
}

IDGenerator defines a strategy for generating unique identifiers.

type LoginRequest

type LoginRequest struct {
	Email    string
	Password string
}

type LoginResponse

type LoginResponse struct {
	AccessToken  string
	RefreshToken string
	ExpiresIn    int64 // Seconds
	TokenType    string
}

type RegisterRequest

type RegisterRequest struct {
	FullName string
	Email    string
	Password string
}

type RegisterResponse

type RegisterResponse struct {
	ID        string
	Email     string
	CreatedAt time.Time
}

type ResetPasswordRequest

type ResetPasswordRequest struct {
	Token       string
	NewPassword string
}

type SignatureVerifier

type SignatureVerifier interface {
	// Verify checks if the signature is valid for the given data and public key.
	Verify(ctx context.Context, pubKey []byte, data []byte, signature []byte) (bool, error)
}

SignatureVerifier defines the strategy for verifying cryptographic signatures. This allows injecting specific logic for WebAuthn, TPM, or raw key verification.

type TokenService

type TokenService interface {
	GenerateTokenPair(userID string, email string) (accessToken, refreshToken string, exp int64, err error)
	ValidateResetToken(token string) (string, error) // Returns email associated with token
	GenerateResetToken(email string) (string, error)
}

TokenService defines operations for JWT handling.

type User

type User struct {
	ID           string
	FullName     string
	Email        string
	PasswordHash string
	CreatedAt    time.Time
	UpdatedAt    time.Time
}

User represents the user entity.

type UserRepository

type UserRepository interface {
	Create(ctx context.Context, user *User) error
	FindByEmail(ctx context.Context, email string) (*User, error)
	FindByID(ctx context.Context, id string) (*User, error)
	UpdatePassword(ctx context.Context, userID, newHash string) error
	ExistsByEmail(ctx context.Context, email string) (bool, error)
}

UserRepository defines the data access layer for users.

type VerifyParams

type VerifyParams struct {
	CredentialID string
	Challenge    []byte // The original challenge sent to the client
	Signature    []byte // The signature returned by the client
	ClientData   []byte // Additional client data (e.g., collected client data in WebAuthn)
}

VerifyParams contains the data required to verify a biometric assertion.

Jump to

Keyboard shortcuts

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