Documentation
¶
Overview ¶
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.
The package itself is stdlib-only. Concrete authenticators live in subpackages so a consumer links only what it uses — a client that dials with a bearer token does not pull in a JWKS cache, and a PKI mTLS server does not pull in go-spiffe.
Implementations must pass authtest.RunAuthenticatorSuite. The interface below states the contract; the suite is what enforces it.
Index ¶
Constants ¶
const HeaderAssertedCaller = "X-Asserted-Caller"
HeaderAssertedCaller is the conventional header a proxy-permitted Caller uses to assert the effective identity. Surfaces may make the name configurable; this is the default.
const HeaderAttachToken = "X-Attach-Token" //nolint:gosec // G101: a header name, not a credential
HeaderAttachToken is the conventional side-channel header a token may be presented in, checked before Authorization. It exists for deployments where an identity gateway — Cloud Run IAM, IAP, Cloudflare Access — owns the Authorization header for its own validation and the service still needs a channel of its own.
It lives here, in the package neither the token table nor the middleware can avoid importing, so that authn/bearer and httpmw cannot drift onto two different spellings of the same header.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Anonymous ¶
type Anonymous struct {
// Caller is returned for every request. A zero Caller means
// purser.Anonymous().
Caller purser.Caller
}
Anonymous resolves every request to the same Caller without verifying anything. It is the authenticator for surfaces that have not enabled per-caller identity, so downstream code always finds a Caller on the context and never has to special-case its absence.
The zero value resolves to purser.Anonymous(). Set Caller to override the identity.
It reports GatesCredentials() == false, so a surface that consults CredentialGate before binding a non-loopback address will not mistake it for a credential check.
func (Anonymous) Authenticate ¶
Authenticate ignores the request and returns the configured Caller. It never returns an error.
The Caller is cloned so a handler that adds a label to what it believes is its own copy cannot rewrite the identity every subsequent request will see.
func (Anonymous) GatesCredentials ¶
GatesCredentials reports false. Nothing is verified, so nothing is gated.
func (Anonymous) Source ¶
func (a Anonymous) Source() purser.AuthSource
Source reports purser.AuthSourceAnonymous: this authenticator succeeds for every request precisely because it verified nothing.
type Authenticator ¶
type Authenticator interface {
Authenticate(r *http.Request) (purser.Caller, error)
Source() purser.AuthSource
}
Authenticator extracts a Caller from an inbound HTTP request.
Authenticate returns purser.ErrUnauthenticated when the request carries no valid credential, and purser.ErrUnavailable when it could not tell — a remote dependency it needs to decide did not answer. Contract for implementations:
- A nil error means the credential was verified. Returning a zero-identity Caller with a nil error is a bug: downstream code reads a non-error result as proof of identity.
- Any error means no identity. Never return a partially populated Caller alongside an error; a consumer that ignores the error would then act on an unverified identity.
- ErrUnauthenticated is the only error that is a verdict on the credential. httpmw maps it to 401 and everything else to 503, so a bad credential reported as a bare error tells the client to retry a request that will never succeed. The conformance suite holds missing and malformed credentials to ErrUnauthenticated.
- A malformed credential is an error, not a panic. Inbound bytes are attacker-controlled.
- Labels on the returned Caller must not alias state the authenticator retains. Return purser.Caller.Clone when resolving from a table.
Source reports the credential class this authenticator verifies. It is a method rather than a type switch at the middleware so that adding an authenticator cannot silently mis-report how a request was authenticated — the new type would otherwise fall into whatever the switch's default arm happened to be. It must return the same value for the lifetime of the authenticator.
type AuthenticatorWithProxy ¶
type AuthenticatorWithProxy interface {
Authenticator
// CanProxyAs reports whether c may assert other identities. It
// must return false for the zero Caller — an unauthenticated
// request must never reach the proxy path.
CanProxyAs(c purser.Caller) bool
}
AuthenticatorWithProxy is the optional extension implemented by authenticators that support the proxy pattern: a Caller permitted to act on behalf of other Callers via the asserted-caller header.
The motivating case is a chat bot. It authenticates as itself, then asserts the human's identity per request, so audit records and per-caller credentials attribute to the human rather than to the bot.
Authenticators that do not implement this interface deny every proxy assertion. That is the safe default and it is implicit: forgetting to implement the interface cannot accidentally permit proxying.
type CredentialGate ¶
type CredentialGate interface {
Authenticator
// GatesCredentials reports whether every admitted request
// presented a credential this authenticator verified. An
// authenticator that falls back to an anonymous identity must
// return false.
GatesCredentials() bool
}
CredentialGate is the optional extension by which an authenticator declares that it admits no unverified request: every request that reaches it has already presented a credential this authenticator checked.
It exists so that a surface deciding whether it is safe to bind a non-loopback address can ask the authenticator instead of inspecting configuration fields. Inspecting fields is how core-agent's listenerAuthenticated ended up gating on a CA file path, which is absent under SPIFFE — a working mTLS listener that the check would refuse as unauthenticated. An authenticator knows what it enforces; a config field only knows how it was spelled.
It answers for the authenticator and not for the surface. Whether a request this authenticator rejects still reaches the handler is the middleware's decision — httpmw.CallerOptions.Enforce — so a bind policy asks the middleware, which reports this only when it is actually enforcing. See httpmw.CallerMiddleware.GatesCredentials. The distinction does not arise for a profile enforced at the transport, where the handshake has already refused the connection.
Authenticators that do not implement this interface are treated as not gating credentials, which is the conservative reading.
type IdentityLookup ¶
type IdentityLookup interface {
// LookupIdentity returns the Caller provisioned under identity.
//
// - Provisioned: the Caller and a nil error. A zero Caller with a
// nil error is a bug.
// - Not provisioned, including the empty string: an error
// wrapping purser.ErrAssertedCallerUnknown. This is a verdict,
// and httpmw answers it 401.
// - Could not tell — the store behind it did not answer: an error
// wrapping purser.ErrUnavailable, never ErrAssertedCallerUnknown
// as well. httpmw answers it 503, so the asserting proxy retries
// rather than concluding the identity does not exist
// (go-steer/purser#28).
//
// ctx is the request's. A remote lookup should honor it, so a
// client that hangs up does not leave a directory query running.
//
// As with Authenticate, the returned Caller's Labels must not
// alias state the authenticator retains — return
// purser.Caller.Clone. The result is handed to a handler, and a
// handler that adds a label to what it believes is its own copy
// would otherwise rewrite the provisioned identity for every later
// request.
LookupIdentity(ctx context.Context, identity string) (purser.Caller, error)
}
IdentityLookup is the optional extension implemented by authenticators backed by a table of provisioned identities. The proxy path uses it to materialize the asserted Caller with the Labels and Admin flag a direct authentication would have produced, and to reject assertions of identities nobody provisioned (purser.ErrAssertedCallerUnknown).
Claim-based authenticators (OIDC) have no such table and do not implement it; for them an asserted identity can only be taken at face value, which is why the proxy allowlist is the real control.
The table need not be in memory. A lookup backed by a directory or a user service can fail without saying anything about the identity, which is why the result is an error rather than a found bit, and why it takes the request's context.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package bearer authenticates callers against a static table of tokens.
|
Package bearer authenticates callers against a static table of tokens. |
|
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. |
|
Package oidc authenticates callers by OpenID Connect ID token.
|
Package oidc authenticates callers by OpenID Connect ID token. |