auth

package
v0.16.0 Latest Latest
Warning

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

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

Overview

Package auth resolves who is making a status-server request. It owns the sign-in surface (the OIDC authorization-code flow with the server as the OAuth2 client, the encrypted chunked session cookies, and the small SPA-visible state cookies) and hands the rest of the server a bare Identity. The package imports nothing from Kubernetes: what an identity may do is internal/web/authz's concern.

Four modes exist, selected by the mounted configuration file:

  • "unconfigured": no file. No session is ever resolved and no sign-in routes are registered, so every data route refuses and only the SPA shell is served.
  • "none": a fixed identity with authorization bypassed, for development.
  • "anonymous": a fixed identity that still passes through RBAC access reviews.
  • "oidc": the real SSO flow.

Index

Constants

View Source
const (
	// ModeNone disables authentication entirely: every request is a fixed
	// development identity and authorization is bypassed.
	ModeNone = "none"
	// ModeAnonymous serves every request as one fixed identity that still
	// passes through RBAC access reviews.
	ModeAnonymous = "anonymous"
	// ModeOIDC runs the SSO authorization-code flow.
	ModeOIDC = "oidc"
)

Modes the configuration file selects between.

View Source
const (

	// CookieProvider tells the SPA whether and how sign-in works:
	// {"provider","authenticated","autoLogin"}, base64url JSON.
	CookieProvider = "patchy-auth-provider"
	// CookieAuthError carries a human-readable sign-in failure to the SPA.
	CookieAuthError = "patchy-auth-error"
	// CookieLogout marks an explicit sign-out so autoLogin pauses for it.
	CookieLogout = "patchy-auth-logout"
)

Cookie names. The session and state cookies are HttpOnly and carry sealed data. The provider/error/logout cookies are SPA-visible and carry no secrets; they only tell the client what the sign-in surface looks like.

View Source
const DefaultSessionDuration = 7 * 24 * time.Hour

DefaultSessionDuration bounds a session when the configuration does not.

Variables

This section is empty.

Functions

func BaseURL added in v0.16.0

func BaseURL(cfg *Config, r *http.Request) string

BaseURL is the status page's public origin (scheme://host) for building OAuth callback URLs: the origin of the configured OIDC redirectURL when one is set, otherwise derived from the request. Deriving trusts X-Forwarded-Proto/-Host, which the deployment docs call out as requiring a trusted fronting proxy.

func ClearCookie added in v0.16.0

func ClearCookie(w http.ResponseWriter, name string, secure bool)

ClearCookie expires one cookie with the hardened attributes.

func IsLocalURL added in v0.16.0

func IsLocalURL(p string) bool

IsLocalURL reports whether p is safe to redirect to: an absolute path on this origin. It must start with one slash; "//host" and "/\\host" are read by browsers as another host.

func LinkKey added in v0.16.0

func LinkKey(cfg *Config) ([]byte, error)

LinkKey derives the key that seals the GitHub link cookies: from the OIDC client secret in mode oidc, from linkKeyFile otherwise. A nil key (with no error) means GitHub linking is off: no configuration, or a mode without a key source.

func RandomToken added in v0.16.0

func RandomToken() (string, error)

RandomToken returns a URL-safe random string (CSRF tokens, PKCE).

func SafePath added in v0.16.0

func SafePath(p string) string

SafePath guards a post-flow redirect against open redirects: only same-origin absolute paths survive.

func Seal added in v0.16.0

func Seal(key []byte, v any) (string, error)

Seal encrypts v's JSON under key (AES-256-GCM, URL-safe): the sealed-blob format the session and state cookies use, for other cookie owners.

func SealKey added in v0.16.0

func SealKey(secret, info string) ([]byte, error)

SealKey derives a 32-byte seal key from secret material under an HKDF label.

func SetJSONCookie added in v0.16.0

func SetJSONCookie(w http.ResponseWriter, name string, v any, maxAge time.Duration, secure bool) error

SetJSONCookie writes an SPA-visible base64url JSON cookie. It must never carry a secret.

func Unseal added in v0.16.0

func Unseal(key []byte, blob string, v any) error

Unseal decrypts a Seal blob into v; tampering or a rotated key fails.

Types

type AnonymousConfig

type AnonymousConfig struct {
	// Username of the fixed identity.
	Username string `yaml:"username"`
	// Groups of the fixed identity.
	Groups []string `yaml:"groups"`
}

AnonymousConfig is the fixed identity every request resolves to in mode anonymous. Access reviews still run for it, so cluster RBAC decides what the identity may see and do.

type Authenticator

