Data model

This documentation was generated with the assistance of AI. Please report any inaccuracies.

auth-server stores its state in PostgreSQL. Flyway applies the migrations in auth-server/infrastructure/persistence/src/main/resources/db/migration (classpath:db/migration) at start.

Migration Content

V100__spring_authorization_server_schema.sql

Tables of Spring Authorization Server: oauth2_registered_client, oauth2_authorization_consent and oauth2_authorization.

V101__users.sql

Tables users, user_authorities and user_passwords.

V200__account.sql

Adds email, email_verified and tenant to users, and the account_tokens table (hashed single-use tokens).

V210__signing_keys.sql

Table signing_keys (public JWK, encrypted private JWK, status NEXT/ACTIVE/RETIRED); at most one ACTIVE key.

V230__consumed_refresh_tokens.sql

Table consumed_refresh_tokens, the hashes of rotated-out refresh tokens used for reuse detection.

V240__authorization_created_at.sql

Adds created_at to oauth2_authorization, so the authorizations that never got a token (abandoned authorization or consent requests) can be cleaned up once they are old enough.

V400__mfa_security_events.sql

Security events and the failed second-factor attempts used for lockout (see Multi-factor authentication and step-up).

V410__totp_recovery_codes.sql

TOTP secrets and recovery codes.

V420__trusted_devices.sql

Devices trusted to skip the second factor.

V500__webauthn.sql

Spring Security’s WebAuthn tables (user_entities, user_credentials) and an index on the credential owner (see Passkeys (WebAuthn)).

V600__one_time_tokens.sql

Spring Security’s one_time_tokens table for magic-link tokens (see One-time tokens (e-mail, magic links and SMS)).

V610__ott_codes.sql

Hashed numeric e-mail / SMS codes with their attempt counters.

V620__user_phones.sql

Verified E.164 phone numbers (one per user) and pending phone verifications.

V630__sms_cost_ledger.sql

Spend ledger used by the SMS cost caps.

V700__federated_identities.sql

Linked external identities (provider and subject unique per user), see Social login (identity brokering).

V701__generated_passwords.sql

Flags a generated (unknown) password, so a just-in-time account does not count a password as a sign-in method.

V710__x509_certificates.sql

Client certificate bindings by SHA-256 fingerprint, see X.509 client certificates.

V800__client_assertion_jtis.sql

Used client-assertion identifiers (client_id, jti) with their expiry, see Client credentials and service-to-service calls.

