middleware

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 19 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

Index

Constants

View Source
const (
	// AuthDisabledForLocal lets requests from local addresses through
	// without credentials; everyone else must log in. The default.
	AuthDisabledForLocal = "disabled_for_local"
	// AuthEnabled requires a login from every address.
	AuthEnabled = "enabled"
)

When a login is required, mirroring Sonarr's "Authentication Required".

View Source
const DefaultSessionTTL = 30 * 24 * time.Hour

DefaultSessionTTL is how long a browser session stays valid after login.

View Source
const SessionCookieName = "pilot_session"

SessionCookieName is the cookie that carries a browser session.

Variables

View Source
var PasswordIterations = 600_000

PasswordIterations is the PBKDF2-SHA256 work factor for new hashes (OWASP 2023 guidance). Existing hashes keep the count they were made with, so raising this never locks anyone out. Tests lower it.

Functions

func CheckPassword

func CheckPassword(hash, password string) bool

CheckPassword reports whether password matches a hash from HashPassword. A malformed hash never matches.

func HashPassword

func HashPassword(password string) (string, error)

HashPassword returns "pbkdf2-sha256$<iterations>$<salt>$<hash>".

func MaxRequestBodySize

func MaxRequestBodySize(maxBytes int64) func(http.Handler) http.Handler

MaxRequestBodySize limits request bodies to maxBytes.

func Recovery

func Recovery(logger *slog.Logger) func(http.Handler) http.Handler

Recovery returns a middleware that recovers from panics, logs the error and stack trace, and returns a 500 response.

func RequestLogger

func RequestLogger(logger *slog.Logger) func(http.Handler) http.Handler

RequestLogger returns a middleware that logs each HTTP request using slog.

func SecurityHeaders

func SecurityHeaders(next http.Handler) http.Handler

SecurityHeaders sets defensive HTTP response headers on every response.

Types

type Auth

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

Auth authenticates API requests. A request is accepted when it carries any of:

  • a valid X-Api-Key header (scripts, Pulse, other services),
  • a valid session cookie issued by Login (the browser UI), or
  • nothing, when it comes from a local address and the settings don't require a login there (AuthDisabledForLocal, the default). This is how Sonarr works: on your own network the UI just opens.

Sec-Fetch-Site is deliberately NOT a credential: any non-browser client can set it to "same-origin". It is only used as a CSRF check on cookie and local requests, where the browser guarantees its value.

Sessions are stateless HMAC-signed tokens. The signing key is derived from the API key and the password hash, so rotating the API key or changing the password invalidates every session.

func NewAuth

func NewAuth(apiKey string, settings AuthSettings) *Auth

NewAuth builds an Auth for the given API key and settings. An empty API key rejects every request (fail closed).

func (*Auth) LoginHandler

func (a *Auth) LoginHandler() http.HandlerFunc

LoginHandler exchanges a username and password for a session cookie.

POST /api/v1/auth/login  {"username": "...", "password": "..."}

func (*Auth) LogoutHandler

func (a *Auth) LogoutHandler() http.HandlerFunc

LogoutHandler clears the session cookie. Sessions are stateless, so a copied cookie stays valid until it expires, the API key is rotated or the password changes.

POST /api/v1/auth/logout

func (*Auth) Middleware

func (a *Auth) Middleware(next http.Handler) http.Handler

Middleware rejects requests that are not authenticated. Mount it on the route group that holds every protected route, not as a global path-prefix filter, so a route can't slip past it through path tricks.

func (*Auth) SetSettings

func (a *Auth) SetSettings(s AuthSettings)

SetSettings replaces the authentication settings. Changing the password hash logs out every existing session.

func (*Auth) Settings

func (a *Auth) Settings() AuthSettings

Settings returns the current authentication settings.

func (*Auth) StatusHandler

func (a *Auth) StatusHandler() http.HandlerFunc

StatusHandler tells the UI whether it is signed in and whether a login form makes sense. Unauthenticated.

GET /api/v1/auth/status

type AuthSettings

type AuthSettings struct {
	// Required is AuthDisabledForLocal or AuthEnabled. Anything else is
	// treated as AuthDisabledForLocal.
	Required string
	// Username and PasswordHash (from HashPassword) are the login for
	// the browser UI. Both empty means no login is set up yet.
	Username     string
	PasswordHash string
}

AuthSettings is the user-editable part of authentication.

func (AuthSettings) CredentialsConfigured

func (s AuthSettings) CredentialsConfigured() bool

CredentialsConfigured reports whether a username and password are set.

func (AuthSettings) LoginRequiredEverywhere

func (s AuthSettings) LoginRequiredEverywhere() bool

LoginRequiredEverywhere reports whether local addresses must log in too. AuthEnabled without credentials would lock everyone out, so it falls back to letting local addresses through.

Jump to

Keyboard shortcuts

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