auth

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: LGPL-3.0 Imports: 23 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Basic

func Basic(log *slog.Logger, users userauth.UserGetter, rnd *web.Renderer) http.Handler

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

func Chain(log *slog.Logger, users userauth.UserGetter, rnd *web.Renderer) http.Handler

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(log *slog.Logger, rnd *web.Renderer) http.Handler

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(log *slog.Logger, rnd *web.Renderer) http.Handler

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

func Recoverable(log *slog.Logger, users userauth.UserGetter, rnd *web.Renderer) http.Handler

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

func Section(log *slog.Logger, users userauth.UserGetter, rnd *web.Renderer) examples.Section

Section describes the authentication-method examples for the demo index and mounts them on the router.

func Token

func Token(log *slog.Logger, users userauth.UserGetter, rnd *web.Renderer) http.Handler

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.

Jump to

Keyboard shortcuts

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