type Authenticator interface {
	// Identify resolves the current session. It may write cookies (token
	// refresh, SPA state), hence the ResponseWriter. (nil, nil) means "no
	// session"; callers map that to 401 on protected routes.
	Identify(w http.ResponseWriter, r *http.Request) (*Identity, error)
	// Register adds the sign-in routes (/oauth2/*, /logout) to mux. Modes
	// without a sign-in surface register nothing.
	Register(mux *http.ServeMux)
}

Authenticator resolves the requester of an HTTP request.

func New

func New(ctx context.Context, cfg *Config, log *slog.Logger) (Authenticator, error)

New builds the Authenticator for cfg. A nil cfg is the unconfigured posture. The context bounds OIDC issuer discovery.

type ClaimsConfig

type ClaimsConfig struct {
	// Username claim access reviews run for (default email).
	Username string `yaml:"username"`
	// Groups claim (default groups).
	Groups string `yaml:"groups"`
	// DisplayName claim the UI shows (default name).
	DisplayName string `yaml:"displayName"`
}

ClaimsConfig names the token claims the identity is read from. These are claim names, not expressions.

type Config

type Config struct {
	// Mode is none, anonymous, or oidc.
	Mode string `yaml:"mode"`
	// SessionDuration caps a session's lifetime (default 168h).
	SessionDuration Duration `yaml:"sessionDuration"`
	// Insecure drops the Secure flag from cookies for plain-HTTP local
	// development. Never set it behind a real ingress.
	Insecure bool `yaml:"insecure"`
	// Anonymous configures the fixed identity of mode anonymous.
	Anonymous *AnonymousConfig `yaml:"anonymous"`
	// OIDC configures the SSO provider of mode oidc.
	OIDC *OIDCConfig `yaml:"oidc"`
	// LinkKeyFile is a file of random bytes that keys the sealed "Connect
	// GitHub" link cookies in modes anonymous and none, which have no client
	// secret to derive a key from. Without it, GitHub linking is off in
	// those modes. Mode oidc derives the key from the client secret and
	// ignores this.
	LinkKeyFile string `yaml:"linkKeyFile"`
}

Config is the mounted authentication configuration.

func LoadConfig

func LoadConfig(path string) (*Config, error)

LoadConfig reads and validates the configuration at path. An empty path or a missing file returns (nil, nil), the unconfigured posture; the caller logs the consequence. A present-but-invalid file is an error: a broken configuration must not silently downgrade to no authentication.

type Duration

type Duration struct {
	time.Duration
}

Duration is a time.Duration that unmarshals from Go duration strings ("168h", "30m"); the YAML decoder has no native duration type.

func (*Duration) UnmarshalYAML

func (d *Duration) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML parses a Go duration string.

type Identity

type Identity struct {
	// Username is the subject access reviews run for.
	Username string
	// Groups are the subject's groups for access reviews.
	Groups []string
	// DisplayName is the human-facing name (falls back to Username).
	DisplayName string
	// Session reports whether the identity is backed by a sign-in session
	// (the UI renders the user menu and sign-out only for sessions).
	Session bool
}

Identity is the resolved requester. Username and Groups feed access reviews; DisplayName is what the UI shows.

func MapClaims added in v0.9.3

func MapClaims(claims map[string]any, cfg ClaimsConfig) (*Identity, error)

MapClaims derives the identity from verified ID-token claims using the configured claim names. It is the one claims-to-identity mapping shared by the status server's session flow and the evaluation API's bearer flow, so the two can never disagree about who a token names.

func (Identity) Display

func (id Identity) Display() string

Display returns the name the UI should render.

type OIDCConfig

type OIDCConfig struct {
	// IssuerURL is the provider's issuer for discovery.
	IssuerURL string `yaml:"issuerURL"`
	// ClientID of the registered client.
	ClientID string `yaml:"clientID"`
	// ClientSecret of the registered client. The secret also keys the
	// cookie/state encryption, so rotating it invalidates live sessions.
	ClientSecret string `yaml:"clientSecret"`
	// ClientSecretFile reads the client secret from a file (e.g. a projected
	// Secret key) instead of inline.
	ClientSecretFile string `yaml:"clientSecretFile"`
	// Scopes requested at authorization (default: openid, offline_access,
	// profile, email, groups).
	Scopes []string `yaml:"scopes"`
	// AuthURLParams are extra authorize-endpoint query parameters.
	AuthURLParams map[string]string `yaml:"authURLParams"`
	// AutoLogin redirects an unauthenticated browser straight to the
	// provider instead of showing the sign-in panel.
	AutoLogin bool `yaml:"autoLogin"`
	// RedirectURL overrides the derived callback URL for deployments where
	// the fronting proxy's forwarded headers cannot be trusted.
	RedirectURL string `yaml:"redirectURL"`
	// Claims maps token claims onto the identity.
	Claims ClaimsConfig `yaml:"claims"`
}

OIDCConfig is the SSO provider for mode oidc.

Jump to

Keyboard shortcuts

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