mirror of
https://github.com/deunix-educ/PyQuest.git
synced 2026-11-25 08:19:44 +01:00
a4df92a7c6
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
60 lines
3.1 KiB
Markdown
60 lines
3.1 KiB
Markdown
# Sync API
|
|
|
|
Available only if the Django backend is deployed. It is always on the **same domain** as the application:
|
|
`/api/sync/`.
|
|
|
|
## Authentication
|
|
|
|
Each request carries a **token**: `Authorization: Bearer <token>`. The token is generated by the client (256 bits,
|
|
43 URL-safe characters; the server accepts 32 to 128 `A-Za-z0-9_-` characters). The server only keeps its
|
|
**SHA-256**. There is no account and no cookie (hence no CSRF).
|
|
|
|
## Calls
|
|
|
|
| Call | Response |
|
|
|---|---|
|
|
| `GET /api/sync/` | `200 {"payload": …, "updated_at": …}`; `404 {"error": …}` if nothing is stored |
|
|
| `PUT /api/sync/` with `{"payload": …, "base": …}` | `200 {"updated_at": …}`; `409` if `base` is stale; `400` invalid or too large payload |
|
|
| `DELETE /api/sync/` | `204`: permanently erases this token's data (idempotent) |
|
|
| any call without a valid token | `401 {"error": …}` |
|
|
|
|
- `payload`: the object exported by the application (`version`, `completedLevels`, `badges`, `streak`). Maximum
|
|
size: 256 KB.
|
|
- `base` (optional): the `updated_at` read by the client. If it no longer matches the server version (another
|
|
device wrote in between), the server answers **`409`** with the current state (`payload`, `updated_at`): the
|
|
client merges and retries. This is **optimistic concurrency**.
|
|
|
|
## Who merges?
|
|
|
|
**The client.** The server stores the last JSON received and merges nothing. Merging keeps the union of completed
|
|
levels and badges, the maximum best streak, and the current streak of the most recently active device.
|
|
|
|
## Client cycle
|
|
|
|
1. `GET` then local merge;
|
|
2. `PUT` of the merged state with `base`; on `409`, merge the received state and retry (4 at most);
|
|
3. triggered on activation, at startup, when the network returns, when the tab becomes visible again and 2 seconds
|
|
after a local change.
|
|
|
|
Errors are visible states (`offline`, `token refused`, `server unavailable`, `data refused`, `invalid data`) and
|
|
never block the application. A `404` that is not JSON (a static server without Django) is treated as "server
|
|
unavailable".
|
|
|
|
## Editor access (author accounts)
|
|
|
|
The same backend exposes, for nginx and for the login page:
|
|
|
|
| Call | Purpose |
|
|
|---|---|
|
|
| `GET /api/auth/check/` | `204` if the session belongs to an active account with the `authors.use_editor` permission (the "Auteurs" group), otherwise `401`; used by nginx `auth_request` (not reachable from outside) |
|
|
| `GET /api/auth/login/` | login form (French or English from `Accept-Language`) |
|
|
| `POST /api/auth/login/` | sign in (CSRF); `302` to `next` (local paths only), `200` with a message if refused, `429` when too many attempts |
|
|
| `GET /api/auth/me/` | `200 {username, csrf}` for a signed-in author (shows the name and carries the token of the editor's sign-out button), otherwise `401` |
|
|
| `POST /api/auth/logout/` | sign out (CSRF); redirects to the login page |
|
|
| `/api/admin/` | Django administration (accounts, "Auteurs" group); static files under `/api/static/` |
|
|
|
|
## Security
|
|
|
|
Any data received from the server or from a file is treated as untrusted and strictly validated before merging
|
|
(types, bounds, identifiers). The server enforces the maximum size; nginx rate-limits.
|