Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions public/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/content/docs/api-reference/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
21 changes: 12 additions & 9 deletions src/content/docs/architecture/auth-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 │
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
```
Expand Down
4 changes: 2 additions & 2 deletions src/content/docs/backend/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|---|---|---|
Expand Down
15 changes: 9 additions & 6 deletions src/content/docs/backend/docker-deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/backend/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/content/docs/contributing/glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions src/content/docs/docker-setup/docker-celery.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
69 changes: 51 additions & 18 deletions src/content/docs/frontend/auth-flows.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
});
```

Expand Down Expand Up @@ -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<ResponseRoot>()
.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

Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/frontend/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
6 changes: 4 additions & 2 deletions src/content/docs/local-setup/local-celery.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,21 @@ Open a third backend terminal and go to the `yaptide/` directory. Run:
<TabItem label="Linux">

```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
```

</TabItem>
<TabItem label="Windows (PowerShell)">

```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
```

</TabItem>
</Tabs>

**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)).
Expand Down
1 change: 0 additions & 1 deletion src/content/docs/local-setup/local-slurm.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down