Documentation
¶
Overview ¶
Package auth implements administrator authentication.
Index ¶
- Constants
- Variables
- func CSRFToken(sessionToken string) string
- func HashPassword(password string, params PasswordParams) (string, error)
- func ThrottleKey(remoteAddr string) string
- func VerifyCSRFToken(sessionToken, csrf string) bool
- func VerifyPassword(encoded, password string) (bool, error)
- type Decision
- type PasswordParams
- type Service
- func (s *Service) Authenticate(ctx context.Context, token string) (Session, error)
- func (s *Service) ChangePassword(ctx context.Context, token, currentPassword, newPassword, remoteAddr string) (Session, error)
- func (s *Service) Login(ctx context.Context, password, remoteAddr string) (Session, error)
- func (s *Service) Logout(ctx context.Context, token, remoteAddr string) error
- func (s *Service) Now() time.Time
- type ServiceConfig
- type Session
- type Throttle
- type ThrottleConfig
- type ThrottledError
Constants ¶
const DefaultPassword = "CHANGEME"
DefaultPassword is the only password accepted before the administrator is initialized.
const (
MinPasswordLength = 12
)
Variables ¶
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") )
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 ¶
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 ¶
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 ¶
VerifyCSRFToken reports whether csrf is bound to sessionToken.
func VerifyPassword ¶
VerifyPassword reports whether password matches encoded, comparing in constant time.
Types ¶
type Decision ¶
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 ¶
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 ¶
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.
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 ¶
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 ¶
Allow records an attempt for key and reports whether it is within the limit.
type ThrottleConfig ¶
ThrottleConfig configures a fixed-window per-key attempt limiter.
type ThrottledError ¶
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