Documentation
¶
Index ¶
- Variables
- type AuthService
- func (s *AuthService) ForgotPassword(ctx context.Context, email string) error
- func (s *AuthService) Login(ctx context.Context, req LoginRequest) (*LoginResponse, error)
- func (s *AuthService) Register(ctx context.Context, req RegisterRequest) (*RegisterResponse, error)
- func (s *AuthService) ResetPassword(ctx context.Context, req ResetPasswordRequest) error
- type BiometricCredential
- type BiometricRepository
- type BiometricService
- func (s *BiometricService) Deregister(ctx context.Context, userID string, credentialID string) error
- func (s *BiometricService) Enroll(ctx context.Context, params EnrollParams) (*BiometricCredential, error)
- func (s *BiometricService) ListCredentials(ctx context.Context, userID string) ([]*BiometricCredential, error)
- func (s *BiometricService) VerifyAuthenticate(ctx context.Context, params VerifyParams) (bool, error)
- type BiometricType
- type EmailService
- type EnrollParams
- type IDGenerator
- type LoginRequest
- type LoginResponse
- type RegisterRequest
- type RegisterResponse
- type ResetPasswordRequest
- type SignatureVerifier
- type TokenService
- type User
- type UserRepository
- type VerifyParams
Constants ¶
This section is empty.
Variables ¶
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") )
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 ¶
func (s *AuthService) Register(ctx context.Context, req RegisterRequest) (*RegisterResponse, error)
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 ¶
func (s *BiometricService) Enroll(ctx context.Context, params EnrollParams) (*BiometricCredential, error)
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 LoginResponse ¶
type RegisterRequest ¶
type ResetPasswordRequest ¶
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.