Documentation
¶
Overview ¶
Package auth holds the demo examples for authentication methods: handlers that authenticate each request (basic auth, trusted header, cookie session) as opposed to login flows that establish a session.
Index ¶
- func Basic(log *slog.Logger, users userauth.UserGetter, rnd *web.Renderer) http.Handler
- func Chain(log *slog.Logger, users userauth.UserGetter, rnd *web.Renderer) http.Handler
- func Cookie(log *slog.Logger, rnd *web.Renderer) http.Handler
- func Header(log *slog.Logger, rnd *web.Renderer) http.Handler
- func Recoverable(log *slog.Logger, users userauth.UserGetter, rnd *web.Renderer) http.Handler
- func Section(log *slog.Logger, users userauth.UserGetter, rnd *web.Renderer) examples.Section
- func Token(log *slog.Logger, users userauth.UserGetter, rnd *web.Renderer) http.Handler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Basic ¶
Basic demonstrates HTTP Basic authentication in two modes: "enforce" sends a WWW-Authenticate challenge so the browser pops up a credentials prompt, while "silent" returns 401 without prompting.
func Chain ¶
Chain demonstrates chain.Authenticator: several auth handlers are evaluated in order and the first success wins. Here a cookie session is tried first (browsers that logged in once), then non-enforcing basic auth (API clients sending an Authorization header); when both fail the unauthorized callback redirects to a login endpoint instead of answering 401.
func Cookie ¶
Cookie demonstrates cookie-session authentication in isolation: the Middleware authenticates each request from an encrypted session cookie. Establishing the session is kept intentionally trivial (a GET that logs in a fixed demo user) to focus on the authentication side — see the login examples for real credential verification in front of LoginUser.
func Header ¶
Header demonstrates authentication from a trusted request header (X-User-Auth), the pattern used behind a reverse proxy that injects the already-authenticated user's identity. The proxy owns the trust decision: the deployment must guarantee clients cannot set the header themselves.
func Recoverable ¶ added in v0.4.1
Recoverable demonstrates PAT recoverable storage (the "user+token" mode): /new mints a Recoverable token for the demo user and splits it via ParseToken into a virtual username (the token ID) and password (the secret); /protected authenticates a Subsonic-style salted challenge — u=<tokenID>, t=md5(secret+salt), s=<salt> — via VerifyMatch, where the server never sees the secret on the wire. The same token also still works whole as a Bearer apiKey. The AES key is generated at startup (demo only — real applications load it from configuration or a KMS; key management is the consumer's concern).
func Section ¶
Section describes the authentication-method examples for the demo index and mounts them on the router.
func Token ¶
Token demonstrates PAT authentication as a chain member: /new mints a token for the demo user (demo convenience only — real applications mint via the session-authenticated flow/pat endpoints), /protected is guarded by a chain that tries cookie session, then PAT token, then basic auth — mirroring the production pattern where tokens, sessions, and fallback auth coexist.
Types ¶
This section is empty.