Schema Designed
The SQL for the tables login uses, in the identity schema. No other service can read them.
-- Identity service, version 1 (login). Schema: identity.
-- IDs are UUIDv7 values generated by the service.
create schema if not exists identity;
create table identity.users (
id uuid primary key,
cognito_sub text not null,
email text not null,
email_verified_at timestamptz not null,
is_admin boolean not null default false,
created_at timestamptz not null default now(),
constraint users_cognito_sub_key unique (cognito_sub),
constraint users_cognito_sub_check check (length(cognito_sub) between 1 and 255),
constraint users_email_format_check check (
email = lower(email)
and email like '%_@_%'
and length(email) between 3 and 254
)
);
create unique index users_email_lower_key on identity.users (lower(email));
create table identity.sessions (
id uuid primary key,
user_id uuid not null,
token_hash text not null,
created_at timestamptz not null default now(),
expires_at timestamptz not null,
revoked_at timestamptz,
constraint sessions_user_id_fkey foreign key (user_id)
references identity.users (id) on delete cascade,
constraint sessions_token_hash_key unique (token_hash),
constraint sessions_token_hash_check check (token_hash ~ '^[0-9a-f]{64}$'),
constraint sessions_expiry_check check (expires_at > created_at),
constraint sessions_revoked_check check (revoked_at is null or revoked_at >= created_at)
);
create index sessions_user_id_idx on identity.sessions (user_id, created_at desc);
create table identity.signing_keys (
kid text primary key,
public_jwk jsonb not null,
private_key_encrypted text not null,
created_at timestamptz not null default now(),
retired_at timestamptz,
constraint signing_keys_kid_check check (length(kid) between 1 and 64),
constraint signing_keys_public_only_check check (
public_jwk ?& array['kty', 'crv', 'x', 'y'] and not public_jwk ? 'd'
),
constraint signing_keys_retired_check check (retired_at is null or retired_at >= created_at)
);
-- Only one key can be active (not retired) at a time.
create unique index signing_keys_one_active_idx
on identity.signing_keys ((true)) where retired_at is null;
create table identity.audit_events (
id uuid primary key,
actor_id uuid,
actor_type text not null,
action text not null,
target_type text,
target_id uuid,
metadata jsonb not null default '{}',
request_id text,
created_at timestamptz not null default now(),
constraint audit_events_actor_type_check
check (actor_type in ('user', 'api_key', 'service', 'system'))
);
create index audit_events_actor_idx on identity.audit_events (actor_id, created_at desc);
-- The service's database login can reach only this schema,
-- and can add and read audit events but never change or delete them.
grant usage on schema identity to identity_service;
grant select, insert, update, delete
on identity.users, identity.sessions, identity.signing_keys to identity_service;
grant select, insert on identity.audit_events to identity_service;
Schema level: enforced by the database, whatever the code does
- Primary keys on every table;
cognito_sub, lower(email) and token_hash are unique. - A session cannot exist without its user (foreign key), and is deleted with the user.
token_hash must be 64 hex characters; expires_at must be after created_at.email must be lower-case, contain @, and be 3 to 254 characters, the longest address a mail server accepts.- Only one signing key can be active; a stored public key can never contain private material.
- Audit events can be added and read but never changed or deleted.
- Identity's database login can reach only the
identity schema. No other service can read these tables.
Constraints and indexes by table
identity.users
users_pkey: primary key (id)users_cognito_sub_key: unique (cognito_sub)users_email_lower_key: unique index on lower(email)users_email_format_check: email = lower(email) and email like '%_@_%' and length(email) between 3 and 254
identity.sessions
sessions_pkey: primary key (id)sessions_user_id_fkey: foreign key (user_id) references identity.users(id) on delete cascadesessions_token_hash_key: unique (token_hash)sessions_token_hash_check: token_hash ~ '^[0-9a-f]{64}$'sessions_expiry_check: expires_at > created_atsessions_revoked_check: revoked_at is null or revoked_at >= created_atsessions_user_id_idx: index on (user_id, created_at desc)
identity.signing_keys
signing_keys_pkey: primary key (kid)signing_keys_public_only_check: public_jwk ?& array['kty','crv','x','y'] and not public_jwk ? 'd'signing_keys_one_active_idx: unique index on ((true)) where retired_at is null, so only one key can be activesigning_keys_retired_check: retired_at is null or retired_at >= created_at
identity.audit_events
audit_events_pkey: primary key (id)audit_events_actor_type_check: actor_type in ('user','api_key','service','system')- The service's database login has
INSERT and SELECT only on this table: no UPDATE, no DELETE audit_events_actor_idx: index on (actor_id, created_at desc)
Sample data
Common queries
Find the session for a cookie (every signed-in request)
select s.id, s.user_id, s.expires_at
from identity.sessions s
where s.token_hash = $1
and s.revoked_at is null
and s.expires_at > now();
Sign out
update identity.sessions
set revoked_at = now()
where id = $1 and revoked_at is null;
Revoke older sessions after a password reset
update identity.sessions
set revoked_at = now()
where user_id = $1
and created_at < $2 -- the sign-in time reported by Cognito
and revoked_at is null;
Schema on GitHub →