Documentation
¶
Overview ¶
Package password implements a user registration and login flows with password.
Index ¶
- Constants
- Variables
- func WithHooker[U any](hooker Hooker[U]) func(*Config[U])
- func WithMinLength(minLength int) func(*PasswordStrengthCheckerConfig)
- func WithPasswordHasher[U any](hasher PasswordHasher) func(*Config[U])
- func WithRequiredChars(requiredChars PasswordRequiredChars) func(*PasswordStrengthCheckerConfig)
- type Config
- type Handler
- func (h *Handler[_, S]) HandleChangeUserPassword(ctx context.Context, oldPassword, newPassword string) error
- func (h *Handler[U, _]) HandleUserLogin(ctx context.Context, email, password string) (shield.User[U], error)
- func (h *Handler[U, _]) HandleUserRegistration(ctx context.Context, email, password string) (shield.User[U], error)
- type Hooker
- type PasswordChecker
- type PasswordHasher
- type PasswordRequiredChars
- type PasswordStrengthCheckerConfig
- type PwndPasswordCheckerConfig
Constants ¶
const (
BcryptDefaultCost = bcrypt.DefaultCost
)
Variables ¶
var ( ErrUserExists = errors.New( "shieldpassword: user already exists", ) ErrPasswordIncorrect = errors.New("shieldpassword: password incorrect") )
var ( ErrPasswordToShort = errors.New("shieldpassword: password is too short") ErrMissingRequiredChars = errors.New( "shieldpassword: password is missing required characters", ) )
var DefaultPasswordHasher = NewBcryptPasswordHasher(BcryptDefaultCost)
DefaultPasswordHasher is the default password hashing algorithm used across.
var ErrPwnedPassword = errors.New("shieldpassword: password has been pwned")
ErrPwnedPassword is returned when a password has been pwned.
Functions ¶
func WithHooker ¶
func WithMinLength ¶
func WithMinLength(minLength int) func(*PasswordStrengthCheckerConfig)
func WithPasswordHasher ¶
func WithPasswordHasher[U any](hasher PasswordHasher) func(*Config[U])
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.
func WithRequiredChars ¶
func WithRequiredChars(requiredChars PasswordRequiredChars) func(*PasswordStrengthCheckerConfig)
Types ¶
type Config ¶
type Config[U any] struct { Logger *slog.Logger PasswordHasher PasswordHasher PasswordChecker PasswordChecker Hooker Hooker[U] }
Config is the configuration for the password handler.
type Handler ¶
type Handler[U, S any] struct { // contains filtered or unexported fields }
func NewHandler ¶
func NewHandler[U, S any]( pool *pgxpool.Pool, authenticator shieldsession.Authenticator[U, S], sender shieldsender.Sender, config *Config[U], ) *Handler[U, S]
It provides functionality to.
func (*Handler[_, S]) HandleChangeUserPassword ¶
func (h *Handler[_, S]) HandleChangeUserPassword( ctx context.Context, oldPassword, newPassword string, ) error
HandleChangeUserPassword changes a user password to a provided newPassword, if the current set password matching oldPassword.
The user ID is expected to be provide via a session assigned to a passed ctx context.
If no password was previously set for a user a new credential will be created.
func (*Handler[U, _]) HandleUserLogin ¶
type Hooker ¶
type Hooker[U any] interface { // OnUserRegistration is called when registering a new user. // Use this method to create an additional context for the user. OnUserRegistration(context.Context, typeid.TypeID, pgx.Tx) (U, error) // OnUserLogin 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. OnUserLogin(context.Context, typeid.TypeID, pgx.Tx) (U, error) }
Hooker allows to hook into the user registration and logging in sessions and perform additional operations.
type PasswordChecker ¶
type PasswordChecker interface {
// Checker checks if the password passes the verification checks.
Check(context.Context, string) error
}
PasswordChecker checks if the password passes the verification checks.
See PwndPasswordVerifier, PasswordStrengthVerifier for concrete implementations.
func JoinPasswordChecker ¶
func JoinPasswordChecker(checkers ...PasswordChecker) PasswordChecker
func NewPasswordStrengthChecker ¶
func NewPasswordStrengthChecker(opts ...func(*PasswordStrengthCheckerConfig)) PasswordChecker
NewPasswordStrengthChecker creates a new password strength verifier.
func NewPwndPasswordChecker ¶
func NewPwndPasswordChecker(opts ...func(*PwndPasswordCheckerConfig)) PasswordChecker
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 creates a password hasher using the bcrypt algorithm.
Please note that bcrypt has a maximum input length of 72 bytes. For passwords requiring more than 72 bytes of data, consider using an alternative algorithm such as Argon2.
type PasswordRequiredChars ¶
type PasswordRequiredChars []string
PasswordRequiredChars represents a list of characters that are mandatory to be presented in the password.
var DefaultPasswordRequiredChars PasswordRequiredChars //nolint:gochecknoglobals
func (*PasswordRequiredChars) Parse ¶
func (s *PasswordRequiredChars) Parse(source string) error
type PasswordStrengthCheckerConfig ¶
type PasswordStrengthCheckerConfig struct {
RequiredChars PasswordRequiredChars
MinLength int
}