Each issue owns a range of migration numbers so features never collide: the walking skeleton uses V100-V199 and the credential and token core (issue #2) uses V200-V299; multi-factor authentication (issue #4) uses V400-V499; passkeys (issue #5) use V500-V599; one-time tokens and SMS (issue #6) use V600-V699; social login, X.509 and the device grant (issue #7) use V700-V799; client authentication, token exchange and PAR (issue #8) use V800-V899. A migration is never written outside the range of the issue that adds it.

Entity diagrams

The diagrams below show every table of the auth-server database, grouped by area. They follow the migrations, which are the source of truth. Each entity lists its columns with their PostgreSQL type and marks primary keys (PK), foreign keys (FK) and unique columns (UK).

Two kinds of relationship are drawn:

  • a solid line is a real foreign key declared in a migration (always to users.id, except consumed_refresh_tokens to oauth2_authorization.id);

  • a dashed line is a logical link with no foreign key. The tables of Spring Authorization Server and Spring Security (oauth2_*, user_entities, user_credentials, one_time_tokens) are joined by value, not by constraint: their schema is the framework’s, and a principal name or client id is copied into them. Deleting a user or a client does not cascade to those rows.

Columns that hold a secret store only a hash (hashed_token, hashed_code, code_hash, pending_code_hash, token_hash) or an encrypted value (private_jwk_encrypted, encrypted_secret); no table holds a code, token or private key in clear.

Users and accounts

users is the account. A password is optional (a just-in-time social account holds a generated one, flagged by generated); account_tokens holds the single-use e-mail verification and password reset tokens. See Passwords and accounts.

erDiagram users { uuid id PK varchar username UK boolean enabled varchar email UK "unique on lower(email)" boolean email_verified varchar tenant "default 'default'" boolean mfa_required "forced by an administrator" timestamptz created_at timestamptz updated_at } user_authorities { uuid user_id PK, FK varchar authority PK } user_passwords { uuid user_id PK, FK varchar encoded_password "Argon2" boolean generated "unknown password of a JIT account" timestamptz updated_at } account_tokens { uuid id PK uuid user_id FK varchar purpose varchar hashed_token UK timestamptz expires_at timestamptz used_at timestamptz created_at } users ||--o{ user_authorities : "has" users ||--o| user_passwords : "has" users ||--o{ account_tokens : "receives"

Clients, authorizations and consents

The three tables of Spring Authorization Server. oauth2_authorization holds one row per authorization (one grant for one client and one principal), with a group of *_value, *_issued_at, *_expires_at and *_metadata columns for each token it carries: authorization code, access token, OIDC ID token, refresh token, user code and device code (only the access token group is shown below). created_at (V240) lets abandoned authorizations be cleaned up. consumed_refresh_tokens keeps the hashes of rotated-out refresh tokens, and client_assertion_jtis the used private_key_jwt assertion ids. See Registered clients, Tokens and signing keys and Client credentials and service-to-service calls.

erDiagram oauth2_registered_client { varchar id PK varchar client_id "unique by the application" timestamp client_id_issued_at varchar client_secret "encoded, null for public clients" timestamp client_secret_expires_at varchar client_name varchar client_authentication_methods varchar authorization_grant_types varchar redirect_uris varchar post_logout_redirect_uris varchar scopes varchar client_settings "JSON, includes mfa.required, jwks, audiences" varchar token_settings "JSON" } oauth2_authorization { varchar id PK varchar registered_client_id "logical link" varchar principal_name "logical link" varchar authorization_grant_type varchar authorized_scopes text attributes "serialized principal and request" varchar state text access_token_value timestamp access_token_issued_at timestamp access_token_expires_at text access_token_metadata varchar access_token_type varchar access_token_scopes timestamptz created_at } oauth2_authorization_consent { varchar registered_client_id PK "logical link" varchar principal_name PK "logical link" varchar authorities "granted scopes" } consumed_refresh_tokens { varchar token_hash PK "SHA-256" varchar authorization_id FK timestamptz consumed_at } client_assertion_jtis { varchar client_id PK "logical link" varchar jti PK timestamptz expires_at } users { uuid id PK varchar username UK } oauth2_registered_client ||..o{ oauth2_authorization : "registered_client_id = id" oauth2_registered_client ||..o{ oauth2_authorization_consent : "registered_client_id = id" oauth2_registered_client ||..o{ client_assertion_jtis : "client_id = client_id" users ||..o{ oauth2_authorization : "principal_name = username" users ||..o{ oauth2_authorization_consent : "principal_name = username" oauth2_authorization ||--o{ consumed_refresh_tokens : "rotated out of"

principal_name is the username for a user grant, and the client id for a client_credentials grant (there is no user then), which is one more reason it has no foreign key.

Signing keys

signing_keys stands alone. A partial unique index on status allows at most one ACTIVE key; the NEXT key is published in the JWK set before it signs, and RETIRED keys stay published until no token they signed can still be valid. See Tokens and signing keys.

erDiagram signing_keys { varchar kid PK varchar alg text public_jwk text private_jwk_encrypted "AES-GCM" varchar status "NEXT, ACTIVE or RETIRED, one ACTIVE at most" timestamptz created_at timestamptz activated_at timestamptz retired_at }

Multi-factor authentication

The lockout counter (second_factor_attempts, one row per user, read FOR UPDATE so attempts of one user are serialized), the audit trail (security_events, which keeps the username after the user is deleted: ON DELETE SET NULL), the authenticator app (totp_credentials), the recovery codes and the trusted browsers. See Multi-factor authentication and step-up.

erDiagram users { uuid id PK varchar username UK boolean mfa_required } security_events { uuid id PK varchar type uuid user_id FK "set null on delete" varchar username varchar detail timestamptz occurred_at } second_factor_attempts { uuid user_id PK, FK integer failed_count timestamptz locked_until } totp_credentials { uuid user_id PK, FK bytea encrypted_secret "AES-GCM" boolean confirmed bigint last_used_step "replay protection" timestamptz created_at } recovery_codes { uuid id PK uuid user_id FK varchar hashed_code timestamptz used_at timestamptz created_at } trusted_devices { uuid id PK uuid user_id FK varchar hashed_token UK "SHA-256 of the cookie token" varchar user_agent timestamptz expires_at timestamptz created_at } users |o--o{ security_events : "subject of" users ||--o| second_factor_attempts : "has" users ||--o| totp_credentials : "enrols" users ||--o{ recovery_codes : "holds" users ||--o{ trusted_devices : "trusts"

Passkeys

Spring Security’s WebAuthn tables. user_entities is the WebAuthn user (its id is the user handle) and is linked to the account by name only; user_credentials holds one row per registered passkey. See Passkeys (WebAuthn).

erDiagram users { uuid id PK varchar username UK } user_entities { varchar id PK "WebAuthn user handle" varchar name UK "logical link" varchar display_name } user_credentials { varchar credential_id PK "base64url" varchar user_entity_user_id "logical link" bytea public_key bigint signature_count boolean uv_initialized boolean backup_eligible boolean backup_state varchar authenticator_transports varchar public_key_credential_type bytea attestation_object bytea attestation_client_data_json varchar label timestamptz created timestamptz last_used } users ||..o| user_entities : "name = username" user_entities ||..o{ user_credentials : "user_entity_user_id = id"

One-time tokens and SMS

one_time_tokens is Spring Security’s table for magic-link tokens; ott_codes holds the numeric e-mail and SMS codes (bound to the browser session through binding_id, spent after too many attempts). Both name the user by username; a second-factor link carries a marker inside username, so it is not a plain copy. user_phones holds the verified number and, at the same time, a pending number being verified. sms_cost_ledger records what each sent SMS cost, for the spend caps. See One-time tokens (e-mail, magic links and SMS).

erDiagram users { uuid id PK varchar username UK } one_time_tokens { varchar token_value PK "UUID" varchar username "logical link" timestamptz expires_at } ott_codes { uuid id PK varchar username "logical link" varchar channel "EMAIL or SMS" varchar purpose "LOGIN or SECOND_FACTOR" varchar code_hash varchar binding_id "browser session" integer attempts timestamptz expires_at timestamptz created_at } user_phones { uuid user_id PK, FK varchar phone_e164 UK "verified number" timestamptz verified_at varchar pending_phone_e164 varchar pending_code_hash integer pending_attempts timestamptz pending_expires_at } sms_cost_ledger { bigint id PK "identity" timestamptz sent_at char country "ISO 3166 alpha-2" bigint cost_micros } users ||..o{ one_time_tokens : "username" users ||..o{ ott_codes : "username" users ||--o| user_phones : "verifies"

Social login and X.509

An account can have several external identities (one per provider and subject, unique across accounts) and several bound client certificates (one fingerprint, unique across accounts). Together with a non-generated password and a passkey they are the account’s sign-in methods, of which the last one cannot be removed. See Social login (identity brokering) and X.509 client certificates.

erDiagram users { uuid id PK varchar username UK } federated_identities { uuid id PK uuid user_id FK varchar provider UK "google, microsoft, apple or github" varchar subject UK "unique with provider" varchar email timestamptz linked_at timestamptz last_login_at } x509_certificates { uuid id PK uuid user_id FK char fingerprint_sha256 UK "lowercase hex" varchar subject_dn timestamptz created_at } users ||--o{ federated_identities : "links" users ||--o{ x509_certificates : "binds"

Schema listings

CREATE TABLE oauth2_registered_client
(
    id                            varchar(100)                            NOT NULL,
    client_id                     varchar(100)                            NOT NULL,
    client_id_issued_at           timestamp     DEFAULT CURRENT_TIMESTAMP NOT NULL,
    client_secret                 varchar(200)  DEFAULT NULL,
    client_secret_expires_at      timestamp     DEFAULT NULL,
    client_name                   varchar(200)                            NOT NULL,
    client_authentication_methods varchar(1000)                           NOT NULL,
    authorization_grant_types     varchar(1000)                           NOT NULL,
    redirect_uris                 varchar(1000) DEFAULT NULL,
    post_logout_redirect_uris     varchar(1000) DEFAULT NULL,
    scopes                        varchar(1000)                           NOT NULL,
    client_settings               varchar(2000)                           NOT NULL,
    token_settings                varchar(2000)                           NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE oauth2_authorization_consent
(
    registered_client_id varchar(100)  NOT NULL,
    principal_name       varchar(200)  NOT NULL,
    authorities          varchar(1000) NOT NULL,
    PRIMARY KEY (registered_client_id, principal_name)
);

CREATE TABLE oauth2_authorization
(
    id                            varchar(100) NOT NULL,
    registered_client_id          varchar(100) NOT NULL,
    principal_name                varchar(200) NOT NULL,
    authorization_grant_type      varchar(100) NOT NULL,
    authorized_scopes             varchar(1000) DEFAULT NULL,
    attributes                    text          DEFAULT NULL,
    state                         varchar(500)  DEFAULT NULL,
    authorization_code_value      text          DEFAULT NULL,
    authorization_code_issued_at  timestamp     DEFAULT NULL,
    authorization_code_expires_at timestamp     DEFAULT NULL,
    authorization_code_metadata   text          DEFAULT NULL,
    access_token_value            text          DEFAULT NULL,
    access_token_issued_at        timestamp     DEFAULT NULL,
    access_token_expires_at       timestamp     DEFAULT NULL,
    access_token_metadata         text          DEFAULT NULL,
    access_token_type             varchar(100)  DEFAULT NULL,
    access_token_scopes           varchar(1000) DEFAULT NULL,
    oidc_id_token_value           text          DEFAULT NULL,
    oidc_id_token_issued_at       timestamp     DEFAULT NULL,
    oidc_id_token_expires_at      timestamp     DEFAULT NULL,
    oidc_id_token_metadata        text          DEFAULT NULL,
    refresh_token_value           text          DEFAULT NULL,
    refresh_token_issued_at       timestamp     DEFAULT NULL,
    refresh_token_expires_at      timestamp     DEFAULT NULL,
    refresh_token_metadata        text          DEFAULT NULL,
    user_code_value               text          DEFAULT NULL,
    user_code_issued_at           timestamp     DEFAULT NULL,
    user_code_expires_at          timestamp     DEFAULT NULL,
    user_code_metadata            text          DEFAULT NULL,
    device_code_value             text          DEFAULT NULL,
    device_code_issued_at         timestamp     DEFAULT NULL,
    device_code_expires_at        timestamp     DEFAULT NULL,
    device_code_metadata          text          DEFAULT NULL,
    PRIMARY KEY (id)
);
CREATE TABLE users
(
    id         uuid                     NOT NULL,
    username   varchar(100)             NOT NULL,
    enabled    boolean                  NOT NULL DEFAULT TRUE,
    created_at timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    CONSTRAINT uq_users_username UNIQUE (username)
);

CREATE TABLE user_authorities
(
    user_id   uuid         NOT NULL REFERENCES users (id) ON DELETE CASCADE,
    authority varchar(100) NOT NULL,
    PRIMARY KEY (user_id, authority)
);

CREATE TABLE user_passwords
(
    user_id          uuid                     NOT NULL REFERENCES users (id) ON DELETE CASCADE,
    encoded_password varchar(255)             NOT NULL,
    updated_at       timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (user_id)
);
ALTER TABLE users
    ADD COLUMN email          varchar(320),
    ADD COLUMN email_verified boolean      NOT NULL DEFAULT FALSE,
    ADD COLUMN tenant         varchar(100) NOT NULL DEFAULT 'default';

-- Existing rows have no e-mail; the unique index is case-insensitive and ignores the NULLs.
CREATE UNIQUE INDEX uq_users_email ON users (lower(email));

CREATE TABLE account_tokens
(
    id           uuid                     NOT NULL,
    user_id      uuid                     NOT NULL REFERENCES users (id) ON DELETE CASCADE,
    purpose      varchar(50)              NOT NULL,
    hashed_token varchar(128)             NOT NULL,
    expires_at   timestamp with time zone NOT NULL,
    used_at      timestamp with time zone,
    created_at   timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    CONSTRAINT uq_account_tokens_hashed_token UNIQUE (hashed_token)
);

CREATE INDEX ix_account_tokens_expires_at ON account_tokens (expires_at);

See Tokens and signing keys for the signing_keys and consumed_refresh_tokens tables and the created_at column of oauth2_authorization.

BFF database

The bff has its own PostgreSQL database (bff, created by compose/init-databases.sql in development), separate from the one of auth-server. Its single Flyway migration, V300__spring_session.sql in bff/infrastructure/session/src/main/resources/db/migration, creates the Spring Session JDBC tables spring_session (one row per browser session) and spring_session_attributes (the serialized session attributes, among them the authorized OAuth2 client with the user’s tokens). Spring Session never creates them (initialize-schema: never). The schema and its use are described in Login pages and the BFF.

erDiagram SPRING_SESSION { char PRIMARY_ID PK char SESSION_ID UK bigint CREATION_TIME bigint LAST_ACCESS_TIME int MAX_INACTIVE_INTERVAL bigint EXPIRY_TIME varchar PRINCIPAL_NAME } SPRING_SESSION_ATTRIBUTES { char SESSION_PRIMARY_ID PK, FK varchar ATTRIBUTE_NAME PK bytea ATTRIBUTE_BYTES "serialized attribute" } SPRING_SESSION ||--o{ SPRING_SESSION_ATTRIBUTES : "holds"

The resource-server and the service-client have no database: the resource server keeps its documents in memory, and the service client caches its token in memory.