Products · Identity V1

Login using AWS

Sign up, sign in and password reset with AWS Cognito, integrated through standard OIDC.

Version 1 · Login with AWS CognitoDesigned, not builtModule 1 of 15

DataSchemaSections

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 cascade
  • sessions_token_hash_key: unique (token_hash)
  • sessions_token_hash_check: token_hash ~ '^[0-9a-f]{64}$'
  • sessions_expiry_check: expires_at > created_at
  • sessions_revoked_check: revoked_at is null or revoked_at >= created_at
  • sessions_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 active
  • signing_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

identity.users

idcognito_subemailemail_verified_atis_admincreated_at
0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3ea1b2c3d4-5e6f-7081-92a3-b4c5d6e7f809priya@example.com2026-11-16 09:14:07+00false2026-11-16 09:14:07+00
0193a5c9-11aa-7d42-8f10-2b7c9d0e1f2a0f9e8d7c-6b5a-4938-2716-0a1b2c3d4e5fowner@example.com2026-11-16 10:02:41+00true2026-11-16 10:02:41+00

identity.sessions

iduser_idtoken_hashcreated_atexpires_atrevoked_at
0193a5b2-9d40-7c11-a6e3-5f1b2c3d4e5f0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3e9f2c1e7ab4d08c35e6f1a2b3c4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f72026-11-16 09:14:07+002026-11-23 09:14:07+00(empty)
0193a61f-03b7-7e90-b1c4-6a7b8c9d0e1f0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3e1b7d4e90c2a5f8361d0e9c8b7a6f5e4d3c2b1a09f8e7d6c5b4a39281706152432026-11-10 18:30:00+002026-11-17 18:30:00+002026-11-12 08:05:19+00

identity.signing_keys

kidpublic_jwkprivate_key_encryptedcreated_atretired_at
k2026-11{"kty":"EC","crv":"P-256","x":"f83OJ3D2…","y":"x_FEzRu9…"}v1:AES-GCM:9c1f…(encrypted)2026-11-01 00:00:00+00(empty)

identity.audit_events

idactor_idactor_typeactiontarget_typetarget_idmetadatacreated_at
0193a5b2-9d41-7f02-8c55-0a1b2c3d4e5f0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3euserauth.signupuser0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3e{}2026-11-16 09:14:07+00
0193a5b2-9d42-7a6b-9d77-1b2c3d4e5f6a0193a5b2-7c1e-7a3f-9b2d-4e6f8a1c2d3euserauth.signinsession0193a5b2-9d40-7c11-a6e3-5f1b2c3d4e5f{"mode":"signup"}2026-11-16 09:14:07+00
0193a7d0-4410-7b3c-8e21-2c3d4e5f6a7b(empty)systemauth.signin_failed(empty)(empty){"reason":"login_state_mismatch"}2026-11-16 11:40:52+00

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;