414 lines
17 KiB
PL/PgSQL
414 lines
17 KiB
PL/PgSQL
-- +goose Up
|
|
-- Versioned consent ledger for the independent consents C1-C5 (PRD FR01,
|
|
-- FR12, and 7.3): the published consent texts and an append-only record of
|
|
-- every grant and revocation. Purposes are independent: a grant or a
|
|
-- revocation of one purpose never changes another.
|
|
--
|
|
-- consent_texts holds the published text of each purpose and version. A
|
|
-- grant references the text it was given for, so a client cannot invent a
|
|
-- version and the stored hash always belongs to a published text.
|
|
CREATE TABLE consent_texts (
|
|
-- c1 personal data processing, c2 publication of the candidate card, c3
|
|
-- automatic categorization, c4 transfer of selected contact data to one
|
|
-- organization, c5 import of FSP results.
|
|
purpose text NOT NULL,
|
|
-- Identifier that clients see as ConsentTextVersion, such as
|
|
-- "c1-2026-10-01".
|
|
version text NOT NULL,
|
|
-- Exact text in plain UTF-8. The C4 text contains the placeholder
|
|
-- {organization}, which the shown text replaces with the name of the
|
|
-- organization.
|
|
body text NOT NULL,
|
|
-- SHA-256 of the UTF-8 bytes of body, 32 raw bytes.
|
|
body_sha256 bytea NOT NULL,
|
|
-- "demo" marks a synthetic text for the demonstration on synthetic data
|
|
-- without a legal review; "operator" a text published by the actual
|
|
-- personal data operator, required before real data are accepted.
|
|
kind text NOT NULL,
|
|
-- The text applies to grants given at or after this time. The current
|
|
-- text of a purpose is the one with the latest published_at that is not
|
|
-- after the grant time.
|
|
published_at timestamptz NOT NULL,
|
|
CONSTRAINT consent_texts_pkey PRIMARY KEY (purpose, version),
|
|
-- Target of the hash binding of consent_records.
|
|
CONSTRAINT consent_texts_purpose_version_body_sha256_key UNIQUE (
|
|
purpose, version, body_sha256
|
|
),
|
|
CONSTRAINT consent_texts_purpose_published_at_key UNIQUE (
|
|
purpose, published_at
|
|
),
|
|
CONSTRAINT consent_texts_purpose_known CHECK (
|
|
purpose IN ('c1', 'c2', 'c3', 'c4', 'c5')
|
|
),
|
|
CONSTRAINT consent_texts_version_format CHECK (
|
|
version ~ '^[A-Za-z0-9._-]{1,64}$'
|
|
),
|
|
CONSTRAINT consent_texts_body_length CHECK (
|
|
char_length(body) BETWEEN 1 AND 100000
|
|
),
|
|
-- The database encoding is fixed for the life of a database, so
|
|
-- convert_to gives the same bytes on every evaluation.
|
|
CONSTRAINT consent_texts_body_sha256_matches CHECK (
|
|
body_sha256 = sha256(convert_to(body, 'UTF8'))
|
|
),
|
|
-- Only the C4 text names an organization.
|
|
CONSTRAINT consent_texts_organization_placeholder CHECK (
|
|
(purpose = 'c4') = (strpos(body, '{organization}') > 0)
|
|
),
|
|
CONSTRAINT consent_texts_kind_known CHECK (kind IN ('demo', 'operator'))
|
|
);
|
|
|
|
-- Refuses every UPDATE, DELETE, and TRUNCATE of consent texts: a grant
|
|
-- proves which text the subject saw only while that text stays unchanged.
|
|
-- A changed text is a new version. The error uses SQLSTATE 23000 and the
|
|
-- constraint name consent_texts_immutable, which the adapter maps without
|
|
-- reading the message.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION consent_texts_refuse_change() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'consent_texts_immutable',
|
|
TABLE = 'consent_texts',
|
|
MESSAGE = 'consent texts are immutable: publish a new version instead';
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER consent_texts_refuse_update_delete
|
|
BEFORE UPDATE OR DELETE ON consent_texts
|
|
FOR EACH ROW EXECUTE FUNCTION consent_texts_refuse_change();
|
|
|
|
CREATE TRIGGER consent_texts_refuse_truncate
|
|
BEFORE TRUNCATE ON consent_texts
|
|
FOR EACH STATEMENT EXECUTE FUNCTION consent_texts_refuse_change();
|
|
|
|
-- Demonstration texts, version 1 of every purpose. They are synthetic and
|
|
-- carry no legal review; the operator publishes its own texts as new
|
|
-- versions in a later migration before real data are accepted (PRD 7.3).
|
|
INSERT INTO consent_texts (
|
|
purpose, version, body, body_sha256, kind, published_at
|
|
)
|
|
SELECT
|
|
seed.purpose,
|
|
seed.version,
|
|
seed.body,
|
|
sha256(convert_to(seed.body, 'UTF8')),
|
|
'demo',
|
|
'2026-10-01T00:00:00Z'::timestamptz
|
|
FROM (
|
|
VALUES
|
|
(
|
|
'c1',
|
|
'c1-2026-10-01',
|
|
'I consent to the processing of my personal data for registration '
|
|
|| 'and the use of the service.' || E'\n\n'
|
|
|| 'The full text is published by the operator.'
|
|
),
|
|
(
|
|
'c2',
|
|
'c2-2026-10-01',
|
|
'I consent to the publication of my candidate card with the fields '
|
|
|| 'I select in the publication settings.' || E'\n\n'
|
|
|| 'The full text is published by the operator.'
|
|
),
|
|
(
|
|
'c3',
|
|
'c3-2026-10-01',
|
|
'I consent to the automatic categorization of my assessment '
|
|
|| 'results. Without this consent I can request a manual assessment '
|
|
|| 'by the same rubric.' || E'\n\n'
|
|
|| 'The full text is published by the operator.'
|
|
),
|
|
(
|
|
'c4',
|
|
'c4-2026-10-01',
|
|
'I consent to the transfer of the contact data I select to '
|
|
|| '{organization} for this invitation.' || E'\n\n'
|
|
|| 'The full text is published by the operator.'
|
|
),
|
|
(
|
|
'c5',
|
|
'c5-2026-10-01',
|
|
'I consent to the import of my FSP results into my profile.'
|
|
|| E'\n\n'
|
|
|| 'The full text is published by the operator.'
|
|
)
|
|
) AS seed (purpose, version, body);
|
|
|
|
-- Reports whether a C4 field set is canonical: one to four distinct known
|
|
-- contact kinds (ContactFieldKind) in ascending byte order without NULLs.
|
|
-- A canonical form makes equal sets equal arrays.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION consent_field_set_is_canonical(fields text []) RETURNS boolean
|
|
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE AS $$
|
|
SELECT
|
|
array_ndims(fields) = 1
|
|
AND cardinality(fields) BETWEEN 1 AND 4
|
|
AND array_position(fields, NULL) IS NULL
|
|
AND fields <@ ARRAY['email', 'full_name', 'phone', 'telegram']::text []
|
|
AND fields = ARRAY(
|
|
SELECT DISTINCT field COLLATE "C"
|
|
FROM unnest(fields) AS field
|
|
ORDER BY field COLLATE "C"
|
|
);
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
-- One row per grant. A grant is never updated except for the one-time
|
|
-- revocation stamp: a new text version or a grant after a revocation is a
|
|
-- new row, so the history of a purpose is the sequence of its rows. Every
|
|
-- time comes from the application clock.
|
|
--
|
|
-- The rows are personal data of the subject: never copy them into logs,
|
|
-- metrics, or events beyond the record ID and purpose.
|
|
CREATE TABLE consent_records (
|
|
-- UUIDv7 generated in Go.
|
|
id uuid NOT NULL,
|
|
-- The account that gave the consent.
|
|
subject_id uuid NOT NULL,
|
|
purpose text NOT NULL,
|
|
text_version text NOT NULL,
|
|
-- SHA-256 of exactly the text the subject saw, 32 raw bytes. For C1-C3
|
|
-- and C5 it equals the hash of the published text; for C4 it is the
|
|
-- hash of the published text with organization_name filled in, which a
|
|
-- trigger checks.
|
|
text_hash bytea NOT NULL,
|
|
-- The hash that must equal the published text: text_hash except for
|
|
-- C4, NULL for C4, so the composite foreign key below skips C4.
|
|
published_text_hash bytea GENERATED ALWAYS AS (
|
|
CASE WHEN purpose <> 'c4' THEN text_hash END
|
|
) STORED,
|
|
-- Where the grant was given: registration (C1 only), consent_settings
|
|
-- (C1-C3 and C5), or invitation_acceptance (C4 only).
|
|
source text NOT NULL,
|
|
-- C4 scope: the organization, the name of the organization as the
|
|
-- shown text named it (a snapshot that a later rename never changes),
|
|
-- the canonical set of contact kinds, and the invitation and its terms
|
|
-- version that the acceptance accepted. NULL for every other purpose.
|
|
-- The organization and invitation tables do not exist yet; their
|
|
-- foreign keys arrive with them.
|
|
organization_id uuid,
|
|
organization_name text,
|
|
field_set text [],
|
|
invitation_id uuid,
|
|
invitation_terms_version bigint,
|
|
granted_at timestamptz NOT NULL,
|
|
-- Set once; the row is never updated again.
|
|
revoked_at timestamptz,
|
|
-- consent_settings: the subject revoked it in the product;
|
|
-- support_request: an operator recorded a revocation the subject asked
|
|
-- for through support.
|
|
revocation_source text,
|
|
CONSTRAINT consent_records_pkey PRIMARY KEY (id),
|
|
-- Consent evidence must outlive a careless account delete: an account
|
|
-- with consent history cannot be deleted until the deleting procedure
|
|
-- handles the ledger explicitly (PRD 7.3 keeps consents for three
|
|
-- years). The unverified account cleanup deletes the records of the
|
|
-- accounts it removes in the same statement, and the re-registration of
|
|
-- an unverified account deletes its registration C1.
|
|
CONSTRAINT consent_records_subject_id_fkey FOREIGN KEY (subject_id)
|
|
REFERENCES users (id) ON DELETE RESTRICT,
|
|
CONSTRAINT consent_records_text_fkey FOREIGN KEY (
|
|
purpose, text_version
|
|
) REFERENCES consent_texts (purpose, version),
|
|
-- MATCH SIMPLE skips the check when published_text_hash is NULL (C4).
|
|
CONSTRAINT consent_records_published_text_fkey FOREIGN KEY (
|
|
purpose, text_version, published_text_hash
|
|
) REFERENCES consent_texts (purpose, version, body_sha256),
|
|
CONSTRAINT consent_records_purpose_known CHECK (
|
|
purpose IN ('c1', 'c2', 'c3', 'c4', 'c5')
|
|
),
|
|
CONSTRAINT consent_records_text_version_format CHECK (
|
|
text_version ~ '^[A-Za-z0-9._-]{1,64}$'
|
|
),
|
|
CONSTRAINT consent_records_text_hash_sha256 CHECK (
|
|
octet_length(text_hash) = 32
|
|
),
|
|
CONSTRAINT consent_records_source_of_purpose CHECK (
|
|
(purpose = 'c1' AND source IN ('registration', 'consent_settings'))
|
|
OR (purpose IN ('c2', 'c3', 'c5') AND source = 'consent_settings')
|
|
OR (purpose = 'c4' AND source = 'invitation_acceptance')
|
|
),
|
|
-- C4 always has its scope; no other purpose has one.
|
|
CONSTRAINT consent_records_c4_scope CHECK (
|
|
(
|
|
purpose = 'c4'
|
|
AND organization_id IS NOT NULL
|
|
AND organization_name IS NOT NULL
|
|
AND char_length(organization_name) BETWEEN 1 AND 200
|
|
AND organization_name !~ '[[:cntrl:]]'
|
|
AND field_set IS NOT NULL
|
|
AND consent_field_set_is_canonical(field_set)
|
|
AND invitation_id IS NOT NULL
|
|
AND invitation_terms_version IS NOT NULL
|
|
AND invitation_terms_version > 0
|
|
)
|
|
OR (
|
|
purpose <> 'c4'
|
|
AND organization_id IS NULL
|
|
AND organization_name IS NULL
|
|
AND field_set IS NULL
|
|
AND invitation_id IS NULL
|
|
AND invitation_terms_version IS NULL
|
|
)
|
|
),
|
|
CONSTRAINT consent_records_revocation_stamp CHECK (
|
|
(revoked_at IS NULL) = (revocation_source IS NULL)
|
|
AND (
|
|
revocation_source IS NULL
|
|
OR revocation_source IN ('consent_settings', 'support_request')
|
|
)
|
|
AND (revoked_at IS NULL OR revoked_at >= granted_at)
|
|
)
|
|
);
|
|
|
|
-- Serves the subject's history, newest first, and the foreign key from
|
|
-- users.
|
|
CREATE INDEX consent_records_subject_granted_idx
|
|
ON consent_records (subject_id, granted_at DESC, id DESC);
|
|
|
|
-- One unrevoked grant per subject, purpose, and text version: repeating a
|
|
-- grant of the same version returns the existing record. A newer version is
|
|
-- a new row next to the older one.
|
|
CREATE UNIQUE INDEX consent_records_active_version_key
|
|
ON consent_records (subject_id, purpose, text_version)
|
|
WHERE revoked_at IS NULL AND purpose <> 'c4';
|
|
|
|
-- One C4 grant per invitation, revoked or not: a repeated acceptance
|
|
-- returns the existing record, and a revoked C4 never comes back.
|
|
CREATE UNIQUE INDEX consent_records_invitation_key
|
|
ON consent_records (invitation_id)
|
|
WHERE invitation_id IS NOT NULL;
|
|
|
|
-- Serves the current C4 grants of a subject for one organization and their
|
|
-- revocation.
|
|
CREATE INDEX consent_records_active_organization_idx
|
|
ON consent_records (subject_id, organization_id)
|
|
WHERE purpose = 'c4' AND revoked_at IS NULL;
|
|
|
|
-- Refuses a grant for a text version that is not published at the grant
|
|
-- time (SQLSTATE 23514, constraint consent_records_text_published) and a C4
|
|
-- grant whose hash is not the hash of the published text with its
|
|
-- organization_name filled in (23514, consent_records_c4_text_hash). A
|
|
-- version that does not exist at all is left to the foreign key, and a C4
|
|
-- row without a name to consent_records_c4_scope.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION consent_records_require_published_text() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF EXISTS (
|
|
SELECT 1 FROM consent_texts AS text
|
|
WHERE
|
|
text.purpose = NEW.purpose
|
|
AND text.version = NEW.text_version
|
|
AND text.published_at > NEW.granted_at
|
|
) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'check_violation',
|
|
CONSTRAINT = 'consent_records_text_published',
|
|
TABLE = 'consent_records',
|
|
MESSAGE = 'the consent text is not published at the grant time';
|
|
END IF;
|
|
IF NEW.purpose = 'c4' AND NEW.organization_name IS NOT NULL AND EXISTS (
|
|
SELECT 1 FROM consent_texts AS text
|
|
WHERE
|
|
text.purpose = NEW.purpose
|
|
AND text.version = NEW.text_version
|
|
AND sha256(convert_to(
|
|
replace(text.body, '{organization}', NEW.organization_name),
|
|
'UTF8'
|
|
)) IS DISTINCT FROM NEW.text_hash
|
|
) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'check_violation',
|
|
CONSTRAINT = 'consent_records_c4_text_hash',
|
|
TABLE = 'consent_records',
|
|
MESSAGE = 'the C4 hash does not match the published text for the organization name';
|
|
END IF;
|
|
RETURN NEW;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER consent_records_require_published_text
|
|
BEFORE INSERT ON consent_records
|
|
FOR EACH ROW EXECUTE FUNCTION consent_records_require_published_text();
|
|
|
|
-- Allows exactly one UPDATE of a record: setting revoked_at and
|
|
-- revocation_source on an unrevoked record, with every other column
|
|
-- unchanged. Every other UPDATE is refused with SQLSTATE 23000 and the
|
|
-- constraint name consent_records_immutable; the message quotes no row
|
|
-- values. Deletes are guarded separately below.
|
|
-- +goose StatementBegin
|
|
CREATE 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();
|
|
|
|
-- Allows deleting a record only while its subject is not a confirmed
|
|
-- account: the unverified account cleanup and the re-registration of an
|
|
-- unverified account. The ledger of a confirmed account is consent
|
|
-- evidence and stays (SQLSTATE 23000, constraint consent_records_retained).
|
|
-- The retention rule for consents (PRD 7.3, three years) and the account
|
|
-- deletion procedure will extend this condition in their own migration.
|
|
-- TRUNCATE fires no row trigger and stays allowed for test isolation.
|
|
-- +goose StatementBegin
|
|
CREATE FUNCTION consent_records_refuse_delete() RETURNS trigger
|
|
LANGUAGE plpgsql AS $$
|
|
BEGIN
|
|
IF EXISTS (
|
|
SELECT 1 FROM users
|
|
WHERE users.id = OLD.subject_id AND users.email_verified_at IS NOT NULL
|
|
) THEN
|
|
RAISE EXCEPTION USING
|
|
ERRCODE = 'integrity_constraint_violation',
|
|
CONSTRAINT = 'consent_records_retained',
|
|
TABLE = 'consent_records',
|
|
MESSAGE = 'consent records of a confirmed account are retained';
|
|
END IF;
|
|
RETURN OLD;
|
|
END;
|
|
$$;
|
|
-- +goose StatementEnd
|
|
|
|
CREATE TRIGGER consent_records_refuse_delete
|
|
BEFORE DELETE ON consent_records
|
|
FOR EACH ROW EXECUTE FUNCTION consent_records_refuse_delete();
|
|
|
|
-- +goose Down
|
|
DROP TABLE consent_records;
|
|
DROP TABLE consent_texts;
|
|
DROP FUNCTION consent_records_refuse_delete();
|
|
DROP FUNCTION consent_records_refuse_change();
|
|
DROP FUNCTION consent_records_require_published_text();
|
|
DROP FUNCTION consent_field_set_is_canonical(text []);
|
|
DROP FUNCTION consent_texts_refuse_change();
|