identity

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 15 Imported by: ? 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 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

View Source
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.

View Source
const (
	DefaultTTL          = time.Hour
	DefaultMinInterval  = 30 * time.Second
	DefaultFetchTimeout = 10 * time.Second
)

Defaults for the zero fields of RemoteJWKS.

View Source
const Header = "X-IAP-Identity"

Header is the request header that carries the identity JWT.

View Source
const Leeway = 30 * time.Second

Leeway is the clock skew allowed on exp, iat and nbf.

Variables

View Source
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.

View Source
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

func ParsePrincipal(s string) (Identity, error)

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

type KeySource interface {
	Key(ctx context.Context, kid string) (ed25519.PublicKey, error)
}

KeySource returns the issuer's public key for a kid.

func StaticJWKS

func StaticJWKS(doc []byte) (KeySource, error)

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.

func (*RemoteJWKS) Key

func (r *RemoteJWKS) Key(ctx context.Context, kid string) (ed25519.PublicKey, error)

Key returns the key for kid, fetching the set when it has none, when kid is unknown, or when the set is older than TTL, within the MinInterval bound. A caller that needs a fetch waits for it until ctx ends.

func (*RemoteJWKS) Refresh

func (r *RemoteJWKS) Refresh(ctx context.Context) error

Refresh fetches the set now, or waits for a fetch already running, and returns its error. Calling it in a goroutine at start-up loads the keys before the first request; Key works without it.

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

func NewVerifier(keys KeySource, issuer, host string) *Verifier

NewVerifier returns a Verifier for tokens Issuer (the iss claim, e.g. https://access.example.com) minted for host (aud https://<host>).

func (*Verifier) Host

func (v *Verifier) Host() string

Host is the host tokens are verified for.

func (*Verifier) Verify

func (v *Verifier) Verify(ctx context.Context, raw string) (Identity, error)

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.

Jump to

Keyboard shortcuts

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