Documentation
¶
Overview ¶
Package auth authenticates requests to the query endpoint with OIDC JWT bearer tokens and authorises them by role, as decided in docs/content/docs/decisions/0006-authentication.md.
A Guard verifies a token against the configured issuers, which turns it into a Principal, checks the principal's role on a database against the configured rules, and wraps an http.Handler to do both for each request. Verification failures are *Error values with a stable Code; neither errors nor log lines contain a token or any part of one.
Index ¶
Constants ¶
const ( // MaxTokenSize is the largest token accepted, in bytes. MaxTokenSize = 16 << 10 // ClockSkew is the allowance applied to exp, nbf and iat. ClockSkew = time.Minute )
Verification policy, fixed by decision record 0006.
Variables ¶
This section is empty.
Functions ¶
func BearerToken ¶
BearerToken returns the token of the request's single Authorization header with the Bearer scheme, or an *Error with CodeMissingToken or CodeMalformed.
Types ¶
type Code ¶
type Code string
Code is a stable identifier for why a request was refused.
const ( CodeMissingToken Code = "missing_token" CodeTokenTooLarge Code = "token_too_large" CodeMalformed Code = "malformed_token" CodeUnsupportedAlgorithm Code = "unsupported_algorithm" CodeUntrustedIssuer Code = "untrusted_issuer" CodeUnknownKey Code = "unknown_key" CodeInvalidSignature Code = "invalid_signature" CodeMissingClaim Code = "missing_claim" CodeInvalidAudience Code = "invalid_audience" CodeExpired Code = "token_expired" CodeNotYetValid Code = "token_not_yet_valid" CodeIssuedInFuture Code = "issued_in_future" CodeForbidden Code = "forbidden" )
The codes. Clients only ever see whether a token was missing, invalid or insufficient; the specific code is for server logs.
type Config ¶
type Config struct {
// Issuers are the token issuers the server trusts. At least one is required.
Issuers []Issuer `json:"issuers"`
// Rules grant roles. A principal no rule matches is denied everything.
Rules []Rule `json:"rules"`
}
Config is the authentication and authorisation configuration of a server.
type Error ¶
Error is a refused token or request. Its fields are safe to log: Issuer is set only when it equals a configured issuer, Subject only when the token's signature verified and then cut to 200 bytes, and KeyID only when it is at most 64 bytes long.
type Guard ¶
type Guard struct {
// contains filtered or unexported fields
}
Guard verifies bearer tokens and authorises principals.
func New ¶
New validates cfg and returns a Guard. client fetches discovery documents and key sets; each fetch is bounded by its own timeout, so client needs none, and a copy of it that follows no redirect is used. now is the clock and log receives refusals and key refresh failures. A nil client, now or log means http.DefaultClient, time.Now or no logging. New makes no network request.
func (*Guard) Authorize ¶
Authorize reports whether p holds at least role need on database. It is deny by default: without a matching rule the answer is false, and a name that is not a valid database name is denied before any rule is read. Subjects, claim values and names are compared exactly, byte for byte.
func (*Guard) LoadKeyFiles ¶
LoadKeyFiles reads the key set of every issuer configured with a JWKS file, as a first refresh, and fails if one cannot be read or holds no usable key. Later refreshes follow the cache policy.
func (*Guard) Require ¶
func (g *Guard) Require(need Role, database func(*http.Request) string, next http.Handler) http.Handler
Require returns a handler that serves next only if the request carries an "Authorization: Bearer" token that verifies, and its principal holds at least role need on the database that database returns for the request. next finds the principal with PrincipalFromContext.
A request without a bearer token gets 401 with "WWW-Authenticate: Bearer"; one whose token does not verify gets 401 with error="invalid_token"; an authenticated principal without the role gets 403. The response names no more than that; the specific Code is logged.
type Issuer ¶
type Issuer struct {
// Issuer is the exact value tokens carry in their iss claim. Unless
// JWKSFile is set, it must be an https URL without query, fragment, user
// info or white space, and the issuer's discovery
// document is fetched from Issuer + "/.well-known/openid-configuration".
Issuer string `json:"issuer"`
// Audience is the value a token's aud claim must contain.
Audience string `json:"audience"`
// JWKSFile, if set, is the path of a JSON Web Key Set file holding the
// issuer's public keys. No network request is then made for this issuer.
JWKSFile string `json:"jwks_file"`
}
Issuer is one trusted token issuer.
type Principal ¶
type Principal struct {
Issuer string
Subject string
Claims map[string]any
// Expires is the token's exp plus ClockSkew: the time from which Verify
// refuses the token. The principal must not be used after it, for example
// on a long-lived connection.
Expires time.Time
}
Principal is an authenticated caller: the pair (Issuer, Subject), with the claims of the token that authenticated it.
func PrincipalFromContext ¶
PrincipalFromContext returns the principal Require stored in ctx.
type Role ¶
type Role string
Role is a level of access to a database. Each role includes the ones before it: read, then write, then admin.
type Rule ¶
type Rule struct {
Issuer string `json:"issuer"`
// Subject matches the sub claim exactly. Exactly one of Subject and
// Claim must be set.
Subject string `json:"subject"`
// Claim names a top-level claim, for example "groups" or "roles".
Claim string `json:"claim"`
// Value is the string the claim must equal or contain.
Value string `json:"value"`
// Database is a database name, which is 1 to 63 bytes of lower-case ASCII
// letters, digits and '-', starting with a letter or a digit, or a pattern.
// A pattern may also hold '*', which matches any run of characters, and
// '?', which matches one character. It is 1 to 63 bytes long and starts
// with a letter, a digit, '*' or '?'.
Database string `json:"database"`
Role Role `json:"role"`
}
Rule grants Role on the databases matching Database to the principals of Issuer that have the given Subject, or, if Claim is set instead, whose token has the claim Claim equal to Value or, for an array claim, containing it.