Documentation
¶
Index ¶
- Constants
- Variables
- func CheckPassword(hash, password string) bool
- func HashPassword(password string) (string, error)
- func MaxRequestBodySize(maxBytes int64) func(http.Handler) http.Handler
- func Recovery(logger *slog.Logger) func(http.Handler) http.Handler
- func RequestLogger(logger *slog.Logger) func(http.Handler) http.Handler
- func SecurityHeaders(next http.Handler) http.Handler
- type Auth
- type AuthSettings
Constants ¶
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".
const DefaultSessionTTL = 30 * 24 * time.Hour
DefaultSessionTTL is how long a browser session stays valid after login.
const SessionCookieName = "pilot_session"
SessionCookieName is the cookie that carries a browser session.
Variables ¶
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 ¶
CheckPassword reports whether password matches a hash from HashPassword. A malformed hash never matches.
func HashPassword ¶
HashPassword returns "pbkdf2-sha256$<iterations>$<salt>$<hash>".
func MaxRequestBodySize ¶
MaxRequestBodySize limits request bodies to maxBytes.
func Recovery ¶
Recovery returns a middleware that recovers from panics, logs the error and stack trace, and returns a 500 response.
func RequestLogger ¶
RequestLogger returns a middleware that logs each HTTP request using slog.
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 ¶
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.