Documentation
¶
Overview ¶
Package purser is the root of go-steer's shared authentication and authorization module: caller identity derived from SPIFFE X.509-SVIDs, standard-CA client certificates, and OIDC tokens.
The root package holds only what every consumer needs and nothing can be defined without — the Caller identity, the AuthSource verdict, the sentinel errors, and the context plumbing that carries them. It is stdlib-only and depends on no other purser package, so it can be imported by a client that merely reads an identity off a context.
Everything that verifies a credential lives in a subpackage, so a consumer links only the machinery it uses: authn for the Authenticator contract and its implementations, authz for the authorization matrix, httpmw for the middleware, client for outbound credentials, and authtest for the conformance suite and an in-memory CA.
See README.md for the layout and docs/DESIGN.md for the design.
Index ¶
- Constants
- Variables
- func ProxyByFromContext(ctx context.Context) (by string, ok bool)
- func WithAuthSource(ctx context.Context, s AuthSource) context.Context
- func WithCaller(ctx context.Context, c Caller) context.Context
- func WithProxyBy(ctx context.Context, by string) context.Context
- type AuthSource
- type Caller
Constants ¶
const AnonymousIdentity = "anon"
AnonymousIdentity is the Identity of the conventional unauthenticated Caller. Compare against it rather than against a Caller value: Caller contains a map and so is not comparable with ==.
Variables ¶
var ErrAssertedCallerForbidden = errors.New("purser: caller is not permitted to assert identities")
ErrAssertedCallerForbidden is returned when a Caller that is not permitted to proxy attempts to assert another identity. Surfaces map it to 401 and should log the attempt: it indicates either misconfiguration or a credential that should not be assertable.
var ErrAssertedCallerUnknown = errors.New("purser: asserted identity is not provisioned")
ErrAssertedCallerUnknown is returned when a proxy-permitted Caller asserts an identity that is not provisioned. Surfaces map it to 401.
var ErrUnauthenticated = errors.New("purser: unauthenticated")
ErrUnauthenticated is returned by an Authenticator when no valid credential is present on the request. HTTP surfaces map it to 401.
It deliberately does not distinguish "no credential presented" from "credential presented but invalid": the difference is useful to an attacker probing which identities exist and useless to a legitimate client, which must retry with a good credential either way.
ErrUnavailable is returned by an Authenticator that could not reach a verdict on the credential at all: something it depends on — an issuer's key set, a token-introspection endpoint, a directory it resolves group membership against — did not answer, or answered with something it could not use. HTTP surfaces map it to 503.
The distinction from ErrUnauthenticated is what a client should do next. A 401 tells it the credential is bad, and a well-behaved client discards it and signs in again — an MCP client re-runs OAuth. That is the wrong reaction to an outage on the server's side: the credential may be perfectly good, and sending every user back through sign-in at once is a second outage stacked on the first. A 503 tells it to retry the same request.
Return it only when the failure says nothing about the credential. Anything the request itself can cause — a missing, malformed, expired or forged credential — is ErrUnauthenticated, and must not wrap this as well: surfaces give ErrUnavailable precedence when an error matches both, so wrapping it on a bad credential turns a 401 into an endless retry.
Functions ¶
func ProxyByFromContext ¶
ProxyByFromContext returns the proxying identity stored on ctx by WithProxyBy. ok is false, and the string empty, when the request did not take the proxy path.
func WithAuthSource ¶
func WithAuthSource(ctx context.Context, s AuthSource) context.Context
WithAuthSource returns a copy of ctx carrying the verdict on how the request was authenticated. Only code that performed or directly observed the verification may call this.
func WithCaller ¶
WithCaller returns a copy of ctx carrying c. Called by the middleware that resolved the request's Caller; downstream code reads it back with CallerFromContext.
func WithProxyBy ¶
WithProxyBy returns a copy of ctx carrying the proxying identity alongside the effective Caller, so audit records capture both: the human the request acts for and the credential that asserted them (effective="alice@example.com", proxy_by="sa:slack-bot").
Pair it with WithCaller, which carries the effective identity.
Types ¶
type AuthSource ¶
type AuthSource string
AuthSource names the class of credential a server actually verified for a request. It answers an operator's "how did this request get in?" — which is a different question from "who is this?" (Caller) and from "may they do this?" (authorization).
The critical property: an AuthSource is stamped by the code that performed the verification, and is never re-derived from request headers. A value inferred from a header is a value any client can forge, and a forgeable "how did they authenticate" field is worse than no field at all, because it is trusted.
The string values are wire-visible (they surface on core-agent's /whoami) and are part of the compatibility contract.
const ( // AuthSourceAnonymous — the server verified no credential and the // surface allowed the request through. This also covers requests // that carried credential-looking headers the server had no // validator for: presenting a credential nobody checked is // indistinguishable from presenting none. AuthSourceAnonymous AuthSource = "anonymous" // AuthSourceBearer — the server validated a bearer token, either // against the static table or at a transport-level bearer gate. // The mere presence of an Authorization header does not produce // this value. AuthSourceBearer AuthSource = "bearer" // AuthSourceMTLS — the server's own TLS stack verified the peer's // certificate chain against the configured CA pool (the PKI // profile: ClientAuth = RequireAndVerifyClientCert), and the // identity was read from the configured subject field. A merely // presented-but-unverified certificate never produces this value. AuthSourceMTLS AuthSource = "mtls" // AuthSourceSPIFFE — the peer presented an X.509-SVID that // verified against the SPIFFE trust bundle, and the identity is // the SVID's URI SAN. // // Distinct from AuthSourceMTLS on purpose. Both are mutual TLS, // but they verify by opposite means and can be configured // independently, so collapsing them would leave an operator unable // to tell which listener profile admitted a caller. AuthSourceSPIFFE AuthSource = "spiffe" // AuthSourceOIDC — the server validated a signed OIDC/JWT token // against the issuer's published keys, including audience and // expiry. AuthSourceOIDC AuthSource = "oidc" // AuthSourceGoogleAccessToken — the server validated an opaque // Google OAuth access token by asking Google (tokeninfo): unexpired, // issued to one of the server's own OAuth clients, carrying the // scopes it requires, for a verified account. // // Distinct from AuthSourceOIDC because no signature was checked: a // Google access token is not a JWT, and its validity is Google's // answer at lookup time, not a local verification against published // keys. Distinct from AuthSourceBearer because the token is the // caller's own Google credential, not an entry in a table the server // provisioned, and the server may go on to act as the caller with it. // Emitted by github.com/go-steer/steward. AuthSourceGoogleAccessToken AuthSource = "google-access-token" // AuthSourceAsserted — a proxy-permitted credential asserted // another identity and the server validated the assertion. The // asserted path is the audit-relevant source and outranks the // underlying credential's own class; the proxying identity is // recorded separately (see WithProxyBy). AuthSourceAsserted AuthSource = "asserted" // AuthSourceIAP — reserved for a verified identity-gateway // assertion (Google IAP, Cloudflare Access, a service-mesh // sidecar). Not emitted yet: it may only be stamped once the // server validates a gateway's signed assertion, never inferred // from the gateway's plaintext headers, which any client that can // reach the port may set. AuthSourceIAP AuthSource = "iap" // AuthSourceProxyHeader — the server read the identity from a // header set by a fronting proxy and verified no credential of its // own. This is the weakest source purser defines, and the only one // whose trustworthiness rests entirely on deployment topology // rather than on cryptography. // // It is worth naming rather than leaving consumers to mis-stamp. // The oauth2-proxy / X-Forwarded-Email shape is the most common // hosted deployment of anything behind an authenticating load // balancer, and a server in that mode really has resolved a // specific person and really will attribute their actions to them // — so AuthSourceAnonymous is untrue. But it validated no // signature, so AuthSourceIAP (which requires exactly that) is // forbidden, and AuthSourceAsserted (which presumes a // proxy-permitted credential underneath) has nothing to be // permitted. An audit record reading "proxy-header" is a truthful // statement about how much the identity is worth. // // Stamping it is a claim about the deployment, not about the // request: that the fronting proxy strips client-supplied copies // of the header, and that nothing else can reach the port. Neither // is checkable from inside this process. A consumer that can // verify a gateway's signature should stamp AuthSourceIAP instead, // and downstream policy is entitled to treat this value as less // trustworthy than any verified one. // // Note that this counts as authenticated everywhere purser tests // for it, which is only AuthSourceAnonymous — so an authenticator // reporting it may be wrapped by authz.WithRules and may reach // httpmw's proxy-assertion path. Both are intended (mapping a // header identity onto rules is the point), but they mean the // operator's topology promise is load-bearing for authorization, // not just for attribution. AuthSourceProxyHeader AuthSource = "proxy-header" )
func AuthSourceFromContext ¶
func AuthSourceFromContext(ctx context.Context) (s AuthSource, ok bool)
AuthSourceFromContext returns the verdict stored on ctx by WithAuthSource. ok is false when no authentication middleware ran; treat absence as AuthSourceAnonymous, since nothing was verified.
func (AuthSource) Known ¶
func (s AuthSource) Known() bool
Known reports whether s is one of the sources purser defines.
Authenticators live outside this package, including in consuming repos, so an unknown source is a real possibility rather than an impossible state; it means some authenticator invented a value the rest of the stack cannot interpret. The conformance suite rejects it.
type Caller ¶
Caller is the identity attached to every request entering an authenticated surface. It is the output of an authn.Authenticator and the input to authorization and audit.
Identity is a stable opaque ID. Its shape depends on the credential that produced it: an email ("alice@example.com") for OIDC or an email-SAN client certificate, a SPIFFE ID ("spiffe://example.org/ns/prod/sa/api") for the SPIFFE mTLS profile, a service-account marker ("sa:platform-agent") for the bearer table, or "anon" for unauthenticated requests where anonymous access is allowed. Identity is the only field compared for equality; consumers must not parse it to recover structure that Labels already carries.
Labels carry free-form metadata from the credential — the OIDC issuer and residual claims, a certificate's issuer DN and serial, a SPIFFE trust domain and path. They are available to authorization rules and audit consumers.
Labels must be treated as read-only by everything downstream of the authenticator. An authenticator that resolves a Caller from a static table would otherwise hand every request a pointer into its own state, where one consumer's mutation silently rewrites another caller's metadata. Authenticators that retain their Labels maps return Clone; consumers that need to add a label call Clone themselves. The authtest conformance suite checks this.
Admin grants the see-everything role. It is set by the consuming service's policy (authz rules), never by a credential's own claim — a token that could assert its own admin bit would be a privilege escalation.
func Anonymous ¶
func Anonymous() Caller
Anonymous returns the conventional Caller for unauthenticated requests where anonymous access is allowed. Consumers may configure a different default identity, but "anon" is the documented one.
It is a function rather than a package-level variable so that no importer can reassign the identity every unauthenticated request in the process resolves to. Each call returns a fresh value carrying no Labels, so there is no shared map to alias either.
func CallerFromContext ¶
CallerFromContext returns the Caller stored on ctx by WithCaller. ok is false when no Caller is present — a code path reached before the authentication middleware, or a handler exercised directly in a test. Absence must be treated as unauthenticated, never as a permissive default.
func (Caller) Clone ¶
Clone returns a deep copy of c: the returned Caller shares no map with the receiver, so the caller may mutate its Labels freely. Cloning a Caller with nil Labels yields nil Labels.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package authn defines the contract every purser authenticator implements: turn an inbound HTTP request into a purser.Caller, or report that no valid credential was present.
|
Package authn defines the contract every purser authenticator implements: turn an inbound HTTP request into a purser.Caller, or report that no valid credential was present. |
|
bearer
Package bearer authenticates callers against a static table of tokens.
|
Package bearer authenticates callers against a static table of tokens. |
|
mtls
Package mtls authenticates callers by the client certificate they presented on the TLS connection.
|
Package mtls authenticates callers by the client certificate they presented on the TLS connection. |
|
oidc
Package oidc authenticates callers by OpenID Connect ID token.
|
Package oidc authenticates callers by OpenID Connect ID token. |
|
Package authtest provides the test harness every purser authenticator is held to: an in-memory certificate authority and a conformance suite.
|
Package authtest provides the test harness every purser authenticator is held to: an in-memory certificate authority and a conformance suite. |
|
Package authz decides what an authenticated caller may do.
|
Package authz decides what an authenticated caller may do. |
|
Package client dials purser-authenticated services.
|
Package client dials purser-authenticated services. |
|
examples
|
|
|
cmd/hello-client
command
Command hello-client calls hello-server over mutual TLS and prints the identity the server saw.
|
Command hello-client calls hello-server over mutual TLS and prints the identity the server saw. |
|
cmd/hello-server
command
Command hello-server serves one mutually authenticated JSON endpoint and reports who called it.
|
Command hello-server serves one mutually authenticated JSON endpoint and reports who called it. |
|
cmd/mint-credentials
command
Command mint-credentials writes a throwaway CA and the certificates the local examples need.
|
Command mint-credentials writes a throwaway CA and the certificates the local examples need. |
|
internal/credentials
Package credentials mints the throwaway certificates the local examples run on.
|
Package credentials mints the throwaway certificates the local examples run on. |
|
internal/hello
Package hello is the REST/JSON service the purser examples authenticate.
|
Package hello is the REST/JSON service the purser examples authenticate. |
|
internal/profile
Package profile turns example command-line flags into the matched pair purser hands out: a *tls.Config and, on the server side, the authenticator that understands the connections it admits.
|
Package profile turns example command-line flags into the matched pair purser hands out: a *tls.Config and, on the server side, the authenticator that understands the connections it admits. |
|
Package httpmw is the HTTP layer that turns purser's authenticators into a serving surface: it resolves the Caller onto the request context, stamps how the request was authenticated, guards the browser CSRF vectors, gates a shared transport token, and answers whether an address is safe to bind.
|
Package httpmw is the HTTP layer that turns purser's authenticators into a serving surface: it resolves the Caller onto the request context, stamps how the request was authenticated, guards the browser CSRF vectors, gates a shared transport token, and answers whether an address is safe to bind. |
|
internal
|
|
|
ca
Package ca is an in-memory certificate authority for tests and examples.
|
Package ca is an in-memory certificate authority for tests and examples. |
|
tlsfloor
Package tlsfloor holds purser's minimum TLS version policy.
|
Package tlsfloor holds purser's minimum TLS version policy. |