Repository Public
1-- MacLustr Identity — schéma initial. Toute table porteuse de données d'une application cliente a app_id + livemode.2create table application (3 id text primary key, -- app_…4 client_id text not null unique, -- mlid_ci_… (public)5 slug text not null unique,6 name text not null,7 status text not null default 'active', -- active | suspended8 allowed_origins text[] not null default '{}',9 redirect_urls text[] not null default '{}',10 branding jsonb not null default '{}', -- logo_url, accent, name affiché11 retention jsonb not null default '{}', -- artifacts_hours, extracted_days, result_days12 methods jsonb not null default '{}', -- méthodes permises par mode13 limits jsonb not null default '{}', -- rate_per_min, sessions_per_day14 created_at timestamptz not null default now()15);1617create table api_key (18 id text primary key, -- key_…19 app_id text not null references application(id) on delete cascade,20 livemode boolean not null,21 prefix text not null, -- affichable22 secret_hash bytea not null unique, -- sha25623 scopes text[] not null,24 label text not null default '',25 created_at timestamptz not null default now(),26 last_used_at timestamptz,27 expires_at timestamptz,28 revoked_at timestamptz29);30create index on api_key(app_id);3132create table policy (33 key text not null, -- identity_document_face_v134 version text not null,35 app_id text references application(id) on delete cascade, -- null = politique globale36 config jsonb not null,37 active boolean not null default true,38 created_at timestamptz not null default now()39);40create unique index policy_uq on policy(key, version, coalesce(app_id, ''));4142create table verification_session (43 id text primary key, -- vs_…44 app_id text not null references application(id),45 livemode boolean not null,46 external_user_ref text, -- identifiant opaque fourni par l'application47 purpose text not null,48 policy_key text not null,49 policy_version text not null,50 policy_config jsonb not null, -- instantané de la politique51 locale text not null default 'fr-CA',52 return_url text,53 status text not null,54 method text, -- méthode choisie par la personne55 client_secret_hash bytea not null,56 metadata jsonb not null default '{}',57 flow jsonb not null default '{}', -- état du parcours (défi, tentatives, scénario sandbox)58 verification_id text,59 created_at timestamptz not null default now(),60 updated_at timestamptz not null default now(),61 expires_at timestamptz not null,62 completed_at timestamptz63);64create index on verification_session(app_id, livemode, created_at desc);65create index on verification_session(app_id, livemode, external_user_ref);66create index on verification_session(status, expires_at);6768create table verification (69 id text primary key, -- ver_…70 session_id text not null references verification_session(id),71 app_id text not null references application(id),72 livemode boolean not null,73 external_user_ref text,74 status text not null, -- verified | inconclusive | rejected | manual_review | revoked75 assurance_level text,76 method text,77 policy_key text not null,78 policy_version text not null,79 decision jsonb not null default '{}', -- dimensions, motifs, versions (sans renseignement personnel)80 engine_versions jsonb not null default '{}',81 created_at timestamptz not null default now(),82 decided_at timestamptz,83 verified_at timestamptz,84 expires_at timestamptz,85 revoked_at timestamptz,86 revoke_reason text,87 personal_data_deleted_at timestamptz88);89create index on verification(app_id, livemode, created_at desc);90create index on verification(app_id, livemode, external_user_ref);9192create table consent (93 id text primary key,94 session_id text not null references verification_session(id),95 app_id text not null,96 text_version text not null,97 locale text not null,98 method text not null,99 biometric boolean not null,100 granted_at timestamptz not null default now(),101 withdrawn_at timestamptz,102 ip_hash bytea,103 user_agent_hash bytea104);105create index on consent(session_id);106107-- Pièces chiffrées (documents, images de selfie). Le contenu vit sur disque, chiffré ; la clé enveloppée est ici.108create table artifact (109 id text primary key, -- art_…110 session_id text not null references verification_session(id),111 app_id text not null,112 livemode boolean not null,113 kind text not null, -- document_front | document_back | selfie_frame114 seq int not null default 0,115 content_type text not null,116 size_bytes int not null,117 sha256 bytea not null,118 wrapped_key bytea, -- null après destruction (effacement cryptographique)119 path text not null,120 created_at timestamptz not null default now(),121 delete_after timestamptz not null,122 deleted_at timestamptz123);124create index on artifact(session_id);125create index on artifact(delete_after) where deleted_at is null;126127-- Champs extraits minimaux, chiffrés par application ; réservés à la révision humaine et aux droits d'accès.128create table extracted_data (129 verification_id text primary key references verification(id) on delete cascade,130 app_id text not null,131 wrapped_key bytea,132 ciphertext bytea,133 delete_after timestamptz not null,134 deleted_at timestamptz135);136137create table job (138 id text primary key, -- job_…139 session_id text not null references verification_session(id),140 kind text not null, -- analyze141 requires text[] not null default '{}', -- capacités exigées du worker142 state text not null default 'queued', -- queued | leased | done | failed143 attempts int not null default 0,144 max_attempts int not null default 3,145 leased_by text,146 lease_until timestamptz,147 created_at timestamptz not null default now(),148 started_at timestamptz,149 finished_at timestamptz,150 error text,151 timings jsonb152);153create index on job(state, created_at);154155create table worker (156 id text primary key, -- wrk_…157 name text not null unique,158 token_hash bytea not null,159 capabilities text[] not null default '{}',160 status text not null default 'active',161 metrics jsonb not null default '{}',162 models jsonb not null default '{}',163 last_seen_at timestamptz,164 created_at timestamptz not null default now()165);166167create table webhook_endpoint (168 id text primary key, -- we_…169 app_id text not null references application(id) on delete cascade,170 livemode boolean not null,171 url text not null,172 events text[] not null,173 secret_enc bytea not null,174 prev_secret_enc bytea,175 prev_secret_until timestamptz,176 status text not null default 'enabled',177 description text not null default '',178 created_at timestamptz not null default now()179);180create index on webhook_endpoint(app_id, livemode);181182create table event (183 id text primary key, -- evt_…184 app_id text not null,185 livemode boolean not null,186 type text not null,187 payload jsonb not null,188 created_at timestamptz not null default now()189);190create index on event(app_id, livemode, created_at desc);191192create table webhook_delivery (193 id text primary key, -- whd_…194 event_id text not null references event(id) on delete cascade,195 endpoint_id text not null references webhook_endpoint(id) on delete cascade,196 app_id text not null,197 state text not null default 'pending', -- pending | delivered | failed (file morte)198 attempts int not null default 0,199 next_attempt_at timestamptz not null default now(),200 last_status int,201 last_error text,202 last_duration_ms int,203 delivered_at timestamptz,204 created_at timestamptz not null default now(),205 unique(event_id, endpoint_id)206);207create index on webhook_delivery(state, next_attempt_at);208209create table idempotency (210 app_id text not null,211 livemode boolean not null,212 key text not null,213 request_hash bytea not null,214 status int,215 response jsonb,216 created_at timestamptz not null default now(),217 primary key(app_id, livemode, key)218);219220create table fraud_signal (221 id bigserial primary key,222 app_id text not null,223 session_id text,224 kind text not null,225 weight real not null,226 detail jsonb not null default '{}',227 created_at timestamptz not null default now()228);229create index on fraud_signal(session_id);230create index on fraud_signal(app_id, created_at desc);231232-- Compteurs de fréquence (clés hachées, jamais d'IP en clair).233create table velocity (234 bucket text not null,235 key_hash bytea not null,236 window_start timestamptz not null,237 n int not null default 0,238 primary key(bucket, key_hash, window_start)239);240241create table operator (242 id text primary key, -- op_…243 handle text not null unique,244 display_name text not null,245 role text not null, -- owner | admin | reviewer | developer | viewer246 app_ids text[] not null default '{}', -- portée (vide = toutes pour owner/admin)247 status text not null default 'active',248 created_at timestamptz not null default now()249);250create table operator_credential (251 id bytea primary key,252 operator_id text not null references operator(id) on delete cascade,253 public_key bytea not null,254 sign_count bigint not null default 0,255 transports text[],256 label text,257 created_at timestamptz not null default now(),258 last_used_at timestamptz259);260create table operator_invite (261 token_hash bytea primary key,262 operator_id text not null references operator(id) on delete cascade,263 expires_at timestamptz not null,264 used_at timestamptz265);266create table operator_session (267 token_hash bytea primary key,268 operator_id text not null references operator(id) on delete cascade,269 csrf text not null,270 created_at timestamptz not null default now(),271 expires_at timestamptz not null,272 ip_hash bytea273);274create table webauthn_challenge (275 id text primary key,276 kind text not null,277 operator_id text,278 challenge bytea not null,279 expires_at timestamptz not null280);281282create table review_case (283 id text primary key, -- rev_…284 verification_id text not null references verification(id) on delete cascade,285 app_id text not null,286 state text not null default 'open', -- open | claimed | resolved287 reasons text[] not null default '{}',288 claimed_by text,289 claim_until timestamptz, -- accès temporaire aux pièces290 resolution text,291 resolution_note text,292 resolved_by text,293 created_at timestamptz not null default now(),294 resolved_at timestamptz295);296create index on review_case(state, created_at);297298create table dispute (299 id text primary key, -- dsp_…300 verification_id text not null references verification(id) on delete cascade,301 app_id text not null,302 message_enc bytea,303 state text not null default 'open',304 created_at timestamptz not null default now(),305 resolved_at timestamptz306);307308-- Verrous de conformité : aucune capacité réelle n'est ouverte sans attestation consignée.309create table compliance_gate (310 key text primary key, -- live_document | live_biometric311 status text not null default 'closed', -- closed | open312 requirements jsonb not null default '[]',313 opened_by text,314 opened_at timestamptz,315 note text316);317insert into compliance_gate(key, requirements) values318 ('live_document', '["efvp_completed","privacy_policy_published","retention_policy_approved","incident_process_approved","security_review_completed"]'),319 ('live_biometric', '["efvp_completed","cai_biometric_declaration_filed","legal_review_completed","non_biometric_alternative_available","model_validation_report_approved","security_review_completed","penetration_test_completed"]');320321-- Journal d'audit en chaîne de hachage (sans renseignement personnel).322create table audit_log (323 seq bigserial primary key,324 at timestamptz not null default now(),325 actor text not null, -- op_… | key_… | system | subject326 action text not null,327 app_id text,328 target text,329 detail jsonb not null default '{}',330 prev_hash bytea,331 hash bytea not null332);333334create table deletion_log (335 id bigserial primary key,336 at timestamptz not null default now(),337 app_id text not null,338 session_id text,339 what text not null, -- artifact | extracted | subject_request | consent_withdrawal340 count int not null,341 reason text not null342);343