shieldpassword

package
v0.0.0-...-f9b482e Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package password implements a user registration and login flows with password.

Index

Constants

View Source
const (
	DefaultFieldNameFirstName = "first_name"
	DefaultFieldNameLastName  = "last_name"
	DefaultFieldNameEmail     = "email"
	DefaultFieldNamePassword  = "password"
)
View Source
const (
	BcryptDefaultCost = bcrypt.DefaultCost
)

Variables

View Source
var (
	FormValidator = validator.New(validator.WithRequiredStructEnabled())
	FormScrubber  = scrubbers.New()
	FormModifier  = modifiers.New()
)
View Source
var (
	ErrEmailAlreadyTaken = fmt.Errorf("shield/password: email already taken")
	ErrPasswordIncorrect = fmt.Errorf("shield/password: password incorrect")
)

DefaultPasswordHasher is the default password hashing algorithm used across.

Functions

func WithHijacker

func WithHijacker[T any](hijacker Hijacker[T]) func(*Config[T])

func WithPasswordHasher

func WithPasswordHasher[T any](hasher PasswordHasher) func(*Config[T])

WithPasswordHasher configures the password hasher.

When setting a password hasher make sure to set it across all modules, i.e., user registration, password reset and password verification.

Types

type Config

type Config[T any] struct {
	Logger           *slog.Logger
	PasswordHasher   PasswordHasher
	PasswordVerifier shieldpasswordverifier.PasswordVerifier
	Hijacker         Hijacker[T]
}

Config is the configuration for the password handler.

func NewConfig

func NewConfig[T any](opts ...func(*Config[T])) *Config[T]

NewConfig creates a new config.

If no password hasher is configured, the DefaultPasswordHasher will be used.

type FormConfig

type FormConfig[T any] struct {
	*Config[T]

	FirstNameFieldName string // optional (default: DefaultFieldNameFirstName)
	LastNameFieldName  string // optional (default: DefaultFieldNameLastName)
	EmailFieldName     string // optional (default: DefaultFieldNameEmail)
	PasswordFieldName  string // optional (default: DefaultFieldNamePassword)
}

func NewFormConfig

func NewFormConfig[T any](opts ...func(*FormConfig[T])) *FormConfig[T]

NewFormConfig[T] creates a new FormConfig[T] with the given configuration options.

type FormHandler

type FormHandler[T any] struct {
	// contains filtered or unexported fields
}

FormHandler[T] is a wrapper around Handler handling HTTP form requests.

func NewFormHandler

func NewFormHandler[T any](pool *pgxpool.Pool, config *FormConfig[T]) *FormHandler[T]

NewFormHandler[T] creates a new FormHandler[T] with the given configuration.

If config is nil, the default config is used.

func (*FormHandler[T]) HandleUserLogin

func (h *FormHandler[T]) HandleUserLogin(r *http.Request) (*shield.User[T], error)

HandleUserLogin handles a user login request.

func (*FormHandler[T]) HandleUserRegistration

func (h *FormHandler[T]) HandleUserRegistration(r *http.Request) (*shield.User[T], error)

HandleUserRegistration handles a user registration request.

type Handler

type Handler[T any] struct {
	// contains filtered or unexported fields
}

func NewHandler

func NewHandler[T any](pool *pgxpool.Pool, config *Config[T]) *Handler[T]

func (*Handler[T]) HandleUserLogin

func (h *Handler[T]) HandleUserLogin(
	ctx context.Context,
	email, password string,
) (*shield.User[T], error)

func (*Handler[T]) HandleUserRegistration

func (h *Handler[T]) HandleUserRegistration(
	ctx context.Context,
	email, password string,
) (*shield.User[T], error)

type Hijacker

type Hijacker[T any] interface {
	// HijackUserRegisteration is called when registring a new user.
	// Use this method to create an additional context for the user.
	HijackUserRegisteration(context.Context, uuid.UUID, pgx.Tx) (T, error)

	// HijackUserLogin is called when a user is trying to login.
	// Use this method to fetch additional data from the database for the user.
	//
	// Note that the user password is not verified at this moment yet.
	HijackUserLogin(context.Context, uuid.UUID, pgx.Tx) (T, error)
}

Hijacker also to hijack into the user registration and logging in sessions and perform additional operations.

type PasswordHasher

type PasswordHasher interface {
	Hash(password string) (string, error)
	Verify(hashedPassword string, password string) (bool, error)
}

PasswordHasher is a hashing algorithm to hash password securely.

func NewBcryptPasswordHasher

func NewBcryptPasswordHasher(cost int) PasswordHasher

NewBcryptPasswordHasher implements a password hashing algorithm with bcrypt.

Jump to

Keyboard shortcuts

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