Files
lincodex/backend/db/migrations/00016_bind_registration_consent.sql

193 lines
6.9 KiB
PL/PgSQL

-- Bind the registration C1 to the registration whose pending credentials are
-- held by its verification token. Older registration code omits this column;
-- the insert trigger below fills it from the account row, which the
-- registration transaction updates before it grants C1.
-- +goose Up
ALTER TABLE consent_records
ADD COLUMN registration_version bigint;
-- Registration and re-registration delete the earlier registration C1 in
-- the same transaction that advances users.registration_version. The new C1
-- is granted after that update, so the current account version is the only
-- version an existing registration C1 can belong to. Refuse to guess if a
-- row predates the account's latest registration or its account is missing.
-- +goose StatementBegin
DO $$
BEGIN
IF EXISTS (
SELECT 1
FROM consent_records AS consent
LEFT JOIN users AS account ON account.id = consent.subject_id
WHERE consent.source = 'registration'
AND consent.purpose = 'c1'
AND (
account.id IS NULL
OR consent.granted_at < account.last_registered_at
)
) THEN
RAISE EXCEPTION 'registration C1 cannot be tied to the current account registration';
END IF;
IF EXISTS (
SELECT 1
FROM consent_records
WHERE source = 'registration' AND purpose = 'c1'
GROUP BY subject_id
HAVING count(*) FILTER (WHERE revoked_at IS NULL) > 1
) THEN
RAISE EXCEPTION 'account has more than one current registration C1';
END IF;
END;
$$;
-- +goose StatementEnd
-- Consent records are append-only. Temporarily remove the update guard only
-- for this one-time binding, then restore it with the new column protected.
DROP TRIGGER consent_records_refuse_change ON consent_records;
UPDATE consent_records AS consent
SET registration_version = account.registration_version
FROM users AS account
WHERE
consent.subject_id = account.id
AND consent.source = 'registration'
AND consent.purpose = 'c1';
ALTER TABLE consent_records
ADD CONSTRAINT consent_records_registration_version CHECK (
(
source = 'registration'
AND purpose = 'c1'
AND registration_version IS NOT NULL
AND registration_version > 0
)
OR (
source <> 'registration'
AND registration_version IS NULL
)
);
CREATE UNIQUE INDEX consent_records_registration_version_key
ON consent_records (subject_id, registration_version)
WHERE
source = 'registration'
AND purpose = 'c1'
AND revoked_at IS NULL;
-- The existing immutable trigger permits only a one-time revocation. It now
-- also protects registration_version from being changed with that stamp.
-- +goose StatementBegin
CREATE OR REPLACE FUNCTION consent_records_refuse_change() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF OLD.revoked_at IS NOT NULL
OR NEW.revoked_at IS NULL
OR (
NEW.id, NEW.subject_id, NEW.purpose, NEW.text_version,
NEW.text_hash, NEW.source, NEW.registration_version,
NEW.organization_id, NEW.organization_name, NEW.field_set,
NEW.invitation_id, NEW.invitation_terms_version, NEW.granted_at
) IS DISTINCT FROM (
OLD.id, OLD.subject_id, OLD.purpose, OLD.text_version,
OLD.text_hash, OLD.source, OLD.registration_version,
OLD.organization_id, OLD.organization_name, OLD.field_set,
OLD.invitation_id, OLD.invitation_terms_version, OLD.granted_at
) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'consent_records_immutable',
TABLE = 'consent_records',
MESSAGE = 'consent records are append-only: only an unrevoked record can be revoked, once';
END IF;
RETURN NEW;
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER consent_records_refuse_change
BEFORE UPDATE ON consent_records
FOR EACH ROW EXECUTE FUNCTION consent_records_refuse_change();
-- Fills the registration version for an older application binary and checks
-- values supplied by the new binary. The registration transaction updates
-- the account before it inserts C1, so this is the current version even when
-- the account row is already locked by that transaction.
-- +goose StatementBegin
CREATE FUNCTION consent_records_bind_registration_version() RETURNS trigger
LANGUAGE plpgsql AS $$
DECLARE
current_version bigint;
BEGIN
IF NEW.source <> 'registration' OR NEW.purpose <> 'c1' THEN
RETURN NEW;
END IF;
SELECT registration_version
INTO current_version
FROM users
WHERE id = NEW.subject_id;
IF NOT FOUND THEN
RETURN NEW;
END IF;
IF NEW.registration_version IS NULL THEN
NEW.registration_version := current_version;
ELSIF NEW.registration_version <> current_version THEN
RAISE EXCEPTION USING
ERRCODE = 'check_violation',
CONSTRAINT = 'consent_records_registration_version_matches_user',
TABLE = 'consent_records',
MESSAGE = 'registration C1 must match the current account registration';
END IF;
RETURN NEW;
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER consent_records_bind_registration_version
BEFORE INSERT ON consent_records
FOR EACH ROW EXECUTE FUNCTION consent_records_bind_registration_version();
-- +goose Down
DROP TRIGGER consent_records_bind_registration_version ON consent_records;
DROP FUNCTION consent_records_bind_registration_version();
DROP INDEX consent_records_registration_version_key;
DROP TRIGGER consent_records_refuse_change ON consent_records;
-- Restore the prior immutability check before removing its column.
-- +goose StatementBegin
CREATE OR REPLACE FUNCTION consent_records_refuse_change() RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
IF OLD.revoked_at IS NOT NULL
OR NEW.revoked_at IS NULL
OR (
NEW.id, NEW.subject_id, NEW.purpose, NEW.text_version,
NEW.text_hash, NEW.source, NEW.organization_id,
NEW.organization_name, NEW.field_set,
NEW.invitation_id, NEW.invitation_terms_version, NEW.granted_at
) IS DISTINCT FROM (
OLD.id, OLD.subject_id, OLD.purpose, OLD.text_version,
OLD.text_hash, OLD.source, OLD.organization_id,
OLD.organization_name, OLD.field_set,
OLD.invitation_id, OLD.invitation_terms_version, OLD.granted_at
) THEN
RAISE EXCEPTION USING
ERRCODE = 'integrity_constraint_violation',
CONSTRAINT = 'consent_records_immutable',
TABLE = 'consent_records',
MESSAGE = 'consent records are append-only: only an unrevoked record can be revoked, once';
END IF;
RETURN NEW;
END;
$$;
-- +goose StatementEnd
CREATE TRIGGER consent_records_refuse_change
BEFORE UPDATE ON consent_records
FOR EACH ROW EXECUTE FUNCTION consent_records_refuse_change();
ALTER TABLE consent_records
DROP CONSTRAINT consent_records_registration_version,
DROP COLUMN registration_version;