Documentation
¶
Overview ¶
Package identity verifies the X-IAP-Identity header an iap-authz identity-aware proxy attaches to every request it allows: an EdDSA JWT naming the caller's principal, bound to one host.
A backend builds one Verifier for its own host with a key source, usually RemoteJWKS on the issuer's JWKS URL, or StaticJWKS on a fixed document, and calls Verify on each request. Verify returns only a canonical, concrete principal of a type this package knows: user:, sa:, agent:user and agent:k8s. Which of those a backend accepts is the backend's own decision.
The package imports only the standard library.
Index ¶
Constants ¶
const ( KindUser = "user" KindSA = "sa" KindAgent = "agent" AgentUser = "user" // agent:user:<email>:<machine>:<program>:<session>, owned by user:<email> AgentK8s = "k8s" // agent:k8s:<cluster>:<workload>, no owner )
Principal kinds and agent types a verified identity can have.
const ( DefaultTTL = time.Hour DefaultMinInterval = 30 * time.Second DefaultFetchTimeout = 10 * time.Second )
Defaults for the zero fields of RemoteJWKS.
const Header = "X-IAP-Identity"
Header is the request header that carries the identity JWT.
const Leeway = 30 * time.Second
Leeway is the clock skew allowed on exp, iat and nbf.
Variables ¶
var ErrNoKeys = errors.New("no JWKS loaded")
ErrNoKeys is returned while a RemoteJWKS has never loaded a key set. It wraps the last fetch's error, if any.
var ErrUnknownKey = errors.New("unknown key")
ErrUnknownKey is returned for a kid the key source does not hold.
Functions ¶
This section is empty.
Types ¶
type Identity ¶
type Identity struct {
Principal string // canonical, e.g. user:alice@example.com
Kind string // KindUser, KindSA or KindAgent
AgentType string // KindAgent only: AgentUser or AgentK8s
Email string // KindUser only
Owner string // agent:user only: user:<email>
}
Identity is a verified caller.
func ParsePrincipal ¶
ParsePrincipal parses a canonical (lowercase) concrete principal of a known type. Patterns, group: and gh: (grant-side names, never a caller), agent:test (which nothing can present to the proxy) and unknown types are errors.
type KeySource ¶
KeySource returns the issuer's public key for a kid.
func StaticJWKS ¶
StaticJWKS is a key source fixed from a JWKS document, such as one pinned in configuration or built by a test. A key rotation means a new document.
type RemoteJWKS ¶
type RemoteJWKS struct {
URL string
Client *http.Client // nil: http.DefaultClient
TTL time.Duration // zero: DefaultTTL
MinInterval time.Duration // zero: DefaultMinInterval
FetchTimeout time.Duration // zero: DefaultFetchTimeout; bounds each fetch
// OnFetch, if set, is called after every fetch with its error (nil on
// success), for logging or metrics, on the goroutine that fetched.
OnFetch func(err error)
Now func() time.Time // nil: time.Now
// contains filtered or unexported fields
}
RemoteJWKS is a key source that fetches the issuer's JWKS document from URL, the JWKS URL the proxy publishes.
It fetches on first use and keeps the set for TTL, then refreshes it in the background while still answering from it. A token naming a kid the set does not hold triggers a fetch, but fetches never start more often than once per MinInterval, so a flood of made-up kids cannot hammer the issuer. A failed fetch keeps the last good set. Until a set has loaded, Key returns ErrNoKeys. Set the fields before the first Key call and leave them alone after.
type Verifier ¶
type Verifier struct {
// Now is the clock; nil means time.Now.
Now func() time.Time
// contains filtered or unexported fields
}
Verifier checks identity tokens issued by Issuer for one host.
func NewVerifier ¶
NewVerifier returns a Verifier for tokens Issuer (the iss claim, e.g. https://access.example.com) minted for host (aud https://<host>).
func (*Verifier) Verify ¶
Verify checks raw, the X-IAP-Identity header's value, and returns its principal. Anything short of a fully valid token for this host is an error.
The audience check is what keeps a backend, which receives its visitors' genuine tokens for its own host, from replaying one to another host.