plugins/apikeys
API keys owned by users and by organizations (pk_live_…, pk_test_…), each carrying a subset of its
owner’s permissions, an optional rate limit of its own, an expiry, rotation with a grace window and
revocation. Presented as Authorization: Bearer pk_… or x-api-key: pk_…, a key authenticates every
Polaris route and every host route behind p.Protect as its owner, delegated: the gate intersects the
owner’s current permissions with the key’s, so a key never does more than its owner may. It
works alone, and with plugins/audit registered every change is recorded.
Install
Section titled “Install”import "github.com/univeros/polaris.go/plugins/apikeys"
cfg, err := polaris.FromEnv(db, audit.New(audit.Config{}), apikeys.New(apikeys.Config{}))p, err := polaris.New(cfg)_, err = p.Migrate(ctx) // creates polaris_api_key
mux.Handle("/", p.Handler()) // serves /api-keysmux.Handle("GET /api/invoices", p.Protect(h, "invoices.read")) // keys pass, as their owner// in h: who, _ := polaris.PrincipalFrom(r.Context()); who.Kind == polaris.KindAPIKey, who.KeyID, who.PermissionsConfig
Section titled “Config”Every field is optional; the zero value is the defaults.
| Field | Default | Purpose |
|---|---|---|
Environment | apikeys.EnvLive | prefix of keys created without one (EnvLive or EnvTest) |
RotationGrace | 24 h (DefaultRotationGrace) | how long a rotated key’s predecessor keeps answering; negative: stops at once |
MaxPerOwner | 50 (DefaultMaxPerOwner) | live keys per user or organization |
Routes
Section titled “Routes”| Method | Path | Purpose |
|---|---|---|
| GET | /api-keys | the caller’s live keys, newest first; organization_id lists the organization’s |
| POST | /api-keys | step-up gated: create a key; the secret is in this response only |
| GET | /api-keys/{id} | read a key (never the secret) |
| PATCH | /api-keys/{id} | change name, permissions, rate limit, expiry, metadata |
| DELETE | /api-keys/{id} | revoke at once |
| POST | /api-keys/{id}/rotate | step-up gated: new secret, acting as the caller; the old one answers until the grace ends |
| POST | /api-keys/verify | check a key (key in the body) for a proxy that checks keys itself: valid as the key itself would be accepted, and the permissions its user still holds |
Create body: name (required, ≤ 80), permissions (subset of what the caller holds in the active
organization), organization_id (makes it the organization’s key; needs org.update), environment
(live/test), rate_limit ({"window": seconds, "max": requests}), expires_at, metadata.
Each is read as its JSON type: name a string, permissions a list, rate_limit and metadata
objects ([] is refused); anything else is 422 invalid_input. Times are answered in RFC 3339, UTC
(2026-11-22T10:00:00Z); expires_at accepts any RFC 3339 offset.
Events and permissions
Section titled “Events and permissions”Emits apikeys.Event with apikeys.AuditCreated (api_keys.created), AuditUpdated, AuditRotated,
AuditRevoked; it implements audit.Auditable and Plugin.AuditNames adds the names to the catalog.
Contributes no permissions; organization keys need core’s org.update
(apikeys.OrganizationPermission). Keys of an erased user or organization, and a departing member’s
organization keys, are deleted by the plugin’s listeners.
Problems
Section titled “Problems”https://polaris.univeros.io/problems/api-keys/not_found (404), …/api-keys/forbidden (403),
…/api-keys/permission_not_held (403, a permission the caller does not hold), …/api-keys/too_many
(409, MaxPerOwner reached), …/api-keys/invalid_input (422), …/api-keys/not_allowed (403, a
step-up route called with a key).
Host integration
Section titled “Host integration”- In a handler:
who, _ := polaris.PrincipalFrom(r.Context())—who.Kindispolaris.KindAPIKey,who.KeyIDthe key id,who.UserIDits owner,who.Permissionsthe key’s permissions intersected with the owner’s; the AMR isapikeys.AMR. - Mint, rotate, revoke from code:
apikeys.Of(g).Keys()(Create(ctx, apikeys.CreateParams{…})returnsapikeys.IssuedwithSecret, shown once;Rotate,Revoke,Update,Authenticate). Refusals matchapikeys.ErrNotFound,ErrTooMany,ErrPermissionNotHeld, … witherrors.Is. - A key’s own
rate_limitsets theX-RateLimit-*headers over core’s. apikeys.Presented(r)returns the secret a request carries (x-api-keyfirst, then the bearer).- Without the audit plugin the plugin still boots; nothing is recorded.
Example
Section titled “Example”Example in example_test.go — go doc github.com/univeros/polaris.go/plugins/apikeys.