Help us shape the future of Go! Take the 2026 Go Developer Survey

password

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: Apache-2.0, MIT Imports: 10 Imported by: 0

Documentation

Overview

Package password owns credential hashing and verification: one argon2id/scrypt hash per user (PHC format via pkg/crypto), plus the identity lookup used by the sign-in flow. HTTP sign-in routes belong to the session feature.

Index

Constants

This section is empty.

Variables

View Source
var ErrDisabled = errors.New("password: account is disabled")

ErrDisabled is returned when the account exists but is disabled.

View Source
var ErrInvalidCredentials = errors.New("password: invalid credentials")

ErrInvalidCredentials is returned for unknown identities and wrong secrets alike, so responses never reveal which one failed.

View Source
var ErrNoPassword = errors.New("password: account has no password credential")

ErrNoPassword is returned when the account has no credential row (e.g. passkey-only accounts).

View Source
var ErrOversizeSecret = errors.New("password: at most 128 characters allowed")

ErrOversizeSecret rejects secrets above the policy ceiling.

View Source
var ErrWeakPassword = errors.New("password: at least 8 characters required")

ErrWeakPassword rejects new passwords shorter than the minimum.

Functions

func ValidateSecret

func ValidateSecret(secret string) error

ValidateSecret exposes the single password policy for sibling features (recovery) so the rules never live in two places.

Types

type PostgresStore

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

PostgresStore persists credentials in public.user_passwords.

func NewPostgresStore

func NewPostgresStore(exec datastore.Executor) *PostgresStore

NewPostgresStore builds the production credential store.

func (*PostgresStore) HashByIdentity

func (s *PostgresStore) HashByIdentity(ctx context.Context, identity string) (string, user.User, error)

HashByIdentity resolves a username or email to the credential hash and the owning user row, for sign-in verification.

func (*PostgresStore) HashByUserID

func (s *PostgresStore) HashByUserID(ctx context.Context, userID user.UserID) (string, error)

HashByUserID returns the credential hash for a known user.

func (*PostgresStore) Upsert

func (s *PostgresStore) Upsert(ctx context.Context, userID user.UserID, passwordHash string) error

Upsert replaces the credential row for the user.

type Service

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

Service verifies credentials and owns password lifecycle rules. Sign-in session creation happens in the session feature; this service only answers "is this secret valid".

func NewService

func NewService(store Store, hasher *crypto.PasswordHasher, recorder identity.Recorder) *Service

NewService builds the credential feature on the given store and hasher. The optional recorder captures credential changes.

func (*Service) Name

func (s *Service) Name() string

Name names the feature for logs. The feature is headless: its routes (sign-in, sign-out) live in the session feature.

func (*Service) SetPassword

func (s *Service) SetPassword(ctx context.Context, userID user.UserID, secret string) error

SetPassword assigns (or rotates) the credential for the user.

func (*Service) VerifyForUser

func (s *Service) VerifyForUser(ctx context.Context, userID user.UserID, secret string) error

VerifyForUser checks the current secret of a known user, used before sensitive operations (password change).

func (*Service) VerifyIdentity

func (s *Service) VerifyIdentity(ctx context.Context, identityText, secret string) (user.User, error)

VerifyIdentity checks a username/email + secret pair and returns the account on success. Unknown identities burn the same hashing work as real ones so timing never short-circuits; disabled accounts fail closed.

type Store

type Store interface {
	Upsert(ctx context.Context, userID user.UserID, passwordHash string) error
	HashByIdentity(ctx context.Context, identity string) (string, user.User, error)
	HashByUserID(ctx context.Context, userID user.UserID) (string, error)
}

Store persists credential hashes. One row per user (user_id PK).

Jump to

Keyboard shortcuts

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