# The data model

> What the engine stores in your database, table by table, the roles it creates, and how row-level security ties them together.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://data.docs.sypnia.com/architecture/data-model/

For agents: sypnia_auth tables: users (identity, argon2id hash, confirmation timestamps, app_metadata, user_metadata, banned_until, deleted_at), identities (provider, provider_id, user_id), sessions (user_id or client_id, policy snapshot, aal, last_refresh_at, not_after, user agent, ip prefix), refresh_tokens (token_hash, session_id, parent_id, revoked_at), one_time_tokens (type, token_hash, code_hash, attempts, expires_at), flow_state (PKCE), oauth_clients, assertion_jti, device_codes, personal_access_tokens, api_keys, rate_limits, audit_log. sypnia_data: migrations, sql_audit, settings, signing_keys (encrypted). Roles anon/authenticated/service_role (NOLOGIN), sypnia_authenticator (LOGIN NOINHERIT), sypnia_admin. Secrets are stored as hashes; signing keys and provider credentials encrypted.

The engine's tables live in two schemas of your own database. Your tables live wherever you put them; they only meet the engine's through `sypnia_auth.users(id)` and the helper functions.

**Figure**: The sypnia_auth schema. A user has identities and sessions; a session has a chain of refresh tokens; machines, tokens and keys sit beside them.

1. `users`: one row per person, identity only.
2. `sessions`: one per signed-in device or machine sign-in, with its policy.
3. `oauth_clients`: one per machine allowed to ask for tokens.

## `sypnia_auth`

| Table                    | Holds                                                                                                                                      | Secrets stored as  |
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------ | ------------------ |
| `users`                  | e-mail, phone, password hash, confirmation times, `app_metadata` (server only), `user_metadata` (the user's), `banned_until`, `deleted_at` | argon2id           |
| `identities`             | each way a user signs in: e-mail, Google, GitHub                                                                                           | —                  |
| `sessions`               | user or client, the policy that applied, `aal`, last refresh, `not_after`, user agent, IP prefix                                           | —                  |
| `refresh_tokens`         | one row per token, its session and its parent (the rotation chain), `revoked_at`                                                           | SHA-256            |
| `one_time_tokens`        | confirmation, recovery, magic link, invite, e-mail change, reauthentication; attempts and expiry                                           | SHA-256            |
| `flow_state`             | the PKCE state of provider sign-ins and authorization codes                                                                                | SHA-256            |
| `oauth_clients`          | machines: authentication method, public keys, scopes, resources, DPoP, policy                                                              | argon2id (secrets) |
| `assertion_jti`          | client assertion ids already used, until they expire                                                                                       | —                  |
| `device_codes`           | pending device-flow approvals                                                                                                              | SHA-256            |
| `personal_access_tokens` | owner, name, scopes, expiry, last use                                                                                                      | SHA-256            |
| `api_keys`               | publishable and secret keys                                                                                                                | SHA-256 (secret)   |
| `rate_limits`            | counters per window and key                                                                                                                | —                  |
| `audit_log`              | every auth event                                                                                                                           | —                  |

## `sypnia_data`

| Table          | Holds                                                                                                |
| -------------- | ---------------------------------------------------------------------------------------------------- |
| `migrations`   | applied migrations: version, name, checksum, when, by whom                                           |
| `sql_audit`    | statements run through the SQL endpoint                                                              |
| `settings`     | the project's settings: session policy, providers, e-mail, templates, limits (credentials encrypted) |
| `signing_keys` | Ed25519 key pairs; the private half encrypted with the project's data key                            |

## Roles

```text
anon, authenticated, service_role     NOLOGIN   requests run as one of these
sypnia_authenticator                  LOGIN     the engine's connection: no rights, member of the three above (NOINHERIT)
sypnia_admin                          owner of your schemas; SQL endpoint and migrations
```

`sypnia_authenticator` can do nothing until the engine runs `set local role` for a request; then it has exactly that role's rights, and your row-level security applies.
