feat(consent): add versioned append-only consent ledger C1-C5
This commit is contained in:
1 parent
008254e44a
commit
de49f93b0b
64 files changed
+9244
-132
No files matched your search
@@ -0,0 +1,377 @@
|
||||
-- name: GetConsentText :one
|
||||
SELECT
|
||||
purpose,
|
||||
version,
|
||||
body,
|
||||
body_sha256,
|
||||
kind,
|
||||
published_at
|
||||
FROM consent_texts
|
||||
WHERE
|
||||
purpose = sqlc.arg(purpose)
|
||||
AND version = sqlc.arg(version);
|
||||
|
||||
-- The text of a purpose that applies at a time: the latest publication not
|
||||
-- after it.
|
||||
-- name: GetCurrentConsentText :one
|
||||
SELECT
|
||||
purpose,
|
||||
version,
|
||||
body,
|
||||
body_sha256,
|
||||
kind,
|
||||
published_at
|
||||
FROM consent_texts
|
||||
WHERE
|
||||
purpose = sqlc.arg(purpose)
|
||||
AND published_at <= sqlc.arg(at)
|
||||
ORDER BY published_at DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- Records a grant of C1, C2, C3, or C5. An unrevoked record of the same
|
||||
-- subject, purpose, and text version already exists when no row returns;
|
||||
-- the insert then changes nothing and does not abort the transaction.
|
||||
-- name: InsertConsentRecord :one
|
||||
INSERT INTO consent_records (
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
granted_at
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(subject_id),
|
||||
sqlc.arg(purpose),
|
||||
sqlc.arg(text_version),
|
||||
sqlc.arg(text_hash),
|
||||
sqlc.arg(source),
|
||||
sqlc.arg(granted_at)
|
||||
)
|
||||
ON CONFLICT (subject_id, purpose, text_version)
|
||||
WHERE revoked_at IS NULL AND purpose <> 'c4'
|
||||
DO NOTHING
|
||||
RETURNING
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
organization_id,
|
||||
organization_name,
|
||||
field_set,
|
||||
invitation_id,
|
||||
invitation_terms_version,
|
||||
granted_at,
|
||||
revoked_at,
|
||||
revocation_source,
|
||||
(
|
||||
CASE
|
||||
WHEN exists(
|
||||
SELECT 1 FROM consent_records AS newer
|
||||
WHERE
|
||||
newer.subject_id = consent_records.subject_id
|
||||
AND newer.purpose = consent_records.purpose
|
||||
AND newer.revoked_at IS NULL
|
||||
AND (newer.granted_at, newer.id)
|
||||
> (consent_records.granted_at, consent_records.id)
|
||||
) THEN 'superseded'
|
||||
ELSE 'active'
|
||||
END
|
||||
)::text AS state;
|
||||
|
||||
-- Records a C4 grant with its scope. The invitation already has a C4 record
|
||||
-- when no row returns; the insert then changes nothing and does not abort
|
||||
-- the transaction.
|
||||
-- name: InsertContactConsentRecord :one
|
||||
INSERT INTO consent_records (
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
organization_id,
|
||||
organization_name,
|
||||
field_set,
|
||||
invitation_id,
|
||||
invitation_terms_version,
|
||||
granted_at
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(subject_id),
|
||||
'c4',
|
||||
sqlc.arg(text_version),
|
||||
sqlc.arg(text_hash),
|
||||
'invitation_acceptance',
|
||||
sqlc.arg(organization_id),
|
||||
sqlc.arg(organization_name),
|
||||
sqlc.arg(field_set)::text [],
|
||||
sqlc.arg(invitation_id),
|
||||
sqlc.arg(invitation_terms_version),
|
||||
sqlc.arg(granted_at)
|
||||
)
|
||||
ON CONFLICT (invitation_id) WHERE invitation_id IS NOT NULL
|
||||
DO NOTHING
|
||||
RETURNING
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
organization_id,
|
||||
organization_name,
|
||||
field_set,
|
||||
invitation_id,
|
||||
invitation_terms_version,
|
||||
granted_at,
|
||||
revoked_at,
|
||||
revocation_source,
|
||||
'active'::text AS state;
|
||||
|
||||
-- name: GetUnrevokedConsentRecordByVersion :one
|
||||
SELECT
|
||||
record.id,
|
||||
record.subject_id,
|
||||
record.purpose,
|
||||
record.text_version,
|
||||
record.text_hash,
|
||||
record.source,
|
||||
record.organization_id,
|
||||
record.organization_name,
|
||||
record.field_set,
|
||||
record.invitation_id,
|
||||
record.invitation_terms_version,
|
||||
record.granted_at,
|
||||
record.revoked_at,
|
||||
record.revocation_source,
|
||||
(
|
||||
CASE
|
||||
WHEN exists(
|
||||
SELECT 1 FROM consent_records AS newer
|
||||
WHERE
|
||||
newer.subject_id = record.subject_id
|
||||
AND newer.purpose = record.purpose
|
||||
AND newer.revoked_at IS NULL
|
||||
AND (newer.granted_at, newer.id)
|
||||
> (record.granted_at, record.id)
|
||||
) THEN 'superseded'
|
||||
ELSE 'active'
|
||||
END
|
||||
)::text AS state
|
||||
FROM consent_records AS record
|
||||
WHERE
|
||||
record.subject_id = sqlc.arg(subject_id)
|
||||
AND record.purpose = sqlc.arg(purpose)
|
||||
AND record.text_version = sqlc.arg(text_version)
|
||||
AND record.revoked_at IS NULL;
|
||||
|
||||
-- name: GetContactConsentRecordByInvitation :one
|
||||
SELECT
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
organization_id,
|
||||
organization_name,
|
||||
field_set,
|
||||
invitation_id,
|
||||
invitation_terms_version,
|
||||
granted_at,
|
||||
revoked_at,
|
||||
revocation_source,
|
||||
(
|
||||
CASE WHEN revoked_at IS NULL THEN 'active' ELSE 'revoked' END
|
||||
)::text AS state
|
||||
FROM consent_records
|
||||
WHERE
|
||||
subject_id = sqlc.arg(subject_id)
|
||||
AND invitation_id = sqlc.arg(invitation_id);
|
||||
|
||||
-- The newest unrevoked record of a purpose other than C4.
|
||||
-- name: GetCurrentConsentRecord :one
|
||||
SELECT
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
organization_id,
|
||||
organization_name,
|
||||
field_set,
|
||||
invitation_id,
|
||||
invitation_terms_version,
|
||||
granted_at,
|
||||
revoked_at,
|
||||
revocation_source,
|
||||
'active'::text AS state
|
||||
FROM consent_records
|
||||
WHERE
|
||||
subject_id = sqlc.arg(subject_id)
|
||||
AND purpose = sqlc.arg(purpose)
|
||||
AND purpose <> 'c4'
|
||||
AND revoked_at IS NULL
|
||||
ORDER BY granted_at DESC, id DESC
|
||||
LIMIT 1;
|
||||
|
||||
-- name: ListCurrentContactConsentRecords :many
|
||||
SELECT
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
organization_id,
|
||||
organization_name,
|
||||
field_set,
|
||||
invitation_id,
|
||||
invitation_terms_version,
|
||||
granted_at,
|
||||
revoked_at,
|
||||
revocation_source,
|
||||
'active'::text AS state
|
||||
FROM consent_records
|
||||
WHERE
|
||||
subject_id = sqlc.arg(subject_id)
|
||||
AND purpose = 'c4'
|
||||
AND organization_id = sqlc.arg(organization_id)
|
||||
AND revoked_at IS NULL
|
||||
ORDER BY granted_at DESC, id DESC;
|
||||
|
||||
-- Revokes every unrevoked record of a purpose other than C4, superseded ones
|
||||
-- included, and returns them. A revocation time before the grant time, from
|
||||
-- clock skew between instances, is stamped at the grant time instead.
|
||||
-- name: RevokeConsentRecords :many
|
||||
UPDATE consent_records
|
||||
SET
|
||||
revoked_at = greatest(sqlc.arg(revoked_at)::timestamptz, granted_at),
|
||||
revocation_source = sqlc.arg(revocation_source)
|
||||
WHERE
|
||||
subject_id = sqlc.arg(subject_id)
|
||||
AND purpose = sqlc.arg(purpose)
|
||||
AND purpose <> 'c4'
|
||||
AND revoked_at IS NULL
|
||||
RETURNING
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
organization_id,
|
||||
organization_name,
|
||||
field_set,
|
||||
invitation_id,
|
||||
invitation_terms_version,
|
||||
granted_at,
|
||||
revoked_at,
|
||||
revocation_source,
|
||||
'revoked'::text AS state;
|
||||
|
||||
-- Revokes every unrevoked C4 record of the subject for one organization and
|
||||
-- returns them, with the same clamp of the revocation time as above.
|
||||
-- name: RevokeContactConsentRecords :many
|
||||
UPDATE consent_records
|
||||
SET
|
||||
revoked_at = greatest(sqlc.arg(revoked_at)::timestamptz, granted_at),
|
||||
revocation_source = sqlc.arg(revocation_source)
|
||||
WHERE
|
||||
subject_id = sqlc.arg(subject_id)
|
||||
AND purpose = 'c4'
|
||||
AND organization_id = sqlc.arg(organization_id)
|
||||
AND revoked_at IS NULL
|
||||
RETURNING
|
||||
id,
|
||||
subject_id,
|
||||
purpose,
|
||||
text_version,
|
||||
text_hash,
|
||||
source,
|
||||
organization_id,
|
||||
organization_name,
|
||||
field_set,
|
||||
invitation_id,
|
||||
invitation_terms_version,
|
||||
granted_at,
|
||||
revoked_at,
|
||||
revocation_source,
|
||||
'revoked'::text AS state;
|
||||
|
||||
-- One page of the subject's ledger, newest grant first, optionally limited
|
||||
-- to one state. Every record query is scoped by the subject, so a caller
|
||||
-- never reads or revokes another subject's consent. The state is computed,
|
||||
-- never stored: revoked once revoked_at is set; superseded when a newer
|
||||
-- unrevoked record of the same subject and purpose exists (never for C4,
|
||||
-- whose records are scoped by invitation); active otherwise. The page continues after the (granted_at, id) position of
|
||||
-- the last record of the previous page.
|
||||
-- name: ListConsentRecords :many
|
||||
WITH ledger AS (
|
||||
SELECT
|
||||
record.id,
|
||||
record.subject_id,
|
||||
record.purpose,
|
||||
record.text_version,
|
||||
record.text_hash,
|
||||
record.source,
|
||||
record.organization_id,
|
||||
record.organization_name,
|
||||
record.field_set,
|
||||
record.invitation_id,
|
||||
record.invitation_terms_version,
|
||||
record.granted_at,
|
||||
record.revoked_at,
|
||||
record.revocation_source,
|
||||
(
|
||||
CASE
|
||||
WHEN record.revoked_at IS NOT NULL THEN 'revoked'
|
||||
WHEN record.purpose <> 'c4' AND exists(
|
||||
SELECT 1 FROM consent_records AS newer
|
||||
WHERE
|
||||
newer.subject_id = record.subject_id
|
||||
AND newer.purpose = record.purpose
|
||||
AND newer.revoked_at IS NULL
|
||||
AND (newer.granted_at, newer.id)
|
||||
> (record.granted_at, record.id)
|
||||
) THEN 'superseded'
|
||||
ELSE 'active'
|
||||
END
|
||||
)::text AS state
|
||||
FROM consent_records AS record
|
||||
WHERE record.subject_id = sqlc.arg(subject_id)
|
||||
)
|
||||
|
||||
SELECT
|
||||
ledger.id,
|
||||
ledger.subject_id,
|
||||
ledger.purpose,
|
||||
ledger.text_version,
|
||||
ledger.text_hash,
|
||||
ledger.source,
|
||||
ledger.organization_id,
|
||||
ledger.organization_name,
|
||||
ledger.field_set,
|
||||
ledger.invitation_id,
|
||||
ledger.invitation_terms_version,
|
||||
ledger.granted_at,
|
||||
ledger.revoked_at,
|
||||
ledger.revocation_source,
|
||||
ledger.state
|
||||
FROM ledger
|
||||
WHERE
|
||||
(sqlc.narg(state)::text IS NULL OR ledger.state = sqlc.narg(state)::text)
|
||||
AND (
|
||||
sqlc.narg(after_granted_at)::timestamptz IS NULL
|
||||
OR (ledger.granted_at, ledger.id) < (
|
||||
sqlc.narg(after_granted_at)::timestamptz,
|
||||
sqlc.narg(after_id)::uuid
|
||||
)
|
||||
)
|
||||
ORDER BY ledger.granted_at DESC, ledger.id DESC
|
||||
LIMIT sqlc.arg(row_limit);
|
||||
@@ -64,35 +64,64 @@ WHERE email_normalized = sqlc.arg(email_normalized);
|
||||
-- Replaces the registration of an unverified account and advances the
|
||||
-- registration version in the same statement, so no caller can choose it.
|
||||
-- The version check rejects a writer that read an older row; a verified
|
||||
-- account never changes.
|
||||
-- account never changes. The same statement deletes the C1 that the earlier
|
||||
-- registration of this account recorded, so the consent of an earlier
|
||||
-- registrant never stands for the new one; the new registration records its
|
||||
-- own C1 in the same transaction. A version conflict deletes nothing.
|
||||
-- name: UpdateUserRegistration :one
|
||||
UPDATE users
|
||||
SET
|
||||
email = sqlc.arg(email),
|
||||
password_hash = sqlc.arg(password_hash),
|
||||
scenario = sqlc.arg(scenario),
|
||||
onboarding_step = sqlc.arg(onboarding_step),
|
||||
registration_version = registration_version + 1,
|
||||
last_registered_at = sqlc.arg(last_registered_at),
|
||||
updated_at = sqlc.arg(updated_at),
|
||||
version = version + 1
|
||||
WHERE
|
||||
id = sqlc.arg(id)
|
||||
AND version = sqlc.arg(expected_version)
|
||||
AND email_verified_at IS NULL
|
||||
RETURNING
|
||||
id,
|
||||
email,
|
||||
email_normalized,
|
||||
password_hash,
|
||||
scenario,
|
||||
onboarding_step,
|
||||
email_verified_at,
|
||||
registration_version,
|
||||
last_registered_at,
|
||||
version,
|
||||
created_at,
|
||||
updated_at;
|
||||
WITH updated AS (
|
||||
UPDATE users
|
||||
SET
|
||||
email = sqlc.arg(email),
|
||||
password_hash = sqlc.arg(password_hash),
|
||||
scenario = sqlc.arg(scenario),
|
||||
onboarding_step = sqlc.arg(onboarding_step),
|
||||
registration_version = registration_version + 1,
|
||||
last_registered_at = sqlc.arg(last_registered_at),
|
||||
updated_at = sqlc.arg(updated_at),
|
||||
version = version + 1
|
||||
WHERE
|
||||
users.id = sqlc.arg(id)
|
||||
AND users.version = sqlc.arg(expected_version)
|
||||
AND users.email_verified_at IS NULL
|
||||
RETURNING
|
||||
users.id,
|
||||
users.email,
|
||||
users.email_normalized,
|
||||
users.password_hash,
|
||||
users.scenario,
|
||||
users.onboarding_step,
|
||||
users.email_verified_at,
|
||||
users.registration_version,
|
||||
users.last_registered_at,
|
||||
users.version,
|
||||
users.created_at,
|
||||
users.updated_at
|
||||
),
|
||||
|
||||
discarded_consent AS (
|
||||
DELETE FROM consent_records
|
||||
USING updated
|
||||
WHERE
|
||||
consent_records.subject_id = updated.id
|
||||
AND consent_records.purpose = 'c1'
|
||||
AND consent_records.source = 'registration'
|
||||
)
|
||||
|
||||
SELECT
|
||||
updated.id,
|
||||
updated.email,
|
||||
updated.email_normalized,
|
||||
updated.password_hash,
|
||||
updated.scenario,
|
||||
updated.onboarding_step,
|
||||
updated.email_verified_at,
|
||||
updated.registration_version,
|
||||
updated.last_registered_at,
|
||||
updated.version,
|
||||
updated.created_at,
|
||||
updated.updated_at
|
||||
FROM updated;
|
||||
|
||||
-- Verifies the email only for the registration version the consumed token
|
||||
-- was issued for.
|
||||
@@ -121,14 +150,17 @@ RETURNING
|
||||
updated_at;
|
||||
|
||||
-- Deletes one batch of accounts that stayed unverified since before cutoff,
|
||||
-- oldest first, with their tokens, sessions, mail requests, and mail
|
||||
-- deliveries (and through the deliveries their attempts). Every lock is taken
|
||||
-- oldest first, with their tokens, sessions, mail requests, mail deliveries
|
||||
-- (and through the deliveries their attempts), and consent records. The
|
||||
-- consent records reference the account with ON DELETE RESTRICT, so they are
|
||||
-- deleted explicitly in the same statement: a registration never confirmed
|
||||
-- by its email owner is not kept as consent evidence. Every lock is taken
|
||||
-- with SKIP LOCKED, so the statement never waits: an account whose row is
|
||||
-- locked, for example by a re-registration, a token insert, or a delivery
|
||||
-- insert, is skipped, and so is an account with any token, session, mail
|
||||
-- request, or delivery row locked, for example by a confirmation that
|
||||
-- consumed a token, the delivery consumer, or a worker recording an attempt.
|
||||
-- Skipped accounts stay for the next run.
|
||||
-- locked, for example by a re-registration, a token insert, a delivery
|
||||
-- insert, or a consent insert, is skipped, and so is an account with any
|
||||
-- token, session, mail request, delivery, or consent row locked, for example
|
||||
-- by a confirmation that consumed a token, the delivery consumer, or a
|
||||
-- worker recording an attempt. Skipped accounts stay for the next run.
|
||||
-- Taking the account lock before the token locks is the reverse of the
|
||||
-- documented order, which is safe only because no lock here waits.
|
||||
-- name: DeleteUnverifiedUsersRegisteredBefore :execrows
|
||||
@@ -171,8 +203,15 @@ locked_delivery AS (
|
||||
FOR UPDATE SKIP LOCKED
|
||||
),
|
||||
|
||||
-- An account qualifies only when every token, session, mail request, and
|
||||
-- delivery row visible to the statement was locked above.
|
||||
locked_consent AS (
|
||||
SELECT consent.subject_id
|
||||
FROM consent_records AS consent
|
||||
WHERE consent.subject_id IN (SELECT candidate.id FROM candidate)
|
||||
FOR UPDATE SKIP LOCKED
|
||||
),
|
||||
|
||||
-- An account qualifies only when every token, session, mail request,
|
||||
-- delivery, and consent row visible to the statement was locked above.
|
||||
deletable AS (
|
||||
SELECT candidate.id
|
||||
FROM candidate
|
||||
@@ -205,6 +244,21 @@ deletable AS (
|
||||
SELECT count(*) FROM locked_delivery
|
||||
WHERE locked_delivery.recipient_user_id = candidate.id
|
||||
)
|
||||
AND (
|
||||
SELECT count(*) FROM consent_records AS consent
|
||||
WHERE consent.subject_id = candidate.id
|
||||
) = (
|
||||
SELECT count(*) FROM locked_consent
|
||||
WHERE locked_consent.subject_id = candidate.id
|
||||
)
|
||||
),
|
||||
|
||||
-- The restricting foreign key is checked at the end of the statement, after
|
||||
-- these rows are gone.
|
||||
deleted_consent AS (
|
||||
DELETE FROM consent_records
|
||||
USING deletable
|
||||
WHERE consent_records.subject_id = deletable.id
|
||||
)
|
||||
|
||||
DELETE FROM users
|
||||
|
||||
Reference in new issue
Block a user