emailpassword

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: Apache-2.0 Imports: 7 Imported by: ? 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

Overview

Package emailpassword orchestrates email and password authentication.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAttemptBlocked          = errors.New("authentication attempt blocked")
	ErrConflict                = errors.New("email/password conflict")
	ErrInvalidConfig           = errors.New("invalid email/password configuration")
	ErrInvalidCredentials      = errors.New("invalid email or password")
	ErrInvalidInput            = errors.New("invalid email/password input")
	ErrInvalidPassword         = errors.New("invalid password")
	ErrInvalidRecord           = errors.New("invalid email/password record")
	ErrLastCredential          = errors.New("cannot remove the last sign-in method")
	ErrNotFound                = errors.New("email identity not found")
	ErrPasswordUnchanged       = errors.New("new password matches current password")
	ErrRegistrationUnavailable = errors.New("registration unavailable")
)

Functions

func NormalizeEmail

func NormalizeEmail(rawEmail string) (string, error)

Types

type AddPasswordInput

type AddPasswordInput struct {
	SubjectID string
	Password  string
	SourceKey string
}

type Attempt

type Attempt struct {
	Operation Operation
	Email     string
	SubjectID string
	SourceKey string
}

type AttemptGuard

type AttemptGuard interface {
	Check(ctx context.Context, attempt Attempt) error
}

type ChangePasswordInput

type ChangePasswordInput struct {
	SubjectID       string
	CurrentPassword string
	NewPassword     string
	SourceKey       string
}

type Config

type Config struct {
	Passwords        Passwords
	Credentials      CredentialStore
	ValidatePassword PasswordValidator
	AttemptGuard     AttemptGuard
	SecurityEvents   SecurityEventSink
	NormalizeEmail   EmailNormalizer
	Now              func() time.Time
}

type CredentialStore

type CredentialStore interface {
	FindBySubject(ctx context.Context, subjectID string) (User, PasswordCredential, error)
	AddPassword(ctx context.Context, subjectID, passwordHash string, addedAt time.Time) (User, error)
	ReplacePasswordHash(
		ctx context.Context,
		userID string,
		currentHash string,
		replacementHash string,
		updatedAt time.Time,
	) error
	RemovePassword(ctx context.Context, subjectID, currentHash string, removedAt time.Time) error
}

CredentialStore makes account credential writes atomic and preserves another sign-in method on removal.

type EmailNormalizer

type EmailNormalizer = emailaddress.Normalizer

type EventType

type EventType string
const (
	EventRegistrationRejected  EventType = "registration_rejected"
	EventRegistrationSucceeded EventType = "registration_succeeded"
	EventLoginFailed           EventType = "login_failed"
	EventLoginSucceeded        EventType = "login_succeeded"
	EventPasswordAdded         EventType = "password_added"
	EventPasswordChangeFailed  EventType = "password_change_failed"
	EventPasswordChanged       EventType = "password_changed"
	EventPasswordRemovalFailed EventType = "password_removal_failed"
	EventPasswordRemoved       EventType = "password_removed"
	EventPasswordRehashed      EventType = "password_rehashed"
)

type LoginInput

type LoginInput struct {
	Email     string
	Password  string
	SourceKey string
}

type LoginResult

type LoginResult struct {
	User             User
	PasswordRehashed bool
}

type Manager

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

func NewManager

func NewManager(store Store, config Config) (*Manager, error)

func (*Manager) AddPassword

func (manager *Manager) AddPassword(ctx context.Context, input AddPasswordInput) (User, error)

func (*Manager) ChangePassword

func (manager *Manager) ChangePassword(
	ctx context.Context,
	input ChangePasswordInput,
) (User, error)

func (*Manager) Login

func (manager *Manager) Login(ctx context.Context, input LoginInput) (LoginResult, error)

Login returns the same credential error for unknown emails and wrong passwords.

func (*Manager) Register

func (manager *Manager) Register(ctx context.Context, input RegisterInput) (User, error)

Register hashes before checking durable identity uniqueness.

func (*Manager) RemovePassword

func (manager *Manager) RemovePassword(ctx context.Context, input RemovePasswordInput) error

type Operation

type Operation string
const (
	OperationRegister       Operation = "register"
	OperationLogin          Operation = "login"
	OperationAddPassword    Operation = "add_password"
	OperationChangePassword Operation = "change_password"
	OperationRemovePassword Operation = "remove_password"
)

type PasswordCredential

type PasswordCredential struct {
	UserID       string
	PasswordHash string
}

type PasswordValidator

type PasswordValidator func(plainPassword string) error

type Passwords

type Passwords interface {
	Hash(plainPassword string) (string, error)
	Verify(plainPassword, encodedHash string) (passwordhash.Verification, error)
}

type RegisterInput

type RegisterInput struct {
	Email     string
	Password  string
	SourceKey string
}

type Registration

type Registration struct {
	Email        string
	PasswordHash string
	CreatedAt    time.Time
}

type RemovePasswordInput

type RemovePasswordInput struct {
	SubjectID       string
	CurrentPassword string
	SourceKey       string
}

type SecurityEvent

type SecurityEvent struct {
	Type       EventType
	SubjectID  string
	SourceKey  string
	OccurredAt time.Time
}

type SecurityEventSink

type SecurityEventSink interface {
	Record(ctx context.Context, event SecurityEvent)
}

type Store

type Store interface {
	Register(ctx context.Context, registration Registration) (User, error)
	FindByEmail(ctx context.Context, normalizedEmail string) (User, PasswordCredential, error)
	ReplacePasswordHash(
		ctx context.Context,
		userID string,
		currentHash string,
		replacementHash string,
		updatedAt time.Time,
	) error
}

type User

type User struct {
	ID    string
	Email string
}

Jump to

Keyboard shortcuts

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