auth

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 15 Imported by: 0 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 auth implements administrator authentication.

Index

Constants

View Source
const DefaultPassword = "CHANGEME"

DefaultPassword is the only password accepted before the administrator is initialized.

View Source
const (
	MinPasswordLength = 12
)

Variables

View Source
var (
	ErrInvalidCredentials = errors.New("auth: invalid credentials")
	ErrUnauthenticated    = errors.New("auth: unauthenticated")
	ErrThrottled          = errors.New("auth: too many attempts")
	ErrInvalidNewPassword = errors.New("auth: new password does not meet requirements")
)
View Source
var DefaultPasswordParams = PasswordParams{
	Memory:      64 * 1024,
	Iterations:  3,
	Parallelism: 4,
	SaltLength:  16,
	KeyLength:   32,
}

DefaultPasswordParams follow the RFC 9106 second recommended option.

Functions

func CSRFToken

func CSRFToken(sessionToken string) string

CSRFToken derives the CSRF token bound to a session token. It is not persisted and cannot be derived from the stored session hash.

func HashPassword

func HashPassword(password string, params PasswordParams) (string, error)

HashPassword returns an encoded Argon2id hash with a random salt.

func ThrottleKey

func ThrottleKey(remoteAddr string) string

ThrottleKey normalizes a remote IP for throttling: IPv4-mapped IPv6 addresses become IPv4, and other IPv6 addresses are reduced to their /64 prefix so one host cannot multiply its budget by rotating addresses. Anything that is not an IP is returned unchanged.

func VerifyCSRFToken

func VerifyCSRFToken(sessionToken, csrf string) bool

VerifyCSRFToken reports whether csrf is bound to sessionToken.

func VerifyPassword

func VerifyPassword(encoded, password string) (bool, error)

VerifyPassword reports whether password matches encoded, comparing in constant time.

Types

type Decision

type Decision struct {
	Allowed     bool
	FirstDenial bool
	RetryAfter  time.Duration
}

Decision is the outcome of one attempt. FirstDenial is set only on the first denied attempt of a key's window, so callers can record one audit event per window instead of one per attempt. RetryAfter is set on denials and is the time left in the key's window.

type PasswordParams

type PasswordParams struct {
	Memory      uint32 // KiB
	Iterations  uint32
	Parallelism uint8
	SaltLength  uint32
	KeyLength   uint32
}

PasswordParams are Argon2id cost parameters.

type Service

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

Service authenticates the single administrator.

func NewService

func NewService(config ServiceConfig) *Service

NewService returns a Service; zero fields receive defaults.

func (*Service) Authenticate

func (s *Service) Authenticate(ctx context.Context, token string) (Session, error)

Authenticate resolves a raw bearer token to a live session.

func (*Service) ChangePassword

func (s *Service) ChangePassword(ctx context.Context, token, currentPassword, newPassword, remoteAddr string) (Session, error)

ChangePassword rotates the administrator password, revokes every existing session, and returns a fresh admin session. The current password is verified against a credential snapshot outside any write transaction; the rotation commits only if that credential and the caller's session are still current.

func (*Service) Login

func (s *Service) Login(ctx context.Context, password, remoteAddr string) (Session, error)

Login verifies password and issues a bootstrap or admin session. The session is persisted only if the credential it was verified against is still current.

func (*Service) Logout

func (s *Service) Logout(ctx context.Context, token, remoteAddr string) error

Logout revokes the session identified by token.

func (*Service) Now

func (s *Service) Now() time.Time

Now returns the service clock's current time.

type ServiceConfig

type ServiceConfig struct {
	Store          *store.Store
	PasswordParams PasswordParams
	SessionTTL     time.Duration
	BootstrapTTL   time.Duration
	Throttle       *Throttle
	// PasswordThrottle limits password-change attempts per session and per IP.
	PasswordThrottle        *Throttle
	PasswordSessionThrottle *Throttle
	Now                     func() time.Time
}

ServiceConfig wires a Service.

type Session

type Session struct {
	Token     string
	Kind      store.SessionKind
	ExpiresAt time.Time
}

Session describes an authenticated session. Token is the raw bearer token and is set only on sessions returned by Login and ChangePassword.

func (Session) PasswordChangeRequired

func (s Session) PasswordChangeRequired() bool

PasswordChangeRequired reports whether the session is a bootstrap session.

type Throttle

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

Throttle limits attempts per key using bounded memory. When full, it drops expired windows first and then the oldest window.

func NewThrottle

func NewThrottle(config ThrottleConfig) *Throttle

NewThrottle returns a Throttle; zero fields receive safe defaults.

func (*Throttle) Allow

func (t *Throttle) Allow(key string) bool

Allow records an attempt for key and reports whether it is within the limit.

func (*Throttle) Check

func (t *Throttle) Check(key string) Decision

Check records an attempt for key and reports the decision.

func (*Throttle) Len

func (t *Throttle) Len() int

Len reports the number of tracked keys.

type ThrottleConfig

type ThrottleConfig struct {
	Limit      int
	Window     time.Duration
	MaxEntries int
	Now        func() time.Time
}

ThrottleConfig configures a fixed-window per-key attempt limiter.

type ThrottledError

type ThrottledError struct{ RetryAfter time.Duration }

ThrottledError reports a throttled attempt. It matches ErrThrottled, and RetryAfter is how long until another attempt can be accepted.

func (*ThrottledError) Error

func (e *ThrottledError) Error() string

func (*ThrottledError) Is

func (e *ThrottledError) Is(target error) bool

Jump to

Keyboard shortcuts

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