Files
tenant-registry/migrations/0015_api_keys_scoped.up.sql
T
sharang 97284b5008
ci / shared (push) Successful in 14s
ci / test (push) Successful in 25m3s
ci / image (push) Successful in 39s
feat(api-keys): Scope catalogue, kinds, expiry and sha256 keys (#70)
2026-10-13 21:21:16 +00:00

19 lines
953 B
SQL

-- Tenant-bound, scoped API keys (ADR: prerequisite 1 of the Werkmeister plan).
-- kind: user (portal, a person's key), ci (CLI licences in CI), integration
-- (held by a product for a tenant, e.g. Werkmeister). New keys are stored as
-- sha256 of the 192-bit random key (indexed lookup); rows created before this
-- migration keep their argon2id hash.
ALTER TABLE api_keys
ADD COLUMN kind TEXT NOT NULL DEFAULT 'user' CHECK (kind IN ('user', 'ci', 'integration')),
ADD COLUMN holder TEXT,
ADD COLUMN expires_at TIMESTAMPTZ,
ADD COLUMN hash_sha256 BYTEA;
ALTER TABLE api_keys ALTER COLUMN hash DROP NOT NULL;
ALTER TABLE api_keys
ADD CONSTRAINT api_keys_hash_present CHECK (hash IS NOT NULL OR hash_sha256 IS NOT NULL),
ADD CONSTRAINT api_keys_holder_kind CHECK ((kind = 'integration') = (holder IS NOT NULL));
CREATE UNIQUE INDEX api_keys_hash_sha256_idx ON api_keys (hash_sha256) WHERE hash_sha256 IS NOT NULL;