From 540c1f44ffd1f5a9a7757537460756bbbade55f1 Mon Sep 17 00:00:00 2001 From: Dariusz Rozmus Date: Tue, 1 Dec 2026 22:36:37 +0100 Subject: [PATCH] docs(auth): align login method vocabulary, configuration, and OpenAPI spec --- public/openapi.yaml | 15 +++- src/content/docs/api-reference/overview.md | 4 +- src/content/docs/architecture/auth-model.md | 21 +++--- src/content/docs/backend/database.md | 4 +- src/content/docs/backend/docker-deployment.md | 15 ++-- src/content/docs/backend/overview.md | 2 +- src/content/docs/contributing/glossary.md | 12 +++- .../docs/docker-setup/docker-celery.mdx | 6 ++ src/content/docs/frontend/auth-flows.md | 69 ++++++++++++++----- src/content/docs/frontend/overview.md | 2 +- src/content/docs/local-setup/local-celery.mdx | 6 +- src/content/docs/local-setup/local-slurm.mdx | 1 - 12 files changed, 110 insertions(+), 47 deletions(-) diff --git a/public/openapi.yaml b/public/openapi.yaml index cfa88c6..5cea8cc 100644 --- a/public/openapi.yaml +++ b/public/openapi.yaml @@ -10,15 +10,15 @@ paths: get: security: - basicAuth: [ ] - summary: Allows to check if server is alive - description: Allows to check if server is alive. If server is down it won't respond to this request + summary: Check server reachability and login capabilities + description: Checks if the server is alive and advertises whether local username/password accounts are enabled (`ENABLE_LOCAL_USERS`). If the server is down it won't respond to this request. responses: '200': description: Successful operation content: application/json: schema: - $ref: '#/components/schemas/BasicResponse' + $ref: '#/components/schemas/RootResponse' # Authorisation Routes /auth/register: @@ -1189,6 +1189,15 @@ components: message: type: string description: body message + RootResponse: + type: object + properties: + message: + type: string + description: body message + local_users_enabled: + type: boolean + description: whether the backend accepts local username/password accounts (controlled by ENABLE_LOCAL_USERS) KeycloakRequest: type: object properties: diff --git a/src/content/docs/api-reference/overview.md b/src/content/docs/api-reference/overview.md index a43fdb4..6af0860 100644 --- a/src/content/docs/api-reference/overview.md +++ b/src/content/docs/api-reference/overview.md @@ -21,8 +21,8 @@ Two authentication modes: | Mode | Login Endpoint | Token Source | |---|---|---| -| **Native** | `POST /auth/login` | Backend-issued JWT | -| **Keycloak SSO** | `POST /auth/keycloak` | Keycloak JWT exchanged for backend JWT | +| **Local login** | `POST /auth/login` | Backend-issued JWT | +| **SSO login** | `POST /auth/keycloak` | Keycloak JWT exchanged for backend JWT | Protected endpoints return `401 Unauthorized` if no valid token is present. diff --git a/src/content/docs/architecture/auth-model.md b/src/content/docs/architecture/auth-model.md index 7b0e4f9..7686ace 100644 --- a/src/content/docs/architecture/auth-model.md +++ b/src/content/docs/architecture/auth-model.md @@ -3,19 +3,21 @@ title: Authentication Model description: How authentication and authorization work across the YAPTIDE system. --- -YAPTIDE supports two authentication methods: **native Yaptide auth** (username/password) and **Keycloak SSO** (PLGrid federation). Both issue JWT tokens stored in httpOnly cookies. +YAPTIDE supports two login methods: **local login** (username/password stored in the YAPTIDE database) and **SSO login** (Keycloak / PLGrid federation). Both issue JWT tokens stored in httpOnly cookies. ## Authentication Methods | Method | When Used | Users | |---|---|---| -| **Yaptide Native** | Development, standalone deployments | Any registered user | -| **Keycloak SSO** | Production, PLGrid-integrated deployments | PLGrid-federated users | +| **Local login** | Development, standalone deployments (`ENABLE_LOCAL_USERS=true`) | Accounts in `YaptideUser` | +| **SSO login** | Production, PLGrid-integrated deployments | PLGrid-federated users (`KeycloakUser`) | -## Native Authentication Flow +## Local Login Flow Simple username/password registration and login. Passwords are hashed with Werkzeug's security module. +Local accounts are **disabled by default** on the backend. Set `ENABLE_LOCAL_USERS=true` to allow local registration, login, and token usage (for example in local development); leave it unset on SSO-only deployments. The backend advertises this setting via `local_users_enabled` in `GET /` so the UI knows whether to offer local login. + ``` ┌──────────┐ ┌─────────┐ │ UI │ │ Backend │ @@ -60,7 +62,7 @@ Simple username/password registration and login. Passwords are hashed with Werkz The UI auto-refreshes the access token at **1/3 of its lifetime** (approximately every 3 minutes) by hitting `GET /auth/refresh`. -## Keycloak SSO Flow +## SSO Login Flow (Keycloak) Used for PLGrid-integrated deployments. The UI manages the Keycloak session, then exchanges the Keycloak token with the backend for a local JWT. @@ -107,17 +109,18 @@ The UI uses `keycloak-js` SDK with these settings: | Silent SSO check | Enabled (`silentCheckSsoRedirectUri`) | | Token refresh | Auto-refresh when <5 min remaining | -Required environment variables: +Required frontend environment variables (setting `REACT_APP_KEYCLOAK_BASE_URL` turns SSO on; `_REALM` and `_CLIENT_ID` are required whenever it is set): ```bash REACT_APP_KEYCLOAK_BASE_URL=https://keycloak.example.com REACT_APP_KEYCLOAK_REALM=yaptide REACT_APP_KEYCLOAK_CLIENT_ID=my-client -REACT_APP_ALT_AUTH=plg ``` ### PLGrid Service Verification +Although the UI uses the generic term **SSO** for external login, HPC batch jobs are currently PLGrid-specific: with a different Keycloak instance users could log in, but batch jobs would not work without PLGrid's `PLG_YAPTIDE_ACCESS` claim and SSH certificate service (`CERT_AUTH_URL`). + When a Keycloak token arrives, the backend checks the `PLG_YAPTIDE_ACCESS` claim in the token. This ensures the user has been granted access to the YAPTIDE service in the PLGrid infrastructure. The backend also: @@ -157,9 +160,9 @@ The user model uses **SQLAlchemy polymorphic inheritance** on the `auth_provider ``` UserModel (base) -├── YaptideUserModel (auth_provider="yaptide") +├── YaptideUserModel (auth_provider="yaptide") # local account │ └── password_hash -└── KeycloakUserModel (auth_provider="keycloak") +└── KeycloakUserModel (auth_provider="keycloak") # SSO account ├── cert (SSH certificate) └── private_key (SSH private key) ``` diff --git a/src/content/docs/backend/database.md b/src/content/docs/backend/database.md index e069ef9..498d4e2 100644 --- a/src/content/docs/backend/database.md +++ b/src/content/docs/backend/database.md @@ -43,14 +43,14 @@ ClusterModel ──< BatchSimulationModel | `username` | String | Unique username | | `auth_provider` | String | `"yaptide"` or `"keycloak"` (discriminator) | -**`YaptideUser`** — native auth users. +**`YaptideUser`** — local accounts (username/password). | Column | Type | Description | |---|---|---| | `id` | Integer (FK → User) | | | `password_hash` | String | Werkzeug PBKDF2 hash | -**`KeycloakUser`** — Keycloak/PLGrid users. +**`KeycloakUser`** — SSO accounts (Keycloak/PLGrid). | Column | Type | Description | |---|---|---| diff --git a/src/content/docs/backend/docker-deployment.md b/src/content/docs/backend/docker-deployment.md index 5c1139a..bc5a907 100644 --- a/src/content/docs/backend/docker-deployment.md +++ b/src/content/docs/backend/docker-deployment.md @@ -61,9 +61,11 @@ container is reached at its new address without reloading nginx. ```bash cd yaptide -docker compose up --build -d +ENABLE_LOCAL_USERS=true docker compose up --build -d ``` +> Local accounts are disabled by default. Include `ENABLE_LOCAL_USERS=true` (or set it in `yaptide/.env`) when developing with local username/password accounts; leave it unset on SSO-only deployments. + The database is created or migrated when `yaptide_flask` starts. Wait for all services to be healthy: ```bash @@ -143,11 +145,12 @@ Set these in a `.env` file in the `yaptide/` root or pass them via Docker: ### Authentication -| Variable | Description | -|---|---| -| `KEYCLOAK_BASE_URL` | Keycloak server URL | -| `KEYCLOAK_REALM` | Keycloak realm | -| `CERT_AUTH_URL` | PLGrid cert-auth service URL | +| Variable | Default | Description | +|---|---|---| +| `ENABLE_LOCAL_USERS` | `false` | Enable local username/password accounts (set `true` for local development; leave unset on SSO-only deployments) | +| `KEYCLOAK_BASE_URL` | — | Keycloak server URL | +| `KEYCLOAK_REALM` | — | Keycloak realm | +| `CERT_AUTH_URL` | — | PLGrid cert-auth service URL | ### Simulator Storage (S3) diff --git a/src/content/docs/backend/overview.md b/src/content/docs/backend/overview.md index 11a6426..607ca85 100644 --- a/src/content/docs/backend/overview.md +++ b/src/content/docs/backend/overview.md @@ -26,7 +26,7 @@ yaptide/ │ ├── application.py # Flask app factory │ ├── routes/ │ │ ├── main_routes.py # Route registration -│ │ ├── auth_routes.py # Native auth (register/login/refresh) +│ │ ├── auth_routes.py # Local login (register/login/refresh) │ │ ├── keycloak_routes.py # Keycloak SSO auth │ │ ├── simulation_routes.py # Jobs (direct + batch) │ │ ├── result_routes.py # Results, estimators, logfiles diff --git a/src/content/docs/contributing/glossary.md b/src/content/docs/contributing/glossary.md index 439ed10..8cd969f 100644 --- a/src/content/docs/contributing/glossary.md +++ b/src/content/docs/contributing/glossary.md @@ -58,11 +58,19 @@ Quick reference for domain-specific terms that appear across the codebase and do **Poetry** : Python dependency manager used by the backend and converter. Replaces pip + requirements.txt with `pyproject.toml` + `poetry.lock`. +**Local login** +: Username and password authentication verified against accounts stored in the YAPTIDE database (`YaptideUser` table). Disabled by default on the backend unless `ENABLE_LOCAL_USERS=true` is set. + +**SSO login** +: Login through an external identity provider — currently PLGrid's Keycloak (`KeycloakUser` table). Enabled in the frontend whenever `REACT_APP_KEYCLOAK_BASE_URL` is configured. + +*Naming rule:* use **local** for accounts stored in the YAPTIDE database (`YaptideUser` = local account), **SSO** when code asks whether external login is available or was used (`KeycloakUser` = SSO account), **Keycloak** only in code and configuration that talk to Keycloak, and **PLGrid** in user-facing text and for PLGrid-specific checks. + **Keycloak** -: An open-source identity provider. YAPTIDE uses it for SSO authentication, especially for PLGrid HPC access. +: The identity provider behind SSO login. Handles OIDC authentication, token signing, and session management. **PLGrid** -: Polish national computing infrastructure. YAPTIDE can submit batch simulations to PLGrid clusters via Slurm. +: Polish national computing infrastructure that runs YAPTIDE's production Keycloak instance and HPC clusters. YAPTIDE submits batch simulations to PLGrid clusters via Slurm, which requires the `PLG_YAPTIDE_ACCESS` token claim and SSH certificates fetched from PLGrid's cert-auth service (`CERT_AUTH_URL`). **Slurm** : A workload manager for HPC clusters. YAPTIDE generates Slurm job scripts for batch execution. diff --git a/src/content/docs/docker-setup/docker-celery.mdx b/src/content/docs/docker-setup/docker-celery.mdx index ce83f9f..409c88e 100644 --- a/src/content/docs/docker-setup/docker-celery.mdx +++ b/src/content/docs/docker-setup/docker-celery.mdx @@ -38,6 +38,12 @@ Navigate to the `yaptide/` directory: cd yaptide ``` +Enable local accounts for development by creating or updating `yaptide/.env` (local login is disabled by default): + +```bash title="yaptide/.env" +ENABLE_LOCAL_USERS=true +``` + Two startup options are available: **Standard mode** — builds and starts all containers: diff --git a/src/content/docs/frontend/auth-flows.md b/src/content/docs/frontend/auth-flows.md index 342f006..0286d86 100644 --- a/src/content/docs/frontend/auth-flows.md +++ b/src/content/docs/frontend/auth-flows.md @@ -3,17 +3,44 @@ title: Auth Flows description: Authentication implementation in the YAPTIDE frontend. --- -The frontend supports two authentication modes and a demo mode that bypasses auth entirely. +The frontend supports two login methods (**local login** and **SSO login**) and a demo mode that bypasses authentication entirely. ## Mode Selection | Mode | Activated By | Backend Required | |---|---|---| -| **Standard** (username/password) | Default | Yes | -| **Keycloak SSO** (PLGrid) | `REACT_APP_ALT_AUTH=plg` | Yes + Keycloak | +| **Local login** (username/password) | Default (requires `ENABLE_LOCAL_USERS=true` on the backend) | Yes | +| **SSO login** (Keycloak / PLGrid) | `REACT_APP_KEYCLOAK_BASE_URL` (plus `_REALM` and `_CLIENT_ID`) | Yes + Keycloak | | **Demo** (no auth) | `REACT_APP_TARGET=demo` | No | -## Standard Authentication +### Configuration Cheat Sheet + +| Setup | Frontend (`ui/.env`) | Backend | Login screen | +|---|---|---|---| +| Local development | nothing | `ENABLE_LOCAL_USERS=true` | username/password form | +| Production (PLGrid) | `REACT_APP_KEYCLOAK_BASE_URL`, `_REALM`, `_CLIENT_ID` | `KEYCLOAK_BASE_URL`, `KEYCLOAK_REALM`, `CERT_AUTH_URL`; `ENABLE_LOCAL_USERS` unset | "Connect with PLGrid" only | +| Staging / debugging | same as production | same as production, plus `ENABLE_LOCAL_USERS=true` | PLGrid button plus "Log in with username and password" link | +| Local Slurm + Keycloak | `REACT_APP_KEYCLOAK_BASE_URL=http://127.0.0.1:8080`, `_REALM=yaptide`, `_CLIENT_ID=yaptide-app` | `KEYCLOAK_BASE_URL=http://127.0.0.1:8080`, … | "Connect with PLGrid" | +| Demo | `REACT_APP_TARGET=demo` | none | no login | + +## Login Screen Selection + +`ConfigService.tsx` derives `ssoEnabled` directly from `Boolean(process.env.REACT_APP_KEYCLOAK_BASE_URL)` (no separate on/off flag), while `AuthService.tsx` tracks `localLoginStatus: 'checking' | 'enabled' | 'disabled'` learned from `GET /`. + +Screen selection lives in a pure function, `chooseLoginScreen` (`src/WrapperApp/components/Login/chooseLoginScreen.ts`), which maps `{ sso, local }` and the user's selected method to one of four screens (`'sso' | 'local' | 'waitingForServer' | 'noLoginMethod'`): + +| SSO in build (`ssoEnabled`) | Backend local accounts (`localLoginStatus`) | Screen | Switch link / back arrow | +|---|---|---|---| +| `true` | `'checking'` | `SsoLoginPanel` (PLGrid button) | no | +| `true` | `'disabled'` | `SsoLoginPanel` (PLGrid button) | no | +| `true` | `'enabled'` | User's choice (SSO by default) | yes (`canChooseLoginMethod`) | +| `false` | `'enabled'` | `LocalLoginPanel` (username/password form) | no | +| `false` | `'checking'` | Loading indicator (`waitingForServer`) | — | +| `false` | `'disabled'` | Configuration help message (`noLoginMethod`) | — | + +`LoginPanel.tsx` simply switches on the returned `LoginScreen` and uses `canChooseLoginMethod()` as the single source of truth for showing the `"Log in with username and password"` link and back arrow. + +## Local Login ### Login Flow @@ -85,17 +112,17 @@ if (savedUser) { } ``` -## Keycloak SSO +## SSO Login (Keycloak) ### Configuration -`KeycloakAuthService.tsx` initializes the Keycloak JS SDK: +Setting `REACT_APP_KEYCLOAK_BASE_URL` turns SSO on (`ssoEnabled = true`). Whenever it is set, `REACT_APP_KEYCLOAK_REALM` and `REACT_APP_KEYCLOAK_CLIENT_ID` are also required — `KeycloakAuthService.tsx` has no placeholder defaults and throws an error on incomplete configuration: ```typescript const keycloak = new Keycloak({ - url: config.keycloakBaseUrl, // REACT_APP_KEYCLOAK_BASE_URL - realm: config.keycloakRealm, // REACT_APP_KEYCLOAK_REALM - clientId: config.keycloakClientId // REACT_APP_KEYCLOAK_CLIENT_ID + url: `${process.env.REACT_APP_KEYCLOAK_BASE_URL}/auth/`, + realm: process.env.REACT_APP_KEYCLOAK_REALM, + clientId: process.env.REACT_APP_KEYCLOAK_CLIENT_ID }); ``` @@ -188,20 +215,26 @@ In demo mode: ## Server Reachability -`AuthService.tsx` includes a **reachability poller** that periodically checks if the backend is accessible: +`AuthService.tsx` includes a **reachability poller** that periodically queries `GET /` on the backend. In addition to checking reachability, that endpoint reports `local_users_enabled` (converted to `localUsersEnabled` by `snakeToCamelCase`), which updates `localLoginStatus`: ```typescript -const checkServerReachable = async () => { - try { - await ky.get('auth/status', { credentials: 'include', timeout: 5000 }); - setServerReachable(true); - } catch { - setServerReachable(false); - } +const reachServer = () => { + if (demoMode) return Promise.resolve(setIsServerReachable(false)); + + return kyIntervalRef + .get('') + .json() + .then((r) => { + // Older backends omit local_users_enabled and accept local accounts. + setLocalLoginStatus(r.localUsersEnabled === false ? 'disabled' : 'enabled'); + return !!r.message; + }) + .then((status) => setIsServerReachable(status)) + .catch(() => setIsServerReachable(false)); }; ``` -If the server becomes unreachable, the UI shows a notification and disables simulation submission (remote simulations only — Geant4 Wasm continues to work). +A failed request marks the server unreachable without changing `localLoginStatus` (so the local-login link never flashes while waiting for the first response on SSO-only deployments). When the server is unreachable, the UI shows a notification and disables remote simulation submission (Geant4 Wasm continues to work). ## The `authKy` Client diff --git a/src/content/docs/frontend/overview.md b/src/content/docs/frontend/overview.md index 3e63290..f38bd2a 100644 --- a/src/content/docs/frontend/overview.md +++ b/src/content/docs/frontend/overview.md @@ -65,7 +65,7 @@ Each provider creates a React Context exposing a hook: | Tab | Component | Purpose | |---|---|---| -| `login` | LoginPanel | Username/password or Keycloak SSO | +| `login` | LoginPanel | Local or SSO login | | `editor` | EditorPanel | 3D scene editor (main working area) | | `examples` | ExamplesPanel | Load pre-built example simulations | | `simulations` | SimulationsPanel | View/manage submitted jobs | diff --git a/src/content/docs/local-setup/local-celery.mdx b/src/content/docs/local-setup/local-celery.mdx index 57c4e85..febdd51 100644 --- a/src/content/docs/local-setup/local-celery.mdx +++ b/src/content/docs/local-setup/local-celery.mdx @@ -176,19 +176,21 @@ Open a third backend terminal and go to the `yaptide/` directory. Run: ```bash -FLASK_SQLALCHEMY_ECHO=True FLASK_USE_CORS=True FLASK_SQLALCHEMY_DATABASE_URI="sqlite:///db.sqlite" CELERY_BROKER_URL=redis://127.0.0.1:6379/0 CELERY_RESULT_BACKEND=redis://127.0.0.1:6379/0 poetry run flask --debug --app yaptide.application run +ENABLE_LOCAL_USERS=true FLASK_SQLALCHEMY_ECHO=True FLASK_USE_CORS=True FLASK_SQLALCHEMY_DATABASE_URI="sqlite:///db.sqlite" CELERY_BROKER_URL=redis://127.0.0.1:6379/0 CELERY_RESULT_BACKEND=redis://127.0.0.1:6379/0 poetry run flask --debug --app yaptide.application run ``` ```powershell -$env:FLASK_SQLALCHEMY_ECHO="True"; $env:FLASK_USE_CORS="True"; $env:FLASK_SQLALCHEMY_DATABASE_URI="sqlite:///db.sqlite"; $env:CELERY_BROKER_URL="redis://127.0.0.1:6379/0"; $env:CELERY_RESULT_BACKEND="redis://127.0.0.1:6379/0"; poetry run flask --debug --app yaptide.application run +$env:ENABLE_LOCAL_USERS="true"; $env:FLASK_SQLALCHEMY_ECHO="True"; $env:FLASK_USE_CORS="True"; $env:FLASK_SQLALCHEMY_DATABASE_URI="sqlite:///db.sqlite"; $env:CELERY_BROKER_URL="redis://127.0.0.1:6379/0"; $env:CELERY_RESULT_BACKEND="redis://127.0.0.1:6379/0"; poetry run flask --debug --app yaptide.application run ``` +**Why `ENABLE_LOCAL_USERS=true`?** Local username/password accounts are disabled by default on the backend. Without this flag, local login is hidden in the UI and rejected with HTTP 403. + **Why `FLASK_USE_CORS=True`?** During development, the frontend runs on `localhost:3000` and the backend on `localhost:5000`. Without CORS enabled, the browser blocks cross-origin requests. This creates `db.sqlite` inside `./instance/` (default [Flask instance folder](https://flask.palletsprojects.com/en/3.0.x/config/#instance-folders)). diff --git a/src/content/docs/local-setup/local-slurm.mdx b/src/content/docs/local-setup/local-slurm.mdx index 7556a80..4ffba4a 100644 --- a/src/content/docs/local-setup/local-slurm.mdx +++ b/src/content/docs/local-setup/local-slurm.mdx @@ -284,7 +284,6 @@ npm install Create a `.env` file in the `ui/` directory: ```bash title="ui/.env" -REACT_APP_ALT_AUTH='plg' REACT_APP_KEYCLOAK_BASE_URL='http://127.0.0.1:8080' REACT_APP_KEYCLOAK_REALM='yaptide' REACT_APP_KEYCLOAK_CLIENT_ID='yaptide-app'