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 |
|---|---|
|
Tables of Spring Authorization Server: |
|
Tables |
|
Adds |
|
Table |
|
Table |
|
Adds |
|
Security events and the failed second-factor attempts used for lockout (see Multi-factor authentication and step-up). |
|
TOTP secrets and recovery codes. |
|
Devices trusted to skip the second factor. |
|
Spring Security’s WebAuthn tables ( |
|
Spring Security’s |
|
Hashed numeric e-mail / SMS codes with their attempt counters. |
|
Verified E.164 phone numbers (one per user) and pending phone verifications. |
|
Spend ledger used by the SMS cost caps. |
|
Linked external identities (provider and subject unique per user), see Social login (identity brokering). |
|
Flags a generated (unknown) password, so a just-in-time account does not count a password as a sign-in method. |
|
Client certificate bindings by SHA-256 fingerprint, see X.509 client certificates. |
|
Used client-assertion identifiers |
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, exceptconsumed_refresh_tokenstooauth2_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.
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.
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.
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.
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).
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).
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.
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.
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.