# {{name}}
A ryuk app: axum + SeaORM + Inertia (Svelte).
## Develop
```bash
(cd client && npm install)
ryuk serve # vite + cargo watch
ryuk migrate # run pending migrations (serve also runs them)
ryuk doctor # check rust / node / pnpm / docker / database
```
## Test
```bash
cargo test # tests/flows.rs: CSRF, register, verify, login, lockout, reset, posts, logout, API keys, OAuth client credentials
```
The tests drive the real router in-process with the log mail driver, so they need no server or network.
They use in-memory SQLite; for a postgres/mysql app set `TEST_DATABASE_URL` to an empty throwaway database and run `cargo test -- --test-threads=1`.
## Web, API and keys
Routes live in two files with two different stacks:
- `src/routes.rs` + `src/controllers/*.rs`: **web** routes for browsers (session cookie, CSRF, Inertia pages). `ryuk make:controller Name`.
- `src/api.rs` + `src/controllers/api/*.rs`: **API** routes for machines under `/api` (no cookie, no CSRF, JSON errors, per-key rate limit). Handlers declare short paths (`/posts`) and are served as `/api/posts`. `ryuk make:api Name` adds `src/controllers/api/<name>.rs` and its route.
Scopes of this template: `profile` (`GET /api/me`), `posts:read`, `posts:write`, `devices` (push device registration), `account` (sessions, authorized apps and `DELETE /api/me` with `"password"` when the account has one; only first-party OAuth clients may get it, never API keys from the UI or `oauth:client` without `--first-party`). API handlers call `principal.require("posts:read")?`. A key acts for its owner (`principal.user_id`); a system key or a client_credentials token has no user and gets 403 on user-owned resources like posts.
API reference (generated): `/api/docs`, spec at `/api/openapi.json`. Browser routes: `/docs`.
**API keys.** Logged-in users create and revoke keys at `/settings/api-keys`; the key is shown once. From the shell:
```bash
ryuk apikey:create --user 1 --name ci --scope posts:read --scope posts:write # prints the key once
ryuk apikey:create --system --name backup --scope posts:read # no owner
ryuk apikey:list --user 1
ryuk apikey:revoke --user 1 <key_id>
curl -H "Authorization: Bearer ryk_..." http://127.0.0.1:3000/api/posts
curl -H "Authorization: Bearer ryk_..." -H "Content-Type: application/json" -d '{"title":"Hi","body":"..."}' http://127.0.0.1:3000/api/posts
```
**OAuth.** The app is also an OAuth 2 server (authorization code + PKCE, refresh, client credentials): `/oauth/authorize` (consent screen), `/oauth/token`, `/oauth/revoke`, `/oauth/introspect`.
```bash
ryuk oauth:client --name "My integration" --redirect https://example.com/callback --scope posts:read # add --public for a PKCE-only client
curl -u CLIENT_ID:CLIENT_SECRET -d grant_type=client_credentials -d scope=posts:read http://127.0.0.1:3000/oauth/token
```
**Mobile apps.** A native app is a *public* OAuth client (no secret can live in an app): it opens the system browser on `/oauth/authorize` with PKCE (S256), the user logs in on the normal web login, the app gets `https://app.example.com/cb?code=..&state=..` (an https app link: iOS universal link / Android app link, see `.well-known` below; a custom scheme like `myapp://callback` also works but shows the consent screen), exchanges the code at `/oauth/token` for a short access token plus a rotating refresh token, and calls `/api/*` with `Authorization: Bearer`. Logout is `/oauth/revoke`. Do not ship API keys in an app, and do not use the password grant.
```bash
ryuk oauth:client --name "My app" --public --first-party --redirect https://app.example.com/cb --scope profile --scope posts:read --scope posts:write --scope devices --scope account
curl -H "Authorization: Bearer $ACCESS" http://127.0.0.1:3000/api/me # {"id","name","email","email_verified"}
```
`--first-party` (needs `--redirect`) means logged-in users skip the consent screen, but ONLY when the redirect URI is `https` (a verified app link only your app can open). A custom scheme such as `myapp://` can be claimed by any app on the device, so it always shows the consent screen: use https app links for silent sign-in. `prompt=consent` on the authorize URL forces it. Third-party clients always see it. `tests/flows.rs` runs this whole flow in-process with a real S256 challenge; the long guide is in the docs (Mobile apps). Not covered here: a real device or system browser.
**Social login.** Set `GITHUB_CLIENT_ID/SECRET` and/or `GOOGLE_CLIENT_ID/SECRET` (see `.env.example`); the login page shows the buttons for configured providers.
**CORS** is off by default (see `src/lib.rs`). The rate limit is under Operations.
## Push notifications
Mobile apps register their APNs / FCM token after login: `POST /api/devices` `{"platform":"ios|android","token":"...","app_id":"...","name":"..."}` (scope `devices`; a token that was registered to another user moves to the caller). `GET /api/devices` lists, `DELETE /api/devices/{id}` removes. Tokens are stored to send with but never returned or logged.
- Sending from code: `jobs::send_push::notify(&queue, user_id, "Title", "Body", [("k", "v")]).await?` queues a `SendPush` job; run `ryuk queue:work`.
- Without keys the **log driver** is used: nothing leaves the machine, sends are logged. Set `PUSH_DRIVER`, `FCM_*` and `APNS_*` (see `.env.example`; keys come from files or a secret store, never commit them).
- `ryuk push:test --user 1 --title Hi --body "It works" --data k=v` sends immediately and prints the report; `ryuk device:list --user 1` lists a user's devices.
- The FCM and APNs integrations are tested against mocks only, never against the real services: try a real device before you ship.
## Account security
- `/settings/security` (web): authorized apps (revoke), push devices (remove), log out everywhere, change password, delete account. API twins: `GET/DELETE /api/sessions`, `DELETE /api/me` with `{"confirm":"DELETE","password":"..."}` (scope `account`; `password` only for accounts that have one).
- Log out everywhere revokes every OAuth token and every database session, including this one. Changing the password revokes OAuth grants and the other sessions but keeps this browser signed in; resetting a forgotten password signs the user out everywhere. API keys are not touched by either (revoke them on `/settings/api-keys`).
- Session revocation works because sessions live in the database store (`DbStore`). If you switch to the file, redis or moka store, sessions cannot be revoked per user: only OAuth tokens are.
- Deleting an account revokes its credentials first, then deletes posts, devices, API keys, OAuth tokens, linked identities and the user in one transaction, then revokes again. It is safe to repeat. Accounts without a password (social login only) set one with `POST /settings/password` without a current password.
## Operations
- The web process runs an hourly maintenance task: expired sessions, one-time tokens, login throttle rows, OAuth codes and tokens and API keys older than 30 days, push devices invalid for 30 days or idle for 270 days, finished rate limit windows. Counts are logged at info (`maintenance`).
- `API_RATE_LIMIT=db` (default) counts API requests in the database so every instance shares the limit; `memory` counts per process. `API_RATE_LIMIT_MAX` (120) and `API_RATE_LIMIT_WINDOW_SECS` (60).
- `MIN_APP_VERSION_IOS` / `MIN_APP_VERSION_ANDROID`: apps that send `X-App-Platform` and an older `X-App-Version` get `426 upgrade_required`.
- App links (the https redirect URI of your first-party client must be covered by these, that is what makes silent sign-in safe): `GET /.well-known/apple-app-site-association` (`APPLE_APP_ID=TEAMID.bundle.id`, optional `APPLE_APP_PATHS`) and `GET /.well-known/assetlinks.json` (`ANDROID_PACKAGE`, `ANDROID_SHA256_CERT_FINGERPRINTS`) answer 404 until configured.
## Deploy with Docker
```bash
docker build -t {{name}} .
docker run -p 3000:3000 --env-file .env.production {{name}}
```
Copy `.env.example` to `.env.production` and set `DATABASE_URL`, `APP_URL`, `TRUSTED_PROXIES` and the mail settings.
The image runs as a non-root user, listens on `$PORT`, health-checks `/healthz` (liveness), serves `/readyz` (database check) for orchestrators and drains on SIGTERM.
Production refuses to start with the log mail driver (`MAIL_DRIVER` unset or `log`) unless `ALLOW_LOG_MAIL=true`; use `MAIL_DRIVER=smtp`. `ryuk migrate:fresh` refuses in production without `--force`. Throttles: login/register/forgot are limited per email+IP (5 per 15 min) and per IP (50 per 15 min); forgot/resend/register mails also per email (10 per 15 min). A password reset and "log out everywhere" also revoke API keys; a password change keeps them.
Put it behind a TLS-terminating reverse proxy and list that proxy in `TRUSTED_PROXIES`, otherwise login throttling sees the proxy's address for every client.
With postgres/mysql, `docker compose --profile app up --build` starts the database and the app together.