Compare commits
45 Commits
feat/waves
...
feat/t14-3
| Author | SHA1 | Date | |
|---|---|---|---|
| 6c2e457225 | |||
| b78b9b3707 | |||
| 40827d5371 | |||
| 73f0b5e69d | |||
| 512f0b6b1d | |||
| ae649b5ed7 | |||
| 4fc6e8a01b | |||
| 2c199f6f3f | |||
| c3519d3e53 | |||
| 1a3468effd | |||
| 83e77ec622 | |||
| c75450a000 | |||
| 5f8ebab5b2 | |||
| 5cf6355a01 | |||
| d137d690f9 | |||
| ad1b8273c0 | |||
| bcef0e8145 | |||
| 2d4e96d668 | |||
| 493ead725d | |||
| 2abd55c745 | |||
| bbfa403117 | |||
| 253886923c | |||
| 763ab8d98d | |||
| f30989d4b8 | |||
| 103e4d40ee | |||
| 2f7de6e33b | |||
| d42ae0c65b | |||
| 9fc2d307bb | |||
| e50a05a1e0 | |||
| b2b3774dea | |||
| cfecdca3f7 | |||
| be3157baf2 | |||
| 0157b4b325 | |||
| c0b736e503 | |||
| f4d74879cc | |||
| d962ab2381 | |||
| 8dd0afaba4 | |||
| 912cf75027 | |||
| d04a0ff414 | |||
| 1c4d1bbccb | |||
| 74d3bc4af7 | |||
| 681048197a | |||
| cf46b3ce42 | |||
| 0e79e17a57 | |||
| 286e629782 |
10
.env.local.example
Normal file
10
.env.local.example
Normal file
@@ -0,0 +1,10 @@
|
||||
# Copy to .env in the repository root for a local Docker run (T13.8).
|
||||
# .env is ignored by git. Use values that are not used anywhere else.
|
||||
POSTGRES_USER=wpsuite
|
||||
POSTGRES_PASSWORD=local-only-change-me
|
||||
POSTGRES_DB=wpsuite
|
||||
# Generate: python -c "import secrets; print(secrets.token_urlsafe(48))"
|
||||
AUTH_SECRET_KEY=local-only-change-me-to-a-long-random-string
|
||||
# Rehearse the Okta sync first. Add OKTA_API_TOKEN and OKTA_ORG_URL only when you
|
||||
# want the local stack to call the real Okta, and keep this on until you read the log.
|
||||
OKTA_SYNC_DRY_RUN=true
|
||||
@@ -24,7 +24,7 @@ In the stack's **Environment variables** section, add:
|
||||
| Name | Value | Notes |
|
||||
|------|-------|-------|
|
||||
| `AUTH_SECRET_KEY` | a long random string | **Required.** Signs the login session cookies. |
|
||||
| `AUTH_SESSION_HOURS` | `12` | *Optional.* Hours a login lasts before re-auth (defaults to 12). |
|
||||
| `AUTH_SESSION_HOURS` | `8` | *Optional.* Hours a login lasts before re-auth (defaults to 8). |
|
||||
|
||||
Generate the secret on the host with:
|
||||
|
||||
|
||||
151
DEPLOYMENT.md
151
DEPLOYMENT.md
@@ -58,15 +58,6 @@ POSTGRES_PASSWORD=<strong-random-password>
|
||||
# openssl rand -base64 48
|
||||
AUTH_SECRET_KEY=<strong-random-secret>
|
||||
|
||||
# OPTIONAL — D18 (2026-09-23): a session slides on activity (AUTH_IDLE_MINUTES,
|
||||
# default 30) capped by a hard ceiling from original sign-in regardless of
|
||||
# activity (AUTH_SESSION_HOURS, default 8). Both are proposed defaults, not
|
||||
# confirmed against this tenant's Okta SSO session policy — if Okta's own
|
||||
# session outlives either one, re-auth here is likely a fast silent redirect
|
||||
# rather than a real login screen. Full explanation in server/.env.example.
|
||||
# AUTH_IDLE_MINUTES=30
|
||||
# AUTH_SESSION_HOURS=8
|
||||
|
||||
# REQUIRED (in spirit — see the note below) — Okta OIDC is the only sign-in
|
||||
# path (D15/D16). There is no local password anywhere in this app to fall back
|
||||
# to, so without these nobody can sign in at all. Get them from the Okta app
|
||||
@@ -413,6 +404,148 @@ Directory page — no shell access needed.
|
||||
unreachable for everyone, including admins, until Okta is restored — the same
|
||||
posture the abandoned LDAPS design took, carried forward deliberately (D16).
|
||||
|
||||
### Accounts the Okta sync disabled (D18)
|
||||
|
||||
**Who this is for:** app admins, and project super users who manage accounts.
|
||||
|
||||
When Okta credentials are set (`OKTA_API_TOKEN`, or the service-app pair below), the `api` container asks Okta every 15 minutes
|
||||
(`OKTA_SYNC_INTERVAL_SECONDS`) who is still active. It **disables** a local
|
||||
account when the person is missing from Okta's list, or when Okta shows the
|
||||
status SUSPENDED or DEPROVISIONED. A password expiry, a lockout, or a recovery
|
||||
in progress does not disable anyone. With no token set, the sync is off and the
|
||||
startup log says `Okta sync DISABLED`.
|
||||
|
||||
**Two ways to sign in to Okta.** Set one of them.
|
||||
|
||||
- *API token (SSWS):* set `OKTA_API_TOKEN` to the token string. It has about 40
|
||||
letters, digits, hyphens and underscores, and no quote marks.
|
||||
- *Service app with a private key (`private_key_jwt`):* set `OKTA_SYNC_CLIENT_ID`
|
||||
(the app's client ID, it starts with `0oa`) and `OKTA_SYNC_PRIVATE_KEY` (the
|
||||
private key as a JWK). The key starts with `{` and has fields such as `kty`,
|
||||
`kid` and `d`. In Portainer, encode it as base64 first, because Portainer can
|
||||
change the quote marks in JSON. In Windows PowerShell, save the key to a file
|
||||
`key.json` and run:
|
||||
|
||||
[Convert]::ToBase64String([IO.File]::ReadAllBytes("key.json"))
|
||||
|
||||
Paste the one-line result as the value. Delete `key.json` afterward. The key is
|
||||
a secret: never put it in Git, a ticket, or a chat.
|
||||
|
||||
The Okta admin must set up the app as follows: the app type is API Services; the
|
||||
scope `okta.users.read` is granted on the Okta API Scopes tab; an admin role
|
||||
that can read users (for example Read-only Administrator) is assigned to the app.
|
||||
DPoP can be on or off. By default (`OKTA_SYNC_DPOP=auto`) the sync asks for a plain
|
||||
token first, and if Okta answers `invalid_dpop_proof` it uses DPoP. Set
|
||||
`OKTA_SYNC_DPOP` to `on` or `off` only to force one way. If both
|
||||
`OKTA_API_TOKEN` and the service-app pair are set, the token is used.
|
||||
|
||||
The startup line in the log says which credential is in use. A failed sign-in
|
||||
shows the HTTP status and the Okta error code, for example
|
||||
`Okta answered HTTP 400 to the token request, error invalid_scope`. Common codes:
|
||||
`invalid_client` (wrong client ID or key, or the key is not registered on the app),
|
||||
`invalid_scope` (the scope is not granted), `access_denied` (no admin role assigned), `invalid_dpop_proof` (DPoP was used and Okta
|
||||
rejected the proof: send the log lines to the developer), `use_dpop_nonce` (Okta kept
|
||||
asking for a new nonce).
|
||||
|
||||
**First deploy: use the dry run.** Set `OKTA_SYNC_DRY_RUN=true` in `.env` (or the
|
||||
Portainer stack environment) with the token, then run `docker compose up -d --build api`.
|
||||
The sync runs as normal but changes no account and writes no audit row. Read the
|
||||
result:
|
||||
|
||||
docker compose logs api | grep "Okta sync"
|
||||
|
||||
The summary line shows how many Okta users and active local accounts it checked,
|
||||
how many matched, and how many it would disable. A line `DRY RUN would disable ...`
|
||||
names each account. If the match count is close to your number of real users and
|
||||
the list holds only people who have left, remove `OKTA_SYNC_DRY_RUN` (or set it
|
||||
to `false`) and run `docker compose up -d api` again to go live. If the match
|
||||
count is low, the Okta attribute is wrong. Set `OKTA_SYNC_PROFILE_ATTRIBUTE`
|
||||
and run the dry run again.
|
||||
|
||||
The sync only ever disables. **It never re-enables an account.** A person comes
|
||||
back only when an admin enables the account by hand.
|
||||
|
||||
**How to see what the sync did**
|
||||
|
||||
1. Open the **Admin Console** (`admin.html`). Find the **Activity log** card.
|
||||
2. Click **Refresh**. Search for `okta_sync`, or set the type to **User accounts**.
|
||||
3. A row from the sync reads: Who `system:okta_sync`, Action `user deprovisioned
|
||||
by sync`, Item the username, Detail `okta_status: SUSPENDED` (or `MISSING` if
|
||||
the person was not in Okta's list at all).
|
||||
|
||||
Only app admins see these rows. They belong to no project.
|
||||
|
||||
**How to re-enable a person**
|
||||
|
||||
1. **Fix the person in Okta first.** Ask the Okta admin to make the person ACTIVE
|
||||
again (or to restore them, for a rehire). If you skip this step, the next sync
|
||||
cycle (up to 15 minutes later) disables the account again.
|
||||
2. Open the **User Directory** (`users.html`).
|
||||
3. Find the person. A disabled account shows the **Enable** button on its row.
|
||||
Click it.
|
||||
4. The person can sign in on their next attempt. The app writes a `user enabled`
|
||||
row to the Activity log under your name.
|
||||
|
||||
**Who may do step 3**
|
||||
|
||||
* An **app admin** may enable any account.
|
||||
* A **project super user** may enable an account only if every project the person
|
||||
belongs to is a project that super user administers. If the person is also on a
|
||||
project that the super user does not administer, the row is read-only and shows
|
||||
the reason. An app admin must make the change.
|
||||
* Nobody can change their own account.
|
||||
|
||||
**If no admin can sign in** (for example, a bad sync run disabled every admin),
|
||||
use the shell on the host. Replace `alice` with the username:
|
||||
|
||||
```bash
|
||||
docker compose exec api python -m server.manage_users list
|
||||
docker compose exec api python -m server.manage_users enable alice
|
||||
```
|
||||
|
||||
**If the sync disabled the wrong people**
|
||||
|
||||
1. Turn the sync off. Remove `OKTA_API_TOKEN` from `.env` and restart the api
|
||||
(`docker compose up -d api`). The sync starts only when the token is set.
|
||||
2. Find the cause before you turn it on again. The usual cause is that the Okta
|
||||
profile attribute does not match `users.username`. See
|
||||
`OKTA_SYNC_PROFILE_ATTRIBUTE` in `server/.env.example`.
|
||||
3. Enable the people who were disabled by mistake (steps above).
|
||||
4. Set the token again and restart.
|
||||
|
||||
If the sync cannot reach Okta, or Okta gives an answer the sync cannot trust, the
|
||||
sync changes **no** account in that cycle and writes a line to the api log:
|
||||
`docker compose logs api | grep "Okta sync"`.
|
||||
|
||||
## Run the stack on your own PC (T13.8)
|
||||
|
||||
Use this to check a build in Docker before you deploy it. It needs Docker Desktop.
|
||||
Do not use it on the host.
|
||||
|
||||
1. Copy `.env.local.example` to `.env` in the repository root. Change the two
|
||||
`CHANGE` values: the database password and `AUTH_SECRET_KEY`.
|
||||
2. Run these commands in the repository root:
|
||||
|
||||
docker compose -f docker-compose.yml -f docker-compose.local.yml up -d --build
|
||||
|
||||
3. Open `http://localhost:8080/api/health`. The answer is `{"ok":true}`.
|
||||
4. Read the api log: `docker compose logs api`. The log shows each database
|
||||
migration, then one `Okta sync` line. With no `OKTA_API_TOKEN`, the line says
|
||||
`DISABLED`. That is correct.
|
||||
5. To stop it and keep the data, run
|
||||
`docker compose -f docker-compose.yml -f docker-compose.local.yml stop`.
|
||||
To stop it and delete the data, replace `stop` with `down -v`.
|
||||
|
||||
What this proves: the images build, all migrations run on Postgres, and the app
|
||||
starts. What it does not prove: sign-in. Okta sends the browser back to the
|
||||
redirect URI that is set for the Okta app, and that is the production host name.
|
||||
To rehearse the Okta sync against your real Okta, add `OKTA_API_TOKEN` and
|
||||
`OKTA_ORG_URL` to `.env` and keep `OKTA_SYNC_DRY_RUN=true`. The local database has
|
||||
no real users, so every real Okta user shows as a non-match. Use it only to check
|
||||
that the token works and that the log lines appear.
|
||||
|
||||
The override file `docker-compose.local.yml` explains each change that it makes.
|
||||
|
||||
## Permissions roles
|
||||
|
||||
`User.role` is the **permissions** role; `User.project_role` is the person's **job
|
||||
|
||||
38
docker-compose.local.yml
Normal file
38
docker-compose.local.yml
Normal file
@@ -0,0 +1,38 @@
|
||||
# Local override (T13.8). Use it only on your own PC, never on the host.
|
||||
#
|
||||
# docker compose -f docker-compose.yml -f docker-compose.local.yml up -d --build
|
||||
#
|
||||
# What it changes, and why:
|
||||
# 1. It publishes the web server on http://localhost:8080 (loopback only). On the
|
||||
# host a reverse proxy reaches the web server over the `proxy` network and no
|
||||
# port is published.
|
||||
# 2. It replaces the external `proxy` network with an ordinary bridge that this
|
||||
# stack owns. A `proxy` network that already exists on a PC may have no gateway
|
||||
# (internal), and Docker does not publish ports through such a network.
|
||||
# 3. It makes the web server answer for any host name, so http://localhost:8080
|
||||
# works. The shipped nginx config answers only for wp.controls.dev, and
|
||||
# without this every /api request from localhost gets a 404. The change is
|
||||
# made when the container starts: it removes the stock default.conf, which
|
||||
# answers to "localhost" first, and marks the app server as the default. The
|
||||
# shipped config file is not edited.
|
||||
# 4. It turns the backup service off. Its scripts are checked out with CRLF line
|
||||
# endings on Windows and cannot start (BL-070). To start it anyway, add
|
||||
# --profile backup.
|
||||
#
|
||||
# Sign-in needs a real Okta redirect URI, so this stack proves the build, the
|
||||
# database migrations on Postgres, and the Okta sync. It does not prove sign-in.
|
||||
services:
|
||||
webserver:
|
||||
ports:
|
||||
- "127.0.0.1:8080:80"
|
||||
command:
|
||||
- sh
|
||||
- -c
|
||||
- rm -f /etc/nginx/conf.d/default.conf && sed -i 's/listen 80;/listen 80 default_server;/' /etc/nginx/conf.d/wp-suite.conf && exec nginx -g 'daemon off;'
|
||||
backup:
|
||||
profiles: ["backup"]
|
||||
|
||||
networks:
|
||||
proxy: !override
|
||||
name: wp_local_proxy
|
||||
driver: bridge
|
||||
@@ -31,7 +31,7 @@ services:
|
||||
# REQUIRED — compose fails fast if it's unset, and the API refuses to
|
||||
# start in production without it (see server/auth.py).
|
||||
AUTH_SECRET_KEY: ${AUTH_SECRET_KEY:?set AUTH_SECRET_KEY in .env (see server/.env.example)}
|
||||
AUTH_SESSION_HOURS: ${AUTH_SESSION_HOURS:-12}
|
||||
AUTH_SESSION_HOURS: ${AUTH_SESSION_HOURS:-8}
|
||||
# Okta OIDC — the only sign-in path (D15/D16). Not marked required the
|
||||
# way AUTH_SECRET_KEY is: the API starts without these, it just refuses
|
||||
# every sign-in and says so in the startup log (server/okta_auth.py
|
||||
@@ -49,6 +49,22 @@ services:
|
||||
# here instead, so an operator who leaves .env's copy commented out gets
|
||||
# the real default, not a broken one.
|
||||
OKTA_IDENTITY_CLAIM: ${OKTA_IDENTITY_CLAIM:-preferred_username}
|
||||
# Okta deprovisioning sync (D18, wave 13). OFF until OKTA_API_TOKEN is set; the
|
||||
# startup log says which. The token is a read-only Okta API token, NOT the OIDC
|
||||
# client secret above. Empty values here mean "unset" to the code, so the
|
||||
# ${VAR:-} form is safe for all four (unlike OKTA_IDENTITY_CLAIM above, whose
|
||||
# code default only applies when the variable is absent).
|
||||
# Reaching Okta needs outbound https. The `outbound` network the api service
|
||||
# already joins is an ordinary bridge with a gateway, so no new network is
|
||||
# needed; the host firewall must allow https to the Okta org.
|
||||
OKTA_API_TOKEN: ${OKTA_API_TOKEN:-}
|
||||
OKTA_ORG_URL: ${OKTA_ORG_URL:-}
|
||||
OKTA_SYNC_CLIENT_ID: ${OKTA_SYNC_CLIENT_ID:-}
|
||||
OKTA_SYNC_PRIVATE_KEY: ${OKTA_SYNC_PRIVATE_KEY:-}
|
||||
OKTA_SYNC_DPOP: ${OKTA_SYNC_DPOP:-}
|
||||
OKTA_SYNC_PROFILE_ATTRIBUTE: ${OKTA_SYNC_PROFILE_ATTRIBUTE:-}
|
||||
OKTA_SYNC_INTERVAL_SECONDS: ${OKTA_SYNC_INTERVAL_SECONDS:-}
|
||||
OKTA_SYNC_DRY_RUN: ${OKTA_SYNC_DRY_RUN:-}
|
||||
# Optional — SMTP password for WP-assignment emails. Email is off by
|
||||
# default and enabled from the Admin console; this is the only email
|
||||
# secret and it is never stored in the DB. Leave unset until configured.
|
||||
|
||||
BIN
docs/reference/baseline/wave-12/users-1440-selected.png
Normal file
BIN
docs/reference/baseline/wave-12/users-1440-selected.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 128 KiB |
BIN
docs/reference/baseline/wave-12/users-1440.png
Normal file
BIN
docs/reference/baseline/wave-12/users-1440.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 112 KiB |
BIN
docs/reference/baseline/wave-12/users-390-selected.png
Normal file
BIN
docs/reference/baseline/wave-12/users-390-selected.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 97 KiB |
BIN
docs/reference/baseline/wave-12/users-390.png
Normal file
BIN
docs/reference/baseline/wave-12/users-390.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 78 KiB |
@@ -318,6 +318,17 @@ python tests/critical_reopen_check.py # BL-021 - on-hold mail reaches PM + CM
|
||||
python tests/console_dialogs_check.py # BL-024 - consoles/launcher: 21 natives -> 0 17 checks
|
||||
```
|
||||
|
||||
Wave 12 (CR-020, bulk user editing) adds these:
|
||||
|
||||
```bash
|
||||
python tests/bulk_users_check.py # CR-020 - bulk routes, guards, audit, CSV; NO browser 61 checks
|
||||
python tests/bulk_users_ui_check.py # CR-020 - selection, toolbar, CSV, typed delete at 1440/390px
|
||||
```
|
||||
|
||||
`console_dialogs_check.py` gained section 4 (the bulk delete prompt). `tests/cdp.py`
|
||||
passes `--do-not-de-elevate` on Windows so the probes start from an Administrator
|
||||
shell (same change as wave 16, T16.4).
|
||||
|
||||
**Three probes were re-pointed at `T7.1`.** `sections_check.py` 5b drove the live
|
||||
cross-frame toggle hand-off, which no longer exists; it now pins the surviving path and
|
||||
the fact that an UNSAVED toggle correctly does not travel - a behaviour change, recorded
|
||||
|
||||
@@ -662,3 +662,194 @@ deliberately deferred.
|
||||
worth confirming rather than assuming).
|
||||
- **Suggested wave or follow-up:** next housekeeping pass — needs Nick on
|
||||
whether to delete, archive, or rewrite it as historical record.
|
||||
|
||||
### BL-060 — A super user is told they can change access the server then refuses
|
||||
|
||||
- **Found during:** T12.6 (CR-020 verification, the CSV path)
|
||||
- **Where:** `server/app.py` `manage_user_problem` (the reason text), `get_user_projects` / `set_user_projects` (the rule)
|
||||
- **What:** for an account that is also on a project the super user does not
|
||||
administer, the refusal reason ends "You can still change their access and
|
||||
role on your own projects." Neither the project-access dialog's routes nor
|
||||
the CR-020 bulk project route allow that: all of them call
|
||||
`require_manage_user`, which refuses the account. The bulk route mirrors the
|
||||
single route on purpose (CR-020: same rules), so the words promise something
|
||||
neither path does. Either the text or the rule is wrong; which one is a
|
||||
product call.
|
||||
- **Why not now:** changing either is out of CR-020's scope (the wave must
|
||||
not widen what a super user can do), and it needs a decision.
|
||||
- **Suggested wave or follow-up:** needs Nick: should a super user be able to
|
||||
add or remove a shared account on their own projects? Then fix the text or
|
||||
the rule, not both.
|
||||
|
||||
### BL-061 — The sync badge covers controls while it shows
|
||||
|
||||
- **Found during:** T12.6 (the wave 12 screenshots)
|
||||
- **Where:** `html/project-data.js` `renderSyncBadge` (fixed bottom-right, `z-index:9998`)
|
||||
- **What:** for about 2 seconds after each sync the "Everything sent to the
|
||||
project" badge sits over whatever is in the bottom-right corner: a Delete
|
||||
button on the User Directory at 1440px, the bulk bar's "Set permissions" at
|
||||
390px. It is `pointer-events:none`, so clicks pass through, but the control is
|
||||
hidden while it shows, and on the console pages there is no outbox work to
|
||||
report at all.
|
||||
- **Why not now:** `project-data.js` is shared by every page and owned by the
|
||||
outbox work in wave 14 (T14.1 to T14.3 change this badge's states anyway).
|
||||
- **Suggested wave or follow-up:** wave 14, alongside T14.1's badge changes:
|
||||
do not show the badge on pages with nothing to sync, and keep it clear of
|
||||
page controls.
|
||||
- **Status:** fixed at T14.3 (2026-10-17). The console pages never show the badge, "Everything sent" shows only
|
||||
after real work, and the badge moves to a free spot. One dense-screen case is left: BL-072.
|
||||
|
||||
### BL-069 - The sync has no cap on one run and no last-admin guard
|
||||
|
||||
- **Found during:** T13.3 (D18)
|
||||
- **Where:** `server/okta_sync.py` `run_sync()`
|
||||
- **What:** a run disables every account the rules select, however many. If the
|
||||
identity attribute is partly wrong (it matches some accounts and not others),
|
||||
one run can disable most of the company, including every admin. The
|
||||
zero-match guard only stops the case where nothing matches. Disabled accounts
|
||||
cannot sign in, so re-enabling needs an admin who is still active, or the
|
||||
`manage_users.py` command line on the host.
|
||||
- **Why not now:** D18 does not ask for it, and the right limit is a product
|
||||
call: a count, a share of accounts, a rule that the last active admin never
|
||||
disables, or all three.
|
||||
- **Suggested wave or follow-up:** before the first live run, or in T13.6 if
|
||||
Matt decides. Needs Matt.
|
||||
|
||||
## Future build: mobile app and scaling
|
||||
|
||||
Logged 2026-10-12 from the meeting with Nick. The answers are in
|
||||
`docs/waves/decisions-2026-10-12.md` (Q2 to Q14). The path from here to a
|
||||
mobile version outside the VPN, across projects, is in
|
||||
`docs/waves/roadmap-mobile-scaling.md`. These are features for a future
|
||||
build. They have no wave and no `CR-`/`D-` id yet. Give each one an id when a
|
||||
wave is planned. Items marked "to confirm" have an open question listed in the
|
||||
decision file ("Still open"). Do not build them until it is answered.
|
||||
|
||||
Follow-up answers from Matt on 2026-10-12 are folded into the entries below.
|
||||
|
||||
Server work is needed for BL-062 to BL-066 (CLAUDE.md, "Frontend and backend
|
||||
boundary"). Do not use `localStorage` as the store of record.
|
||||
|
||||
### BL-062 - Offline Field View for crews
|
||||
|
||||
- **From:** Q2, Q3
|
||||
- **What:** Field View opens offline for up to **7 days** after the last sign-in
|
||||
(today: 12 hours). Only crews in Field View work offline. The creator has no
|
||||
offline mode. When the app opens online again and the account is disabled,
|
||||
wipe that account's local data.
|
||||
- **Why not now:** new scope. It needs an id, and it follows the outbox work in
|
||||
wave 14 (T14.1 to T14.3).
|
||||
- **Suggested wave or follow-up:** future mobile wave. Check it against the D18
|
||||
session rules (idle timeout, absolute ceiling): a 7 day offline window and a
|
||||
session ceiling must agree on what "signed in" means.
|
||||
|
||||
### BL-063 - Offline edit conflicts: merge, notify, log
|
||||
|
||||
- **From:** Q4
|
||||
- **What:** When an offline edit meets a newer server version, the app merges
|
||||
the two, shows a notification to the crew, writes a log entry, and updates the
|
||||
owner. If both sides changed the same field, **the field value wins**, and the
|
||||
app keeps a **history of changes**. This replaces the earlier default (keep
|
||||
both, crew chooses).
|
||||
- **To confirm:** who "the owner" is, how long the history is kept, and who can
|
||||
read it. See the decision file, "Still open" item 2.
|
||||
- **Suggested wave or follow-up:** same wave as BL-062.
|
||||
|
||||
### BL-064 - Field photo limits
|
||||
|
||||
- **From:** Q5
|
||||
- **What:** a limit of **5 MB per photo**. This equals the D8 limit that the
|
||||
server already enforces for drawings (`FILE_MAX_BYTES`), so photos can use the
|
||||
same upload path and the same D19 download headers.
|
||||
- **To confirm:** the count per package, and whether the app resizes a photo
|
||||
before upload (phone photos are often 3 MB to 8 MB). Decision file, "Still
|
||||
open" item 1.
|
||||
- **Suggested wave or follow-up:** same wave as BL-062.
|
||||
|
||||
### BL-065 - Batch print and merged PDF
|
||||
|
||||
- **From:** Q6, Q7, Q8, Q9
|
||||
- **What:** print many packages in one PDF.
|
||||
- Limit: **25 packages on phones**. On desktop the user can scale the filter
|
||||
up to 50 or 100. Open: who sets the larger value (each user, or an admin
|
||||
setting). Decision file, "Still open" item 5.
|
||||
- Drawings go **after the form**, in **upload order**, **scaled to fit**.
|
||||
- SVG drawings are **left out**. A page names each file that was left out.
|
||||
- Every page shows the **package number, the print date, and page X of Y**.
|
||||
- **Related:** BL-000d (merge or list attachments on export) asked the same
|
||||
question for CR-008. This entry answers the layout part of it.
|
||||
- **Suggested wave or follow-up:** future print wave.
|
||||
|
||||
### BL-066 - Keep drawings when a package is deleted
|
||||
|
||||
- **From:** Q13
|
||||
- **What:** deleting a package no longer deletes its drawings. Keep them for
|
||||
data retention. **The storage quota is freed** when the package is deleted.
|
||||
Storage will move to cloud storage in a future deployment. This replaces the
|
||||
earlier default (delete with the package).
|
||||
- **Today:** drawing bytes live in the Postgres `wp_files` table (CR-007, D8).
|
||||
The 2 GB per project limit counts them. Kept files must not count toward that
|
||||
limit, so the count has to change.
|
||||
- **To confirm:** retention period, who can delete for good, who can recover.
|
||||
See the decision file, "Still open" item 3.
|
||||
- **Suggested wave or follow-up:** roadmap Phase 3 (object storage). It affects
|
||||
D19 (safe download) paths too: recovered files must keep the attachment and
|
||||
sandbox headers.
|
||||
|
||||
### BL-067 - Ready for QA progress weight is 0.85
|
||||
|
||||
- **From:** Q12
|
||||
- **What:** change the progress weight for Ready for QA from 0 to **0.85**.
|
||||
Today a package falls from 75% to 0% when it reaches Ready for QA.
|
||||
- **Why not now:** it changes a shipped number on dashboards and roll-ups. Use
|
||||
its own item id and re-run the roll-up checks.
|
||||
- **Suggested wave or follow-up:** future wave. Small task.
|
||||
|
||||
### BL-068 - One cost-code list, kept in the SOP
|
||||
|
||||
- **From:** Q14
|
||||
- **What:** the creator has 38 cost codes, the SOP has 39, and 13 differ. Keep
|
||||
**one list, in the SOP**, and make the creator read it.
|
||||
- **Blocked on:** the correct codes. The **PM** supplies them (decided
|
||||
2026-10-12). The date is not set. This is a data question and not a code
|
||||
question.
|
||||
- **Related:** BL-019 (closed) made a stored code that has left the list stay
|
||||
as an option. Keep that behavior when the list changes.
|
||||
- **Suggested wave or follow-up:** future wave, after the codes arrive.
|
||||
|
||||
### BL-070 - Shell scripts get CRLF line endings on Windows, and the backup container cannot start
|
||||
|
||||
- **Found during:** T13.8 (local Docker run)
|
||||
- **Where:** `scripts/*.sh`, no `.gitattributes` in the repository
|
||||
- **What:** Git for Windows checks out `scripts/entrypoint.sh` with CRLF line endings.
|
||||
The container then fails with `exec /app/entrypoint.sh: no such file or directory`
|
||||
and restarts in a loop. The files are stored with LF in Git, so the host (Linux) is
|
||||
not affected. Any PC build is.
|
||||
- **Why not now:** T13.8 only makes the local run work. The override turns the backup
|
||||
service off. A `.gitattributes` rule (`*.sh text eol=lf`) is a repository-wide change.
|
||||
- **Suggested wave or follow-up:** next housekeeping pass. Add `*.sh text eol=lf` and
|
||||
renormalize.
|
||||
|
||||
### BL-071 - smoketest.py cannot sign in against http://localhost
|
||||
|
||||
- **Found during:** T13.6 (final verification)
|
||||
- **Where:** `server/smoketest.py` `seed_session_cookie()`
|
||||
- **What:** the script puts the session cookie in a Python cookie jar for the domain `localhost`. The jar treats
|
||||
a host without a dot as `localhost.local`, so it does not send the cookie, and every call after sign-in returns 401.
|
||||
With `http://127.0.0.1:PORT` the same script passes 27 of 27.
|
||||
- **Why not now:** it is a test-script quirk and not part of D18. The documented usage lines show `http://localhost:8000`.
|
||||
- **Suggested wave or follow-up:** next housekeeping pass: use the domain `localhost.local` for a dotless host,
|
||||
or change the usage lines to `127.0.0.1`.
|
||||
|
||||
### BL-072 - The sync badge can cover a control on a very dense screen
|
||||
|
||||
- **Found during:** T14.3 (D26, BL-061)
|
||||
- **Where:** `html/project-data.js` `placeBadge()`; seen on `wp-creation-index.html` at 390px
|
||||
- **What:** the badge looks for a free spot on the screen, and it covers no control when the screen has room for it.
|
||||
With the longest state (a change refused by the project, with a long reason) on the creator page at 390px, no free
|
||||
spot of that size exists. The badge then covers the one control it overlaps least ("Load sample data" at the
|
||||
time of the check). The person can press Dismiss, or scroll (the badge moves again within 2 seconds).
|
||||
- **Why not now:** a fixed overlay cannot fix a screen with no gap. The real fix is a layout change: a bar that
|
||||
takes its own space in the page, or a place for it in the shared header. That is a design decision for all pages,
|
||||
not part of T14.3.
|
||||
- **Suggested wave or follow-up:** decide with Nick whether the shared page layout reserves a status line.
|
||||
|
||||
97
docs/waves/context-handoff-2026-10-12.md
Normal file
97
docs/waves/context-handoff-2026-10-12.md
Normal file
@@ -0,0 +1,97 @@
|
||||
# Context handoff: Work Package Suite
|
||||
|
||||
> Update 2026-10-16: waves 11 and 13 (T13.2 to T13.5) are on the release branch `release/waves-11-13-okta-sync`, with T13.1 held back. The branch and wave status tables below describe 2026-10-12. See `docs/waves/wave-13.md` for the current state.
|
||||
|
||||
Date: 2026-10-12
|
||||
Purpose: Give a new chat the facts about waves 11, 12, 13, and 16.
|
||||
This file is a handoff note. It is not a spec. The spec files stay the source of truth (`IMPLEMENTATION.md`, `docs/waves/*`, `CLAUDE.md`).
|
||||
It was checked against `git` on 2026-10-12. Earlier chat summaries about this repo are out of date. Use this file.
|
||||
|
||||
## 1. Branch state
|
||||
|
||||
| Branch | State |
|
||||
|---|---|
|
||||
| `main` | At `d42ae0c` (2026-10-06). Contains wave 12 (CR-020, bulk user editing) and wave 16 (D19, D20, D21), both merged by PR. |
|
||||
| `feat/waves-11-13` | Local and `origin` are the same commit, `da1325c`. It is pushed. It is not merged. It is 13 commits ahead of `main` and 19 behind. |
|
||||
| `notes/cr019-usage-metrics-d18-deprovisioning` | Holds the same wave 11 work plus the session notes. |
|
||||
|
||||
Correction to an earlier statement: the Gitea push is no longer blocked. The branch is on `origin`.
|
||||
|
||||
Merge rule from `wave-12.md`: T13.1 (idle timeout) must not reach `main` before T14.1. Do not merge `feat/waves-11-13` into `main` as a whole without a decision from the user. Wave 11 and T13.1 are on the same branch.
|
||||
|
||||
Working tree on `main` (2026-10-12) has two untracked files: `WorkPackageSuite_Consolidated_Change_Request_R2.docx` and `docs/waves/decisions-2026-08-25.md`. Do not delete them.
|
||||
|
||||
## 2. What is on `feat/waves-11-13`
|
||||
|
||||
| Commit | Task | Item |
|
||||
|---|---|---|
|
||||
| `75ac930` | Planning docs for CR-019, CR-020, D18 | CR-019, CR-020, D18 |
|
||||
| `0652fa7` | T11.1 UsageEvent model and migration | CR-019 |
|
||||
| `850b789` | T11.2 capture usage events | CR-019 |
|
||||
| `3584695`, `6cde6e3` | T13.1 idle timeout with an absolute ceiling | D18 |
|
||||
| `8e863ae` | T11.3 aggregation endpoint with filters | CR-019 |
|
||||
| `2de76d5` | T11.4 usage export (raw and sanitized CSV) | CR-019 |
|
||||
| `a6c3fbf`, `0b5ab59` | T11.5 admin console "Activity & usage" card, checked at 390px and 1440px | CR-019 |
|
||||
| `b2a083a` | T11.6 retire the per-browser Usage report | CR-019 |
|
||||
| `2842ec9` | Session notes for 2026-09-23 | none |
|
||||
| `4f3a94b` | T11.6 follow-up: fix `tests/autosave_check.py` | CR-019 |
|
||||
| `da1325c` | T11.7 full verification pass. Wave 11 closed on 2026-09-24. | CR-019 |
|
||||
|
||||
Notes:
|
||||
- `html/wp-usage.js` and `tests/usage_check.py` were deleted in T11.6. The `track()` functions in `wp-creation-app.js` and `work-package-suite-app.js` are now documented no-ops. About 45 call sites stay.
|
||||
- T11.7 found a real regression that T11.6 caused. `tests/autosave_check.py` still expected the retired tracker to write to `localStorage`. The test was fixed. It now passes 33 of 33. The cause was that T11.6 ran only `tests/baseline_shots.py`, not the full suite. Run the full suite for any frontend task.
|
||||
|
||||
## 3. T11.7 verification results (2026-09-24)
|
||||
|
||||
- Alembic: upgrade, downgrade, upgrade on SQLite passed. The SQL render for Postgres was clean (this catches SQLite-only DDL, the "BL-027 class" of defect).
|
||||
- Test scripts: 38 of 39 passed after the autosave fix.
|
||||
- Two known failures that are not regressions:
|
||||
- `token_check.py` exits 2. This is a CLI usage detail.
|
||||
- `generalinfo_check.py` finds an old color-literal gap in `wp-creation-styles.css`.
|
||||
|
||||
## 4. Wave status
|
||||
|
||||
| Wave | Items | Status |
|
||||
|---|---|---|
|
||||
| 10 | Okta OIDC sign-in (D15, D16) | Done. In production. |
|
||||
| 11 | CR-019 usage and activity metrics | Done on `feat/waves-11-13`. Not on `main`. |
|
||||
| 12 | CR-020 bulk user editing | Done. Merged to `main` on 2026-10-06 (T12.1 to T12.6). |
|
||||
| 13 | D18 Okta/AD deprovisioning sync | T13.1 done on `feat/waves-11-13`. T13.2 and later are not started. T13.3 must fail closed. Do not relax that. |
|
||||
| 14, 15 | Not checked in this handoff | T14.1 must land before T13.1 reaches `main`. Read the wave files before you plan. |
|
||||
| 16 | D19 safe downloads, D20 one SOP per project, D21 session decode fails closed | Done. Merged to `main` on 2026-10-06. |
|
||||
|
||||
Wave 16 decisions (`docs/waves/decisions-2026-10-02.md`): SEC-06 (session revocation) waits for the AWS work. Other SEC findings stay as backlog drafts in `docs/reference/security-review/backlog-drafts.md`.
|
||||
|
||||
## 5. Rules that apply here (`CLAUDE.md`)
|
||||
|
||||
- Every change traces to an item ID (`CR-`, `F`, `S`, `A`, `B`, `C`, `D`, task `T<wave>.<n>`). New scope gets a new ID.
|
||||
- One task per PR. Put the task ID and item IDs in the commit message and PR title.
|
||||
- Do not fix other problems that you see. Log them in `docs/waves/backlog.md`.
|
||||
- Do not reorder waves. Do not start a wave before the previous wave is merged, unless the task says it is independent.
|
||||
- Server work is needed for CR-004, CR-018, CR-007, CR-011, CR-014, CR-013, and B4. Do not use `localStorage` for these.
|
||||
- Do not change: Actual Hours in Closeout (CR-017), localization (A7), uppercase card headers in `console.css` (A5), the logged-override path `confirmEarlyRelease()` (A1), or hidden-not-deleted fields (CR-002, CR-016).
|
||||
- Token rule: one place defines color, spacing, and type. Do not add raw hex values to page stylesheets.
|
||||
- Accessibility (C1): use real `<button>`, `<a>`, or input elements. Add `aria-live` for status changes. Keep focus visible. Text contrast is 4.5:1 (3:1 for large text).
|
||||
- Frontend checks: run `uvicorn server.app:app` on a throwaway SQLite database. Check 390px and 1440px. Capture before and after screenshots. Run the smoke test.
|
||||
- If a done-when check cannot be run, do not mark the task complete. Say which check failed and why.
|
||||
|
||||
## 6. Environment on the user's PC
|
||||
|
||||
- Python was installed with `winget`. Tests use the repo `.venv`.
|
||||
- Browser tests (`tests/cdp.py` and about 40 `*_check.py` scripts) need Edge or Chrome. `cdp.py` starts from an elevated shell (wave 16 note). The Linux sandbox cannot run them.
|
||||
- See `C:\Users\m.mabrey\GIT\Field Value Driver\sde-meeting-toolkit\docs\context\environment-notes.md` for tool problems (PowerShell `$` removal, long git paths, the 180 second command limit).
|
||||
|
||||
## 7. Open items
|
||||
|
||||
1. Decide how `feat/waves-11-13` reaches `main`. The T13.1 and T14.1 order is the constraint.
|
||||
2. Plan the next wave. T13.2 and later need an explicit instruction.
|
||||
3. Future build list added 2026-10-12: `BL-062` to `BL-068` in `docs/waves/backlog.md` (mobile app, offline Field View, batch print, and related items). Answers are in `docs/waves/decisions-2026-10-12.md` (follow-ups from Matt included). The path to a mobile version outside the VPN is in `docs/waves/roadmap-mobile-scaling.md` (phases 0 to 5; confirmed: all users in the Okta group, multiple customers, company-managed devices; nine open questions in its section 7). These files are uncommitted edits on `main`.
|
||||
4. Work from the other chat (the SDE Meeting Toolkit) does not touch this repo. See the toolkit `docs/context/` folder.
|
||||
|
||||
## 8. Related files
|
||||
|
||||
- `IMPLEMENTATION.md` (sections 2 and 4)
|
||||
- `docs/waves/wave-11.md`, `wave-12.md`, `wave-13.md` (on `feat/waves-11-13`), `wave-16.md`
|
||||
- `docs/waves/decisions-2026-09-17.md` (CR-019, CR-020, D18), `decisions-2026-10-02.md` (D19, D20, D21)
|
||||
- `docs/waves/notes-2026-09-23.md` (on `feat/waves-11-13`)
|
||||
- `docs/waves/backlog.md`
|
||||
@@ -200,6 +200,21 @@ bad multi-select now removes more than one account at once, permanently.
|
||||
version is a materially different (and riskier) feature if that turns out to
|
||||
be what's actually wanted.
|
||||
|
||||
### Confirmed 2026-10-06 (Matt Mabrey, at the start of wave 12)
|
||||
|
||||
Both recommendations above are confirmed as written, and the two details
|
||||
`wave-12.md` left to the build are decided:
|
||||
|
||||
1. **Bulk delete:** the dialog lists the affected usernames and the request is
|
||||
not sent until the word `DELETE` is typed, whatever the count.
|
||||
2. **Project assignment is add/remove** to or from one project at a time, with
|
||||
the per-project permissions role (`ProjectMember.role`) set on add. It never
|
||||
replaces a user's whole project list.
|
||||
3. **Changing the table filter clears the selection** (T12.2), and the toolbar
|
||||
says so, so no row hidden by the filter stays selected.
|
||||
4. **The CSV holds usernames only** (T12.4). The action and its settings are
|
||||
chosen in the toolbar and apply to every valid row of the file.
|
||||
|
||||
### Frontend/backend boundary
|
||||
|
||||
Selection state (which rows are checked) is fine as client-side UI state — it
|
||||
|
||||
84
docs/waves/decisions-2026-10-02.md
Normal file
84
docs/waves/decisions-2026-10-02.md
Normal file
@@ -0,0 +1,84 @@
|
||||
# Decisions — October 2, 2026
|
||||
|
||||
Three items, from the security review of 2026-10-01
|
||||
(`docs/reference/security-review-2026-10-01.md`). They get `D` ids because they
|
||||
are internal security calls made mid-build, the same category as `D15` and
|
||||
`D18`, not features a user asked for.
|
||||
|
||||
Decided by Matt Mabrey, 2026-10-02.
|
||||
|
||||
| Item | Title | Review finding | Task |
|
||||
|---|---|---|---|
|
||||
| `D19` | Uploaded files download safely | SEC-01, SEC-15e (BL-035, BL-036) | T16.2 |
|
||||
| `D20` | One SOP per project | SEC-02 (BL-048) | T16.3 |
|
||||
| `D21` | Session decode fails closed; PyJWT upgraded | SEC-07 (BL-052) | T16.1 |
|
||||
|
||||
**Schedule:** wave 16. It is **independent of waves 12 to 15**: it touches
|
||||
`server/` only, and no file in it is changed by a task in those waves except
|
||||
`server/app.py`, in functions they do not touch (`get_wp_file`, `upsert_sop`,
|
||||
the SOP baseline readers). Wave 16 may merge before wave 12.
|
||||
|
||||
**Out of this round, by decision:** SEC-06 (session revocation) waits for the
|
||||
AWS work. All other SEC findings stay as backlog drafts in
|
||||
`docs/reference/security-review/backlog-drafts.md`.
|
||||
|
||||
---
|
||||
|
||||
## D19 — Uploaded files download safely
|
||||
|
||||
- **Problem:** an SVG drawing can carry a script. The server sends it back
|
||||
`inline` with its own content type, on the app's origin, so the script runs
|
||||
as whoever opens it (SEC-01). Separately, a file name that is not Latin-1
|
||||
(for example `Plan é.pdf`) makes the download fail with 500, and CR/LF in a
|
||||
name reaches the response header (SEC-15e).
|
||||
- **Decision:** SVG uploads stay **accepted**, but every file that is not a
|
||||
PDF or a raster image downloads as an **attachment** inside a **sandbox**:
|
||||
`Content-Disposition: attachment` and
|
||||
`Content-Security-Policy: sandbox; default-src 'none'`. PDF and raster images
|
||||
(PNG, JPEG, GIF, WebP, BMP, AVIF) keep opening inline. Every file name goes
|
||||
out in the RFC 6266 / RFC 5987 form (ASCII fallback plus `filename*`).
|
||||
- **Why accept SVG instead of refusing it:** no change for the person who
|
||||
uploads, and no migration for SVGs already stored. The protection is on the
|
||||
way out, which also covers any SVG already in the database.
|
||||
- **Must not change:** inline preview of PDF and images (CR-007), the
|
||||
`Cache-Control` the offline drawing cache relies on, the D8 limits, and the
|
||||
upload API.
|
||||
|
||||
## D20 — One SOP per project
|
||||
|
||||
- **Problem:** the rule "changing a completed SOP is a Project Admin action"
|
||||
checks only the SOP row being posted. A `project_user` can post a NEW SOP id
|
||||
with `complete: true`. Every reader picks "the latest complete SOP", so that
|
||||
row becomes the baseline every page loads and the source of PM, CM and QA
|
||||
mail routing (SEC-02, probe S3).
|
||||
- **Decision:**
|
||||
1. A project has one SOP, with the id `sop__<project_id>`. The browser has
|
||||
always written that id (`pushSOP` in `project-data.js`). A request with no
|
||||
id gets it. A request that would CREATE a SOP with any other id on a
|
||||
project is refused with 400.
|
||||
2. Existing SOP rows with other ids (for example rows `seed_demo.py` made)
|
||||
stay. They can still be updated under the old rules. Nothing is deleted.
|
||||
3. While a project has a complete SOP, writing any SOP on that project with
|
||||
`complete: true`, and changing a complete SOP, needs Project Admin on the
|
||||
project.
|
||||
4. The readers prefer the canonical row: `project_sop_team`,
|
||||
`project_qa_group`, `project_summary` and `GET /api/sops/latest` with
|
||||
`complete=true&project_id=` read `sop__<project_id>` when it is complete,
|
||||
and fall back to "latest complete" only when it is not. So a rogue row
|
||||
made before this fix cannot stay the baseline.
|
||||
- **Must not change:** a project member drafting the SOP and completing it the
|
||||
first time (allowed today, allowed after); admin access to project-less
|
||||
SOPs; the response shape of every SOP route.
|
||||
|
||||
## D21 — Session decode fails closed; PyJWT upgraded
|
||||
|
||||
- **Problem:** a crafted 1.6 KB cookie makes PyJWT 2.13.0 raise an error that
|
||||
`decode_token` does not catch, so an unauthenticated request gets a 500 and
|
||||
a stack trace in the log (SEC-07, probe S12). `pip-audit` lists 13
|
||||
advisories for 2.13.0.
|
||||
- **Decision:** pin **PyJWT 2.15.1** (`pip-audit`: no known vulnerabilities on
|
||||
2026-10-02). `decode_token` treats ANY decode failure, and any cookie longer
|
||||
than 4096 characters, as "not signed in" (401). It also requires the `exp`
|
||||
and `sub` claims, which every token this app issues carries.
|
||||
- **Must not change:** every valid session, the D18 sliding refresh and
|
||||
ceiling, and the fake Okta test seam.
|
||||
51
docs/waves/decisions-2026-10-12.md
Normal file
51
docs/waves/decisions-2026-10-12.md
Normal file
@@ -0,0 +1,51 @@
|
||||
# Decisions: October 12, 2026 (meeting with Nick)
|
||||
|
||||
Topic: mobile app (offline Field View), batch print, and related scaling questions.
|
||||
Recorded by Matt Mabrey from the meeting with Nick, 2026-10-12. Follow-up answers from Matt are in the section "Follow-up answers".
|
||||
|
||||
This file records the answers. It does not schedule any work. The features that come from these answers are logged in `docs/waves/backlog.md` as `BL-062` to `BL-068` for a future build. The path to a mobile version that works outside the VPN is in `docs/waves/roadmap-mobile-scaling.md`. None of these have a wave or a `CR-` or `D-` id yet. Assign the ids when a wave is planned.
|
||||
|
||||
Question numbers (`Q2` to `Q14`) come from the decision table that Matt received. `Q1`, `Q10`, and `Q11` were not in the table. They are not recorded here.
|
||||
|
||||
## Answers
|
||||
|
||||
| Q | Decision needed | Default that was proposed | Answer |
|
||||
|---|---|---|---|
|
||||
| Q2 | Days the app may open offline after the last sign-in (today: 12 hours) | 7 days. Wipe a disabled account's data on the next online open. | **7 days.** The default wipe rule stands. |
|
||||
| Q3 | Must planners edit in the creator offline, or only crews in Field View? | Field View only | **Only crews in the field.** The creator needs no offline mode. |
|
||||
| Q4 | An offline edit meets a newer server version. What does the crew see? | Keep both, show differences, crew chooses. Server wins if nobody acts. | **Merge with a notification. Log it. Update the owner.** If the same field changed on both sides, **the field value wins**, and the app keeps a history of changes. This replaces the default. |
|
||||
| Q5 | Photo limit per photo and per package | Resize to 1280 px, 50 per package | **5 MB per photo.** |
|
||||
| Q6 | Maximum packages in one batch print | 50, with a message to narrow the filter | **25 on phones.** On desktop the user can scale the filter up to 50 or 100. |
|
||||
| Q7 | Where drawings go in the PDF, and at what size | After the form, upload order, scaled to fit | **After the form, upload order, scaled to fit.** Same as the default. |
|
||||
| Q8 | SVG drawings in the merged PDF | Leave out, with a page that names the file | **Leave out, with a page that names the file.** Same as the default. |
|
||||
| Q9 | What goes on every page | Package no., print date, page X of Y | **Package no., print date, page X of Y.** Same as the default. |
|
||||
| Q12 | Progress weight for Ready for QA (today 0, so a package drops from 75% to 0%) | 0.85 | **0.85.** Same as the default. |
|
||||
| Q13 | What happens to a package's drawings when the package is deleted | Delete with the package | **Keep them (data retention). The storage quota is freed.** Storage will move to cloud storage in a future deployment. This replaces the default. |
|
||||
| Q14 | Which cost-code list is correct (creator has 38, SOP has 39, 13 differ) | One list in the SOP. Needs codes from Accounting or the PM. | **One list in the SOP.** The **PM** supplies the codes. |
|
||||
|
||||
## Follow-up answers (Matt, 2026-10-12)
|
||||
|
||||
These answer the six questions that were open after the first record.
|
||||
|
||||
1. **Q5:** the 5 MB limit is **per photo**. This matches the limit that already exists for drawings (D8, enforced in `server/app.py`: 5 MB a file, 2 GB per project).
|
||||
2. **Q6:** 25 on phones. Desktop users can scale up (50 or 100).
|
||||
3. **Q4:** on a conflict in the same field, **the field value wins**. The app keeps a history of changes.
|
||||
4. **Q13:** the quota is freed. Cloud storage will hold the kept files in a future deployment.
|
||||
5. **Q14:** the **PM** gives the codes.
|
||||
6. **Scaling:** Matt asked for a path to a mobile version that scales to multiple projects, outside VPN access, for field agents on phones and tablets on site. That path is in `docs/waves/roadmap-mobile-scaling.md`.
|
||||
7. **Who uses it:** all end users will eventually be in the Okta group.
|
||||
8. **Scale:** expect multiple **customers**. The data model needs a customer (organization) level above a project. See Phase 4 of the roadmap.
|
||||
9. **Devices:** company-managed.
|
||||
|
||||
## Still open
|
||||
|
||||
1. **Q5:** the count of photos per package, and whether the app resizes a photo before upload. Phone photos are often 3 MB to 8 MB. A 5 MB cap per photo with no resize will reject some normal photos.
|
||||
2. **Q4:** who "the owner" is (package owner, project owner, or the person who made the server change). Also: how long the history of changes is kept, and who can read it.
|
||||
3. **Q13:** how long a kept drawing is retained, who can delete it for good, and who can recover it. The quota is freed, so the retained bytes must not count toward the 2 GB project limit. Decide where they are counted instead.
|
||||
4. **Q14:** the date the PM gives the codes.
|
||||
5. **Q6:** who sets the larger desktop limit (each user, or an admin setting).
|
||||
6. The questions in `roadmap-mobile-scaling.md`, section 7 (nine open). The first two (one person on more than one customer, and the isolation model) change the data design. Answer them first.
|
||||
|
||||
## Source
|
||||
|
||||
Meeting with Nick. Status column in the original table: "Open. Team builds to the default" for every row, as of October 12. The answers above came after that status.
|
||||
115
docs/waves/decisions-2026-10-16.md
Normal file
115
docs/waves/decisions-2026-10-16.md
Normal file
@@ -0,0 +1,115 @@
|
||||
# Decisions: October 16, 2026
|
||||
|
||||
## D22: dry run for the Okta deprovisioning sync
|
||||
|
||||
**Asked by:** Matt Mabrey, 2026-10-16. **Extends:** D18 (wave 13). **Task:** T13.7.
|
||||
|
||||
**Why:** The sync disables a local account when Okta does not list the person.
|
||||
If the Okta profile attribute is wrong for some accounts, the first live run can
|
||||
disable people who have not left (see BL-069). The attribute `profile.login` is
|
||||
the default for `preferred_username`. It is not confirmed against the tenant, and
|
||||
the person who deploys has no access to the Okta admin console.
|
||||
|
||||
**Decision:** Add the environment variable `OKTA_SYNC_DRY_RUN`.
|
||||
|
||||
- Values `1`, `true`, `yes`, `on` (any case) turn it on. Any other value, or no
|
||||
value, leaves the sync live. It is off by default, so a deploy that does not set
|
||||
it behaves as before.
|
||||
- In a dry run the sync does every step of a live run: the fetch, the match, the
|
||||
zero-match guard and the fail-closed rules. It then changes no account and writes
|
||||
no audit row.
|
||||
- It logs one line for each account it would disable, and one summary line with the
|
||||
number of Okta users, active local accounts, matches, and accounts that would be
|
||||
disabled. Read them with `docker compose logs api | grep "Okta sync"`.
|
||||
- The startup line shows the mode.
|
||||
|
||||
**Not decided here:** a limit on accounts disabled in one run, and a rule to keep the
|
||||
last active admin (BL-069). The dry run lets a person see the result before the
|
||||
first live run. It does not stop a bad live run.
|
||||
|
||||
**Rules from D18 that stay:** fail closed, only disable, one audit row for each
|
||||
change, actor `system:okta_sync`.
|
||||
|
||||
## D23: sign in to the Okta Management API as an OAuth service app
|
||||
|
||||
**Asked by:** Matt Mabrey, 2026-10-16. **Extends:** D18, T13.2. **Task:** T13.9.
|
||||
|
||||
**Why:** The first deploy of the sync ended with `Okta answered HTTP 401`. The
|
||||
credential that Okta security gave Matt is an API Services app that uses client
|
||||
authentication `private_key_jwt`. It is a private key (a JWK with the fields `kty`,
|
||||
`use` and `kid`), not an SSWS API token. The sync sent that value as an SSWS token,
|
||||
and Okta refused it.
|
||||
|
||||
**Decision:** The sync accepts a second credential.
|
||||
|
||||
- `OKTA_SYNC_CLIENT_ID` and `OKTA_SYNC_PRIVATE_KEY` (a JWK as JSON, or as base64 of
|
||||
that JSON). The sync signs a JWT (RS256 or ES256, from the key's `alg`; `kid` goes
|
||||
in the header; `iss` and `sub` are the client ID; `aud` is `<org>/oauth2/v1/token`;
|
||||
life 5 minutes; a new `jti` each time) and posts it to `<org>/oauth2/v1/token`
|
||||
with `grant_type=client_credentials` and `scope=okta.users.read`.
|
||||
- The access token is used as `Authorization: Bearer` on `/api/v1/users`. It is
|
||||
requested again at the start of each run. It is not stored.
|
||||
- `OKTA_API_TOKEN` still works. If both are set, the token wins. A deploy that sets
|
||||
only a token is unchanged.
|
||||
- A DPoP token (`token_type` not Bearer) is refused with a clear message.
|
||||
|
||||
**Rules from D18 that stay:** fail closed. Any problem with the key, the token
|
||||
request or the user list raises an error, and the sync changes no account. The key,
|
||||
the signed JWT and the access token are never logged and never put in an error
|
||||
message. An error carries the HTTP status and the Okta error code only when that
|
||||
code is a short lower-case word.
|
||||
|
||||
**Not decided here:** how often the key is replaced. A key is registered on the Okta
|
||||
app and has no expiry that this code checks.
|
||||
|
||||
## D24: DPoP for the Okta Management API calls
|
||||
|
||||
**Asked by:** Matt Mabrey, 2026-10-16. **Extends:** D23. **Task:** T13.10.
|
||||
|
||||
**Why:** The first live token request with the service-app key ended with
|
||||
`error invalid_dpop_proof`. The Okta app requires DPoP (RFC 9449). With DPoP, every
|
||||
request carries a signed proof from a key that only the caller holds, so a stolen
|
||||
access token alone does not work. The choice was to ask the Okta admin to switch DPoP
|
||||
off, or to build it. Matt chose to build it.
|
||||
|
||||
**Decision:**
|
||||
|
||||
- `OKTA_SYNC_DPOP` is `auto` (default), `on` or `off`. In auto mode the sync asks for a
|
||||
plain Bearer token first. If Okta answers `invalid_dpop_proof`, the sync asks again with
|
||||
DPoP. So one build works whether the Okta app requires DPoP or not.
|
||||
- Each run makes its own throwaway EC P-256 key. It is held in memory only, never
|
||||
stored, logged or configured. The public half goes in each proof header.
|
||||
- Each request gets its own proof (ES256, `typ` dpop+jwt): `htm`, `htu` without the
|
||||
query, a one-time `jti`, `iat`, the server's latest `nonce`, and on an API call `ath`
|
||||
(a hash of the access token). The same key is used for the token request and the API
|
||||
calls. A new client assertion is built for every token attempt.
|
||||
- A nonce challenge is answered once per request, at the token endpoint and at the API.
|
||||
The token request is tried at most 4 times.
|
||||
- A DPoP-bound token is sent as `Authorization: DPoP <token>`. If the setting is `off`
|
||||
and Okta returns a DPoP-bound token, the sync stops with a clear message.
|
||||
|
||||
**Rules from D18 and D23 that stay:** fail closed; the key, the assertions, the proofs and
|
||||
the access token are never logged or put in an error message; an error carries the HTTP
|
||||
status and Okta's short error code only. The SSWS token path is unchanged.
|
||||
|
||||
**Not verified:** this has been tested only against a fake Okta that follows the RFC. The
|
||||
first run against the real tenant is the real test.
|
||||
|
||||
## D25: the session limit is 8 hours for now
|
||||
|
||||
**Decided by:** Matt Mabrey, 2026-10-17. **Closes:** the 2-versus-8 question in `wave-13.md`. **Task:** T13.11.
|
||||
|
||||
**The conflict:** the wave 13 exit criteria said `AUTH_SESSION_HOURS` defaults to 2. T13.1's own text and
|
||||
the T13.1 code say 8. The shipped default on `main` was 12.
|
||||
|
||||
**Decision:** The default is **8 hours**. This replaces the exit criterion of 2 hours.
|
||||
|
||||
- `AUTH_SESSION_HOURS` defaults to 8 in `server/auth.py`, `docker-compose.yml`, `server/.env.example` and
|
||||
`DEPLOY-login-portal.md`. A value that is set still wins.
|
||||
- Until T13.1 lands, 8 hours is one flat limit from the moment of sign-in, with no sliding. Once T13.1 lands, the
|
||||
same variable is the absolute ceiling and an idle timer sits under it. The default does not change then.
|
||||
- A person who signs in at the start of a shift is signed out 8 hours later, whatever they were doing. Signing in
|
||||
again needs Okta to be reachable.
|
||||
|
||||
**Open, for wave 14:** how the limit works for a crew that is offline. `wave-14.md` sets that rule.
|
||||
**Not decided here:** the idle time (T13.1 proposes 30 minutes). It is a proposed value, not a decision.
|
||||
35
docs/waves/decisions-2026-10-17.md
Normal file
35
docs/waves/decisions-2026-10-17.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# Decisions: October 17, 2026
|
||||
|
||||
Status key: **Proposed** means the text is a recommendation. It needs Matt's OK before any build starts.
|
||||
**Accepted** means Matt approved it and the work may start.
|
||||
|
||||
Matt accepted D26 and D27 as written on 2026-10-17 (open questions 1, 2 and 3 in `wave-14.md`: hold the changes of
|
||||
another person, the offline window ends with the session and then works in read-and-queue mode, and a full device
|
||||
keeps the change in memory and tells the person). The idle time (question 4) was not decided. It belongs to `T13.1`.
|
||||
|
||||
## D26: a sign-in problem never destroys a queued change (Accepted)
|
||||
|
||||
**Why:** The outbox (`html/project-data.js`) marks any `4xx` answer except `429` as dead. A `401` means the session ended, and it
|
||||
gets the same treatment. With a session limit (D25, 8 hours) or an idle timeout (T13.1), a crew that works offline past the limit
|
||||
comes back online and every queued change is rejected for good. The outbox also hides a full device store, and it sends a queued
|
||||
change under whoever is signed in, not under the person who made it.
|
||||
|
||||
**Proposal:**
|
||||
|
||||
- `401` holds the queue. It does not mark anything dead and does not count as a try. The queue sends after a good sign-in.
|
||||
- `403` stays a permanent refusal, with a clear reason.
|
||||
- A full device store is reported. The change is never dropped silently.
|
||||
- Each queued change has an owner. It is sent only when that person is signed in.
|
||||
|
||||
**Tasks:** `T14.1`, `T14.3`. **Needs Matt:** open questions 1 and 3 in `wave-14.md`.
|
||||
|
||||
## D27: the offline sign-in window follows the server (Accepted)
|
||||
|
||||
**Why:** `auth-guard.js` allows the app to open offline for a fixed 12 hours after the last good check, and every page load resets that
|
||||
time. The server session lasts 8 hours (D25). The two do not agree.
|
||||
|
||||
**Proposal:** `/api/auth/me` returns `session_expires_at`. The browser stores it and does not extend it. Offline, the app opens until
|
||||
that time. After that time, offline, it works in read-and-queue mode and does not redirect. Online, a `401` goes to sign-in and the
|
||||
queue is kept. The 7 day offline window (`BL-062`) is a later wave.
|
||||
|
||||
**Task:** `T14.2`. **Needs Matt:** open question 2 in `wave-14.md`.
|
||||
178
docs/waves/roadmap-mobile-scaling.md
Normal file
178
docs/waves/roadmap-mobile-scaling.md
Normal file
@@ -0,0 +1,178 @@
|
||||
# Roadmap: mobile version, multiple projects, access outside the VPN
|
||||
|
||||
Date: 2026-10-12
|
||||
Status: plan for a future build. It is not a spec. No wave, `CR-`, or `D-` id exists for it yet.
|
||||
Inputs: `docs/waves/decisions-2026-10-12.md` (Q2 to Q14), `BL-062` to `BL-068` in `docs/waves/backlog.md`, and the code as of `main` (`d42ae0c`).
|
||||
|
||||
Goal (from Matt, 2026-10-12): field agents use the app on phones and tablets on site, from outside the VPN, across more than one project.
|
||||
|
||||
Scope facts confirmed by Matt on 2026-10-12:
|
||||
- All end users will be in the Okta group. There are no contractors without an Okta account.
|
||||
- The product will scale to **multiple customers**, not only more projects for one customer.
|
||||
- The devices will be **company-managed**.
|
||||
|
||||
## 1. Where the product is today
|
||||
|
||||
| Area | Fact today | Source |
|
||||
|---|---|---|
|
||||
| Mobile shell | A PWA already exists: `manifest.webmanifest` (standalone, "Field View" shortcut) and `sw.js` (caches the app shell and uploaded drawings). | `html/manifest.webmanifest`, `html/sw.js` |
|
||||
| Offline writes | Writes queue in a sync outbox in `project-data.js`. Reads fall back to a `localStorage` cache. `/api/*` is never cached by the service worker. | `html/sw.js` header |
|
||||
| Offline drawings | Drawings for the user's assigned packages are prefetched and cached (CR-007, D8). | `html/sw.js`, `html/field.js` |
|
||||
| Files | The bytes live in the Postgres `wp_files` table. Limit: 5 MB a file, 2 GB per project. | `server/models.py` |
|
||||
| Sign-in | Okta OIDC. A signed cookie (`HttpOnly`, `SameSite=Lax`, `Secure` over HTTPS). Idle timeout (D18, T13.1) is on `feat/waves-11-13`, not on `main`. | `server/auth.py`, `docs/waves/wave-13.md` |
|
||||
| Projects | `Project` and `ProjectMember` exist. Access is by project role. There is no organization or customer level above the project. | `server/models.py` |
|
||||
| Hosting | Docker Compose on an internal host. NGINX serves the site. An external proxy ends TLS. The database and backup containers have no internet route. | `docker-compose.yml`, `nginx/conf.d/wp-suite.conf` |
|
||||
| Asset picker | Reads the Micron SQL Server catalog through `MICRON_DB_URL`. That database sits on the company network. | `server/assets_db.py` |
|
||||
| Abuse limits | No rate limiting or login throttling was found in `server/`. CSRF origin checks and security headers (CSP, HSTS) exist. | `server/app.py`, `wp-suite.conf` |
|
||||
| Security review | The 2026-10-01 review put session revocation (SEC-06) on hold until the AWS work. Wave 16 fixed SEC-01, SEC-02, SEC-07. | `docs/waves/decisions-2026-10-02.md` |
|
||||
|
||||
Result: the "mobile app" is mostly built as a PWA. The main work is not a new app. It is (a) finishing offline Field View, (b) making the service safe to put on the internet, and (c) removing the single-host limits.
|
||||
|
||||
## 2. Path in phases
|
||||
|
||||
Each phase has a gate. Do not start a phase before the gate of the one before it is met, unless this file says the phases are independent. Sizes are rough estimates (S under 1 week, M 1 to 3 weeks, L over 3 weeks). They are not commitments.
|
||||
|
||||
### Phase 0: Close the open decisions (S)
|
||||
|
||||
- Answer the questions in section 7 and the "Still open" list in `decisions-2026-10-12.md`.
|
||||
- Assign `CR-`/`D-` ids and write the wave files.
|
||||
- **Gate:** every item in `BL-062` to `BL-068` is free of "to confirm".
|
||||
|
||||
### Phase 1: Finish offline Field View, still inside the VPN (L)
|
||||
|
||||
Scope: `BL-062`, `BL-063`, `BL-064`.
|
||||
|
||||
1. Land the outbox work (wave 14, T14.1 to T14.3). `wave-12.md` says T13.1 must not reach `main` before T14.1.
|
||||
2. Set the offline window to 7 days (Q2). Reconcile it with the D18 idle timeout and absolute ceiling. Define what "signed in" means offline. Decide what the 7 day window does when Okta says the account is disabled (wipe on the next online open).
|
||||
3. Conflict handling (Q4): merge, field value wins on the same field, history of changes, a notification to the crew, a log entry, and an update to the owner.
|
||||
4. Photos: capture on the device, 5 MB per photo (Q5). Add resize before upload if Matt chooses it. Store photos like drawings (server-side, with D19 download headers).
|
||||
5. Add a phone and tablet test matrix (real devices): iOS Safari installed to the home screen, Android Chrome, one tablet.
|
||||
6. Test the weak-link case: no signal, then a slow signal, then a sync with a conflict.
|
||||
|
||||
Known platform risks to test on the devices (verify; do not assume):
|
||||
- iOS may delete site storage after a period of no use unless the PWA is installed to the home screen.
|
||||
- iOS Safari has no Background Sync. The outbox then sends only while the app is open. A crew member must open the app to push changes.
|
||||
- A lost device holds up to 7 days of project data. Web storage is not encrypted by the app. See Phase 2.
|
||||
|
||||
**Gate:** the crew completes a full shift offline on a test device, reconnects, and the server holds every change. The conflict case works as Q4 says. `tests/mobile_check.py` and the 390px/1440px checks pass.
|
||||
|
||||
### Phase 2: Make the service safe to reach from the internet (M to L)
|
||||
|
||||
Today the design assumes the firewall. Outside the VPN, the app becomes the perimeter. Do this before any real user connects from the internet.
|
||||
|
||||
| Control | Why |
|
||||
|---|---|
|
||||
| Rate limiting and login throttling on the API (none today) | Stops guessing and scraping. The toolkit (`sde-meeting-toolkit`) needed the same fix. |
|
||||
| A WAF or reverse proxy with request limits in front of NGINX | Filters bad requests before they reach the API. |
|
||||
| Real client IP handling (`X-Forwarded-For` from the trusted proxy only) | Limits and logs must use the real address, not the proxy. |
|
||||
| Okta policy for the app: MFA, session length, device trust | Okta is the only sign-in path (D15, D16). The policy is the control. Ask the Okta admin. |
|
||||
| Session revocation (SEC-06) | When a phone is lost, an admin must end that session now. This was put on hold for the AWS work. Bring it forward. |
|
||||
| A rule for lost devices | Devices are company-managed (decided). Require a device PIN and disk encryption through the device policy. Remote wipe through the device manager. Pair it with session revocation (above), because a wipe cannot reach an offline device. Confirm with IT that the policy covers every phone and tablet that runs the app, and that a lost device is reported quickly. |
|
||||
| Okta access rule | All end users are in the Okta group (decided). Gate the app on that group. Today the API creates the account on first sign-in; check that a person who is not in the group is refused, and that removal from the group ends access (this is what D18 and wave 13 do). Ask the Okta admin to add MFA and device-trust conditions for the outside path. |
|
||||
| Cookie flags on every cookie (the OIDC state cookie uses `https_only=False` in `server/app.py`) | Must be `Secure` on the public site. |
|
||||
| Review the 2026-10-01 findings that stayed in backlog drafts | Re-rank them for an internet-facing service. |
|
||||
| Outside review (a penetration test) | Required before the customer-IP data is reachable from the internet. |
|
||||
| Alerting on failed sign-ins and on odd upload sizes | Detect abuse early. |
|
||||
|
||||
**Gate:** the controls above are in place and an outside test finds no high findings open.
|
||||
|
||||
### Phase 3: Hosting that can leave the office network (L)
|
||||
|
||||
The AWS work is already planned (SEC-06 note). Proposed shape. The choice of services is open for Matt and the AWS owner.
|
||||
|
||||
- A public edge (load balancer, WAF, TLS) in front of the API and the static site.
|
||||
- API containers on a managed container service, with a managed Postgres database.
|
||||
- Files in object storage instead of the database (`BL-066`, Q13). Move the bytes out of `wp_files`. Keep the metadata in Postgres. Kept files from deleted packages go to a retention area with a lifecycle rule. The quota counts only live files.
|
||||
- Backups and restore tests on the new stack (the current backup container writes to a host folder).
|
||||
- A private link back to the Micron asset database, or a different design for the asset picker (the catalog is on the company network). Until this exists, the picker falls back to manual entry outside the VPN. Check that fallback works for a user who is online but cannot reach the database (the code treats "unconfigured" as a normal state, but not "unreachable").
|
||||
- Infrastructure as code, so the environment can be rebuilt.
|
||||
- Keep the internal deployment working until the cutover.
|
||||
|
||||
**Gate:** a full restore test passes, the migration runs on Postgres (render the SQL for the Postgres dialect first, BL-027), and a rollback plan is written and rehearsed.
|
||||
|
||||
### Phase 4: Many projects and many customers (L)
|
||||
|
||||
Matt confirmed multiple customers. A customer boundary is now part of the plan, not an option. The data model has `Project` and `ProjectMember` but nothing above a project, so this phase changes the base design. Plan for it early. Phase 3 choices (storage layout, database layout) must already allow it.
|
||||
|
||||
- Add an organization (customer) level above `Project`. Every project, user role, file, report, export, and search belongs to one organization. A user may belong to more than one organization (for example Prime Controls staff who work for several customers). Decide this rule first (section 7, question 1).
|
||||
- Isolation: choose the model before any code. Options: one database with an organization id on every row and checks on every query; a separate schema per customer; or a separate database or stack per customer. Matt and the AWS owner decide. The choice sets cost, backup, and the strength of the separation that a customer can be shown.
|
||||
- Files: one storage prefix per customer, with its own access rule and, if a customer asks, its own encryption key.
|
||||
- Customer-specific data: the cost-code list, the asset catalog (today Micron only), and the SOP templates must belong to the organization or the project. `BL-068` (one cost-code list in the SOP) must not become one list for all customers.
|
||||
- Okta: map Okta groups to organizations and projects, so the Okta admin controls who sees which customer.
|
||||
- Reports and the admin console: super users see only the organizations they are given. Check CR-019 usage reports, the user directory, and the bulk user tools from wave 12 against this rule.
|
||||
- Onboarding a customer: a runbook with the steps and a checklist.
|
||||
- Project onboarding: create a project, load its SOP, cost codes (`BL-068`, one list in the SOP, codes from the PM), people, and roles. Use the bulk user tools from wave 12.
|
||||
- Okta group to project membership mapping, so the Okta admin controls who joins a project.
|
||||
- Per-project storage and photo quotas (2 GB per project exists today) and per-project usage reports (CR-019 on `feat/waves-11-13`).
|
||||
- Performance work with a load test: many crews syncing at shift start. Check the outbox flush, the `wp_files` move, and the list endpoints on a large project.
|
||||
- Data retention rules per project.
|
||||
|
||||
**Gate:** two real customers run side by side. A test user in one customer cannot reach any data of the other through any screen, report, export, search, file link, or API call. A test that tries to cross the boundary on purpose passes. The load test meets targets that Matt sets.
|
||||
|
||||
### Phase 5: Native wrapper (only if needed) (M)
|
||||
|
||||
Wrap the PWA in a native shell (for example Capacitor) only if Phase 1 testing shows a platform limit that the PWA cannot meet: storage eviction, no background sync, camera control, or a company rule for app-store distribution. A wrapper adds app-store review, signing, and update cost. Do not start it by default.
|
||||
|
||||
## 3. Order and dependencies
|
||||
|
||||
```
|
||||
Phase 0 -> Phase 1 -> Phase 2 -> Phase 3 -> Phase 4
|
||||
\_ Phase 5 (decide after Phase 1 device tests)
|
||||
```
|
||||
|
||||
- Phase 2 may start in part while Phase 1 runs (rate limiting, Okta policy review). No crew uses the internet path before Phase 2 is done.
|
||||
- Phase 3 (storage move) and `BL-066` (retained drawings) are the same work. Do them together.
|
||||
- Phase 4 needs the Phase 2 and Phase 3 gates. Opening a second project to the internet first would repeat any gap twice.
|
||||
|
||||
## 4. Work that this path adds to the backlog
|
||||
|
||||
| Id | Item | Phase |
|
||||
|---|---|---|
|
||||
| BL-062 to BL-068 | Already logged | 1, 3, 4 |
|
||||
| New | API rate limiting and login throttling | 2 |
|
||||
| New | Real client IP handling behind the proxy | 2 |
|
||||
| New | Session revocation (SEC-06) brought forward | 2 |
|
||||
| New | Device policy check with IT (PIN, encryption, remote wipe) | 1, 2 |
|
||||
| New | Okta group gate and MFA/device-trust conditions for the outside path | 2 |
|
||||
| New | Object storage and lifecycle rules for files, one prefix per customer | 3 |
|
||||
| New | Asset picker when the Micron database is unreachable | 3 |
|
||||
| New | Organization (customer) level above `Project`, with the isolation model chosen first | 3, 4 |
|
||||
| New | Per-customer cost codes, asset catalog, and SOP templates | 4 |
|
||||
| New | Okta group to organization and project mapping | 4 |
|
||||
| New | Cross-customer access tests (a test that tries to cross the boundary) | 4 |
|
||||
| New | Customer onboarding runbook | 4 |
|
||||
| New | Load test and capacity targets | 4 |
|
||||
|
||||
Log these in `docs/waves/backlog.md` when Matt agrees with the plan. This file does not add them, so the backlog stays free of unconfirmed scope.
|
||||
|
||||
## 5. Risks
|
||||
|
||||
1. **Customer data on lost devices.** Offline data for 7 days on a phone or tablet. Devices are company-managed, which lowers this risk a lot, but only if the device policy (PIN, disk encryption, remote wipe) is real and covers every device. An offline device cannot receive a wipe until it connects. Keep the 7 day window only if IT confirms the policy.
|
||||
2. **Customer boundary added late.** The data model has no organization level. If Phases 1 to 3 are built without it, every later change touches every table and every query. Choose the isolation model in Phase 0 and design Phase 3 storage around it.
|
||||
3. **Contract terms.** Customer drawings and asset data may have rules about where they are stored and who can reach them. With several customers, each contract may differ. Check before Phase 3 (cloud storage) and before Phase 2 (internet access).
|
||||
4. **Okta.** The app has no password of its own. A weak Okta policy for the outside path is a weak app. The Okta admin owns that setting.
|
||||
5. **Single database host.** The current database is one container with a host-folder backup. Phase 3 fixes this. Until then, do not add projects that need high availability.
|
||||
6. **Sequence risk.** T13.1 (idle timeout) must wait for T14.1 (outbox). A short idle timeout can end a session while a crew is offline. Settle the rule in Phase 1.
|
||||
|
||||
## 6. What does not change
|
||||
|
||||
These decisions stay as recorded: Actual Hours stays (CR-017), localization stays (A7), uppercase card headers stay (A5), the logged-override path for predecessors stays (A1), removed fields are hidden and not deleted (CR-002, CR-016), and the token rule for CSS. Mobile work follows the accessibility rules (C1): real buttons, visible focus, `aria-live` for status, hover is not enough on tablets.
|
||||
|
||||
## 7. Questions that decide the plan
|
||||
|
||||
Answered on 2026-10-12:
|
||||
- Who are the outside users? **All end users will be in the Okta group.** No separate sign-in design is needed.
|
||||
- What does "multiple projects" mean? **Multiple customers.** See Phase 4.
|
||||
- Which devices? **Company-managed.**
|
||||
|
||||
Answer these before Phase 0 ends. The first four are new and follow from the answers above.
|
||||
|
||||
1. **Can one person work for more than one customer?** For example, a Prime Controls planner on two customers' projects. This sets how the organization level and the Okta groups map to each other.
|
||||
2. **Which isolation model?** One database with an organization id on every row, a schema per customer, or a database or stack per customer. Do any customers already require a separate stack in their contract?
|
||||
3. **Who manages the Okta groups?** One group for all users, or one group per customer or project? Does the Okta admin accept that mapping?
|
||||
4. **What does the device policy enforce today?** PIN, disk encryption, remote wipe, and a time limit for a device to check in. Ask IT.
|
||||
5. **Connectivity on site.** No signal for days, or weak signal that comes and goes? This sets how long the offline window must really be, and how big the photo queue gets.
|
||||
6. **AWS.** Is the AWS move approved? Who owns the account? Which region? Can the Micron database be reached from it?
|
||||
7. **Contract.** Can customer drawings and asset data be stored in cloud storage and reached from the internet? Does each customer have its own terms?
|
||||
8. **Distribution.** Is an installed PWA enough, or does the company need an app-store app? Company-managed devices can often install a web app from a device policy. Ask IT.
|
||||
9. **Targets.** How many customers, projects, and users, and how many crews syncing at the same time, in the first year?
|
||||
@@ -3,7 +3,14 @@
|
||||
**Items:** `CR-020`
|
||||
**Depends on:** wave 10 merged (it is). Independent of wave 11 (`CR-019`) — no
|
||||
shared files, may build in either order or in parallel.
|
||||
**Decision record:** `docs/waves/decisions-2026-09-17.md`
|
||||
**Decision record:** `docs/waves/decisions-2026-09-17.md` (open details
|
||||
confirmed 2026-10-06, see "Confirmed 2026-10-06" in the CR-020 section)
|
||||
|
||||
**Branch (decided 2026-10-06):** `feat/wave-12-bulk-users`, cut from `main`
|
||||
(`df20b8f`, production). Only this file and the decision record come across
|
||||
from `feat/waves-11-13`, so the PR into `main` is wave 12 alone: no wave 11
|
||||
code and no T13.1, which must not reach `main` before T14.1. Wave 16 is not
|
||||
merged yet; it touches different functions, and whichever lands second rebases.
|
||||
|
||||
Six tasks, in build order.
|
||||
|
||||
@@ -38,15 +45,15 @@ exist for the single case — bulk delete stays a hard delete, matching
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [ ] each of the four bulk actions is callable with a list of user ids and a
|
||||
- [x] each of the four bulk actions is callable with a list of user ids and a
|
||||
single set of parameters
|
||||
- [ ] a batch containing the actor's own account rejects only that row, not
|
||||
- [x] a batch containing the actor's own account rejects only that row, not
|
||||
the whole batch — verified for disable, demote, and delete
|
||||
- [ ] a super user's batch that includes a user/project outside what they
|
||||
- [x] a super user's batch that includes a user/project outside what they
|
||||
manage rejects only that row, with a stated reason
|
||||
- [ ] every successful row produces its own `AuditLog` entry, identical in
|
||||
- [x] every successful row produces its own `AuditLog` entry, identical in
|
||||
shape to what the single-user endpoint would have written
|
||||
- [ ] a batch with some failing rows still applies to the rows that succeed,
|
||||
- [x] a batch with some failing rows still applies to the rows that succeed,
|
||||
and the response lists exactly which rows failed and why
|
||||
|
||||
---
|
||||
@@ -67,12 +74,12 @@ disappears at zero.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [ ] select-all selects exactly the rows currently visible under the active
|
||||
- [x] select-all selects exactly the rows currently visible under the active
|
||||
filter, not the full unfiltered table
|
||||
- [ ] changing the filter while rows are selected does something sane and
|
||||
- [x] changing the filter while rows are selected does something sane and
|
||||
visible (either clears the selection or keeps it explicit which rows are
|
||||
still selected) — pick one and state it, don't leave it undefined
|
||||
- [ ] the toolbar is keyboard-reachable and only present when >=1 row is
|
||||
- [x] the toolbar is keyboard-reachable and only present when >=1 row is
|
||||
selected
|
||||
|
||||
---
|
||||
@@ -97,13 +104,13 @@ partial failure.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [ ] role change, activate/disable, and project assignment each work end to
|
||||
- [x] role change, activate/disable, and project assignment each work end to
|
||||
end against a multi-row selection
|
||||
- [ ] the confirmation dialog names the exact affected count before anything is
|
||||
- [x] the confirmation dialog names the exact affected count before anything is
|
||||
sent
|
||||
- [ ] a batch with a partial failure shows which rows failed, not just an
|
||||
- [x] a batch with a partial failure shows which rows failed, not just an
|
||||
undifferentiated error
|
||||
- [ ] `aria-live` announces the outcome
|
||||
- [x] `aria-live` announces the outcome
|
||||
|
||||
---
|
||||
|
||||
@@ -123,11 +130,11 @@ T12.1 endpoints, not a third implementation of the bulk logic.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [ ] a CSV with a mix of valid and invalid usernames applies to the valid
|
||||
- [x] a CSV with a mix of valid and invalid usernames applies to the valid
|
||||
rows and reports the invalid ones by row, with a reason
|
||||
- [ ] the same permission/self-action guards from T12.1 apply here — a CSV
|
||||
- [x] the same permission/self-action guards from T12.1 apply here — a CSV
|
||||
cannot reach a user a checkbox-driven batch couldn't
|
||||
- [ ] duplicate usernames in one CSV are handled without double-applying or
|
||||
- [x] duplicate usernames in one CSV are handled without double-applying or
|
||||
erroring confusingly
|
||||
|
||||
---
|
||||
@@ -150,10 +157,10 @@ change it, not a reason to skip building a real confirmation now.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [ ] the affected usernames are listed in the confirmation dialog before
|
||||
- [x] the affected usernames are listed in the confirmation dialog before
|
||||
delete is sent
|
||||
- [ ] the request is not sent until the confirmation phrase is typed correctly
|
||||
- [ ] the actor's own account, if somehow selected, is rejected with a clear
|
||||
- [x] the request is not sent until the confirmation phrase is typed correctly
|
||||
- [x] the actor's own account, if somehow selected, is rejected with a clear
|
||||
reason rather than silently included or silently dropped
|
||||
|
||||
---
|
||||
@@ -173,21 +180,84 @@ suite.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [ ] all `CR-020` acceptance criteria in `decisions-2026-09-17.md` are met or
|
||||
- [x] all `CR-020` acceptance criteria in `decisions-2026-09-17.md` are met or
|
||||
a failure is stated with a reason
|
||||
- [ ] screenshots committed
|
||||
- [ ] smoke test and `seed_demo.py` both still pass
|
||||
- [ ] full test suite passes
|
||||
- [x] screenshots committed
|
||||
- [x] smoke test and `seed_demo.py` both still pass
|
||||
- [x] full test suite passes
|
||||
|
||||
---
|
||||
|
||||
## Wave 12 exit criteria
|
||||
|
||||
- [ ] all four bulk actions work against both a checkbox selection and a CSV
|
||||
- [x] all four bulk actions work against both a checkbox selection and a CSV
|
||||
upload
|
||||
- [ ] every existing single-user guardrail (self-action, scope, grantable
|
||||
- [x] every existing single-user guardrail (self-action, scope, grantable
|
||||
roles) holds under bulk use
|
||||
- [ ] bulk delete requires typed confirmation and lists affected usernames
|
||||
- [ ] partial failures are always reported, never hidden behind a blanket
|
||||
- [x] bulk delete requires typed confirmation and lists affected usernames
|
||||
- [x] partial failures are always reported, never hidden behind a blanket
|
||||
success
|
||||
- [ ] `CR-020` fully accounted for, no open acceptance criteria
|
||||
- [x] `CR-020` fully accounted for, no open acceptance criteria
|
||||
|
||||
## Verification record
|
||||
|
||||
Built 2026-10-06 on `feat/wave-12-bulk-users`
|
||||
(from `main`, `df20b8f`), one commit per task. Run in the build sandbox (Linux,
|
||||
Python 3.10, no browser).
|
||||
|
||||
| Check | Result |
|
||||
|---|---|
|
||||
| `tests/bulk_users_check.py` (new, no browser) | **61/61**: all four actions, own-row refusals, super-user scope, per-row audit in the single route's shape, partial failure, project add/remove keeping other projects and roles, CSV resolve, 403/401/422 gates |
|
||||
| Same check against `main` | 7/18: the 7 single-route checks pass; every bulk check fails, because the routes do not exist there |
|
||||
| Single-user routes before/after (16 calls: 200/400/403/404, as admin and super user) | **Identical** responses, audit rows and memberships against `main`. The routes now call the same `apply_*` functions as the bulk routes |
|
||||
| Page logic, DOM harness (jsdom, the real `users.html` and scripts, mocked API) | **48/48**: selection, select-all over the filtered rows, filter clears selection, toolbar, confirm counts, per-row report, CSV, typed DELETE |
|
||||
| Same flows, jsdom against a real server seeded by `browser_check.seed()` | **13/13**, including the CSV report texts the browser probe expects |
|
||||
| `server/smoketest.py` | **27/27** |
|
||||
| `server/seed_demo.py` | Seeds the demo project |
|
||||
|
||||
**Decisions applied (confirmed 2026-10-06):** typed `DELETE`; project assignment
|
||||
is add/remove per project, with the per-project role (`ProjectMember.role`) set
|
||||
on add; filter or search change clears the selection; CSV holds usernames only.
|
||||
A row already in the requested state reports `unchanged` and writes no audit
|
||||
row (nothing changed). An email address in the CSV resolves only when exactly
|
||||
one account has it.
|
||||
|
||||
**Found and logged, not fixed:** BL-060, a super user's refusal reason promises
|
||||
a project-access change that neither the dialog nor the bulk route allows.
|
||||
|
||||
**Browser probes, run 2026-10-06 on Matt's machine (Windows, Chrome 153 / Edge 154,
|
||||
`.venv`), one at a time:**
|
||||
|
||||
| Probe | Result |
|
||||
|---|---|
|
||||
| `tests/bulk_users_ui_check.py` (new) | **19/19**: selection, Space on a row box, role change, super-user CSV (another job / not found / herself / duplicate, each by line), add-to-project with a role, typed DELETE, 390px bar fit, no JS errors |
|
||||
| `tests/console_dialogs_check.py` | **16/16**, including the new section 4 (bulk delete prompt, natives poisoned) |
|
||||
| `tests/mobile_check.py` | **24/24**, users page included |
|
||||
| `tests/a11y_check.py` | **22/22**: users page 66 focusable elements, all with a visible ring; helper text 5.02:1 at worst |
|
||||
| `tests/browser_check.py` | **67/71**, then re-pointed (below); **rerun after the re-point: all passed** (reported by Matt, 2026-10-06) |
|
||||
|
||||
**`browser_check.py` re-pointed (T12.6).** Its four failures were assertions
|
||||
written against column POSITIONS: the manager table had 9 columns, and the
|
||||
own-row checks read `r.cells[3]` (Permissions) and `r.cells[4]` (Project role).
|
||||
CR-020's selection column is first, so the table has 10 and those indexes
|
||||
landed on Username and Email. Not an app defect. The probe now expects 10
|
||||
columns with the selection column first, finds the two cells by their header
|
||||
name rather than their position (so the next column cannot break it the same
|
||||
way), and adds one check: your own row has no selection box. Verified against
|
||||
the real page in the DOM harness, then rerun in the browser: all passed.
|
||||
|
||||
**Screenshots** (after; the before-shots are `baseline/users-1440.png` and
|
||||
`baseline/after-wave9/users-390.png`): `baseline/wave-12/users-1440.png`,
|
||||
`users-1440-selected.png`, `users-390.png`, `users-390-selected.png`. Reviewed:
|
||||
the bar, its four groups and the selected-row marking render as designed at
|
||||
both widths; nothing scrolls sideways at 390px.
|
||||
|
||||
**Seen in the screenshots, not caused by wave 12, logged as BL-061:** the
|
||||
pre-existing sync badge ("Everything sent to the project", `project-data.js`)
|
||||
is drawn over a row's Delete button at 1440px and over "Set permissions" at
|
||||
390px for about 2 seconds after a load. It is `pointer-events:none`, so it never
|
||||
blocks a click, but it hides a control while it shows.
|
||||
|
||||
**Wave 12 closed 2026-10-06.** Every task done-when and every exit criterion
|
||||
above is checked. Pushed as `feat/wave-12-bulk-users`; PR into `main` open.
|
||||
Merge after wave 16 (`feat/wave-16-on-main`); both orders tested conflict-free.
|
||||
|
||||
@@ -61,22 +61,40 @@ its own `iat` as `login_at`, not bypass the ceiling entirely.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [ ] a session with continuous activity stays alive past 30 minutes but is
|
||||
- [x] a session with continuous activity stays alive past 30 minutes but is
|
||||
cut off at the `AUTH_SESSION_HOURS` ceiling regardless
|
||||
- [ ] a session with no activity for 30+ minutes is rejected on its next
|
||||
*(2026-10-16, T13.6: a request every minute for 10 hours: alive well past 30 minutes, never extended past login + 8 h, cut off at the ceiling, original `login_at` kept.)*
|
||||
- [x] a session with no activity for 30+ minutes is rejected on its next
|
||||
request
|
||||
- [ ] the cookie is not rewritten on every single request — verify the
|
||||
*(2026-10-16, T13.6: an idle token gets 401 on a gated route; one inside the window gets 200.)*
|
||||
- [x] the cookie is not rewritten on every single request — verify the
|
||||
refresh is throttled, not unconditional
|
||||
- [ ] a pre-existing token with no `login_at` claim (simulating a session
|
||||
*(2026-10-16, T13.6: 20 rapid requests on a fresh session: 0 rewrites; 480 requests over 8 h: about one rewrite per 10 minutes; a refreshed cookie is not rewritten again on the next request. Mutation test: removing the throttle fails 3 checks.)*
|
||||
- [x] a pre-existing token with no `login_at` claim (simulating a session
|
||||
issued before this shipped) still gets a hard ceiling, via the `iat`
|
||||
fallback
|
||||
- [ ] `.env.example` and `DEPLOYMENT.md` explain both variables and flag both
|
||||
*(2026-10-16, T13.6: a token with iat 9 h ago and a still-valid old exp is not extended; one with iat 1 h ago is refreshed with `login_at` taken from `iat`; no `login_at` and no `iat` is not refreshed and does not crash. Mutation test: giving it a fresh ceiling fails.)*
|
||||
- [x] `.env.example` and `DEPLOYMENT.md` explain both variables and flag both
|
||||
defaults as proposed, not confirmed against Okta's own session policy
|
||||
- [ ] existing session/auth tests updated for the new mechanism, not just a
|
||||
*(2026-10-16, T13.6: both variables are in both files, and both call the defaults proposed.)*
|
||||
- [x] existing session/auth tests updated for the new mechanism, not just a
|
||||
new number
|
||||
*(2026-10-16, T13.6: there were none. T13.1 shipped with no test of its own. `tests/session_idle_check.py` is that test (T13.6 added it).)*
|
||||
|
||||
---
|
||||
|
||||
**Found while writing that test (T13.6, 2026-10-16):**
|
||||
- Nothing under `/api/auth/` slides the session. `auth._EXEMPT_PREFIXES` covers
|
||||
that whole prefix, so `auth_gate` never refreshes there (including
|
||||
`/api/auth/me`). A page that only called `/api/auth/me` would not extend its
|
||||
session. The spec says "after confirming a request is authenticated", and these
|
||||
routes are not confirmed by the gate, so this is as written. Noted, not changed.
|
||||
- The wave's exit criteria below say `AUTH_SESSION_HOURS` defaults to **2**. T13.1
|
||||
(this section), `auth.py`, `.env.example` and `DEPLOYMENT.md` all say **8**. The
|
||||
code and the T13.1 text agree, so the exit line looks like a leftover from
|
||||
before the 2026-09-23 revision. Not changed here: it is a product number. Needs
|
||||
Matt to say which one he means.
|
||||
|
||||
### T13.2 — D18: Okta Management API client
|
||||
|
||||
- **Items:** `D18`
|
||||
@@ -105,13 +123,17 @@ unnecessarily.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [ ] the client authenticates with its own credential, distinct from the OIDC
|
||||
- [x] the client authenticates with its own credential, distinct from the OIDC
|
||||
client
|
||||
- [ ] it fetches the complete Okta user list, handling pagination
|
||||
- [ ] an auth failure or malformed response raises a clear, specific error
|
||||
*(2026-10-16: `OKTA_API_TOKEN`, sent as `Authorization: SSWS`. `tests/okta_sync_check.py` sets a different OIDC client secret and asserts it is never sent.)*
|
||||
- [x] it fetches the complete Okta user list, handling pagination
|
||||
*(2026-10-16: follows `Link: rel="next"`, 200 per page, 2-page case checked. A next link to another host is refused, a chain longer than 50 pages raises instead of cutting the list off, redirects are never followed.)*
|
||||
- [x] an auth failure or malformed response raises a clear, specific error
|
||||
rather than returning an empty list indistinguishable from "everyone was
|
||||
deprovisioned" — this distinction is what T13.3 depends on
|
||||
- [ ] the credential is never logged or surfaced in any API response
|
||||
*(2026-10-16: `OktaSyncError` for HTTP 401/403/404/429/5xx, redirects, network errors, timeouts, non-JSON, non-list, a bad item (never skipped), and an empty list. 31 failure checks. Mutation test: making the client return `[]` on an error fails 10 of them.)*
|
||||
- [x] the credential is never logged or surfaced in any API response
|
||||
*(2026-10-16: error messages carry the class name and HTTP status only, never the URL or a header. `describe()` has no token. Nothing is wired to an API response yet; T13.4 wires the startup log.)*
|
||||
|
||||
---
|
||||
|
||||
@@ -140,18 +162,44 @@ assert zero rows changed and zero `AuditLog` entries written.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [ ] a user missing from Okta's list, or present but not active, is disabled
|
||||
- [x] a user missing from Okta's list, or present but not active, is disabled
|
||||
with a correctly-detailed audit row
|
||||
- [ ] a user already disabled is left alone (no duplicate audit row each run)
|
||||
- [ ] an active Okta user already active locally produces no audit row (only
|
||||
*(2026-10-16: see "Decided 2026-10-16" below for which statuses count as "not active". Audit row: actor `system:okta_sync`, action `user_deprovisioned_by_sync`, `entity_type` user, `detail` `{okta_status, via}`; a missing user is recorded as `MISSING`.)*
|
||||
- [x] a user already disabled is left alone (no duplicate audit row each run)
|
||||
*(2026-10-16: a second run over the same data writes no new rows.)*
|
||||
- [x] an active Okta user already active locally produces no audit row (only
|
||||
changes are logged, not a clean bill of health every cycle)
|
||||
- [ ] **a simulated Okta API failure results in zero account changes and zero
|
||||
*(2026-10-16: verified in `tests/okta_sync_job_check.py`.)*
|
||||
- [x] **a simulated Okta API failure results in zero account changes and zero
|
||||
audit rows** — this is the one check that must never be skipped or
|
||||
weakened
|
||||
- [ ] the job never re-enables an account under any input
|
||||
*(2026-10-16: 10 stand-in failures (OktaSyncError, RuntimeError, ConnectionError, TimeoutError, ValueError, KeyError, empty list, None, two kinds of garbage), 4 through the real client (HTTP 401/403/429/500), the default path with no token, a failed commit, and a wrong identity attribute. Each: `ok` is False, zero accounts changed, zero audit rows. Mutation test: removing the fail-closed branch and the zero-match guard fails 28 of these.)*
|
||||
- [x] the job never re-enables an account under any input
|
||||
*(2026-10-16: a disabled account that is ACTIVE in Okta stays disabled, and `okta_sync.py` contains no assignment of `is_active` to True.)*
|
||||
|
||||
---
|
||||
|
||||
**Decided 2026-10-16 (Matt, in chat):** which Okta statuses disable an account.
|
||||
The spec says "a non-active status". Read literally, that also catches
|
||||
`PASSWORD_EXPIRED`, `LOCKED_OUT` and `RECOVERY`, which people leave on their
|
||||
own, and this job never re-enables, so one AD password expiry would lock someone
|
||||
out of the app until an admin stepped in. Decided: an account disables when it is
|
||||
**missing** from Okta's list, or every Okta entry for it is **SUSPENDED** or
|
||||
**DEPROVISIONED** (`DISABLING_STATUSES` in `server/okta_sync.py`). Okta's default
|
||||
user list already leaves out DEPROVISIONED users, so those arrive as "missing".
|
||||
|
||||
**Added 2026-10-16, inside decision 4's "anything the sync can't confidently
|
||||
parse":** if there are active local accounts and **none** of them matches any
|
||||
Okta user, the run is refused and nothing changes. That pattern means the
|
||||
identity attribute is wrong (`OKTA_SYNC_PROFILE_ATTRIBUTE`), not that the whole
|
||||
company left. It is a guard, not a new feature. Raise it in the PR.
|
||||
|
||||
**Open, for the PR / Nick:** which Okta profile attribute equals the
|
||||
`preferred_username` claim in this tenant. The client assumes `profile.login`,
|
||||
Okta's default. Confirm before the first live run. A wrong value is caught by the
|
||||
zero-match guard above only when it matches nobody at all; a value that matches
|
||||
some accounts and not others would disable the rest. See `BL-069`.
|
||||
|
||||
### T13.4 — D18: run it on a schedule
|
||||
|
||||
- **Items:** `D18`
|
||||
@@ -172,14 +220,39 @@ is sufficient or Okta needs a distinct allowance).
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [ ] the job runs automatically on the configured interval without manual
|
||||
- [x] the job runs automatically on the configured interval without manual
|
||||
invocation
|
||||
- [ ] interval is env-configurable with a sane default
|
||||
- [ ] a container restart does not produce a duplicate/overlapping run, and a
|
||||
*(2026-10-16: a daemon timer thread starts from the app's lifespan hook (`server/app.py`), checks once a minute, and ran the sync on its own in `tests/okta_sync_schedule_check.py`. With `OKTA_API_TOKEN` unset it starts nothing and the startup log says `Okta sync DISABLED`. Booted the real app both ways; the thread starts and ends with the app.)*
|
||||
- [x] interval is env-configurable with a sane default
|
||||
*(2026-10-16: `OKTA_SYNC_INTERVAL_SECONDS`, default 900. A non-number uses the default. A value under 300 is raised to 300. Passed through `docker-compose.yml`.)*
|
||||
- [x] a container restart does not produce a duplicate/overlapping run, and a
|
||||
slow cycle does not stack with the next one
|
||||
*(2026-10-16: the api runs 2 gunicorn workers, so each starts its own timer. One row in `app_settings` (key `okta_sync`) is the gate: a worker claims a cycle with one atomic conditional UPDATE, so exactly one wins. Checked with 8 racing workers on a file database, 15 rounds plus the first-insert race: exactly 1 winner every time. The row outlives a restart, so a restart inside the interval starts nothing. A slow run cannot overlap the next: a fetch is capped at 120 s in total and the interval cannot go below 300 s. Inside one process the thread runs one cycle at a time. This needed no migration: `app_settings` already exists.)*
|
||||
|
||||
---
|
||||
|
||||
**Decided 2026-10-16 (task's call, per this task's "Do"):** in-process timer in the
|
||||
`api` container, as D18 proposed, not a sidecar. The `backup` pattern was the
|
||||
alternative; it would need its own image and would not remove the 2-worker
|
||||
duplicate question, because the sync reads the application database anyway.
|
||||
**Network check, as the task asked:** the `outbound` network the `api` service
|
||||
already joins is an ordinary bridge with a gateway, so Okta is reachable with no
|
||||
network change. The host firewall must allow https to the Okta org.
|
||||
**One change to T13.2's code:** `fetch_users()` now also stops after 120 seconds in
|
||||
total (`TOTAL_DEADLINE_SECONDS`), which is what lets the scheduler promise no
|
||||
overlap. Tested in `tests/okta_sync_check.py` section 8.
|
||||
|
||||
**T13.4 follow-up, 2026-10-16:** the sync's INFO log lines did not appear in
|
||||
`docker compose logs api`. Nothing in the app configures Python logging, and
|
||||
gunicorn configures only its own loggers, so INFO from any `wpsuite.*` logger is
|
||||
dropped. The T13.4 check booted the app in-process with logging switched on,
|
||||
which hid it. With the exact Dockerfile launch (2 gunicorn workers), a healthy
|
||||
start printed zero Okta lines. `okta_sync.py` now gives its own logger one stderr
|
||||
handler (only when the root logger has none). Re-run with the real gunicorn
|
||||
command: `configured` and `scheduled` (or `DISABLED`) now print once per worker.
|
||||
No other module's output changed. App-wide logging stays a separate question
|
||||
(BL-028 is the same family).
|
||||
|
||||
### T13.5 — D18: admin visibility
|
||||
|
||||
- **Items:** `D18`
|
||||
@@ -199,13 +272,21 @@ build it in this task; log it instead if it's tempting to add.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [ ] an auto-disabled account's audit entry is visible and legible in the
|
||||
- [x] an auto-disabled account's audit entry is visible and legible in the
|
||||
existing admin UI without special-casing
|
||||
- [ ] nothing here silently assumes `CR-019`'s activity view exists yet — this
|
||||
*(2026-10-16: run on the PC against the real page, 1440px and 390px, `tests/okta_sync_ui_check.py` 28/28. The admin Activity log card reads: Who `system:okta_sync`, Action `user deprovisioned by sync`, Type `user`, Item the username, Detail `okta_status: SUSPENDED, via: okta_sync` (a person missing from Okta reads `okta_status: MISSING`). Search for `okta_sync` and the User accounts filter both find the rows. No sideways scroll, no JavaScript errors. The disabled account is refused on its very next request; an account still active in Okta is not. A project super user does not see these rows: they belong to no project, so only admins do, the same as other user-management events. No code in `html/` changed.)*
|
||||
- [x] nothing here silently assumes `CR-019`'s activity view exists yet — this
|
||||
must work standalone
|
||||
*(2026-10-16: the check reads only the Activity log (AuditLog) card. The new Activity & usage card is on the page but is not read.)*
|
||||
|
||||
---
|
||||
|
||||
**No UI was built.** The existing card renders the new rows legibly as is. Two
|
||||
things you might expect and did not get, both on purpose: no admin email on an
|
||||
auto-disable (D18 calls it a fast-follow; not built, not logged as a new item
|
||||
because D18 already names it), and no per-user history view in the User
|
||||
Directory (it has none today, and the task says not to build one).
|
||||
|
||||
### T13.6 — D18: verification
|
||||
|
||||
- **Items:** `D18`
|
||||
@@ -223,20 +304,183 @@ introduce a path that swallows the client's error and proceeds anyway.
|
||||
|
||||
- [ ] all `D18` acceptance criteria in `decisions-2026-09-17.md` are met or a
|
||||
failure is stated with a reason
|
||||
- [ ] the fail-closed behavior is verified end to end through the scheduled
|
||||
- [x] the fail-closed behavior is verified end to end through the scheduled
|
||||
wrapper, not just the bare sync function
|
||||
- [ ] smoke test and `seed_demo.py` both still pass
|
||||
- [ ] full test suite passes
|
||||
- [x] smoke test and `seed_demo.py` both still pass
|
||||
- [x] full test suite passes (48 of 50; the two failures are the known ones, see the result note below)
|
||||
|
||||
---
|
||||
|
||||
## Wave 13 exit criteria
|
||||
|
||||
- [ ] `AUTH_SESSION_HOURS` defaults to 2, documented
|
||||
- [ ] the sync job runs on a schedule and correctly disables accounts Okta no
|
||||
- [x] `AUTH_SESSION_HOURS` defaults to 8, documented (D25 replaced the 2 hours in this line; T13.1's idle timer is still held back)
|
||||
- [x] the sync job runs on a schedule and correctly disables accounts Okta no
|
||||
longer shows as active
|
||||
- [ ] every auto-disable is individually audited with a clearly non-human actor
|
||||
- [ ] an Okta API failure of any kind changes zero accounts — verified, not
|
||||
- [x] every auto-disable is individually audited with a clearly non-human actor
|
||||
- [x] an Okta API failure of any kind changes zero accounts — verified, not
|
||||
assumed
|
||||
- [ ] the sync never re-enables an account
|
||||
- [x] the sync never re-enables an account
|
||||
- [ ] `D18` fully accounted for, no open acceptance criteria
|
||||
|
||||
## Release note (2026-10-16)
|
||||
|
||||
T13.1 (commit `6cde6e3`) is held back from `main` until T14.1 lands, per the merge
|
||||
rule in `wave-12.md`. The release branch contains wave 11 and T13.2 to T13.5.
|
||||
`tests/session_idle_check.py` skips (exit 0) when `auth.maybe_refresh_token` is absent.
|
||||
Sessions keep the flat `AUTH_SESSION_HOURS` lifetime (default 12) until T13.1 lands.
|
||||
|
||||
T13.6 result (2026-10-16, merged release tree, Edge on the PC): 47 test scripts, 45 pass.
|
||||
The two failures are not regressions. `token_check.py` exits 2 (CLI usage detail).
|
||||
`generalinfo_check.py` finds an old color-literal gap in `wp-creation-styles.css`.
|
||||
Alembic upgrade, downgrade, upgrade passed. Import check passed.
|
||||
|
||||
## T13.7: D22 dry run for the Okta sync
|
||||
|
||||
**Item:** D22 (`docs/waves/decisions-2026-10-16.md`). Extends T13.3 and T13.4.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [x] `OKTA_SYNC_DRY_RUN` on: the sync fetches and matches, logs the accounts it would disable, and changes no account and writes no audit row (`tests/okta_sync_dry_run_check.py`)
|
||||
- [x] both guards (zero match, fail closed) still apply in a dry run
|
||||
- [x] flag off or unset: behavior is unchanged (`okta_sync_job_check.py` still passes)
|
||||
- [x] the startup line names the mode
|
||||
- [x] `.env.example`, `docker-compose.yml` and `DEPLOYMENT.md` describe the flag and the first-deploy steps
|
||||
- [x] mutation check: if the dry-run guard is removed, the new test fails
|
||||
|
||||
Result 2026-10-16: all Okta sync tests and `security_check.py` pass in the sandbox.
|
||||
This task changes no frontend file.
|
||||
|
||||
## T13.8: run the stack in Docker on a PC
|
||||
|
||||
**Item:** D22 (`docs/waves/decisions-2026-10-16.md`), as a way to check T13.7 and later builds before a deploy.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [x] one command starts the api, the database and the web server from the shipped compose file plus `docker-compose.local.yml`
|
||||
- [x] `http://localhost:8080/api/health` answers 200 and `/api/projects` answers 401 without a session
|
||||
- [x] every migration runs on Postgres (head `8e2cb3003f8a`)
|
||||
- [x] the api log shows the `Okta sync` startup line
|
||||
- [x] the steps are in `DEPLOYMENT.md`, "Run the stack on your own PC"
|
||||
- [x] the shipped `docker-compose.yml` and nginx config are not changed
|
||||
|
||||
Result 2026-10-16 (Docker Desktop 29.7.2, Compose 5.4.0, Windows): passed. Three
|
||||
things in the shipped files stop a local run. The override handles the first two
|
||||
and the third is logged:
|
||||
|
||||
1. The web server publishes no port (the host's reverse proxy reaches it). The override publishes 8080 on loopback.
|
||||
2. An existing `proxy` network on a PC can have no gateway, and Docker then does not publish ports. The override uses its own bridge. The stock nginx `default.conf` also answers "localhost" first and the app server answers only wp.controls.dev, so the override removes the stock file and makes the app server the default.
|
||||
3. The backup container cannot start on Windows: `scripts/entrypoint.sh` has CRLF line endings (BL-070). The override turns the backup service off.
|
||||
|
||||
## T13.9: D23 sign in as an OAuth service app (private_key_jwt)
|
||||
|
||||
**Item:** D23 (`docs/waves/decisions-2026-10-16.md`). Extends T13.2.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [x] with `OKTA_SYNC_CLIENT_ID` and `OKTA_SYNC_PRIVATE_KEY`, the sync gets an access token and lists users with `Bearer` (`tests/okta_sync_oauth_check.py`)
|
||||
- [x] the assertion verifies against the public key: `iss`, `sub`, `aud`, `kid`, life, `jti`
|
||||
- [x] RSA and EC keys; JSON and base64 forms
|
||||
- [x] a failed token request, a missing token, a DPoP token, a public key, or an unreadable key raises, and the users endpoint is never called
|
||||
- [x] `OKTA_API_TOKEN` still works and wins when both are set
|
||||
- [x] the startup line names the credential and holds no secret; no error message holds key text
|
||||
- [x] mutation checks: Bearer to SSWS, wrong `aud`, DPoP accepted, public-key check removed: each makes the test fail
|
||||
- [x] `.env.example`, `docker-compose.yml`, `DEPLOYMENT.md` describe the variables and the Okta app setup
|
||||
|
||||
Result 2026-10-16: sandbox, fake Okta. Not yet run against the real tenant.
|
||||
No new dependency: PyJWT signs with `cryptography`, which Authlib already installs.
|
||||
|
||||
T13.9 follow-up (2026-10-16): the first live deploy failed with "OKTA_SYNC_PRIVATE_KEY is not a
|
||||
JWK as JSON or as base64 of JSON". The first loader used strict base64, which rejects any
|
||||
space or line break, and it gave one message for every failure. The loader now forgives quote
|
||||
marks, spaces, line breaks, missing padding, the URL-safe alphabet, a byte-order mark and a
|
||||
one-key JWK Set, and each failure names its step (counts only, no key text). The new checks
|
||||
in `tests/okta_sync_oauth_check.py` fail against the first loader and pass now.
|
||||
|
||||
T13.9 follow-up 2 (2026-10-16): the second deploy decoded the value but the text was not JSON.
|
||||
The loader now also names a PEM key, adds missing outer braces, unwraps a value that was encoded
|
||||
twice, and its "not JSON" message gives the length and the kind of first character (counts only).
|
||||
|
||||
T13.9 follow-up 3 (2026-10-16): the third deploy decoded the base64 to 1,760 characters that start
|
||||
with a brace, but Python's strict JSON reader refused them (PowerShell accepted the same text).
|
||||
The loader now accepts control characters such as line breaks inside a string, curly quote marks and
|
||||
a trailing comma, removes spaces inside the key's number fields, and a real syntax error reports the
|
||||
line and column (position and rule only, never key text).
|
||||
|
||||
## T13.10: D24 DPoP for the Okta Management API
|
||||
|
||||
**Item:** D24 (`docs/waves/decisions-2026-10-16.md`). Extends T13.9.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [x] auto mode: a plain Bearer token is tried first; `invalid_dpop_proof` makes the sync retry with DPoP (`tests/okta_sync_dpop_check.py`)
|
||||
- [x] each proof verifies: header (`dpop+jwt`, ES256, public jwk only), signature, `htm`, `htu` without query, current `iat`, one-time `jti`, `ath` on API calls, same key for token and API calls
|
||||
- [x] nonce challenges at the token endpoint and at the API are answered once, and the retries are bounded
|
||||
- [x] paging: a new proof for every page
|
||||
- [x] mode `on` and mode `off`; a DPoP-bound token with mode off is refused; each run makes a new key
|
||||
- [x] no secret in any message or the startup line; the SSWS path is unchanged
|
||||
- [x] mutation checks: query kept in `htu`, `ath` omitted, `jti` reused, nonce not retried (token and API), private key in the proof header, and auto detection removed each make a test fail
|
||||
- [x] `.env.example`, `docker-compose.yml`, `DEPLOYMENT.md` updated
|
||||
|
||||
Result 2026-10-16: sandbox, strict fake Okta. Not yet run against the real tenant.
|
||||
|
||||
## Live run and final verification (2026-10-17)
|
||||
|
||||
**Live deploy.** The api container runs the build from `main` (`1a3468e`) in Portainer. It signs in with the
|
||||
service-app key (`private_key_jwt`) and DPoP. The first dry run (2026-10-17 18:31) fetched 2,564 Okta users and
|
||||
checked 42 active local accounts. 41 matched (so `profile.login` is the right attribute) and 1 would be disabled:
|
||||
`admin`, Okta status MISSING. Matt confirmed that `admin` is a leftover account, and switched the dry run off.
|
||||
|
||||
Live run log lines (api log):
|
||||
|
||||
- 18:46:21 `Okta sync disabled 'admin' (Okta status: MISSING)`, then `done: 2564 Okta user(s), 42 active local account(s) checked, 1 disabled`
|
||||
- 19:01:50 `done: ... 41 active local account(s) checked, 0 disabled`
|
||||
- 19:16:30 `done: ... 41 active local account(s) checked, 0 disabled`
|
||||
|
||||
This shows: the job runs on its schedule (every 900 seconds), it disables an account that Okta no longer lists, a
|
||||
disabled account is not touched again and gets no new row, and the account count fell from 42 to 41.
|
||||
The first session logs of fail-closed behavior are in the earlier runs: with a rejected token, a bad key, and an
|
||||
unreadable key, the sync logged `SKIPPED, no account was changed` every time and no account changed.
|
||||
|
||||
**Admin recovery.** `python -m server.manage_users list | promote | enable | disable` works from the Portainer
|
||||
console of `wp_api` (no `docker compose exec` prefix). `promote` changes an existing account. It does not create one.
|
||||
|
||||
**Full verification on the final tree (`1a3468e`).**
|
||||
|
||||
- Test suite on Matt's PC (Edge): 50 scripts, 48 pass. The two failures are not regressions:
|
||||
`token_check.py` exits 2 (it needs `--out` or `--compare`), and `generalinfo_check.py` finds an old colour literal
|
||||
(`rgba(`) in `wp-creation-styles.css`.
|
||||
- `server/smoketest.py`: 27 of 27 checks pass. `server/seed_demo.py`: runs clean. Both ran in the sandbox against
|
||||
a throwaway SQLite database. Note: use `http://127.0.0.1:PORT`, not `http://localhost:PORT`, because Python's cookie
|
||||
jar does not send a cookie set for `localhost` (logged as BL-071).
|
||||
- The Okta tests (`okta_sync_check`, `_job_`, `_schedule_`, `_dry_run_`, `_oauth_`, `_dpop_`, `_ui_`) and `security_check.py` pass.
|
||||
|
||||
**Still open for wave 13.**
|
||||
|
||||
- `T13.1` (idle timeout) is held back from `main` until T14.1 lands. The exit criterion "`AUTH_SESSION_HOURS` defaults to 2"
|
||||
is not met, and it conflicts with T13.1's own text (8 hours). Matt has not chosen.
|
||||
- ~~"Every auto-disable is individually audited with a clearly non-human actor" was not checked after the live run.~~
|
||||
Checked 2026-10-17: the Admin Console activity list shows one row, actor `system:okta_sync`, action "user deprovisioned
|
||||
by sync", item `admin`, detail `okta_status: MISSING, via: okta_sync`, at 11:46 AM local (18:46 UTC).
|
||||
- `D18` cannot be called fully accounted for while T13.1 is outstanding.
|
||||
- `BL-069` (a cap on one run and a last-admin guard) is open.
|
||||
|
||||
## T13.11: D25 the session limit is 8 hours
|
||||
|
||||
**Item:** D25 (`docs/waves/decisions-2026-10-16.md`).
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [x] `AUTH_SESSION_HOURS` defaults to 8 in `server/auth.py`, `docker-compose.yml`, `server/.env.example`, `DEPLOY-login-portal.md` (`tests/session_default_check.py`)
|
||||
- [x] the token expiry and the cookie max-age follow it; a set value overrides it
|
||||
- [x] mutation check: putting the default back to 12 makes the test fail
|
||||
- [x] the wave 13 exit criterion that said 2 is updated
|
||||
|
||||
Effect on the live system: after the next deploy, sessions last 8 hours instead of 12. To get the same result
|
||||
before a deploy, set `AUTH_SESSION_HOURS` to `8` in the Portainer stack variables.
|
||||
|
||||
## Gate for T13.1 (2026-10-17)
|
||||
|
||||
T13.1 now has a written gate: `docs/waves/wave-14.md`. It lands in T14.4, after T14.1 (a `401` holds the outbox and does not
|
||||
destroy it) and T14.2 (the offline window follows the server) are done. The reason is a fault found in the outbox on
|
||||
2026-10-17: it marks a `401` as a permanent rejection, so an idle timeout would reject a crew's queued changes for good.
|
||||
That fault already exists with the 8 hour limit, to a lesser degree.
|
||||
|
||||
190
docs/waves/wave-14.md
Normal file
190
docs/waves/wave-14.md
Normal file
@@ -0,0 +1,190 @@
|
||||
# Wave 14: The outbox keeps every change, and the sign-in limit cannot destroy it
|
||||
|
||||
**Items:** `D26`, `D27`, `BL-061` (`B5` is context only: its wording rules stay, and this wave does not widen it)
|
||||
**Depends on:** wave 13 merged (it is, except T13.1). Independent of wave 15.
|
||||
**Decision record:** `docs/waves/decisions-2026-10-17.md` (D26 and D27 are **accepted** as of 2026-10-17, see "Open questions")
|
||||
**Unblocks:** `T13.1` (idle timeout). T13.1 must not reach `main` before T14.1 and T14.2 are done.
|
||||
**Branch rule:** one task for each pull request. Cut each branch from `main`.
|
||||
|
||||
## Why this wave exists
|
||||
|
||||
The outbox in `html/project-data.js` queues each save in `localStorage` and sends it later. A crew in the field
|
||||
can work with no signal, and the changes go out when the network returns. The code was checked on 2026-10-17
|
||||
against the 8 hour session limit (D25) and the idle timeout that T13.1 adds. It has four faults that lose or hide
|
||||
work. They are small now. The idle timeout would make them common.
|
||||
|
||||
| # | Fault | Where | Result |
|
||||
|---|---|---|---|
|
||||
| 1 | A `401` (session ended) or `403` is treated like any other `4xx`. The change is marked **dead** and is never sent again. | `project-data.js`, `flush()` | A crew that works past the session limit, or past an idle timeout, comes back online and every queued change is rejected for good. The badge says "rejected by the project". The change is not lost from the device, but nothing can send it. |
|
||||
| 2 | `qWrite()` swallows a storage error. | `project-data.js`, `qWrite()` | If the device storage is full, a change is **not queued and nobody is told**. |
|
||||
| 3 | The queue belongs to the browser, not to a person. A queued change has no owner. | `project-data.js`, `enqueue()` | On a shared device, person B signs in and the queue of person A is sent under the name of person B. |
|
||||
| 4 | The offline sign-in window is a fixed 12 hours in the browser. | `auth-guard.js`, around line 183 | It does not follow `AUTH_SESSION_HOURS` (8 since D25). It also slides: every online page load resets it. The browser and the server disagree about when the session ends. |
|
||||
|
||||
Also: the sync badge sits over page controls (`BL-061`) and cannot be used with a keyboard or a finger. The
|
||||
badge is a `div` with `pointer-events:none`. A badge that must ask a person to sign in again needs a real link or button.
|
||||
|
||||
## Rules for this wave
|
||||
|
||||
- Do not use a `div` with `onclick` (`CLAUDE.md`, accessibility). The sign-in action is an `<a>` or a `<button>`.
|
||||
- Status text goes in an `aria-live` region. The badge already has `role="status"`. Keep it.
|
||||
- Server work is needed for T14.2 (one new field). Do not store session data in `localStorage` as the source of truth.
|
||||
- Colors, spacing and type come from the shared tokens. Do not add a raw hex value to a page stylesheet.
|
||||
- Frontend checks: run the app on a throwaway SQLite database, check at 390 px and 1440 px, capture before and after
|
||||
screenshots, run the smoke test (use `http://127.0.0.1`, see `BL-071`).
|
||||
- Do not fix other problems you see. Log them in `docs/waves/backlog.md`.
|
||||
|
||||
---
|
||||
|
||||
### T14.1: D26 a sign-in problem never destroys a queued change
|
||||
|
||||
- **Items:** `D26`
|
||||
- **Depends on:** nothing (first task)
|
||||
- **Blocks:** T14.2, T14.3, T13.1
|
||||
- **Surface:** `html/`
|
||||
- **Files:** `html/project-data.js`, `html/auth-guard.js`, a new `tests/outbox_check.py`
|
||||
|
||||
**Do:**
|
||||
|
||||
1. In `flush()`, treat `401` as **held**, not dead. Stop the run. Do not add to `tries`. Do not mark any change
|
||||
dead. Keep the whole queue in its order. Set a "held for sign-in" state that the badge can read through
|
||||
`ProjectData.syncStatus()` (add a field, for example `held: true`).
|
||||
2. Keep `403` as a permanent refusal, but say why in the reason text ("you no longer have access to this project").
|
||||
Keep `409` (archived project) and the other `4xx` as they are.
|
||||
3. When the person signs in again (the page loads with a good session, or `wp-auth-ready` fires), clear the held
|
||||
state and flush at once. The `online` event, the 20 second timer and the tab-focus trigger must **not** clear it
|
||||
early: a held queue sends only after a good session check.
|
||||
4. Make `qWrite()` return `true` or `false`. If it fails, `enqueue()` must tell the caller and raise the
|
||||
`device full` state. The change stays in the page memory and the form stays open. The badge says the change is
|
||||
**not saved on this device**. Do not drop it silently.
|
||||
5. Give each queued change an `owner` (the signed-in username) when it is queued. At flush time, if `WP_USER` is not the
|
||||
owner, hold that change and name the owner in the message. Do not send it. Do not discard it. (See open question 1.)
|
||||
6. Make sure `auth-guard.js` does not delete the queue when it sends a person to `login.html` after a `401`. It must
|
||||
keep `wp_sync_outbox_v1`.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [x] a queue of N changes meets a `401`: all N are still in the queue, none is dead, `tries` did not change, and the badge state is "held"
|
||||
- [x] after a good sign-in, all N changes reach the server in their original order, and the queue is empty
|
||||
- [x] a `403` marks only that change dead, with a reason that names the cause
|
||||
- [x] a full device store (a forced `QuotaExceededError`) is reported to the caller and the badge. No change disappears silently
|
||||
- [x] a change queued by person A is not sent when person B is signed in; it is sent after A signs in
|
||||
- [x] `tests/outbox_check.py` covers each case above and fails when `flush()` is put back to the old rule (a mutation check)
|
||||
- [x] the full suite passes (2026-10-17: 52 of 54 checks pass; the 2 others are `generalinfo_check` and `token_check`, known and not regressions). `mobile_check` covers 390 px. No before and after screenshots were taken: the badge looks the same until T14.3. The badge words for the new states are plain text. T14.3 gives them their final wording and the Sign in control.
|
||||
|
||||
**Built (2026-10-17):** commit `73f0b5e`. Open question 1 as accepted: another person's changes are held and the badge names the owner. A change queued before the sign-in check finished has no owner and can be sent by anyone, as before.
|
||||
|
||||
---
|
||||
|
||||
### T14.2: D27 the offline sign-in window follows the server
|
||||
|
||||
- **Items:** `D27`
|
||||
- **Depends on:** T14.1
|
||||
- **Blocks:** T13.1
|
||||
- **Surface:** `server/` and `html/`
|
||||
- **Files:** `server/app.py` (`whoami()`), `html/auth-guard.js`, `html/wp-format.js` (line 223 writes the cache), tests
|
||||
|
||||
**Do:**
|
||||
|
||||
1. Add one field to `/api/auth/me`: `session_expires_at` (the `exp` claim of the session, as a UTC time). Read the claim from the
|
||||
token that `get_current_user` already validated. Do not add a database column. When T13.1 lands, this field is the
|
||||
current `exp` and moves with each refresh.
|
||||
2. In `auth-guard.js`, store `session_expires_at` in `wp_auth_cache`. Replace the fixed `12 * 3600 * 1000` with a rule
|
||||
that uses it. The rule for this wave (see open question 2):
|
||||
- offline, the app **opens** while the current time is before `session_expires_at`;
|
||||
- after that time, offline, the app still opens in a **read and queue** mode: the person can see cached data and add
|
||||
changes, which go to the outbox and are held (T14.1) until a good sign-in. The app does not go to `login.html`
|
||||
while it has no network, because that page cannot load an Okta sign-in without a network;
|
||||
- online, a `401` goes to `login.html` as now, and the outbox is kept (T14.1).
|
||||
3. Stop sliding the window. A page load must not extend `wp_auth_cache`. Only the server can extend a session.
|
||||
4. Show the person which mode they are in. One line: "Working offline. Sign in again when you have a signal to send your changes."
|
||||
Use the same `aria-live` region as the badge.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [x] `/api/auth/me` returns `session_expires_at`, and it matches the token (`tests/session_expiry_check.py`, no database change)
|
||||
- [x] the fixed 12 hours is gone from `auth-guard.js`
|
||||
- [x] opening the app offline before the time works; opening it offline after the time gives read-and-queue mode and does not redirect
|
||||
- [x] online with a dead session: redirect to `login.html?next=...`, queue kept, changes sent after sign-in (the send after sign-in is the T14.1 test, section 3)
|
||||
- [x] a page load does not extend the cached window
|
||||
- [x] the mode line is announced by a screen reader (`aria-live`) and fits at 390 px. The line is in the badge, which has `role="status"`. T14.3 checks that it fits at 390 px and covers no control (`sync_badge_check.py`, state `readqueue`). It was **not** tried with a real screen reader.
|
||||
|
||||
**Built (2026-10-17):** `tests/session_expiry_check.py` (22 checks) fails when the old 12 hour rule is put back (7 failures). The mode line shows only in read-and-queue mode, not when the app opens offline before the session ends, because "sign in again" would be wrong while the session is still good. In read-and-queue mode the outbox does not send at all until a page load gets a good answer from the server. Saving display preferences refreshes the cached user and keeps the end time (`wpCacheAuth`).
|
||||
|
||||
---
|
||||
|
||||
### T14.3: D26 and BL-061 the sync badge shows the true state and gets out of the way
|
||||
|
||||
- **Items:** `D26`, `BL-061`
|
||||
- **Depends on:** T14.1
|
||||
- **Blocks:** nothing
|
||||
- **Surface:** `html/`
|
||||
- **Files:** `html/project-data.js` (`renderSyncBadge`), `html/wp-chrome.css` or the shared token file, `tests/a11y_check.py`, `tests/autosave_check.py` (it reads the badge strings)
|
||||
|
||||
**Do:**
|
||||
|
||||
1. Show these states, each with its own words that name the **project**, never a draft save (the `B5` rule that wave 4 set): `sending`, `retrying`,
|
||||
`held for sign-in` (with a link or button "Sign in"), `not saved on this device` (store full), `rejected` (with the reason),
|
||||
and `all sent` (hides after a short time).
|
||||
2. Make the badge usable: the "Sign in" and "Dismiss" controls are `<a>` or `<button>` elements with a visible focus ring
|
||||
and a touch target of at least 44 px. Remove `pointer-events:none` for the states that have controls.
|
||||
3. Keep the badge clear of page controls (`BL-061`): do not show it on pages that have no outbox work (the console
|
||||
pages), and place it so that it does not cover the bottom-right action buttons at 390 px or 1440 px.
|
||||
4. Keep text contrast at 4.5:1 or better in every state.
|
||||
5. Update the tests that read the badge text (`autosave_check.py` reads its symbols; `a11y_check.py` reads the element).
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [x] each state shows in a browser test, with its text (`tests/sync_badge_check.py`). The states are checked at 1440 px; the placement is checked at 390 px and 1440 px
|
||||
- [x] "Sign in" is a real link (`<a>`), reachable by keyboard, 44 px or more, with a focus ring, and goes to `login.html?next=...`. Touch is covered by the size and by the element type: no touch event was sent
|
||||
- [x] the badge covers no control on the User Directory or the bulk bar: the console pages never show it (`data-wp-no-sync-badge`)
|
||||
- [x] no badge on the console pages
|
||||
- [x] the contrast check passes for every state (4.5:1 or better, light theme only; the dark theme was **not** checked)
|
||||
- [x] `autosave_check.py` and `a11y_check.py` pass. Full suite 2026-10-17: 53 of 55 pass; `generalinfo_check` and `token_check` fail as before (known, not regressions). `sync_badge_check.py` (87 checks) fails when the "nothing to send" rule and the console opt-out are removed (3 failures)
|
||||
|
||||
**Built (2026-10-17):** the badge words are in one function, `badgeModel()`. The mode line for read-and-queue mode now says "...to send your changes to the project." so it follows the B5 rule. "Dismiss" removes only the changes the project refused, after the person presses it. The badge picks a free spot on the screen (BL-061). One case is left, a very dense screen with no free gap: `BL-072`. No before and after screenshots were taken: the check measures overlap in the browser instead. A person should look at the badge once on a phone.
|
||||
|
||||
---
|
||||
|
||||
### T14.4: verify the wave and release T13.1
|
||||
|
||||
- **Items:** `D26`, `D27`, `BL-061`, `T13.1`
|
||||
- **Depends on:** T14.1, T14.2, T14.3
|
||||
- **Blocks:** nothing
|
||||
|
||||
**Do:** Run the full suite, the smoke test and `seed_demo.py`. Then test the real case by hand on a phone or tablet:
|
||||
sign in, set the device to airplane mode, make three changes in Field View, wait until the session limit has passed
|
||||
(use `AUTH_SESSION_HOURS=1` on a test stack with a clock change, or a forced expiry), turn the network on, and check that the badge shows
|
||||
"held for sign-in", that signing in sends all three, and that the server has them. Then rebase `feat/waves-11-13`
|
||||
T13.1 (`6cde6e3`) onto `main`, run `tests/session_idle_check.py` and the same hand test with the idle timeout on, and merge T13.1 as its own pull request.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [ ] the hand test passes on one iPhone and one Android device (or the failure and the reason are written here)
|
||||
- [ ] the full suite and the smoke test pass on the final tree
|
||||
- [ ] T13.1 is on `main`, and `tests/session_idle_check.py` runs (it no longer skips)
|
||||
- [ ] the wave 13 exit line about D18 is ticked
|
||||
|
||||
---
|
||||
|
||||
## Open questions
|
||||
|
||||
**Answered 2026-10-17 (Matt accepted the proposal for questions 1, 2 and 3).** Question 4 is still open and belongs to `T13.1`.
|
||||
|
||||
1. **Shared devices.** If person B signs in on a device that holds unsent changes from person A, what must happen?
|
||||
Proposed: hold A's changes, name A in the badge, send them when A signs in. Do not send them as B and do not discard them.
|
||||
Other choices: allow B to discard A's changes after a confirmation; or send them as B (not proposed, wrong author in the log).
|
||||
2. **Offline window.** Proposed for this wave: the app opens offline until the session ends, and after that works in read-and-queue mode.
|
||||
The 7 day offline window (`BL-062`, Q2) is a **later** wave. Do you accept this order, or must 7 days come first?
|
||||
3. **Device full.** Proposed: do not drop the change, keep it in the page memory, and tell the person. Is "tell the person and keep the
|
||||
form open" enough, or must the app also stop new edits?
|
||||
4. **Idle time.** T13.1 proposes 30 minutes. It is a proposed value, not a decision. It matters here because every minute of idle
|
||||
time offline ends the session on the server side.
|
||||
|
||||
## Wave 14 exit criteria
|
||||
|
||||
- [ ] a session that ends while changes are queued loses none of them
|
||||
- [ ] a full device store is never silent
|
||||
- [ ] a change is sent only under the signed-in person who made it
|
||||
- [ ] the offline window follows the server (no fixed 12 hours in the browser)
|
||||
- [ ] the badge shows the true state, is usable, and covers no control
|
||||
- [ ] T13.1 is merged to `main` and `D18` is fully accounted for
|
||||
155
docs/waves/wave-16.md
Normal file
155
docs/waves/wave-16.md
Normal file
@@ -0,0 +1,155 @@
|
||||
# Wave 16 — Urgent security fixes
|
||||
|
||||
**Items:** `D19`, `D20`, `D21`
|
||||
**Depends on:** nothing. Independent of waves 12 to 15 (see the decision record).
|
||||
**Decision record:** `docs/waves/decisions-2026-10-02.md`
|
||||
**Background:** `docs/reference/security-review-2026-10-01.md`
|
||||
|
||||
Four tasks, in build order. All server-side. No page markup or stylesheet
|
||||
changes, so the 390px / 1440px screenshot rule does not apply; the
|
||||
browser probes that read files and SOPs are still listed for a run on a
|
||||
machine with Edge.
|
||||
|
||||
New check for this wave: `tests/security_check.py`. It is self-contained (its
|
||||
own SQLite database, in-process, no browser), so it runs anywhere.
|
||||
|
||||
---
|
||||
|
||||
### T16.1 — D21: session decode fails closed; PyJWT 2.15.1
|
||||
|
||||
- **Items:** `D21`
|
||||
- **Depends on:** nothing (first task)
|
||||
- **Blocks:** T16.4
|
||||
- **Surface:** `server/`
|
||||
- **Files:** `server/requirements.txt`, `server/auth.py` (`decode_token`), `tests/security_check.py` (new)
|
||||
|
||||
**Do:** pin `PyJWT==2.15.1`. In `decode_token`, return `None` for an empty or
|
||||
over-long token (over 4096 characters) and for any exception from
|
||||
`jwt.decode`. Require the `exp` and `sub` claims.
|
||||
|
||||
**Do not:** change token contents, the cookie, or the D18 refresh.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [x] the crafted-cookie request returns 401, not 500
|
||||
- [x] a valid session still works, and the D18 refresh still reissues
|
||||
- [x] `pip-audit -r server/requirements.txt` reports no PyJWT advisory
|
||||
- [x] smoke test 27/27
|
||||
|
||||
---
|
||||
|
||||
### T16.2 — D19: uploaded files download safely
|
||||
|
||||
- **Items:** `D19`
|
||||
- **Depends on:** nothing
|
||||
- **Blocks:** T16.4
|
||||
- **Surface:** `server/`
|
||||
- **Files:** `server/files_http.py` (new), `server/app.py` (`get_wp_file`), `tests/security_check.py`
|
||||
|
||||
**Do:** serve PDF and raster images inline as today. Serve every other type,
|
||||
SVG included, as an attachment with `Content-Security-Policy: sandbox;
|
||||
default-src 'none'`. Send `X-Content-Type-Options: nosniff` from the API too
|
||||
(NGINX already does; the dev server does not). Build `Content-Disposition`
|
||||
with an ASCII fallback and an RFC 5987 `filename*`. Reference code:
|
||||
`docs/reference/architecture-review/proposals/server/files_http.py` (P-6).
|
||||
|
||||
**Do not:** refuse SVG uploads (decided: accept, download only). Do not change
|
||||
`Cache-Control`.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [x] an SVG downloads as an attachment with the sandbox header
|
||||
- [x] PNG, JPEG and PDF still open inline
|
||||
- [x] `Plan é.pdf` downloads (was 500)
|
||||
- [x] a name with CR/LF produces a valid header
|
||||
- [x] `tests/files_check.py` passes on a machine with Edge (CR-007 offline drawings)
|
||||
|
||||
---
|
||||
|
||||
### T16.3 — D20: one SOP per project
|
||||
|
||||
- **Items:** `D20`
|
||||
- **Depends on:** nothing
|
||||
- **Blocks:** T16.4
|
||||
- **Surface:** `server/`
|
||||
- **Files:** `server/app.py` (`upsert_sop`, `latest_sop`, `project_summary`, `project_sop_team`, `project_qa_group`), `tests/security_check.py`
|
||||
|
||||
**Do:** the four rules in D20. One helper picks the project's baseline SOP
|
||||
(canonical row if complete, else latest complete) and every baseline reader
|
||||
uses it.
|
||||
|
||||
**Do not:** delete or rename existing SOP rows. Do not change any response shape.
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [x] a `project_user` cannot create a second SOP on a project (400) or post
|
||||
`complete: true` while a complete SOP exists (403)
|
||||
- [x] a `project_user` can still draft and complete the first SOP of a project
|
||||
- [x] a Project Admin can still change the completed SOP
|
||||
- [x] POST without an id lands on `sop__<project_id>`
|
||||
- [x] a rogue complete SOP made before the fix no longer drives the baseline
|
||||
or the QA and PM/CM recipients
|
||||
- [x] `tests/sample_check.py` and `tests/critical_reopen_check.py` pass on a
|
||||
machine with Edge
|
||||
|
||||
---
|
||||
|
||||
### T16.4 — D19, D20, D21: verification
|
||||
|
||||
- **Items:** `D19`, `D20`, `D21`
|
||||
- **Depends on:** T16.1 to T16.3
|
||||
- **Surface:** verification
|
||||
- **Files:** this file (verification record)
|
||||
|
||||
**Done when:**
|
||||
|
||||
- [x] `tests/security_check.py` passes
|
||||
- [x] `docs/reference/security-review/sec_probe.py`: S3 and S12 change as
|
||||
expected; every other line is unchanged
|
||||
- [x] the architecture review `equivalence_check.py` (current branch vs wave
|
||||
16) is IDENTICAL: the scenario does not use these paths, so it proves the
|
||||
rest of the work-package flow did not move
|
||||
- [x] smoke test 27/27
|
||||
- [x] the browser probes named in T16.2 and T16.3 are listed for a run on a
|
||||
machine with Edge (not available in the build sandbox)
|
||||
|
||||
## Verification record
|
||||
|
||||
Run 2026-10-02 in the build sandbox (Linux, Python 3.10, no browser), on branch
|
||||
`feat/wave-16-security` at T16.3.
|
||||
|
||||
| Check | Result |
|
||||
|---|---|
|
||||
| `tests/security_check.py` | **36/36** (D21 6, D19 14, D20 16) |
|
||||
| Same check against `da1325c` with PyJWT 2.13.0 | 20/36. The 16 failures are the findings (D21 3, D19 6, D20 7). The 20 passes are the "still works" checks plus three that already held (over-long cookie 401, non-member 404, SOP move refused by the existing completed-SOP rule) |
|
||||
| `docs/reference/security-review/sec_probe.py`, before vs after | Exactly 4 of 27 lines change: S3 (new complete SOP 200 -> **400**; baseline `sop_rogue` -> **`sop__proj_a`**; QA group attacker -> **real group**) and S12 (crafted cookie 500 -> **401**). All other lines identical |
|
||||
| `equivalence_check.py` (architecture review), `da1325c` vs wave 16 | **IDENTICAL**: 78 HTTP results, 89 audit rows, 24 notifications. The scenario's SOP id was changed from `sop_eq` to the canonical `sop__proj_eq` on both sides, because D20 refuses a non-canonical id on purpose |
|
||||
| `server/smoketest.py` | **27/27** after each of T16.1, T16.2, T16.3 |
|
||||
| `server/seed_demo.py` | Seeds the demo project; its SOP lands on the canonical id |
|
||||
| `pip-audit -r server/requirements.txt` | No known vulnerabilities |
|
||||
|
||||
**Browser probes, run 2026-10-02 on Matt's machine (Windows, Edge 154,
|
||||
Python 3.12.10, `.venv`), one at a time:**
|
||||
|
||||
| Probe | Task | Result |
|
||||
|---|---|---|
|
||||
| `tests/files_check.py` | T16.2 | **36/36**, including the 1440px creator, 390px Field View, and the offline drawing cache with the server gone |
|
||||
| `tests/sample_check.py` | T16.3 | **10/10** |
|
||||
| `tests/critical_reopen_check.py` | T16.3 | **11/11**, PM + CM + assignee mail, sink-verified |
|
||||
|
||||
T16.2 and T16.3 are done.
|
||||
|
||||
**Production data check for D20, 2026-10-02.** On the production database
|
||||
(`wp_db`, psql): projects with more than one complete SOP: **0 rows**. D20 has
|
||||
no legacy duplicates to handle on deploy.
|
||||
|
||||
**Harness fix needed to get there (`tests/cdp.py`, committed with this record).**
|
||||
Every probe failed first with "browser would not start ... exited rc=0
|
||||
without binding the port". Cause: the PowerShell session was elevated, and
|
||||
Edge and Chrome de-elevate by relaunching themselves through Explorer as a
|
||||
normal user; the relaunch drops `--remote-debugging-port`. Edge's log said so
|
||||
(`Edge is running elevated: 1`). No browser policy was involved (all hives
|
||||
checked). Two changes, both inert on a normal shell and on Linux/macOS:
|
||||
`--do-not-de-elevate` on Windows, and a 15-second grace for the port when the
|
||||
launched process exits rc=0. Probe results are unchanged by it; it only lets
|
||||
the browser start.
|
||||
@@ -58,7 +58,7 @@
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<body data-wp-no-sync-badge>
|
||||
|
||||
<!-- SHARED DARK APP BAR -->
|
||||
<header class="wp-appbar">
|
||||
|
||||
@@ -163,6 +163,31 @@
|
||||
}
|
||||
}
|
||||
|
||||
// D27. The offline sign-in window is the SERVER session, not a timer of the
|
||||
// browser's own. /api/auth/me returns `session_expires_at` (the session token's
|
||||
// exp), and that is what the cache keeps. Only a good answer from the server
|
||||
// changes it: loading a page offline, or saving preferences, never extends it.
|
||||
// (This replaced a fixed 12 hour window that slid forward on every page load and
|
||||
// disagreed with AUTH_SESSION_HOURS.)
|
||||
function readAuthCache() {
|
||||
try { return JSON.parse(localStorage.getItem('wp_auth_cache') || 'null'); } catch (e) { return null; }
|
||||
}
|
||||
function writeAuthCache(user, expMs) {
|
||||
try { if (user) localStorage.setItem('wp_auth_cache', JSON.stringify({ user: user, exp: expMs || 0, at: Date.now() })); } catch (e) {}
|
||||
}
|
||||
// For callers that refresh the cached user (the preferences dialog): the user
|
||||
// changes, the end of the session does not.
|
||||
window.wpCacheAuth = function (user) {
|
||||
var c = readAuthCache();
|
||||
writeAuthCache(user, c && c.exp);
|
||||
};
|
||||
|
||||
// 'online' (a good answer from the server), 'offline' (opened from the cache
|
||||
// before the session ended) or 'readqueue' (opened from the cache after it ended:
|
||||
// cached data can be read and changes are queued, but nothing is sent until a
|
||||
// good sign-in). The sync badge shows the one line for 'readqueue'.
|
||||
window.WP_SESSION_MODE = 'online';
|
||||
|
||||
fetch('/api/auth/me', { headers: { 'Accept': 'application/json' } })
|
||||
.then(function (r) {
|
||||
if (r.status === 401 || r.status === 403) { try { localStorage.removeItem('wp_auth_cache'); } catch (e) {} goToLogin(); return; }
|
||||
@@ -171,17 +196,26 @@
|
||||
var user = data && data.user;
|
||||
// Remember the last good auth so the PWA can open offline. The server is
|
||||
// still the real gate; offline writes queue in the outbox until reconnect.
|
||||
try { if (user) localStorage.setItem('wp_auth_cache', JSON.stringify({ user: user, at: Date.now() })); } catch (e) {}
|
||||
var expMs = data && data.session_expires_at ? Date.parse(data.session_expires_at) : 0;
|
||||
writeAuthCache(user, isNaN(expMs) ? 0 : expMs);
|
||||
proceed(user);
|
||||
});
|
||||
})
|
||||
.catch(function () {
|
||||
// Offline / API unreachable: fall back to a recent cached auth if present,
|
||||
// so the app (and the field view) still open without a network.
|
||||
try {
|
||||
var c = JSON.parse(localStorage.getItem('wp_auth_cache') || 'null');
|
||||
if (c && c.user && (Date.now() - (c.at || 0)) < 12 * 3600 * 1000) { proceed(c.user); return; }
|
||||
} catch (e) {}
|
||||
// Offline / API unreachable: open from the cached sign-in so the app (and the
|
||||
// field view) still work without a network. With no cache this device has
|
||||
// never signed in, so there is nothing to open: go to the sign-in page.
|
||||
var c = readAuthCache();
|
||||
if (c && c.user) {
|
||||
// Before the session ends: open as normal. After it (or when the cache has
|
||||
// no end time, an older cache): read and queue. The page does NOT redirect,
|
||||
// because login.html cannot reach Okta without a network.
|
||||
var live = !!c.exp && Date.now() < c.exp;
|
||||
window.WP_SESSION_MODE = live ? 'offline' : 'readqueue';
|
||||
proceed(c.user);
|
||||
try { document.dispatchEvent(new CustomEvent('wp-session-mode', { detail: window.WP_SESSION_MODE })); } catch (e) {}
|
||||
return;
|
||||
}
|
||||
goToLogin();
|
||||
});
|
||||
})();
|
||||
|
||||
@@ -245,25 +245,64 @@
|
||||
// local cache still updates immediately, so rendering never waits on the network.
|
||||
var OUTBOX_KEY = 'wp_sync_outbox_v1';
|
||||
var _flushTimer = null, _backoff = 0, _flushing = false;
|
||||
// D26. `_held` is set when the server answers 401: the session ended, so the
|
||||
// queue waits for a good sign-in. It is cleared ONLY by `wp-auth-ready` (a page
|
||||
// load with a good session check), never by the online event, the timer or tab
|
||||
// focus, because none of those prove the session is back.
|
||||
var _held = false;
|
||||
// D26. Changes the device could not store (localStorage full) stay here, in page
|
||||
// memory, and are still sent. They are lost if the page closes, so the badge says
|
||||
// they are not saved on this device. They are never dropped without a word.
|
||||
var _mem = [];
|
||||
var _deviceFull = false;
|
||||
|
||||
function qRead() { try { return JSON.parse(localStorage.getItem(OUTBOX_KEY) || '[]') || []; } catch (e) { return []; } }
|
||||
function qWrite(list) { try { localStorage.setItem(OUTBOX_KEY, JSON.stringify(list)); } catch (e) {} }
|
||||
// Returns true when the list is stored. A storage error used to be swallowed here,
|
||||
// so a full device lost the change and nobody knew (D26, fault 2).
|
||||
function qWrite(list) {
|
||||
try { localStorage.setItem(OUTBOX_KEY, JSON.stringify(list)); return true; }
|
||||
catch (e) { return false; }
|
||||
}
|
||||
// Stored ops plus any held only in memory, stored ones first (send order).
|
||||
function qAll() {
|
||||
var q = qRead();
|
||||
if (!_mem.length) return q;
|
||||
var ids = {};
|
||||
q.forEach(function (o) { ids[o.opId] = true; });
|
||||
return q.concat(_mem.filter(function (o) { return !ids[o.opId]; }));
|
||||
}
|
||||
// Remove one op everywhere it can be held.
|
||||
function qDrop(opId) {
|
||||
_mem = _mem.filter(function (o) { return o.opId !== opId; });
|
||||
qWrite(qRead().filter(function (o) { return o.opId !== opId; }));
|
||||
}
|
||||
function readQueueMode() { try { return window.WP_SESSION_MODE === 'readqueue'; } catch (e) { return false; } }
|
||||
function sameUser(a, b) { return String(a || '').toLowerCase() === String(b || '').toLowerCase(); }
|
||||
|
||||
// Append an op, coalescing by (kind,key) so only the latest write per entity is
|
||||
// queued. A delete supersedes any pending upsert for the same id.
|
||||
// queued. A delete supersedes any pending upsert for the same id. Returns true
|
||||
// when the change is stored on this device, false when it is only in memory.
|
||||
function enqueue(op) {
|
||||
var q = qRead();
|
||||
if (op.kind === 'wp-del') {
|
||||
q = q.filter(function (o) { return !(o.key === op.key && (o.kind === 'wp' || o.kind === 'wp-del')); });
|
||||
} else {
|
||||
q = q.filter(function (o) { return !(o.kind === op.kind && o.key === op.key); });
|
||||
}
|
||||
var supersedes = (op.kind === 'wp-del')
|
||||
? function (o) { return o.key === op.key && (o.kind === 'wp' || o.kind === 'wp-del'); }
|
||||
: function (o) { return o.kind === op.kind && o.key === op.key; };
|
||||
var q = qRead().filter(function (o) { return !supersedes(o); });
|
||||
_mem = _mem.filter(function (o) { return !supersedes(o); });
|
||||
op.opId = op.kind + ':' + op.key + ':' + Date.now().toString(36) + Math.random().toString(36).slice(2, 6);
|
||||
op.tries = 0;
|
||||
op.owner = currentUser(); // D26: sent only when this person is signed in
|
||||
q.push(op);
|
||||
qWrite(q);
|
||||
var stored = qWrite(q);
|
||||
if (stored) {
|
||||
// Room again: bring anything held in memory onto the device.
|
||||
if (_mem.length && qWrite(q.concat(_mem))) _mem = [];
|
||||
_deviceFull = _mem.length > 0;
|
||||
} else {
|
||||
_mem.push(op); _deviceFull = true;
|
||||
}
|
||||
notifySync();
|
||||
scheduleFlush(0);
|
||||
return stored;
|
||||
}
|
||||
|
||||
function opRequest(op) {
|
||||
@@ -275,41 +314,65 @@
|
||||
});
|
||||
}
|
||||
|
||||
// Apply `fn` to the op with this id, wherever it is held (device or memory).
|
||||
function patchOp(opId, fn) {
|
||||
var q = qRead(), hit = false;
|
||||
for (var i = 0; i < q.length; i++) { if (q[i].opId === opId) { fn(q[i]); hit = true; break; } }
|
||||
if (hit) qWrite(q);
|
||||
for (var j = 0; j < _mem.length; j++) { if (_mem[j].opId === opId) { fn(_mem[j]); break; } }
|
||||
}
|
||||
function bumpTries(opId, err) {
|
||||
var q = qRead();
|
||||
for (var i = 0; i < q.length; i++) { if (q[i].opId === opId) { q[i].tries = (q[i].tries || 0) + 1; q[i].lastErr = err; break; } }
|
||||
qWrite(q);
|
||||
patchOp(opId, function (o) { o.tries = (o.tries || 0) + 1; o.lastErr = err; });
|
||||
}
|
||||
// Permanently-failed op (a 4xx client error) — keep it for visibility but stop
|
||||
// retrying, so a rejected write can't loop forever. `err` is the server's own
|
||||
// explanation when it sent one; it is what the sync badge shows the user.
|
||||
function markDead(opId, err) {
|
||||
var q = qRead();
|
||||
for (var i = 0; i < q.length; i++) { if (q[i].opId === opId) { q[i].dead = true; q[i].lastErr = err; break; } }
|
||||
qWrite(q);
|
||||
patchOp(opId, function (o) { o.dead = true; o.lastErr = err; });
|
||||
}
|
||||
|
||||
// Attempt every live op; successes are removed, 4xx client errors are marked
|
||||
// dead (won't succeed on retry), transient failures (429/5xx/network) stay queued.
|
||||
// True when this op is waiting for another person to sign in (D26). An op with no
|
||||
// owner (queued before the sign-in check finished) can be sent by anyone, which is
|
||||
// what every op did before owners existed.
|
||||
function foreignOp(o) {
|
||||
var me = currentUser();
|
||||
return !!o.owner && !sameUser(o.owner, me);
|
||||
}
|
||||
function liveOps() { return qAll().filter(function (o) { return !o.dead; }); }
|
||||
function sendableOps() { return liveOps().filter(function (o) { return !foreignOp(o); }); }
|
||||
|
||||
// Attempt every live op that belongs to the signed-in person; successes are
|
||||
// removed. A 401 HOLDS the queue (D26): the session ended, which says nothing
|
||||
// about the changes, so nothing is marked dead and no try is counted. A 403 or
|
||||
// other 4xx is marked dead (it will not succeed on retry); transient failures
|
||||
// (429/5xx/network) stay queued.
|
||||
function flush() {
|
||||
if (_flushing) return Promise.resolve();
|
||||
var q = qRead().filter(function (o) { return !o.dead; });
|
||||
// D27: opened offline after the session ended (read and queue). Changes are kept
|
||||
// and wait; nothing is sent until a page load gets a good answer from the server.
|
||||
if (_held || readQueueMode()) { notifySync(); return Promise.resolve(); }
|
||||
var q = sendableOps();
|
||||
if (!q.length) { notifySync(); return Promise.resolve(); }
|
||||
_flushing = true; notifySync();
|
||||
var chain = Promise.resolve(), anyFail = false;
|
||||
var chain = Promise.resolve(), anyFail = false, halted = false;
|
||||
q.forEach(function (op) {
|
||||
chain = chain.then(function () {
|
||||
if (halted) return; // a 401 stops the run; later ops keep their place
|
||||
return opRequest(op).then(function (r) {
|
||||
var status = r ? r.status : 0;
|
||||
var done = r && (r.ok || (op.kind === 'wp-del' && status === 404)); // 404 on delete = already gone
|
||||
if (done) { qWrite(qRead().filter(function (o) { return o.opId !== op.opId; })); }
|
||||
if (done) { qDrop(op.opId); }
|
||||
else if (status === 401) { halted = true; _held = true; }
|
||||
else if (status >= 400 && status < 500 && status !== 429) {
|
||||
// Refused once, refused forever — so the only useful thing left is the
|
||||
// reason. A 409 here is the archived-project gate, whose detail tells
|
||||
// the user the project is read-only and how to get it unarchived; a
|
||||
// bare "HTTP 409" would leave them staring at a change that vanished.
|
||||
return r.json().catch(function () { return null; }).then(function (j) {
|
||||
var why = (j && typeof j.detail === 'string' && j.detail) || ('HTTP ' + status);
|
||||
var detail = (j && typeof j.detail === 'string' && j.detail) || '';
|
||||
var why = status === 403
|
||||
? 'You no longer have access to this project.' + (detail ? ' (' + detail + ')' : '')
|
||||
: (detail || ('HTTP ' + status));
|
||||
markDead(op.opId, why);
|
||||
});
|
||||
}
|
||||
@@ -320,7 +383,8 @@
|
||||
return chain.then(function () {
|
||||
_flushing = false;
|
||||
notifySync();
|
||||
if (qRead().filter(function (o) { return !o.dead; }).length) {
|
||||
if (_held) return; // wait for wp-auth-ready; no timer retries a dead session
|
||||
if (sendableOps().length) {
|
||||
_backoff = anyFail ? Math.min((_backoff || 5000) * 2, 60000) : 0;
|
||||
scheduleFlush(_backoff || 15000);
|
||||
} else { _backoff = 0; }
|
||||
@@ -336,14 +400,21 @@
|
||||
// `failed` stays the total not-getting-through count (what listeners already
|
||||
// read); `dead` splits out the ops the server has permanently refused, with the
|
||||
// first reason it gave, because those two states need different words.
|
||||
// D26 adds: `held` (the session ended; the queue waits for a sign-in), `others`
|
||||
// and `otherOwner` (changes that belong to someone else, waiting for them),
|
||||
// `deviceFull` and `unsaved` (changes that exist only in page memory).
|
||||
function syncCounts() {
|
||||
var q = qRead(), pending = 0, failed = 0, dead = 0, reason = '';
|
||||
var q = qAll(), pending = 0, failed = 0, dead = 0, reason = '', others = 0, otherOwner = '';
|
||||
for (var i = 0; i < q.length; i++) {
|
||||
if (q[i].dead) { dead++; if (!reason && q[i].lastErr) reason = String(q[i].lastErr); }
|
||||
else if (foreignOp(q[i])) { others++; if (!otherOwner) otherOwner = String(q[i].owner); }
|
||||
else if ((q[i].tries || 0) >= 3) failed++;
|
||||
else pending++;
|
||||
}
|
||||
return { pending: pending, failed: failed + dead, dead: dead, reason: reason, syncing: _flushing };
|
||||
return {
|
||||
pending: pending, failed: failed + dead, dead: dead, reason: reason, syncing: _flushing,
|
||||
held: _held, others: others, otherOwner: otherOwner, deviceFull: _deviceFull, unsaved: _mem.length
|
||||
};
|
||||
}
|
||||
ProjectData.syncStatus = syncCounts;
|
||||
function notifySync() {
|
||||
@@ -352,63 +423,212 @@
|
||||
renderSyncBadge(c);
|
||||
}
|
||||
|
||||
// Tiny sync indicator (bottom-left). This was gated on being the top-level
|
||||
// window so it was not drawn twice - once by the suite page and again inside
|
||||
// the embedded creator. B7/T7.1 dissolved that frame, so there is one document
|
||||
// and one badge. The 'storage' listener below stays: it is what keeps two
|
||||
// TABS in step, which is a different thing and still happens.
|
||||
var _badgeHideTimer = null;
|
||||
// The sync badge (D26, BL-061). It reports the OUTBOX: whether saved records have
|
||||
// reached the project. This was gated on being the top-level window so it was not
|
||||
// drawn twice (the suite page and the embedded creator); B7/T7.1 dissolved that
|
||||
// frame, so there is one document and one badge. The 'storage' listener below
|
||||
// stays: it is what keeps two TABS in step, which is a different thing.
|
||||
var _badgeHideTimer = null, _badgePlaceTimer = null, _hadWork = false;
|
||||
// BL-011 (fixed at T9.9): the badge used to mount on the FIRST SYNC EVENT,
|
||||
// which is async, so the three fixed overlays on the SOP page landed in a
|
||||
// different DOM order run to run and every index-keyed comparison saw
|
||||
// phantom diffs. Mounting the (hidden) holder at DOMContentLoaded puts the
|
||||
// three in script order, deterministically.
|
||||
document.addEventListener('DOMContentLoaded', function () { renderSyncBadge(null); });
|
||||
|
||||
// BL-061: the console pages (Admin, User Directory) never queue project work, and
|
||||
// the badge used to sit over their bottom-right buttons. They opt out with
|
||||
// data-wp-no-sync-badge on <body>.
|
||||
function badgeExempt() { return !!(document.body && document.body.hasAttribute('data-wp-no-sync-badge')); }
|
||||
|
||||
// Discard the changes the project refused. The person has read the reason and
|
||||
// chose to dismiss it; nothing else ever removes a queued change by itself.
|
||||
function dismissRejected() {
|
||||
_mem = _mem.filter(function (o) { return !o.dead; });
|
||||
qWrite(qRead().filter(function (o) { return !o.dead; }));
|
||||
notifySync();
|
||||
}
|
||||
function signInUrl() {
|
||||
return 'login.html?next=' + encodeURIComponent(location.pathname + location.search);
|
||||
}
|
||||
|
||||
// One place decides what the badge says. B5: every string names the PROJECT, so it
|
||||
// cannot be read as the draft indicator (WPAutosave.mountIndicator), which speaks
|
||||
// for the form. tone is one of neutral, ok, warn, error.
|
||||
function badgeModel(c) {
|
||||
var n;
|
||||
if (c.deviceFull) {
|
||||
n = c.unsaved;
|
||||
return { tone: 'error', text: '✕ ' + n + ' change' + (n === 1 ? ' is' : 's are') + ' not saved on this device. Keep this page open until it is sent to the project.' };
|
||||
}
|
||||
if (readQueueMode()) {
|
||||
return { tone: 'warn', text: 'Working offline. Sign in again when you have a signal to send your changes to the project.', signIn: true };
|
||||
}
|
||||
if (c.held) {
|
||||
return { tone: 'warn', text: '⚠ Sign in again to send your changes to the project. They are kept on this device.', signIn: true };
|
||||
}
|
||||
if (c.dead) {
|
||||
return { tone: 'error', text: '✕ ' + c.dead + ' change' + (c.dead === 1 ? '' : 's') + ' rejected by the project. Not saved.', reason: c.reason, dismiss: true };
|
||||
}
|
||||
if (c.failed) {
|
||||
return { tone: 'warn', text: '⚠ ' + c.failed + ' change' + (c.failed === 1 ? '' : 's') + ' not yet sent to the project. Retrying.' };
|
||||
}
|
||||
if (c.pending) {
|
||||
return { tone: 'neutral', text: '↻ Sending ' + c.pending + ' change' + (c.pending === 1 ? '' : 's') + ' to the project…' };
|
||||
}
|
||||
if (c.others) {
|
||||
return { tone: 'warn', text: '⚠ ' + c.others + ' change' + (c.others === 1 ? ' belongs' : 's belong') + ' to ' + c.otherOwner + '. ' + (c.others === 1 ? 'It goes' : 'They go') + ' to the project when ' + c.otherOwner + ' signs in.' };
|
||||
}
|
||||
// Nothing to report. "Everything sent" follows real work only: on a page that
|
||||
// never queued anything it is noise that sits over the controls (BL-061).
|
||||
if (!_hadWork) return null;
|
||||
return { tone: 'ok', text: '✓ Everything sent to the project', autoHide: true };
|
||||
}
|
||||
|
||||
var TONES = {
|
||||
neutral: ['var(--cds-text-secondary)', 'var(--cds-border-subtle)', 'var(--cds-layer)'],
|
||||
ok: ['var(--wp-status-success-text)', 'var(--wp-status-success-border-a)', 'var(--wp-status-success-bg)'],
|
||||
warn: ['var(--wp-status-warning-text)', 'var(--cds-support-warning)', 'var(--wp-status-warning-bg)'],
|
||||
error: ['var(--wp-status-error-text)', 'var(--wp-status-error-border-a)', 'var(--wp-status-error-bg)']
|
||||
};
|
||||
|
||||
function ensureBadgeStyle() {
|
||||
if (document.getElementById('wp-sync-badge-style')) return;
|
||||
var st = document.createElement('style');
|
||||
st.id = 'wp-sync-badge-style';
|
||||
// Controls are real <a> and <button> elements: reachable by keyboard and touch,
|
||||
// at least 44px high, with a focus ring from the shared focus token.
|
||||
st.textContent =
|
||||
'#wp-sync-badge{max-width:min(420px,calc(100vw - 24px));box-sizing:border-box}' +
|
||||
'@media (max-width:480px){#wp-sync-badge{max-width:min(300px,calc(100vw - 24px))}}' +
|
||||
'#wp-sync-badge .wp-sb-msg{display:flex;flex-direction:column;gap:2px;min-width:0}' +
|
||||
'#wp-sync-badge .wp-sb-reason{font-weight:400}' +
|
||||
'#wp-sync-badge .wp-sb-ctl{display:inline-flex;align-items:center;justify-content:center;box-sizing:border-box;' +
|
||||
'min-width:44px;min-height:44px;padding:0 10px;margin:-6px -12px -6px -4px;border:0;background:transparent;color:inherit;' +
|
||||
'font-family:inherit;font-weight:600;font-size:12px;line-height:1.3;text-decoration:underline;cursor:pointer}' +
|
||||
'#wp-sync-badge .wp-sb-ctl:focus-visible{outline:2px solid var(--cds-focus);outline-offset:-2px}';
|
||||
(document.head || document.documentElement).appendChild(st);
|
||||
}
|
||||
|
||||
// BL-061: keep the badge clear of page controls. Measure every visible control,
|
||||
// then put the badge in the first free spot: the four corners first, then any gap
|
||||
// on the screen that is big enough, scanning from the bottom up. Page headers and
|
||||
// bottom bars fill the width at 390px, so fixed corners alone cannot clear them.
|
||||
// If no spot is free, take the one that covers the fewest controls.
|
||||
var CONTROLS = 'a[href],button,input,select,textarea,summary,[role="button"],[tabindex]:not([tabindex="-1"])';
|
||||
function controlRects(el) {
|
||||
var out = [], list = document.querySelectorAll(CONTROLS);
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
var o = list[i];
|
||||
if (o === el || el.contains(o)) continue;
|
||||
var b = o.getBoundingClientRect();
|
||||
if (!b.width || !b.height || !o.getClientRects().length) continue;
|
||||
var cs = window.getComputedStyle(o);
|
||||
if (cs.visibility === 'hidden' || cs.display === 'none') continue;
|
||||
out.push(b);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
function hitCount(rects, x, y, w, h) {
|
||||
var n = 0;
|
||||
for (var i = 0; i < rects.length; i++) {
|
||||
var b = rects[i];
|
||||
if (b.left < x + w && b.right > x && b.top < y + h && b.bottom > y) n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
function placeBadge(el) {
|
||||
if (el.style.display === 'none') return;
|
||||
var vw = document.documentElement.clientWidth, vh = window.innerHeight;
|
||||
// Measure at the left edge, where the box has all the room it will ever have,
|
||||
// then pin that width: a box measured near the right edge wraps and grows.
|
||||
el.style.width = ''; el.style.right = 'auto'; el.style.bottom = 'auto';
|
||||
el.style.left = '0px'; el.style.top = '0px';
|
||||
var r = el.getBoundingClientRect(), w = Math.ceil(r.width), h = Math.ceil(r.height), m = 12;
|
||||
el.style.width = w + 'px';
|
||||
var rects = controlRects(el);
|
||||
var spots = [
|
||||
[vw - w - m, vh - h - m], [m, vh - h - m], [vw - w - m, m], [m, m]
|
||||
];
|
||||
// Then every gap, bottom to top, right to left, in 8px steps.
|
||||
for (var y = vh - h - m; y >= m; y -= 8) {
|
||||
for (var x = vw - w - m; x >= m; x -= 8) spots.push([x, y]);
|
||||
}
|
||||
var best = spots[0], bestN = Infinity;
|
||||
for (var i = 0; i < spots.length; i++) {
|
||||
var n = hitCount(rects, spots[i][0], spots[i][1], w, h);
|
||||
if (n < bestN) { bestN = n; best = spots[i]; }
|
||||
if (n === 0) break;
|
||||
}
|
||||
el.style.right = 'auto'; el.style.bottom = 'auto';
|
||||
el.style.left = Math.max(0, Math.round(best[0])) + 'px';
|
||||
el.style.top = Math.max(0, Math.round(best[1])) + 'px';
|
||||
}
|
||||
|
||||
function renderSyncBadge(c) {
|
||||
if (!document.body) return;
|
||||
var el = document.getElementById('wp-sync-badge');
|
||||
if (!el) {
|
||||
ensureBadgeStyle();
|
||||
el = document.createElement('div');
|
||||
el.id = 'wp-sync-badge';
|
||||
// S10. Polite: this reports background syncing, and interrupting someone to
|
||||
// say a queue drained is exactly the noise that gets aria-live turned off.
|
||||
el.setAttribute('role', 'status');
|
||||
el.style.cssText = 'position:fixed;right:12px;bottom:12px;z-index:9998;pointer-events:none;display:none;align-items:center;gap:7px;' +
|
||||
el.style.cssText = 'position:fixed;right:12px;bottom:12px;z-index:9998;pointer-events:none;display:none;align-items:center;gap:12px;' +
|
||||
'font:500 12px/1.3 "IBM Plex Sans",-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;' +
|
||||
'padding:6px 12px;border:1px solid var(--cds-border-subtle);background:var(--cds-layer);color:var(--cds-text-secondary);box-shadow:0 1px 4px rgba(0,0,0,.12);transition:opacity .2s;';
|
||||
document.body.appendChild(el);
|
||||
window.addEventListener('resize', function () { placeBadge(el); });
|
||||
}
|
||||
if (!c) return; // the eager DOMContentLoaded mount: holder only, no state yet
|
||||
if (_badgeHideTimer) { clearTimeout(_badgeHideTimer); _badgeHideTimer = null; }
|
||||
// A dead op is a refusal, not a hiccup — "retrying" would be a lie, and the
|
||||
// reason is the only thing that tells the user what to do (e.g. the project is
|
||||
// archived). Stack it under the headline; the badge never hides in this state.
|
||||
el.style.flexDirection = c.dead ? 'column' : 'row';
|
||||
el.style.alignItems = c.dead ? 'flex-start' : 'center';
|
||||
el.style.maxWidth = c.dead ? 'min(340px, calc(100vw - 32px))' : 'none';
|
||||
// B5. This badge reports the OUTBOX — whether saved records have reached the
|
||||
// project — and it used to say "✓ All changes saved", which is what a draft
|
||||
// autosave says. So the app was already making the promise B5 says it does not
|
||||
// keep: the badge went green when the queue emptied, whether or not anything in
|
||||
// the form had been saved at all.
|
||||
//
|
||||
// Every string here now names the project explicitly. The draft indicator
|
||||
// (WPAutosave.mountIndicator) is the one that speaks for the form, and the two
|
||||
// can no longer be read as each other.
|
||||
if (c.dead) {
|
||||
el.innerHTML = '<span>✕ ' + c.dead + ' change' + (c.dead === 1 ? '' : 's') + ' rejected by the project — not saved</span>' +
|
||||
(c.reason ? '<span style="font-weight:400">' + esc(c.reason) + '</span>' : '');
|
||||
el.style.color = 'var(--wp-status-error-text)'; el.style.borderColor = 'var(--wp-status-error-border-a)'; el.style.background = 'var(--wp-status-error-bg)'; el.style.display = 'inline-flex';
|
||||
} else if (c.failed) {
|
||||
el.textContent = '⚠ ' + c.failed + ' change' + (c.failed === 1 ? '' : 's') + ' not yet sent to the project — retrying';
|
||||
el.style.color = 'var(--wp-status-warning-text)'; el.style.borderColor = 'var(--cds-support-warning)'; el.style.background = 'var(--wp-status-warning-bg)'; el.style.display = 'inline-flex';
|
||||
} else if (c.pending) {
|
||||
el.textContent = '↻ Sending ' + c.pending + ' change' + (c.pending === 1 ? '' : 's') + ' to the project…';
|
||||
el.style.color = 'var(--cds-text-secondary)'; el.style.borderColor = 'var(--cds-border-subtle)'; el.style.background = 'var(--cds-layer)'; el.style.display = 'inline-flex';
|
||||
} else {
|
||||
el.textContent = '✓ Everything sent to the project';
|
||||
el.style.color = 'var(--wp-status-success-text)'; el.style.borderColor = 'var(--wp-status-success-border-a)'; el.style.background = 'var(--wp-status-success-bg)'; el.style.display = 'inline-flex';
|
||||
if (_badgePlaceTimer) { clearInterval(_badgePlaceTimer); _badgePlaceTimer = null; }
|
||||
if (c.pending || c.failed || c.held || c.others || c.deviceFull) _hadWork = true;
|
||||
var m = badgeExempt() ? null : badgeModel(c);
|
||||
if (!m) { el.style.display = 'none'; el.style.pointerEvents = 'none'; return; }
|
||||
|
||||
var t = TONES[m.tone] || TONES.neutral;
|
||||
el.style.color = t[0]; el.style.borderColor = t[1]; el.style.background = t[2];
|
||||
el.style.pointerEvents = (m.signIn || m.dismiss) ? 'auto' : 'none';
|
||||
// Rebuild the contents. The container keeps role="status", so a change of text
|
||||
// is announced; the controls inside it are ordinary focusable elements.
|
||||
while (el.firstChild) el.removeChild(el.firstChild);
|
||||
var msg = document.createElement('span');
|
||||
msg.className = 'wp-sb-msg';
|
||||
var head = document.createElement('span');
|
||||
head.textContent = m.text;
|
||||
msg.appendChild(head);
|
||||
if (m.reason) {
|
||||
var why = document.createElement('span');
|
||||
why.className = 'wp-sb-reason';
|
||||
why.textContent = String(m.reason);
|
||||
msg.appendChild(why);
|
||||
}
|
||||
el.appendChild(msg);
|
||||
if (m.signIn) {
|
||||
var a = document.createElement('a');
|
||||
a.className = 'wp-sb-ctl';
|
||||
a.href = signInUrl();
|
||||
a.textContent = 'Sign in';
|
||||
el.appendChild(a);
|
||||
}
|
||||
if (m.dismiss) {
|
||||
var d = document.createElement('button');
|
||||
d.type = 'button';
|
||||
d.className = 'wp-sb-ctl';
|
||||
d.textContent = 'Dismiss';
|
||||
d.addEventListener('click', dismissRejected);
|
||||
el.appendChild(d);
|
||||
}
|
||||
el.style.display = 'inline-flex';
|
||||
placeBadge(el);
|
||||
if (m.autoHide) {
|
||||
_hadWork = false; // the next "sent" needs new work
|
||||
_badgeHideTimer = setTimeout(function () { if (el) el.style.display = 'none'; }, 1800);
|
||||
} else {
|
||||
// A state that stays: re-check the spot now and then, since the page under it changes.
|
||||
_badgePlaceTimer = setInterval(function () { placeBadge(el); }, 2000);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -418,7 +638,12 @@
|
||||
window.addEventListener('online', function () { _backoff = 0; scheduleFlush(0); });
|
||||
window.addEventListener('storage', function (e) { if (e.key === OUTBOX_KEY) { notifySync(); scheduleFlush(0); } });
|
||||
document.addEventListener('visibilitychange', function () { if (!document.hidden) scheduleFlush(0); });
|
||||
setInterval(function () { if (qRead().filter(function (o) { return !o.dead; }).length) scheduleFlush(0); }, 20000);
|
||||
setInterval(function () { if (sendableOps().length) scheduleFlush(0); }, 20000);
|
||||
// D26: the one thing that releases a held queue. auth-guard fires this after a
|
||||
// good /api/auth/me (or the offline cache), and the next flush finds out for
|
||||
// itself whether the session is really good: a 401 holds the queue again.
|
||||
document.addEventListener('wp-session-mode', function () { notifySync(); });
|
||||
document.addEventListener('wp-auth-ready', function () { _held = false; _backoff = 0; notifySync(); scheduleFlush(0); });
|
||||
} catch (e) {}
|
||||
if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { notifySync(); scheduleFlush(0); }); }
|
||||
else { setTimeout(function () { notifySync(); scheduleFlush(0); }, 0); }
|
||||
@@ -428,7 +653,7 @@
|
||||
// project so re-completing updates the same row.
|
||||
ProjectData.pushSOP = function (projectId, sop, state) {
|
||||
if (!projectId) return Promise.resolve(null);
|
||||
enqueue({
|
||||
return Promise.resolve(enqueue({
|
||||
kind: 'sop', key: 'sop__' + projectId,
|
||||
body: {
|
||||
id: 'sop__' + projectId, project_id: projectId,
|
||||
@@ -436,22 +661,21 @@
|
||||
number: (sop && sop.project && sop.project.number) || '',
|
||||
complete: true, created_by: currentUser(), data: { sop: sop, state: state }
|
||||
}
|
||||
});
|
||||
return Promise.resolve(true);
|
||||
}));
|
||||
};
|
||||
|
||||
// Upsert a single Work Package. The local cache stays the source of truth for
|
||||
// immediate rendering; the outbox guarantees the write reaches the server.
|
||||
ProjectData.pushWP = function (p, projectId) {
|
||||
if (!p || !p.id) return Promise.resolve(null);
|
||||
enqueue({ kind: 'wp', key: p.id, body: pkgToServer(p, projectId) });
|
||||
return Promise.resolve(true);
|
||||
// D26: resolves false when the device could not store the change (it is still
|
||||
// sent from memory, but it is lost if the page closes first).
|
||||
return Promise.resolve(enqueue({ kind: 'wp', key: p.id, body: pkgToServer(p, projectId) }));
|
||||
};
|
||||
|
||||
ProjectData.removeWP = function (id) {
|
||||
if (!id) return Promise.resolve();
|
||||
enqueue({ kind: 'wp-del', key: id });
|
||||
return Promise.resolve(true);
|
||||
return Promise.resolve(enqueue({ kind: 'wp-del', key: id }));
|
||||
};
|
||||
|
||||
// Force a flush now and resolve when the queue drains (or a round-trip is done).
|
||||
|
||||
@@ -24,7 +24,10 @@
|
||||
The directory is one wide table, so the column exceptions live here: email is
|
||||
the one cell long enough to stretch a row, and the two role dropdowns need
|
||||
room for "Assistant Project Manager" without pushing Actions off screen. */
|
||||
#users-table table td:nth-child(3){ max-width:230px; overflow:hidden; text-overflow:ellipsis; }
|
||||
/* The email cell, by its class: CR-020's selection column moved it from third to
|
||||
fourth on the managers' table, and a position-based rule would have followed
|
||||
the wrong column. */
|
||||
#users-table table td.ell{ max-width:230px; overflow:hidden; text-overflow:ellipsis; }
|
||||
#users-banner:not(:empty), #scope-banner:not(:empty){ margin-bottom:var(--s3); }
|
||||
#nu-projects{ margin-top:var(--s2); }
|
||||
#nu-projects .pickrow{ padding:var(--s1) var(--s1); }
|
||||
@@ -33,9 +36,48 @@
|
||||
#nu-project-list{ max-height:200px; overflow:auto; border:1px solid var(--border); }
|
||||
.whoami-chip{ font-size:12px; color:var(--muted); }
|
||||
.whoami-chip strong{ color:var(--text); }
|
||||
|
||||
/* CR-020: row selection and the bulk bar. Tokens only (the token rule). */
|
||||
#sel-banner:not(:empty), #bulk-banner:not(:empty), #csv-banner:not(:empty){ margin-bottom:var(--s3); }
|
||||
#bulk-banner ul, #csv-banner ul{ margin:var(--s1) 0 0; padding-left:var(--s5); font-weight:400;
|
||||
max-height:220px; overflow:auto; }
|
||||
#csv-btn[hidden]{ display:none; }
|
||||
table.grid th.selcell, table.grid td.selcell{ width:1%; padding:0 var(--s1); text-align:center; }
|
||||
/* The label is the hit area: the 16px box alone is under the 24px floor. */
|
||||
.selhit{ display:inline-flex; align-items:center; justify-content:center;
|
||||
min-width:28px; min-height:28px; cursor:pointer; }
|
||||
.selhit input{ width:16px; height:16px; margin:0; accent-color:var(--accent); cursor:pointer; }
|
||||
.selhit input:focus-visible{ outline:2px solid var(--accent); outline-offset:2px; }
|
||||
.selhit input:disabled{ cursor:default; }
|
||||
/* Not --accent-soft: that is the admin pill's own fill (console.css, hover note). */
|
||||
table.grid tbody tr.is-selected td{ background:var(--cds-layer-selected); }
|
||||
table.grid tbody tr.is-selected td.selcell{ box-shadow:inset 3px 0 0 var(--accent); }
|
||||
.bulkbar{ border:1px solid var(--accent); border-left:3px solid var(--accent);
|
||||
background:var(--surface); padding:var(--s2) var(--s3); margin:0 0 var(--s3); }
|
||||
.bulkbar[hidden]{ display:none; }
|
||||
.bulkhead{ display:flex; align-items:center; justify-content:space-between; gap:var(--s2);
|
||||
flex-wrap:wrap; margin-bottom:var(--s2); }
|
||||
.bulkgroup{ display:flex; flex-wrap:wrap; align-items:center; gap:var(--s2);
|
||||
padding:var(--s2) 0; border-top:1px solid var(--border); }
|
||||
.bulkgroup > .bulklabel{ font-size:12px; font-weight:600; color:var(--muted); min-width:110px; }
|
||||
.bulkgroup select{ height:var(--ctl); padding:0 var(--s1) 0 var(--s2); font:inherit; font-size:13px;
|
||||
border:1px solid var(--border-strong); border-radius:0; background:var(--surface); color:var(--text);
|
||||
max-width:100%; }
|
||||
.bulkgroup select:focus-visible{ outline:2px solid var(--accent); outline-offset:-2px; }
|
||||
/* The bulk delete dialog lists every affected username; a long list scrolls
|
||||
inside the dialog instead of pushing its buttons off the screen. */
|
||||
#wp-dlg-msg{ max-height:50vh; overflow:auto; }
|
||||
@media (max-width:620px){
|
||||
.bulkgroup > .bulklabel{ min-width:0; flex:1 1 100%; }
|
||||
.bulkgroup select, .bulkgroup button{ flex:1 1 100%; }
|
||||
}
|
||||
@media (pointer:coarse){
|
||||
.selhit{ min-width:44px; min-height:44px; }
|
||||
.bulkbar button, .bulkgroup select{ min-height:44px; }
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<body data-wp-no-sync-badge>
|
||||
|
||||
<!-- SHARED DARK APP BAR -->
|
||||
<header class="wp-appbar">
|
||||
@@ -63,15 +105,33 @@
|
||||
<div class="sub" id="people-sub"></div>
|
||||
<div class="toolbar">
|
||||
<button onclick="loadUsers()">Refresh</button>
|
||||
<input id="user-search" placeholder="Search name / username / email / job function…" oninput="renderUsers()">
|
||||
<select id="user-filter" onchange="renderUsers()">
|
||||
<input id="user-search" placeholder="Search name / username / email / job function…" oninput="filterChanged()"
|
||||
aria-label="Search the directory">
|
||||
<select id="user-filter" onchange="filterChanged()" aria-label="Show">
|
||||
<option value="">Everyone</option>
|
||||
<option value="active">Active only</option>
|
||||
<option value="disabled">Disabled only</option>
|
||||
<option value="mine">Accounts I manage</option>
|
||||
</select>
|
||||
<!-- CR-020 (T12.4): a list of usernames from a file becomes the selection.
|
||||
Managers only; users.js shows it. -->
|
||||
<button type="button" id="csv-btn" onclick="pickCsv()" hidden
|
||||
title="A CSV with one username (or email) per line, first column. The action is chosen in the bulk bar.">Select from CSV…</button>
|
||||
<input type="file" id="csv-file" accept=".csv,.txt,text/csv,text/plain" hidden>
|
||||
</div>
|
||||
<div id="users-banner"></div>
|
||||
<div id="csv-banner"></div>
|
||||
<!-- CR-020: bulk actions on the selected rows. Present only while at least one
|
||||
row is selected (the hidden attribute also takes it out of the tab order). -->
|
||||
<div id="bulk-bar" class="bulkbar" role="region" aria-label="Bulk actions on the selected users" hidden>
|
||||
<div class="bulkhead">
|
||||
<strong id="bulk-count">0 selected</strong>
|
||||
<button type="button" onclick="clearSelection(true)">Clear selection</button>
|
||||
</div>
|
||||
<div id="bulk-actions"></div>
|
||||
</div>
|
||||
<div id="sel-banner"></div>
|
||||
<div id="bulk-banner"></div>
|
||||
<div id="users-table"><div class="note">Loading…</div></div>
|
||||
</div>
|
||||
|
||||
|
||||
316
html/users.js
316
html/users.js
@@ -23,6 +23,10 @@
|
||||
let _users = []; // the directory as the server scoped it
|
||||
let _scope = null; // GET /api/auth/user-scope
|
||||
let _meId = null;
|
||||
// CR-020: the rows checked for a bulk action. Client-side UI state only - every
|
||||
// bulk action is decided and audited by the server, row by row (T12.1).
|
||||
let _selected = new Set();
|
||||
let _shownSelectable = []; // ids the current filter shows AND this caller may select
|
||||
|
||||
// ── boot ──────────────────────────────────────────────────────────────────────
|
||||
async function boot(){
|
||||
@@ -40,6 +44,7 @@ async function boot(){
|
||||
renderScope();
|
||||
}
|
||||
renderCreateForm();
|
||||
renderBulkActions();
|
||||
loadUsers();
|
||||
}
|
||||
|
||||
@@ -90,11 +95,309 @@ async function loadUsers(){
|
||||
}
|
||||
banner('users-banner','', '');
|
||||
_users = json;
|
||||
// Fresh data: any earlier selection may name accounts that changed or are gone.
|
||||
clearSelection(false);
|
||||
renderUsers();
|
||||
}
|
||||
|
||||
function manages(){ return !!(_scope && _scope.can_manage_users); }
|
||||
|
||||
// ── selection (CR-020, T12.2) ─────────────────────────────────────────────────
|
||||
// A row is selectable when this caller may change the account. Your own row is
|
||||
// not: every bulk action that could touch it (disable, demote, delete) is refused
|
||||
// for your own account, so offering it would only produce a guaranteed failure.
|
||||
function selectable(u){ return manages() && !!u.manageable && u.id !== _meId; }
|
||||
|
||||
// The search box and the filter both decide which rows are shown. Changing either
|
||||
// clears the selection (decided 2026-10-06), so no row hidden by the filter can
|
||||
// stay selected and be acted on without being seen.
|
||||
function filterChanged(){
|
||||
const had = _selected.size;
|
||||
clearSelection(false);
|
||||
renderUsers();
|
||||
banner('sel-banner', '', had ? 'Selection cleared: the filter changed. Select the rows again.' : '');
|
||||
}
|
||||
|
||||
function clearSelection(announce){
|
||||
const had = _selected.size;
|
||||
_selected = new Set();
|
||||
updateSelectionUI();
|
||||
banner('sel-banner', '', (announce && had) ? 'Selection cleared.' : '');
|
||||
}
|
||||
|
||||
function setRowSelected(id, on){
|
||||
if(on) _selected.add(id); else _selected.delete(id);
|
||||
}
|
||||
|
||||
// Select-all acts on the rows the filter shows, never on the whole directory.
|
||||
function toggleSelectAll(on){
|
||||
_shownSelectable.forEach(id => setRowSelected(id, on));
|
||||
banner('sel-banner', '', '');
|
||||
updateSelectionUI();
|
||||
}
|
||||
|
||||
// Reflect _selected into the page without re-rendering the table, so keyboard
|
||||
// focus stays on the checkbox that was just used.
|
||||
function updateSelectionUI(){
|
||||
const wrap = document.getElementById('users-table');
|
||||
if(wrap){
|
||||
wrap.querySelectorAll('input.selbox').forEach(cb => {
|
||||
const on = _selected.has(cb.dataset.id);
|
||||
cb.checked = on;
|
||||
const tr = cb.closest('tr');
|
||||
if(tr) tr.classList.toggle('is-selected', on);
|
||||
});
|
||||
const all = document.getElementById('sel-all');
|
||||
if(all){
|
||||
const n = _shownSelectable.filter(id => _selected.has(id)).length;
|
||||
all.checked = n > 0 && n === _shownSelectable.length;
|
||||
all.indeterminate = n > 0 && n < _shownSelectable.length;
|
||||
all.disabled = !_shownSelectable.length;
|
||||
}
|
||||
}
|
||||
const bar = document.getElementById('bulk-bar');
|
||||
if(bar){
|
||||
const n = _selected.size;
|
||||
bar.hidden = n === 0;
|
||||
const c = document.getElementById('bulk-count');
|
||||
if(c) c.textContent = n + ' selected';
|
||||
}
|
||||
}
|
||||
|
||||
// One listener for every checkbox in the table (event delegation): no ids are
|
||||
// bound into inline handlers, so there is nothing to escape.
|
||||
document.addEventListener('change', e => {
|
||||
const t = e.target;
|
||||
if(!(t instanceof HTMLInputElement)) return;
|
||||
if(t.id === 'sel-all'){ toggleSelectAll(t.checked); return; }
|
||||
if(t.classList.contains('selbox')){
|
||||
setRowSelected(t.dataset.id, t.checked);
|
||||
banner('sel-banner', '', '');
|
||||
updateSelectionUI();
|
||||
}
|
||||
});
|
||||
|
||||
// ── bulk actions (CR-020, T12.3) ──────────────────────────────────────────────
|
||||
// The toolbar offers only what this caller may do: the roles they may grant, the
|
||||
// projects they administer, the per-project roles they may hand out. The server
|
||||
// re-checks every row anyway (T12.1); this is so nobody is offered a choice that
|
||||
// can only fail.
|
||||
function renderBulkActions(){
|
||||
const box = document.getElementById('bulk-actions');
|
||||
const csvBtn = document.getElementById('csv-btn');
|
||||
if(csvBtn) csvBtn.hidden = !manages();
|
||||
if(!box || !manages()) return;
|
||||
const grantable = PERM_ROLES.filter(r => (_scope.grantable_roles || []).indexOf(r) >= 0);
|
||||
const projects = (_scope.managed_projects || []).slice()
|
||||
.sort((a,b) => (a.archived?1:0) - (b.archived?1:0));
|
||||
const projRoles = PROJECT_SCOPED_ROLES.filter(r => (_scope.grantable_project_roles || []).indexOf(r) >= 0);
|
||||
const group = (label, inner) => '<div class="bulkgroup" role="group" aria-label="'+uesc(label)+'">'+
|
||||
'<span class="bulklabel">'+uesc(label)+'</span>'+inner+'</div>';
|
||||
box.innerHTML =
|
||||
group('Permissions',
|
||||
'<select id="bulk-role" aria-label="New permissions role">'+
|
||||
grantable.map(r => '<option value="'+r+'"'+(r==='project_user'?' selected':'')+'>'+uesc(PERM_LABELS[r])+'</option>').join('')+
|
||||
'</select>'+
|
||||
'<button type="button" onclick="bulkRole()">Set permissions</button>') +
|
||||
group('Sign-in',
|
||||
'<button type="button" onclick="bulkActive(true)">Enable</button>'+
|
||||
'<button type="button" onclick="bulkActive(false)">Disable</button>') +
|
||||
(projects.length ? group('Project access',
|
||||
'<select id="bulk-project" aria-label="Project">'+
|
||||
projects.map(p => '<option value="'+uesc(p.id)+'">'+uesc((p.name||'(unnamed)')+(p.number?' '+p.number:'')+
|
||||
(p.archived?' (archived)':''))+'</option>').join('')+
|
||||
'</select>'+
|
||||
'<select id="bulk-project-role" aria-label="Role on that project, for Add">'+
|
||||
'<option value="">Role: same as account</option>'+
|
||||
projRoles.map(r => '<option value="'+r+'">Role: '+uesc(PERM_LABELS[r])+' here</option>').join('')+
|
||||
'</select>'+
|
||||
'<button type="button" onclick="bulkProject(\'add\')">Add to project</button>'+
|
||||
'<button type="button" onclick="bulkProject(\'remove\')">Remove from project</button>') : '') +
|
||||
group('Delete',
|
||||
'<button type="button" class="danger" onclick="bulkDelete()">Delete selected…</button>');
|
||||
}
|
||||
|
||||
function selectedIds(){ return _users.filter(u => _selected.has(u.id)).map(u => u.id); }
|
||||
function people(n){ return n + ' user' + (n === 1 ? '' : 's'); }
|
||||
function selVal(id){ return (document.getElementById(id) || {}).value || ''; }
|
||||
function selText(id){
|
||||
const s = document.getElementById(id);
|
||||
return (s && s.selectedIndex >= 0) ? s.options[s.selectedIndex].text : '';
|
||||
}
|
||||
|
||||
// One path for every bulk action: confirm through the dialog kit with the exact
|
||||
// count, send, report row by row, reload. A reload clears the selection, which is
|
||||
// right: the rows may have changed underneath it.
|
||||
async function runBulk(path, body, ask){
|
||||
const ids = selectedIds();
|
||||
if(!ids.length) return;
|
||||
if(!(await wpConfirmDialog({title: ask.title, okLabel: ask.ok, message: ask.message(ids.length)}))) return;
|
||||
return sendBulk(path, body, ask.title, ids);
|
||||
}
|
||||
|
||||
async function sendBulk(path, body, title, ids){
|
||||
bannerHtml('bulk-banner', '', uesc(title)+': working on '+uesc(people(ids.length))+'…');
|
||||
const { status, json } = await api('POST', path, Object.assign({ user_ids: ids }, body));
|
||||
if(status !== 200 || !json || !Array.isArray(json.results)){
|
||||
bannerHtml('bulk-banner', 'bad', '✕ '+uesc(apiError(status, json, title+' failed'))+' Nothing was changed.');
|
||||
return;
|
||||
}
|
||||
showBulkResult(json, title);
|
||||
loadUsers();
|
||||
}
|
||||
|
||||
// T12.5: delete is permanent (a hard delete, as for one account) and one bad
|
||||
// multi-select now removes many, so it asks more than the other actions do:
|
||||
// every affected username is listed, and nothing is sent until the word DELETE
|
||||
// is typed, whatever the count (confirmed 2026-10-06). Your own account cannot
|
||||
// be selected; if it ever reached the request, the server refuses that row and
|
||||
// the report names it.
|
||||
async function bulkDelete(){
|
||||
const chosen = _users.filter(u => _selected.has(u.id));
|
||||
if(!chosen.length) return;
|
||||
const n = chosen.length;
|
||||
const typed = await wpPromptDialog({
|
||||
title: 'Delete '+people(n),
|
||||
message: 'Delete '+(n === 1 ? 'this account' : 'these '+n+' accounts')+'? Each one, and every project '+
|
||||
'assignment it has, is removed. This cannot be undone. Disable them instead if you only want to '+
|
||||
'block sign-in.\n\n'+chosen.map(u => '• '+u.username+(u.full_name ? ' ('+u.full_name+')' : '')).join('\n'),
|
||||
label: 'Type DELETE to confirm',
|
||||
value: '',
|
||||
okLabel: 'Delete '+people(n),
|
||||
validate: v => v === 'DELETE' ? '' : 'Type DELETE, in capital letters, to confirm.',
|
||||
});
|
||||
if(typed !== 'DELETE') return;
|
||||
return sendBulk('/api/auth/bulk-users/delete', {}, 'Delete accounts', chosen.map(u => u.id));
|
||||
}
|
||||
|
||||
// Every row is accounted for: changed, already so, or failed with the server's
|
||||
// reason. A batch that partly failed never reads as a success (CR-020).
|
||||
function showBulkResult(json, title){
|
||||
const failed = json.results.filter(r => r.status === 'failed');
|
||||
const parts = [json.ok+' changed'];
|
||||
if(json.unchanged) parts.push(json.unchanged+' already set');
|
||||
if(failed.length) parts.push(failed.length+' failed');
|
||||
const kind = !failed.length ? 'ok' : (json.ok + json.unchanged ? 'warn' : 'bad');
|
||||
const lead = (failed.length ? (kind === 'bad' ? '✕ ' : '⚠ ') : '✓ ')+uesc(title)+': '+parts.join(', ')+'.';
|
||||
const list = failed.length
|
||||
? '<ul>'+failed.map(r => '<li><strong>'+uesc(r.username || r.user_id)+'</strong>: '+uesc(r.reason)+'</li>').join('')+'</ul>'
|
||||
: '';
|
||||
bannerHtml('bulk-banner', kind, lead + list);
|
||||
}
|
||||
|
||||
function bannerHtml(id, kind, html){
|
||||
const el = document.getElementById(id);
|
||||
if(!el) return;
|
||||
el.className = 'banner' + (kind ? ' '+kind : '');
|
||||
el.innerHTML = html;
|
||||
}
|
||||
|
||||
function bulkRole(){
|
||||
const role = selVal('bulk-role');
|
||||
if(!role) return;
|
||||
return runBulk('/api/auth/bulk-users/role', { role }, {
|
||||
title: 'Set permissions', ok: 'Change permissions',
|
||||
message: n => 'Set permissions to '+(PERM_LABELS[role] || role)+' for '+people(n)+'?\n\n'+
|
||||
'Each account is checked on its own. Any that cannot be changed are listed afterwards.'});
|
||||
}
|
||||
|
||||
function bulkActive(on){
|
||||
return runBulk('/api/auth/bulk-users/active', { is_active: !!on }, {
|
||||
title: on ? 'Enable accounts' : 'Disable accounts', ok: on ? 'Enable' : 'Disable',
|
||||
message: n => (on ? 'Enable sign-in for ' : 'Disable sign-in for ')+people(n)+'?'+
|
||||
(on ? '' : '\n\nA disabled account cannot sign in. Nothing else about it changes.')});
|
||||
}
|
||||
|
||||
function bulkProject(op){
|
||||
const pid = selVal('bulk-project');
|
||||
if(!pid) return;
|
||||
const role = op === 'add' ? selVal('bulk-project-role') : '';
|
||||
const pname = selText('bulk-project');
|
||||
return runBulk('/api/auth/bulk-users/projects', { project_id: pid, op, role }, {
|
||||
title: op === 'add' ? 'Add to project' : 'Remove from project',
|
||||
ok: op === 'add' ? 'Add' : 'Remove',
|
||||
message: n => op === 'add'
|
||||
? 'Add '+people(n)+' to '+pname+' with '+(role ? PERM_LABELS[role]+' on that project' : 'their account role')+'?\n\n'+
|
||||
'Their other projects, and their roles there, stay as they are.'
|
||||
: 'Remove '+people(n)+' from '+pname+'?\n\nTheir other projects stay as they are.'});
|
||||
}
|
||||
|
||||
// ── CSV upload (CR-020, T12.4) ────────────────────────────────────────────────
|
||||
// The file holds usernames only (decided 2026-10-06). The server reads it and
|
||||
// answers line by line; the accepted accounts become the ordinary selection, so
|
||||
// the action, its confirmation and its row-by-row report are the same ones the
|
||||
// checkboxes use. A CSV cannot reach an account a checkbox could not: the bulk
|
||||
// route re-checks every row.
|
||||
const CSV_MAX_BYTES = 1024 * 1024;
|
||||
|
||||
// Blob.text() where the browser has it, FileReader where it does not (older
|
||||
// tablet browsers).
|
||||
function readFileText(file){
|
||||
if(typeof file.text === 'function') return file.text();
|
||||
return new Promise((resolve, reject) => {
|
||||
const r = new FileReader();
|
||||
r.onload = () => resolve(String(r.result || ''));
|
||||
r.onerror = () => reject(r.error);
|
||||
r.readAsText(file);
|
||||
});
|
||||
}
|
||||
|
||||
function pickCsv(){
|
||||
const f = document.getElementById('csv-file');
|
||||
if(f){ f.value = ''; f.click(); }
|
||||
}
|
||||
|
||||
document.addEventListener('change', e => {
|
||||
if(e.target && e.target.id === 'csv-file' && e.target.files && e.target.files[0]) loadCsv(e.target.files[0]);
|
||||
});
|
||||
|
||||
async function loadCsv(file){
|
||||
if(file.size > CSV_MAX_BYTES){
|
||||
bannerHtml('csv-banner', 'bad', '✕ '+uesc(file.name)+' is larger than 1 MB. Split it and load the parts one at a time.');
|
||||
return;
|
||||
}
|
||||
let text = '';
|
||||
try { text = await readFileText(file); }
|
||||
catch(_){ bannerHtml('csv-banner', 'bad', '✕ Could not read '+uesc(file.name)+'.'); return; }
|
||||
bannerHtml('csv-banner', '', 'Reading '+uesc(file.name)+'…');
|
||||
const { status, json } = await api('POST', '/api/auth/bulk-users/resolve', { text });
|
||||
if(status !== 200 || !json || !Array.isArray(json.rows)){
|
||||
bannerHtml('csv-banner', 'bad', '✕ '+uesc(apiError(status, json, 'Could not read the list')));
|
||||
return;
|
||||
}
|
||||
// Show everyone, so every accepted account is a visible, checked row: a
|
||||
// selection must never include a row the filter hides.
|
||||
const s = document.getElementById('user-search'), f = document.getElementById('user-filter');
|
||||
if(s) s.value = '';
|
||||
if(f) f.value = '';
|
||||
await loadUsers();
|
||||
const want = new Set(json.user_ids || []);
|
||||
_shownSelectable.forEach(id => { if(want.has(id)) _selected.add(id); });
|
||||
updateSelectionUI();
|
||||
const missed = [...want].filter(id => !_selected.has(id)).length;
|
||||
const rejected = json.rows.filter(r => r.status !== 'ok');
|
||||
const lead = (rejected.length || missed ? '⚠ ' : '✓ ')+uesc(file.name)+': '+json.read+' line'+(json.read===1?'':'s')+' read, '+
|
||||
_selected.size+' selected'+(rejected.length ? ', '+rejected.length+' rejected' : '')+'.'+
|
||||
(missed ? ' '+missed+' accepted account'+(missed===1?' is':'s are')+' no longer in the directory; reload and try again.' : '')+
|
||||
(_selected.size ? ' Pick an action in the bulk bar to apply it to the selected accounts.' : '');
|
||||
const list = rejected.length
|
||||
? '<ul>'+rejected.map(r => '<li>Line '+r.line+' ('+uesc(r.text)+'): '+uesc(r.reason)+'</li>').join('')+'</ul>'
|
||||
: '';
|
||||
bannerHtml('csv-banner', (rejected.length || missed) ? 'warn' : 'ok', lead + list);
|
||||
}
|
||||
|
||||
function selCell(u){
|
||||
if(!selectable(u)){
|
||||
const why = u.id === _meId ? 'Your own account: change it one setting at a time'
|
||||
: (u.manage_blocked_reason || 'You cannot change this account');
|
||||
return '<td class="selcell"><span class="selhit" title="'+uesc(why)+'">'+
|
||||
'<input type="checkbox" disabled aria-label="'+uesc('Cannot select '+u.username+': '+why)+'"></span></td>';
|
||||
}
|
||||
return '<td class="selcell"><label class="selhit">'+
|
||||
'<input type="checkbox" class="selbox" data-id="'+uesc(u.id)+'"'+(_selected.has(u.id)?' checked':'')+
|
||||
' aria-label="'+uesc('Select '+u.username)+'"></label></td>';
|
||||
}
|
||||
|
||||
function renderUsers(){
|
||||
const wrap = document.getElementById('users-table');
|
||||
const q = ((document.getElementById('user-search')||{}).value||'').trim().toLowerCase();
|
||||
@@ -116,7 +419,8 @@ function renderUsers(){
|
||||
(u.project_role||'')+' '+roleLabel(u.role)).toLowerCase().indexOf(q) >= 0;
|
||||
});
|
||||
const count = '<div class="note">'+list.length+' of '+total+' '+(total===1?'person':'people')+'</div>';
|
||||
if(!list.length){ wrap.innerHTML = count+'<div class="note">Nothing matches.</div>'; return; }
|
||||
_shownSelectable = list.filter(selectable).map(u => u.id);
|
||||
if(!list.length){ wrap.innerHTML = count+'<div class="note">Nothing matches.</div>'; updateSelectionUI(); return; }
|
||||
|
||||
const head = manages()
|
||||
? ['Username','Name','Email',
|
||||
@@ -130,9 +434,16 @@ function renderUsers(){
|
||||
'Status'];
|
||||
const ths = head.map(h => Array.isArray(h)
|
||||
? '<th title="'+uesc(h[1])+'">'+uesc(h[0])+'</th>' : '<th>'+uesc(h)+'</th>').join('');
|
||||
// CR-020: a selection column for anyone who manages accounts. Select-all names
|
||||
// how many rows it covers, because it covers only what the filter shows.
|
||||
const selTh = manages()
|
||||
? '<th class="selcell"><label class="selhit"><input type="checkbox" id="sel-all" aria-label="'+
|
||||
uesc('Select all '+_shownSelectable.length+' shown that you can change')+'"></label></th>'
|
||||
: '';
|
||||
const rows = list.map(manages() ? managerRow : readonlyRow).join('');
|
||||
wrap.innerHTML = count+'<div class="tscroll"><table class="grid"><thead><tr>'+ths+
|
||||
wrap.innerHTML = count+'<div class="tscroll"><table class="grid"><thead><tr>'+selTh+ths+
|
||||
'</tr></thead><tbody>'+rows+'</tbody></table></div>'+legend();
|
||||
updateSelectionUI();
|
||||
}
|
||||
|
||||
function legend(){
|
||||
@@ -189,6 +500,7 @@ function managerRow(u){
|
||||
if(!can && !me) actions.push('<span class="note" style="margin:0" title="'+uesc(why)+'">read-only</span>');
|
||||
|
||||
return '<tr'+(can||me ? '' : ' class="is-locked"')+'>'+
|
||||
selCell(u)+
|
||||
'<td><strong>'+uesc(u.username)+'</strong>'+(me?'<span class="me-tag">you</span>':'')+'</td>'+
|
||||
'<td>'+uesc(u.full_name||'')+'</td>'+
|
||||
// The address is truncated with the full value on the title: a long one used to
|
||||
|
||||
@@ -220,7 +220,8 @@
|
||||
.then(function (res) {
|
||||
if (!res.ok) { msg((res.j && res.j.detail) || ('Could not save (HTTP ' + res.status + ').'), false); return; }
|
||||
if (res.j && res.j.user) window.WP_USER = res.j.user;
|
||||
try { localStorage.setItem('wp_auth_cache', JSON.stringify({ user: window.WP_USER, at: Date.now() })); } catch (e) {}
|
||||
// D27: refresh the cached user, but not the end of the session.
|
||||
try { if (window.wpCacheAuth) window.wpCacheAuth(window.WP_USER); } catch (e) {}
|
||||
msg('Saved. Reloading so every date on the page agrees…', true);
|
||||
// Dates are formatted at render time all over the app; a reload is the
|
||||
// honest way to apply the change everywhere at once.
|
||||
|
||||
@@ -27,17 +27,7 @@ DATABASE_URL=postgresql+psycopg://wpsuite:CHANGE_ME@localhost:5432/wpsuite
|
||||
# python -c "import secrets; print(secrets.token_urlsafe(48))"
|
||||
AUTH_SECRET_KEY=CHANGE_ME_run_the_command_above
|
||||
|
||||
# D18 (2026-09-23): a session slides on activity, capped by a hard ceiling
|
||||
# underneath - not one flat lifetime. Both defaults are proposed, not
|
||||
# confirmed against this tenant's actual Okta SSO session policy - if Okta's
|
||||
# own session outlives either number, re-auth here is likely a fast redirect
|
||||
# rather than a real login screen, so these cost less than they look like.
|
||||
#
|
||||
# No request for this many minutes ends the session outright.
|
||||
# AUTH_IDLE_MINUTES=30
|
||||
#
|
||||
# The absolute ceiling from original sign-in, regardless of activity - no
|
||||
# session outlives this no matter how continuously active it is. Default 8.
|
||||
# How long a login lasts before re-authentication (hours). Default 8 (D25).
|
||||
# AUTH_SESSION_HOURS=8
|
||||
|
||||
# ── Okta OIDC (required — this is the only sign-in path) ───────────────────────
|
||||
@@ -65,6 +55,59 @@ OKTA_REDIRECT_URI=CHANGE_ME
|
||||
# a custom claim, …) — no code change needed, just this value.
|
||||
# OKTA_IDENTITY_CLAIM=preferred_username
|
||||
|
||||
# ── Okta deprovisioning sync (D18, wave 13) ────────────────────────────────────
|
||||
# A scheduled job asks Okta who is still active and disables local accounts
|
||||
# Okta no longer shows as active. It only ever DISABLES. It never re-enables.
|
||||
# If Okta cannot be reached or answers with anything unexpected, the job changes
|
||||
# NOTHING that cycle and logs the failure.
|
||||
#
|
||||
# A read-only Okta API token, created by the person who administers the Okta
|
||||
# tenant (Security > API > Tokens). This is NOT the OIDC client secret above.
|
||||
# It is a different credential with a different scope. Treat it like
|
||||
# AUTH_SECRET_KEY: never commit it, never log it. Leave it unset and the sync
|
||||
# stays off (the startup log says so).
|
||||
# OKTA_API_TOKEN=CHANGE_ME
|
||||
#
|
||||
# Second way to sign in to the Management API (D23): an Okta "API Services" app that
|
||||
# uses client authentication private_key_jwt. Set these two INSTEAD of OKTA_API_TOKEN.
|
||||
# If OKTA_API_TOKEN is also set, the token is used.
|
||||
# OKTA_SYNC_CLIENT_ID the client ID of that app (starts with 0oa)
|
||||
# OKTA_SYNC_PRIVATE_KEY its PRIVATE key as a JWK, as JSON or as base64 of that JSON.
|
||||
# In Portainer use base64: it has no quote marks to be mangled.
|
||||
# The Okta app needs the scope okta.users.read granted (Okta API Scopes tab), an admin
|
||||
# role such as Read-only Administrator assigned to it. If the app has "Require Demonstrating
|
||||
# Proof of Possession (DPoP)" switched on, the sync detects that and uses DPoP by itself. The key is a secret: never commit it or paste it
|
||||
# into chat or a ticket.
|
||||
# OKTA_SYNC_DPOP: auto (default), on, or off. auto asks for a plain Bearer token first, and
|
||||
# uses DPoP only if Okta answers invalid_dpop_proof. Set "on" to always use DPoP.
|
||||
# Set "off" to never use it. You normally set nothing.
|
||||
# OKTA_SYNC_DPOP=auto
|
||||
# OKTA_SYNC_CLIENT_ID=0oaXXXXXXXXXXXXXXXXX
|
||||
# OKTA_SYNC_PRIVATE_KEY=
|
||||
|
||||
# The Okta org URL that serves the Management API, e.g. https://yourorg.okta.com
|
||||
# Optional. If unset, it is the scheme and host of OKTA_ISSUER. Must be https.
|
||||
# OKTA_ORG_URL=https://your-org.okta.com
|
||||
#
|
||||
# Which Okta PROFILE attribute holds the value that matches users.username.
|
||||
# Optional. By default it follows OKTA_IDENTITY_CLAIM: preferred_username reads
|
||||
# profile.login, email reads profile.email, any other claim name reads the
|
||||
# profile attribute of the same name. Set this only if your tenant builds the
|
||||
# claim from a different attribute.
|
||||
# OKTA_SYNC_PROFILE_ATTRIBUTE=login
|
||||
#
|
||||
# How often the sync runs, in seconds. Default 900 (15 minutes), proposed and not
|
||||
# confirmed with IT. A value under 300 is raised to 300. Every api worker checks
|
||||
# once a minute and one of them wins each cycle, so raising the worker count does
|
||||
# not raise the call rate to Okta.
|
||||
# OKTA_SYNC_INTERVAL_SECONDS=900
|
||||
#
|
||||
# Dry run (D22). Set to true for the first deploy. The sync then does everything
|
||||
# except the change: it logs who it WOULD disable and writes nothing, and it writes
|
||||
# no audit rows. Read the result with: docker compose logs api | grep "Okta sync"
|
||||
# Remove it (or set it to false) to go live. Off by default.
|
||||
# OKTA_SYNC_DRY_RUN=true
|
||||
|
||||
# ── Email notifications (optional) ─────────────────────────────────────────────
|
||||
# WP-assignment emails are OFF by default and are turned on from the Admin
|
||||
# console (Notifications & email card), where the SMTP host/port/from-address
|
||||
|
||||
515
server/app.py
515
server/app.py
@@ -10,6 +10,7 @@ Interactive docs: http://<host>/api/docs
|
||||
"""
|
||||
import base64
|
||||
import csv
|
||||
import contextlib
|
||||
import hashlib
|
||||
import hmac
|
||||
import io
|
||||
@@ -31,8 +32,8 @@ from sqlalchemy import select, delete, func, or_
|
||||
from sqlalchemy.orm import Session
|
||||
from starlette.middleware.sessions import SessionMiddleware
|
||||
|
||||
from .db import Base, engine, get_db
|
||||
from . import models, auth, notify, assets_db, okta_auth, okta_fake
|
||||
from .db import Base, SessionLocal, engine, get_db
|
||||
from . import models, auth, notify, assets_db, okta_auth, okta_fake, files_http, okta_sync
|
||||
|
||||
log = logging.getLogger("wpsuite.app")
|
||||
|
||||
@@ -47,7 +48,21 @@ if engine.dialect.name == "sqlite":
|
||||
# Interactive docs are handy in dev but hand an attacker the full API map in prod,
|
||||
# so enable them only on the SQLite dev fallback (production runs on Postgres).
|
||||
_docs_enabled = engine.dialect.name == "sqlite"
|
||||
@contextlib.asynccontextmanager
|
||||
async def _lifespan(_app):
|
||||
"""Start the Okta deprovisioning sync (D18 / T13.4) when the process starts and
|
||||
stop it when the process ends. With no OKTA_API_TOKEN it starts nothing and
|
||||
says so in the startup log."""
|
||||
stop = okta_sync.start_scheduler(SessionLocal)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
if stop is not None:
|
||||
stop.set()
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
lifespan=_lifespan,
|
||||
title="Work Package Suite API",
|
||||
docs_url="/api/docs" if _docs_enabled else None,
|
||||
redoc_url=None,
|
||||
@@ -108,23 +123,12 @@ def _csrf_ok(request: Request) -> bool:
|
||||
async def auth_gate(request: Request, call_next):
|
||||
path = request.url.path
|
||||
method = request.method
|
||||
claims = None
|
||||
if method != "OPTIONS" and auth._needs_auth(path):
|
||||
claims = auth.is_request_authenticated(request)
|
||||
if not claims:
|
||||
if not auth.is_request_authenticated(request):
|
||||
return JSONResponse(status_code=401, content={"detail": "Not authenticated"})
|
||||
if method in _UNSAFE_METHODS and path.startswith("/api/") and not _csrf_ok(request):
|
||||
return JSONResponse(status_code=403, content={"detail": "Cross-origin request rejected"})
|
||||
response = await call_next(request)
|
||||
# D18: slide the session forward on activity, capped by its absolute
|
||||
# ceiling. Reads only the already-validated claims - no DB hit here, and
|
||||
# the separate is_active/token_version check in get_current_user still
|
||||
# runs on its own for every request regardless of whether this refreshes.
|
||||
if claims is not None:
|
||||
refreshed = auth.maybe_refresh_token(claims)
|
||||
if refreshed:
|
||||
auth.set_session_cookie(response, request, refreshed)
|
||||
return response
|
||||
return await call_next(request)
|
||||
|
||||
|
||||
def gen_id(prefix: str) -> str:
|
||||
@@ -830,12 +834,22 @@ if okta_fake.is_active():
|
||||
|
||||
|
||||
@app.get("/api/auth/me")
|
||||
def whoami(user: models.User = Depends(auth.get_current_user)):
|
||||
def whoami(request: Request, user: models.User = Depends(auth.get_current_user)):
|
||||
"""Who is logged in. The frontend guard calls this on every page load.
|
||||
|
||||
`role` is normalized here so no page has to know that a pre-roles account stores
|
||||
'user' where it now means 'project_user'."""
|
||||
return {"user": {**user.to_dict(), "role": auth.normalize_role(user.role)}}
|
||||
'user' where it now means 'project_user'.
|
||||
|
||||
D27: `session_expires_at` is the `exp` claim of the session token, as a UTC time.
|
||||
The browser keeps it so the offline sign-in window ends when the server session
|
||||
ends. No column holds it: get_current_user has already validated this token, so
|
||||
the claim is read back from the same cookie."""
|
||||
claims = auth.is_request_authenticated(request) or {}
|
||||
exp = claims.get("exp")
|
||||
expires_at = (datetime.fromtimestamp(exp, tz=timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
if isinstance(exp, (int, float)) else None)
|
||||
return {"user": {**user.to_dict(), "role": auth.normalize_role(user.role)},
|
||||
"session_expires_at": expires_at}
|
||||
|
||||
|
||||
# ── Display preferences (self-service) ─────────────────────────────────────────
|
||||
@@ -1034,16 +1048,100 @@ def create_user(body: NewUserIn, actor: models.User = Depends(require_user_manag
|
||||
return directory_entry(db, u, actor)
|
||||
|
||||
|
||||
# ── One account-level change, used by the single-user routes AND the bulk routes ──
|
||||
# CR-020 (T12.1): a bulk action is the single-user action, once per row. So each
|
||||
# rule lives in exactly one function below, and both kinds of route call it: a
|
||||
# bulk request cannot reach further than a one-at-a-time request, because there
|
||||
# is no second copy of the rules for it to reach through. Every function checks
|
||||
# all of its guards BEFORE it changes anything, so a row that is refused leaves
|
||||
# nothing half-written, and writes its AuditLog row the way the single route
|
||||
# always has. None of them commits; the route owns the transaction.
|
||||
def guard_target(db: Session, actor: "models.User", u: "models.User") -> None:
|
||||
"""The two checks every account change starts with: may the caller see this
|
||||
account (404 if not, so existence does not leak), and may they change it."""
|
||||
require_see_user(db, actor, u)
|
||||
require_manage_user(db, actor, u)
|
||||
|
||||
|
||||
def apply_user_active(db: Session, actor: "models.User", u: "models.User", is_active: bool) -> None:
|
||||
if u.id == actor.id and not is_active:
|
||||
raise HTTPException(status_code=400, detail="You cannot disable your own account")
|
||||
u.is_active = is_active
|
||||
log_event(db, actor, "user_enabled" if is_active else "user_disabled", "user", u.id,
|
||||
summary=u.username, detail={"is_active": bool(is_active)})
|
||||
|
||||
|
||||
def apply_user_role(db: Session, actor: "models.User", u: "models.User", role: str) -> None:
|
||||
"""The role guards: never your own role, never the last active admin. The
|
||||
grantable-roles check is the caller's, made once per request before this."""
|
||||
if u.id == actor.id:
|
||||
raise HTTPException(status_code=400, detail="You cannot change your own role")
|
||||
if auth.is_admin(u) and role != auth.ROLE_ADMIN:
|
||||
other_admins = db.scalars(
|
||||
select(models.User.id).where(
|
||||
(models.User.role == auth.ROLE_ADMIN)
|
||||
& (models.User.id != u.id)
|
||||
& (models.User.is_active.is_(True))
|
||||
)
|
||||
).all()
|
||||
if not other_admins:
|
||||
raise HTTPException(status_code=400, detail="Can't remove the last admin account")
|
||||
old_role = u.role
|
||||
u.role = role
|
||||
detail = {"from": old_role, "to": role}
|
||||
# Promoting someone to admin retires their default-member flag: an admin already
|
||||
# reaches every project, so the flag would do nothing except sit there invisibly
|
||||
# (the console shows admins no controls) and come back to life the day they are
|
||||
# demoted. Same reasoning as clearing the role in set_user_auto_add.
|
||||
if role == auth.ROLE_ADMIN and (u.auto_add_projects or u.auto_add_role):
|
||||
u.auto_add_projects = False
|
||||
u.auto_add_role = ""
|
||||
detail["auto_add_cleared"] = True
|
||||
log_event(db, actor, "role_changed", "user", u.id, summary=u.username, detail=detail)
|
||||
|
||||
|
||||
def apply_user_delete(db: Session, actor: "models.User", u: "models.User") -> None:
|
||||
if u.id == actor.id:
|
||||
raise HTTPException(status_code=400, detail="You cannot delete your own account")
|
||||
log_event(db, actor, "user_deleted", "user", u.id, summary=u.username, detail={"role": u.role})
|
||||
db.delete(u)
|
||||
|
||||
|
||||
def apply_user_projects(db: Session, actor: "models.User", u: "models.User",
|
||||
managed: Optional[set], valid: set, roles: dict) -> None:
|
||||
"""Replace the caller's slice of `u`'s memberships with `valid`, each carrying
|
||||
its role from `roles` ('' = inherit). `valid` must already be checked to exist
|
||||
and to sit inside `managed`."""
|
||||
# Rebuild only the rows this caller owns. Scoping the DELETE is the whole of the
|
||||
# "leave other jobs alone" guarantee — get it wrong and a super user's save
|
||||
# silently revokes access everywhere else.
|
||||
doomed = delete(models.ProjectMember).where(models.ProjectMember.user_id == u.id)
|
||||
if managed is not None:
|
||||
# in_() on an empty set is a valid always-false predicate, so a caller who
|
||||
# administers nothing deletes nothing (require_manage_user already refused them).
|
||||
doomed = doomed.where(models.ProjectMember.project_id.in_(managed))
|
||||
db.execute(doomed)
|
||||
for pid in valid:
|
||||
db.add(models.ProjectMember(id=gen_id("pm"), user_id=u.id, project_id=pid,
|
||||
role=roles.get(pid, "")))
|
||||
log_event(db, actor, "project_access_changed", "user", u.id, summary=u.username,
|
||||
detail={"projects": len(valid),
|
||||
"scope": "all" if managed is None else "managed",
|
||||
"overrides": {p: r for p, r in roles.items() if p in valid}})
|
||||
|
||||
|
||||
def project_scoped_grantable(actor: "models.User") -> tuple:
|
||||
"""Per-project roles `actor` may hand out. Only an app admin can make someone a
|
||||
super user on a project. user_scope and get_user_projects still spell the same
|
||||
rule inline for the console; left as they are to keep this diff to CR-020."""
|
||||
return auth.PROJECT_SCOPED_ROLES if auth.is_admin(actor) else (auth.ROLE_PROJECT_ADMIN, auth.ROLE_PROJECT_USER)
|
||||
|
||||
|
||||
@app.post("/api/auth/users/{user_id}/active")
|
||||
def set_user_active(user_id: str, body: ActiveIn, actor: models.User = Depends(require_user_manager), db: Session = Depends(get_db)):
|
||||
u = load_target_user(db, user_id)
|
||||
require_see_user(db, actor, u)
|
||||
require_manage_user(db, actor, u)
|
||||
if u.id == actor.id and not body.is_active:
|
||||
raise HTTPException(status_code=400, detail="You cannot disable your own account")
|
||||
u.is_active = body.is_active
|
||||
log_event(db, actor, "user_enabled" if body.is_active else "user_disabled", "user", u.id,
|
||||
summary=u.username, detail={"is_active": bool(body.is_active)})
|
||||
guard_target(db, actor, u)
|
||||
apply_user_active(db, actor, u, body.is_active)
|
||||
db.commit()
|
||||
return directory_entry(db, u, actor)
|
||||
|
||||
@@ -1060,32 +1158,8 @@ def set_user_role(user_id: str, body: RoleIn, actor: models.User = Depends(requi
|
||||
if body.role not in allowed:
|
||||
raise HTTPException(status_code=400, detail=f"role must be one of {', '.join(allowed)}")
|
||||
u = load_target_user(db, user_id)
|
||||
require_see_user(db, actor, u)
|
||||
require_manage_user(db, actor, u)
|
||||
if u.id == actor.id:
|
||||
raise HTTPException(status_code=400, detail="You cannot change your own role")
|
||||
if auth.is_admin(u) and body.role != auth.ROLE_ADMIN:
|
||||
other_admins = db.scalars(
|
||||
select(models.User.id).where(
|
||||
(models.User.role == auth.ROLE_ADMIN)
|
||||
& (models.User.id != u.id)
|
||||
& (models.User.is_active.is_(True))
|
||||
)
|
||||
).all()
|
||||
if not other_admins:
|
||||
raise HTTPException(status_code=400, detail="Can't remove the last admin account")
|
||||
old_role = u.role
|
||||
u.role = body.role
|
||||
detail = {"from": old_role, "to": body.role}
|
||||
# Promoting someone to admin retires their default-member flag: an admin already
|
||||
# reaches every project, so the flag would do nothing except sit there invisibly
|
||||
# (the console shows admins no controls) and come back to life the day they are
|
||||
# demoted. Same reasoning as clearing the role in set_user_auto_add.
|
||||
if body.role == auth.ROLE_ADMIN and (u.auto_add_projects or u.auto_add_role):
|
||||
u.auto_add_projects = False
|
||||
u.auto_add_role = ""
|
||||
detail["auto_add_cleared"] = True
|
||||
log_event(db, actor, "role_changed", "user", u.id, summary=u.username, detail=detail)
|
||||
guard_target(db, actor, u)
|
||||
apply_user_role(db, actor, u, body.role)
|
||||
db.commit()
|
||||
db.refresh(u)
|
||||
return directory_entry(db, u, actor)
|
||||
@@ -1142,12 +1216,8 @@ def set_user_auto_add(user_id: str, body: AutoAddIn, admin: models.User = Depend
|
||||
@app.delete("/api/auth/users/{user_id}")
|
||||
def delete_user(user_id: str, actor: models.User = Depends(require_user_manager), db: Session = Depends(get_db)):
|
||||
u = load_target_user(db, user_id)
|
||||
require_see_user(db, actor, u)
|
||||
require_manage_user(db, actor, u)
|
||||
if u.id == actor.id:
|
||||
raise HTTPException(status_code=400, detail="You cannot delete your own account")
|
||||
log_event(db, actor, "user_deleted", "user", u.id, summary=u.username, detail={"role": u.role})
|
||||
db.delete(u)
|
||||
guard_target(db, actor, u)
|
||||
apply_user_delete(db, actor, u)
|
||||
db.commit()
|
||||
return {"deleted": user_id}
|
||||
|
||||
@@ -1200,8 +1270,7 @@ def set_user_projects(user_id: str, body: ProjectAssignIn, actor: models.User =
|
||||
exactly as they were, because a payload that simply omits them would otherwise cut
|
||||
someone off from a job the caller can't even see."""
|
||||
u = load_target_user(db, user_id)
|
||||
require_see_user(db, actor, u)
|
||||
require_manage_user(db, actor, u)
|
||||
guard_target(db, actor, u)
|
||||
requested = [p for p in dict.fromkeys(body.project_ids) if p]
|
||||
for pid in requested:
|
||||
check_id(pid)
|
||||
@@ -1214,28 +1283,227 @@ def set_user_projects(user_id: str, body: ProjectAssignIn, actor: models.User =
|
||||
# A super user may hand out the project-scoped roles below their own; only an app
|
||||
# admin can make someone a super user on a project. Anything unrecognised falls
|
||||
# back to inheriting the account's own role.
|
||||
allowed = auth.PROJECT_SCOPED_ROLES if auth.is_admin(actor) else (auth.ROLE_PROJECT_ADMIN, auth.ROLE_PROJECT_USER)
|
||||
allowed = project_scoped_grantable(actor)
|
||||
roles = {pid: r for pid, r in (body.roles or {}).items() if r in allowed}
|
||||
# Rebuild only the rows this caller owns. Scoping the DELETE is the whole of the
|
||||
# "leave other jobs alone" guarantee — get it wrong and a super user's save
|
||||
# silently revokes access everywhere else.
|
||||
doomed = delete(models.ProjectMember).where(models.ProjectMember.user_id == user_id)
|
||||
if managed is not None:
|
||||
# in_() on an empty set is a valid always-false predicate, so a caller who
|
||||
# administers nothing deletes nothing (require_manage_user already refused them).
|
||||
doomed = doomed.where(models.ProjectMember.project_id.in_(managed))
|
||||
db.execute(doomed)
|
||||
for pid in valid:
|
||||
db.add(models.ProjectMember(id=gen_id("pm"), user_id=user_id, project_id=pid,
|
||||
role=roles.get(pid, "")))
|
||||
log_event(db, actor, "project_access_changed", "user", u.id, summary=u.username,
|
||||
detail={"projects": len(valid),
|
||||
"scope": "all" if managed is None else "managed",
|
||||
"overrides": {p: r for p, r in roles.items() if p in valid}})
|
||||
apply_user_projects(db, actor, u, managed, valid, roles)
|
||||
db.commit()
|
||||
return {"assigned": sorted(valid), "roles": {p: roles.get(p, "") for p in sorted(valid)}}
|
||||
|
||||
|
||||
# ── Bulk user administration — CR-020 (T12.1) ─────────────────────────────────
|
||||
# Four actions over a list of accounts: activate/disable, permissions role,
|
||||
# project add/remove (with the per-project role), delete. Each row runs exactly
|
||||
# the guards and the audit of the single-user route, through the same apply_*
|
||||
# function above. Rules, decided 2026-09-17 and confirmed 2026-10-06:
|
||||
# * a row that is refused is reported with its reason and the rest still run;
|
||||
# a batch is never reported as a success when part of it failed;
|
||||
# * one AuditLog row per changed account, the same as the single route writes -
|
||||
# never one opaque "bulk" entry;
|
||||
# * the caller's own account is refused only on its own row (disable, demote,
|
||||
# delete), never by failing the whole batch;
|
||||
# * project assignment adds to or removes from ONE project; it never replaces a
|
||||
# user's whole project list.
|
||||
# A row already in the requested state is reported "unchanged" and writes no
|
||||
# audit row: nothing changed, so there is nothing to audit.
|
||||
#
|
||||
# The routes live under /api/auth/bulk-users/ rather than /api/auth/users/bulk/,
|
||||
# because POST /api/auth/users/{user_id}/active would otherwise match "bulk" as
|
||||
# a user id.
|
||||
BULK_MAX_USERS = 500
|
||||
|
||||
|
||||
class BulkUsersIn(BaseModel):
|
||||
user_ids: list[str] = Field(default_factory=list, max_length=BULK_MAX_USERS)
|
||||
|
||||
|
||||
class BulkActiveIn(BulkUsersIn):
|
||||
is_active: bool
|
||||
|
||||
|
||||
class BulkRoleIn(BulkUsersIn):
|
||||
role: str
|
||||
|
||||
|
||||
class BulkProjectIn(BulkUsersIn):
|
||||
project_id: str
|
||||
op: str # "add" | "remove"
|
||||
role: str = "" # per-project role on add; '' = inherit the account's own
|
||||
|
||||
|
||||
def run_bulk(db: Session, actor: "models.User", user_ids: list[str], action: str, apply) -> dict:
|
||||
"""Run `apply(u)` once per distinct id, in the order given. `apply` returns
|
||||
"ok" or "unchanged", or raises HTTPException with the reason the single-user
|
||||
route would have given. The flush after every row matters: the session does
|
||||
not autoflush, and a later row's guard (the last-admin count, for one) has to
|
||||
see what an earlier row already changed."""
|
||||
results = []
|
||||
for uid in dict.fromkeys(i for i in user_ids if i):
|
||||
row = {"user_id": uid, "username": "", "status": "", "reason": ""}
|
||||
try:
|
||||
check_id(uid)
|
||||
u = load_target_user(db, uid)
|
||||
row["username"] = u.username
|
||||
guard_target(db, actor, u)
|
||||
row["status"] = apply(u)
|
||||
db.flush()
|
||||
except HTTPException as exc:
|
||||
row["status"] = "failed"
|
||||
row["reason"] = exc.detail if isinstance(exc.detail, str) else str(exc.detail)
|
||||
results.append(row)
|
||||
db.commit()
|
||||
counts = {s: sum(1 for r in results if r["status"] == s) for s in ("ok", "unchanged", "failed")}
|
||||
return {"action": action, "requested": len(results), **counts, "results": results}
|
||||
|
||||
|
||||
@app.post("/api/auth/bulk-users/active")
|
||||
def bulk_set_active(body: BulkActiveIn, actor: models.User = Depends(require_user_manager), db: Session = Depends(get_db)):
|
||||
def apply(u):
|
||||
if bool(u.is_active) == bool(body.is_active):
|
||||
return "unchanged"
|
||||
apply_user_active(db, actor, u, body.is_active)
|
||||
return "ok"
|
||||
return run_bulk(db, actor, body.user_ids, "enable" if body.is_active else "disable", apply)
|
||||
|
||||
|
||||
@app.post("/api/auth/bulk-users/role")
|
||||
def bulk_set_role(body: BulkRoleIn, actor: models.User = Depends(require_user_manager), db: Session = Depends(get_db)):
|
||||
allowed = grantable_roles(actor)
|
||||
if body.role not in allowed:
|
||||
raise HTTPException(status_code=400, detail=f"role must be one of {', '.join(allowed)}")
|
||||
|
||||
def apply(u):
|
||||
# Compared as stored: a legacy 'user' row moved to 'project_user' is a real
|
||||
# change of the stored value, so it is applied and audited, as one at a time.
|
||||
if u.role == body.role:
|
||||
return "unchanged"
|
||||
apply_user_role(db, actor, u, body.role)
|
||||
return "ok"
|
||||
return run_bulk(db, actor, body.user_ids, "role", apply)
|
||||
|
||||
|
||||
@app.post("/api/auth/bulk-users/projects")
|
||||
def bulk_set_project(body: BulkProjectIn, actor: models.User = Depends(require_user_manager), db: Session = Depends(get_db)):
|
||||
"""Add every listed account to one project (with a per-project role), or remove
|
||||
them from it. Implemented as the single route's replace of the caller's slice,
|
||||
computed per user, so the audit row is the same shape the dialog writes."""
|
||||
if body.op not in ("add", "remove"):
|
||||
raise HTTPException(status_code=400, detail="op must be 'add' or 'remove'")
|
||||
check_id(body.project_id)
|
||||
if not db.get(models.Project, body.project_id):
|
||||
raise HTTPException(status_code=404, detail="Project not found")
|
||||
managed = managed_project_ids(db, actor)
|
||||
if managed is not None and body.project_id not in managed:
|
||||
raise HTTPException(status_code=403, detail="You don't administer the users of that project")
|
||||
role = (body.role or "").strip()
|
||||
if body.op == "add" and role and role not in project_scoped_grantable(actor):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"role must be '' (inherit) or one of {', '.join(project_scoped_grantable(actor))}")
|
||||
pid = body.project_id
|
||||
|
||||
def apply(u):
|
||||
mine = [m for m in db.scalars(select(models.ProjectMember)
|
||||
.where(models.ProjectMember.user_id == u.id)).all()
|
||||
if managed is None or m.project_id in managed]
|
||||
current = {m.project_id: (m.role or "") for m in mine}
|
||||
if body.op == "add":
|
||||
if current.get(pid) == role:
|
||||
return "unchanged"
|
||||
current[pid] = role
|
||||
else:
|
||||
if pid not in current:
|
||||
return "unchanged"
|
||||
current.pop(pid)
|
||||
# Roles on the caller's OTHER projects are carried over exactly as stored -
|
||||
# adding someone to one job must never reset their role on another.
|
||||
apply_user_projects(db, actor, u, managed, set(current),
|
||||
{p: r for p, r in current.items() if r})
|
||||
return "ok"
|
||||
return run_bulk(db, actor, body.user_ids, "project_" + body.op, apply)
|
||||
|
||||
|
||||
class BulkResolveIn(BaseModel):
|
||||
text: str = Field(default="", max_length=200_000)
|
||||
|
||||
|
||||
_CSV_HEADER_WORDS = ("username", "user", "login", "email", "user name")
|
||||
|
||||
|
||||
@app.post("/api/auth/bulk-users/resolve")
|
||||
def bulk_resolve(body: BulkResolveIn, actor: models.User = Depends(require_user_manager), db: Session = Depends(get_db)):
|
||||
"""CR-020 (T12.4): turn an uploaded list of usernames into the account ids a
|
||||
bulk action will run on - a second way IN to the same four bulk routes, not a
|
||||
third copy of their rules. The CSV holds usernames only (decided 2026-10-06);
|
||||
the action is chosen in the console and applies to every accepted row.
|
||||
|
||||
Validate and report, the CR-005 pattern: every line is either accepted or
|
||||
rejected WITH ITS LINE NUMBER and a reason - never silently skipped. A line is
|
||||
rejected when it names nobody this caller can see (the same 'not found' a
|
||||
hidden account gets everywhere else), an account they may not change (with
|
||||
the same reason the single route would give), their own account, or a
|
||||
username already listed on an earlier line. Nothing is changed here; the
|
||||
bulk route the console calls next re-checks every row anyway."""
|
||||
rows = []
|
||||
seen: dict[str, int] = {}
|
||||
visible = visible_user_ids(db, actor)
|
||||
cache: dict = {}
|
||||
for line_no, raw in enumerate((body.text or "").splitlines(), start=1):
|
||||
line = raw.strip().lstrip("")
|
||||
if not line:
|
||||
continue
|
||||
cell = re.split(r"[,;\t]", line, maxsplit=1)[0].strip().strip('"').strip()
|
||||
if not cell:
|
||||
rows.append({"line": line_no, "text": line[:120], "status": "rejected",
|
||||
"reason": "no username in the first column"})
|
||||
continue
|
||||
if line_no == 1 and cell.lower() in _CSV_HEADER_WORDS:
|
||||
continue # header row
|
||||
row = {"line": line_no, "text": cell[:120], "status": "rejected", "reason": "",
|
||||
"user_id": None, "username": ""}
|
||||
if any(ord(ch) < 32 for ch in cell) or len(cell) > 120:
|
||||
row["reason"] = "not a valid username"
|
||||
rows.append(row)
|
||||
continue
|
||||
key = cell.lower()
|
||||
if key in seen:
|
||||
row["reason"] = f"duplicate of line {seen[key]}"
|
||||
rows.append(row)
|
||||
continue
|
||||
seen[key] = line_no
|
||||
u = db.scalars(select(models.User).where(func.lower(models.User.username) == key)).first()
|
||||
if u is None:
|
||||
# An offboarding list usually carries email addresses. Accepted only
|
||||
# when exactly one account has that address, so a shared or reused
|
||||
# address can never pick the wrong person.
|
||||
by_mail = db.scalars(select(models.User).where(func.lower(models.User.email) == key)).all()
|
||||
u = by_mail[0] if len(by_mail) == 1 else None
|
||||
if u is None or (visible is not None and u.id not in visible):
|
||||
row["reason"] = "User not found"
|
||||
elif u.id == actor.id:
|
||||
row["reason"] = "your own account - change it one setting at a time"
|
||||
else:
|
||||
problem = manage_user_problem(db, actor, u, cache)
|
||||
if problem:
|
||||
row["reason"] = problem
|
||||
else:
|
||||
row.update(status="ok", user_id=u.id, username=u.username)
|
||||
rows.append(row)
|
||||
accepted = [r for r in rows if r["status"] == "ok"]
|
||||
return {"read": len(rows), "accepted": len(accepted), "rejected": len(rows) - len(accepted),
|
||||
"user_ids": [r["user_id"] for r in accepted], "rows": rows}
|
||||
|
||||
|
||||
@app.post("/api/auth/bulk-users/delete")
|
||||
def bulk_delete(body: BulkUsersIn, actor: models.User = Depends(require_user_manager), db: Session = Depends(get_db)):
|
||||
"""Hard delete, the same as DELETE /api/auth/users/{id} (decided 2026-09-17:
|
||||
no soft delete invented for the bulk case). The console asks for the typed
|
||||
word DELETE first; the server's guard is the per-row rules, as for one."""
|
||||
def apply(u):
|
||||
apply_user_delete(db, actor, u)
|
||||
return "ok"
|
||||
return run_bulk(db, actor, body.user_ids, "delete", apply)
|
||||
|
||||
|
||||
# ── Projects ─────────────────────────────────────────────────────────────────
|
||||
@app.post("/api/projects")
|
||||
def upsert_project(body: ProjectIn, user: models.User = Depends(auth.get_current_user), db: Session = Depends(get_db)):
|
||||
@@ -1352,6 +1620,32 @@ def archive_project(project_id: str, body: ArchiveIn, user: models.User = Depend
|
||||
|
||||
|
||||
# ── SOPs ─────────────────────────────────────────────────────────────────────
|
||||
def canonical_sop_id(project_id: Optional[str]) -> Optional[str]:
|
||||
"""D20: the one id a project's SOP lives at - the id pushSOP has always used."""
|
||||
return f"sop__{project_id}" if project_id else None
|
||||
|
||||
|
||||
def project_baseline_sop(db: Session, project_id: Optional[str]) -> Optional["models.Sop"]:
|
||||
"""D20: THE completed SOP of a project, for every reader that treats it as the
|
||||
baseline (page load, PM/CM mail, QA mail, the launcher summary).
|
||||
|
||||
The canonical row wins when it is complete. "Most recent complete" is only the
|
||||
fallback, for projects whose SOP predates the fixed id (seed data, API
|
||||
clients). Before D20 "most recent" was the whole rule, which is what let a
|
||||
second complete SOP take the baseline over (security review SEC-02)."""
|
||||
if not project_id:
|
||||
return None
|
||||
row = db.get(models.Sop, canonical_sop_id(project_id))
|
||||
if row is not None and row.complete and row.project_id == project_id:
|
||||
return row
|
||||
return db.scalars(
|
||||
select(models.Sop)
|
||||
.where((models.Sop.project_id == project_id) & (models.Sop.complete.is_(True)))
|
||||
.order_by(models.Sop.updated_at.desc())
|
||||
.limit(1)
|
||||
).first()
|
||||
|
||||
|
||||
@app.post("/api/sops")
|
||||
def upsert_sop(body: SopIn, user: models.User = Depends(auth.get_current_user), db: Session = Depends(get_db)):
|
||||
check_id(body.id)
|
||||
@@ -1360,6 +1654,11 @@ def upsert_sop(body: SopIn, user: models.User = Depends(auth.get_current_user),
|
||||
# one it currently sits on below — so an archived job can be neither edited nor
|
||||
# used as a source to move a SOP out of.
|
||||
require_project_writable(db, user, body.project_id, "Saving a SOP")
|
||||
# D20: one SOP per project, at a fixed id. The browser has always written
|
||||
# `sop__<project_id>` (pushSOP); a request with no id now gets it too.
|
||||
canonical = canonical_sop_id(body.project_id)
|
||||
if not body.id and canonical:
|
||||
body.id = canonical
|
||||
sop = db.get(models.Sop, body.id) if body.id else None
|
||||
if sop is not None:
|
||||
require_project_access(db, user, sop.project_id)
|
||||
@@ -1369,6 +1668,28 @@ def upsert_sop(body: SopIn, user: models.User = Depends(auth.get_current_user),
|
||||
# project member, including marking it complete the first time.
|
||||
if sop.complete:
|
||||
require_project_admin(db, user, sop.project_id, "Changing a completed SOP")
|
||||
# D20 (security review SEC-02): the rule above checked only the row being
|
||||
# posted, so a project_user could post a NEW id with complete=true and every
|
||||
# "latest complete SOP" reader - the page baseline and the PM/CM/QA mail
|
||||
# routing - followed it. A project gets no second SOP row (creating one, or
|
||||
# moving one in, under any id but the canonical one is refused), and while
|
||||
# it has a complete SOP, writing another complete one is a Project Admin act.
|
||||
# Rows made before D20 under other ids stay and keep their old rules.
|
||||
if canonical and body.id != canonical and (sop is None or sop.project_id != body.project_id):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"A project has one SOP. Save it as {canonical}.",
|
||||
)
|
||||
if body.project_id and body.complete:
|
||||
other_complete = db.scalar(
|
||||
select(models.Sop.id).where(
|
||||
(models.Sop.project_id == body.project_id)
|
||||
& (models.Sop.complete.is_(True))
|
||||
& (models.Sop.id != body.id)
|
||||
).limit(1)
|
||||
)
|
||||
if other_complete:
|
||||
require_project_admin(db, user, body.project_id, "Changing a completed SOP")
|
||||
is_new = sop is None
|
||||
if sop is None:
|
||||
sop = models.Sop(id=body.id or gen_id("sop"))
|
||||
@@ -1401,6 +1722,15 @@ def list_sops(project_id: Optional[str] = Query(None), full: bool = Query(False)
|
||||
|
||||
@app.get("/api/sops/latest")
|
||||
def latest_sop(complete: Optional[bool] = None, project_id: Optional[str] = Query(None), user: models.User = Depends(auth.get_current_user), db: Session = Depends(get_db)):
|
||||
# D20: "the project's completed SOP" is what pullProject() asks for on every
|
||||
# page load, so it is answered by the baseline rule, not by "most recent".
|
||||
# Same 404 as before for a project the caller cannot reach.
|
||||
if complete is True and project_id:
|
||||
ids = accessible_project_ids(db, user)
|
||||
sop = project_baseline_sop(db, project_id) if (ids is None or project_id in ids) else None
|
||||
if not sop:
|
||||
raise HTTPException(status_code=404, detail="No SOP found")
|
||||
return sop.to_dict()
|
||||
stmt = select(models.Sop)
|
||||
if complete is not None:
|
||||
stmt = stmt.where(models.Sop.complete == complete)
|
||||
@@ -1581,15 +1911,10 @@ def reopened_critical(old_data: Optional[dict], new_data: Optional[dict]) -> lis
|
||||
|
||||
|
||||
def project_sop_team(db: Session, project_id: Optional[str]) -> list[str]:
|
||||
"""PM + CM user ids from the project's most recent complete SOP."""
|
||||
"""PM + CM user ids from the project's baseline SOP (D20: project_baseline_sop)."""
|
||||
if not project_id:
|
||||
return []
|
||||
sop = db.scalars(
|
||||
select(models.Sop)
|
||||
.where((models.Sop.project_id == project_id) & (models.Sop.complete.is_(True)))
|
||||
.order_by(models.Sop.updated_at.desc())
|
||||
.limit(1)
|
||||
).first()
|
||||
sop = project_baseline_sop(db, project_id)
|
||||
if not sop:
|
||||
return []
|
||||
data = sop.data or {}
|
||||
@@ -1607,15 +1932,11 @@ def project_qa_group(db: Session, project_id: Optional[str]) -> list["models.Use
|
||||
"""D2: the QA group named on the project's latest complete SOP. pushSOP writes
|
||||
the row as data={sop, state}, so the project block is data['sop']['project'] -
|
||||
project_sop_team above read the flat shape until BL-021 was fixed
|
||||
(2026-08-20); both now read nested-first, exactly alike."""
|
||||
(2026-08-20); both now read nested-first, exactly alike. D20: both also read
|
||||
the same row, project_baseline_sop."""
|
||||
if not project_id:
|
||||
return []
|
||||
sop = db.scalars(
|
||||
select(models.Sop)
|
||||
.where((models.Sop.project_id == project_id) & (models.Sop.complete.is_(True)))
|
||||
.order_by(models.Sop.updated_at.desc())
|
||||
.limit(1)
|
||||
).first()
|
||||
sop = project_baseline_sop(db, project_id)
|
||||
if not sop:
|
||||
return []
|
||||
data = sop.data or {}
|
||||
@@ -2682,12 +3003,7 @@ def project_summary(project_id: str, user: models.User = Depends(auth.get_curren
|
||||
raise HTTPException(status_code=404, detail="Project not found")
|
||||
require_project_access(db, user, project_id)
|
||||
|
||||
sop = db.scalars(
|
||||
select(models.Sop)
|
||||
.where(models.Sop.project_id == project_id, models.Sop.complete.is_(True))
|
||||
.order_by(models.Sop.updated_at.desc())
|
||||
.limit(1)
|
||||
).first()
|
||||
sop = project_baseline_sop(db, project_id) # D20: the same row every page loads
|
||||
|
||||
wp_total = db.scalar(
|
||||
select(func.count()).select_from(models.WorkPackage).where(
|
||||
@@ -2931,10 +3247,9 @@ def get_wp_file(file_id: str, user: models.User = Depends(auth.get_current_user)
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="File not found")
|
||||
require_project_access(db, user, row.project_id)
|
||||
return Response(content=row.data, media_type=row.mime or "application/octet-stream",
|
||||
headers={"Content-Disposition":
|
||||
f'inline; filename="{(row.name or "file").replace(chr(34), "")}"',
|
||||
"Cache-Control": "private, max-age=86400"})
|
||||
# D19: PDF and raster images inline as before; anything else (SVG included)
|
||||
# downloads as a sandboxed attachment, and every name is a valid header.
|
||||
return files_http.file_response(row)
|
||||
|
||||
|
||||
@app.patch("/api/files/{file_id}")
|
||||
|
||||
107
server/auth.py
107
server/auth.py
@@ -59,26 +59,10 @@ log = logging.getLogger("wpsuite.auth")
|
||||
|
||||
COOKIE_NAME = "wp_session"
|
||||
JWT_ALG = "HS256"
|
||||
# D18 (2026-09-23): a session now slides on activity, capped by a hard ceiling
|
||||
# underneath - not a single flat lifetime. A pure idle timer with no ceiling
|
||||
# would let a continuously-active session never force a fresh Okta recheck,
|
||||
# which is a worse fit for this item's own purpose (catching someone still
|
||||
# active after being deprovisioned) than a flat expiry would have been. Both
|
||||
# numbers are proposed defaults, not confirmed against the tenant's actual
|
||||
# Okta SSO session policy - see docs/waves/decisions-2026-09-17.md.
|
||||
#
|
||||
# No request for this long invalidates the session outright.
|
||||
IDLE_MINUTES = int(os.getenv("AUTH_IDLE_MINUTES", "30"))
|
||||
# The absolute ceiling from the ORIGINAL sign-in, regardless of activity. Same
|
||||
# env var name as the old flat-lifetime design; the meaning changed, the name
|
||||
# didn't, because it still answers "how long can this session possibly live."
|
||||
# How long a session lasts before the person must sign in again. Default 8 hours (D25).
|
||||
# One flat limit from the moment of sign-in. When T13.1 lands, the same variable becomes
|
||||
# the absolute ceiling and an idle timer is added under it.
|
||||
SESSION_HOURS = int(os.getenv("AUTH_SESSION_HOURS", "8"))
|
||||
# How much later a refreshed exp must be before it's worth rewriting the
|
||||
# cookie. Without this, an active session gets a new Set-Cookie on literally
|
||||
# every request - correct, but wasteful, and it makes the cookie a busier
|
||||
# target than it needs to be. A third of the idle window is a reasonable
|
||||
# balance: refreshed a few times within any idle window, never every request.
|
||||
_REFRESH_SLACK = timedelta(minutes=max(1, IDLE_MINUTES // 3))
|
||||
|
||||
# ── permissions roles ─────────────────────────────────────────────────────────
|
||||
ROLE_ADMIN = "admin"
|
||||
@@ -178,30 +162,6 @@ SECRET_KEY = _load_secret()
|
||||
|
||||
|
||||
# ── tokens ──────────────────────────────────────────────────────────────────
|
||||
def _parse_claim_dt(v) -> Optional[datetime]:
|
||||
"""`login_at` is a custom claim, so unlike `exp`/`iat` (which PyJWT
|
||||
special-cases for a datetime -> POSIX-timestamp conversion on encode) it
|
||||
is stored and read back as a plain numeric timestamp. Returns None for
|
||||
anything unparseable rather than raising - a malformed or legacy token
|
||||
should fail closed into "no refresh", not 500."""
|
||||
if v is None:
|
||||
return None
|
||||
try:
|
||||
return datetime.fromtimestamp(float(v), tz=timezone.utc)
|
||||
except (TypeError, ValueError, OSError):
|
||||
return None
|
||||
|
||||
|
||||
def _next_exp(login_at: datetime, now: datetime) -> datetime:
|
||||
"""Whichever comes first: another IDLE_MINUTES of quiet from now, or the
|
||||
absolute SESSION_HOURS ceiling measured from the session's original
|
||||
sign-in. Shared by create_token and maybe_refresh_token so the two can't
|
||||
drift apart."""
|
||||
ceiling = login_at + timedelta(hours=SESSION_HOURS)
|
||||
idle_edge = now + timedelta(minutes=IDLE_MINUTES)
|
||||
return min(ceiling, idle_edge)
|
||||
|
||||
|
||||
def create_token(user: "models.User") -> str:
|
||||
now = datetime.now(timezone.utc)
|
||||
payload = {
|
||||
@@ -210,58 +170,33 @@ def create_token(user: "models.User") -> str:
|
||||
"role": user.role,
|
||||
"ver": user.token_version or 0,
|
||||
"iat": now,
|
||||
"login_at": now.timestamp(),
|
||||
"exp": _next_exp(now, now),
|
||||
"exp": now + timedelta(hours=SESSION_HOURS),
|
||||
}
|
||||
return jwt.encode(payload, SECRET_KEY, algorithm=JWT_ALG)
|
||||
|
||||
|
||||
def maybe_refresh_token(claims: dict) -> Optional[str]:
|
||||
"""Given a validated token's claims, return a reissued token if the
|
||||
session is worth extending, or None if nothing should change. Called from
|
||||
`auth_gate` on every authenticated request (D18) - deliberately reads only
|
||||
the already-validated claims, never the database, so it costs nothing
|
||||
beyond the JWT encode itself. The separate is_active/token_version check
|
||||
in get_current_user is unaffected either way.
|
||||
|
||||
Three ways this returns None: past the absolute ceiling (session is done,
|
||||
full re-login required - never extended, not even by a second); the
|
||||
claims can't be parsed (fails closed into no-refresh rather than guessing);
|
||||
or a refresh happened recently enough that a new cookie isn't worth
|
||||
writing yet (_REFRESH_SLACK)."""
|
||||
now = datetime.now(timezone.utc)
|
||||
login_at = _parse_claim_dt(claims.get("login_at"))
|
||||
if login_at is None:
|
||||
# Pre-D18 token (no login_at claim) - fall back to iat so it still
|
||||
# gets a real ceiling instead of riding on the old flat exp forever.
|
||||
login_at = _parse_claim_dt(claims.get("iat"))
|
||||
if login_at is None:
|
||||
return None
|
||||
ceiling = login_at + timedelta(hours=SESSION_HOURS)
|
||||
if now >= ceiling:
|
||||
return None
|
||||
new_exp = _next_exp(login_at, now)
|
||||
current_exp = _parse_claim_dt(claims.get("exp"))
|
||||
if current_exp is not None and (new_exp - current_exp) < _REFRESH_SLACK:
|
||||
return None
|
||||
payload = {
|
||||
"sub": claims.get("sub"),
|
||||
"username": claims.get("username"),
|
||||
"role": claims.get("role"),
|
||||
"ver": claims.get("ver", 0),
|
||||
"iat": now,
|
||||
"login_at": login_at.timestamp(),
|
||||
"exp": new_exp,
|
||||
}
|
||||
return jwt.encode(payload, SECRET_KEY, algorithm=JWT_ALG)
|
||||
# D21: a real session token is about 300 characters. Anything far larger is not
|
||||
# one of ours, and refusing it before parsing keeps a crafted cookie from making
|
||||
# the parser do work on our behalf.
|
||||
_MAX_TOKEN_CHARS = 4096
|
||||
|
||||
|
||||
def decode_token(token: str) -> Optional[dict]:
|
||||
"""Return the token claims if the signature and expiry are valid, else None.
|
||||
Session cookies only — a token of any other type is rejected."""
|
||||
Session cookies only — a token of any other type is rejected.
|
||||
|
||||
D21: fails CLOSED on any error. This used to catch jwt.PyJWTError only, and
|
||||
PyJWT 2.13.0 let a RecursionError out of header parsing, so a 1.6 KB cookie
|
||||
from someone with no account produced a 500 and a stack trace instead of a
|
||||
401 (security review SEC-07). Whatever goes wrong inside the decoder, the
|
||||
answer is the same: not signed in."""
|
||||
if not token or len(token) > _MAX_TOKEN_CHARS:
|
||||
return None
|
||||
try:
|
||||
claims = jwt.decode(token, SECRET_KEY, algorithms=[JWT_ALG])
|
||||
except jwt.PyJWTError:
|
||||
# Every token this app issues carries exp and sub (create_token); one without them is not ours.
|
||||
claims = jwt.decode(token, SECRET_KEY, algorithms=[JWT_ALG],
|
||||
options={"require": ["exp", "sub"]})
|
||||
except Exception: # noqa: BLE001 — see docstring: every failure is "not signed in"
|
||||
return None
|
||||
# No route issues a `typ`-carrying token anymore (that was the password-reset
|
||||
# token, removed in T10.4), but a session token still must never validate as
|
||||
|
||||
66
server/files_http.py
Normal file
66
server/files_http.py
Normal file
@@ -0,0 +1,66 @@
|
||||
"""Safe HTTP responses for uploaded drawings — D19 (T16.2).
|
||||
|
||||
Security review 2026-10-01, SEC-01 and SEC-15e.
|
||||
|
||||
WHY THIS EXISTS
|
||||
Uploads accept `image/*`, which includes `image/svg+xml`. An SVG is a
|
||||
document, not a picture: it can carry <script>. GET /api/files/{id} used to
|
||||
return every file `inline` with the uploader's content type, on the app's own
|
||||
origin, so a script inside an SVG ran as whoever opened the drawing - with
|
||||
their session, and with an admin's power if an admin opened it.
|
||||
|
||||
The download name was built by hand with only `"` removed. A name outside
|
||||
Latin-1 (`Plan é.pdf`) made the header unencodable, so the download failed
|
||||
with 500; CR/LF in a name went into the header as typed.
|
||||
|
||||
THE RULE (D19, decided 2026-10-02)
|
||||
* PDF and raster images open INLINE, exactly as before. That is what the
|
||||
creator's drawing list, the printed package and Field View link to
|
||||
(CR-007), and the offline drawing cache in sw.js stores.
|
||||
* Every other type, SVG included, is an ATTACHMENT inside a sandbox: the
|
||||
browser saves it instead of rendering it on our origin, and
|
||||
`Content-Security-Policy: sandbox` strips script and same-origin access
|
||||
even if someone opens the saved response directly.
|
||||
* SVG UPLOADS STAY ACCEPTED (decided: accept, download only). The fix is on
|
||||
the way out, so SVGs already in the database are covered too.
|
||||
* `Cache-Control` is unchanged - the offline drawing cache depends on it.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from urllib.parse import quote
|
||||
|
||||
from fastapi.responses import Response
|
||||
|
||||
# Formats a browser shows as inert pixels, or in its own PDF viewer.
|
||||
INLINE_SAFE_MIME = frozenset({
|
||||
"application/pdf",
|
||||
"image/png", "image/jpeg", "image/gif", "image/webp", "image/bmp", "image/avif",
|
||||
})
|
||||
|
||||
# Control characters, and the characters that end or escape a quoted
|
||||
# header parameter. Replaced, never passed through.
|
||||
_UNSAFE_NAME = re.compile(r'[\x00-\x1f\x7f"\\;]')
|
||||
|
||||
|
||||
def content_disposition(disposition: str, name: str) -> str:
|
||||
"""A header value that is valid for ANY user-supplied file name: an ASCII
|
||||
fallback for old clients plus RFC 5987 `filename*` with the real name."""
|
||||
name = _UNSAFE_NAME.sub("_", name or "file").strip() or "file"
|
||||
ascii_name = name.encode("ascii", "replace").decode("ascii").replace("?", "_")
|
||||
return f"{disposition}; filename=\"{ascii_name}\"; filename*=UTF-8''{quote(name, safe='')}"
|
||||
|
||||
|
||||
def file_response(row) -> Response:
|
||||
"""The response for GET /api/files/{id}. `row` is a models.WpFile."""
|
||||
mime = (row.mime or "application/octet-stream").lower()
|
||||
headers = {
|
||||
"Cache-Control": "private, max-age=86400", # unchanged: sw.js caches drawings offline
|
||||
"X-Content-Type-Options": "nosniff", # NGINX adds it too; the dev server did not
|
||||
}
|
||||
if mime in INLINE_SAFE_MIME:
|
||||
headers["Content-Disposition"] = content_disposition("inline", row.name)
|
||||
else:
|
||||
headers["Content-Disposition"] = content_disposition("attachment", row.name)
|
||||
headers["Content-Security-Policy"] = "sandbox; default-src 'none'"
|
||||
return Response(content=row.data, media_type=mime, headers=headers)
|
||||
815
server/okta_sync.py
Normal file
815
server/okta_sync.py
Normal file
@@ -0,0 +1,815 @@
|
||||
"""Okta Management API client - T13.2, wave 13 (D18).
|
||||
|
||||
This module asks Okta one question: who is in the org, and what is each person's
|
||||
status? It is the first half of the deprovisioning sync. It does not touch the
|
||||
database and it does not disable anyone. T13.3 does that, using the list this
|
||||
module returns.
|
||||
|
||||
Trust boundary. Sign-in uses the OIDC client (OKTA_CLIENT_ID and OKTA_CLIENT_SECRET,
|
||||
see okta_auth.py). This module uses a different credential, OKTA_API_TOKEN, a
|
||||
read-only Okta API token. The two never mix. A rotation or a leak of one does not
|
||||
touch the other.
|
||||
|
||||
Failure rule (D18, decision 4). This is the most important rule in this file:
|
||||
**an error is never an empty list.** If anything goes wrong, this module raises
|
||||
OktaSyncError. It never returns [] to mean "the call failed". T13.3 reads an empty
|
||||
list as "nobody is active", so a function that returned [] on a network error
|
||||
would disable every account on an Okta outage. The cases that raise are:
|
||||
|
||||
- the token or the org URL is not set
|
||||
- a network error or a timeout
|
||||
- any HTTP status that is not 200 (401, 403 and 429 included)
|
||||
- a body that is not a JSON list, or an item that is not shaped as expected
|
||||
- a user with no usable identity value
|
||||
- a list with no users at all (a real org is never empty; an empty list means
|
||||
the token cannot see the users)
|
||||
- a page limit reached (a cut-off list would make real users look missing)
|
||||
- a "next page" link that points to a different host
|
||||
|
||||
T13.3 (the sync job, `run_sync` below) uses that list to disable local accounts.
|
||||
Its rules are in its own docstring. In short: it only ever disables, it never
|
||||
re-enables, it writes one audit row per change, and any failure means NO change.
|
||||
|
||||
The token is read from the environment each time it is used. It is never logged,
|
||||
never put in an error message, and never returned to a browser. Errors carry the
|
||||
exception class name and the HTTP status only. httpx exceptions can include the
|
||||
request URL, so the URL is not copied into the message either.
|
||||
"""
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Callable, Optional
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import httpx
|
||||
import jwt
|
||||
from cryptography.hazmat.primitives.asymmetric import ec
|
||||
from sqlalchemy import select, update
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from . import models, okta_auth
|
||||
|
||||
log = logging.getLogger("wpsuite.okta_sync")
|
||||
|
||||
|
||||
def _make_logs_visible() -> None:
|
||||
"""Make this module's INFO lines reach `docker compose logs api`.
|
||||
|
||||
Nothing in this app configures Python logging, and gunicorn only configures its
|
||||
own loggers. So the root logger sits at WARNING with no handler, and every
|
||||
INFO line from a `wpsuite.*` logger is dropped. Found on 2026-10-16: with the
|
||||
exact production launch command, `grep "Okta sync"` found nothing on a healthy
|
||||
start, because "configured", "scheduled" and "done" are INFO lines. Warnings
|
||||
and errors (a disable, a skipped run) showed, but the lines that say the sync
|
||||
is alive did not. An operator cannot tell "off" from "on and quiet".
|
||||
|
||||
This adds one stderr handler to THIS module's logger only, and only when the
|
||||
root logger has none, so it never doubles up if logging is configured later and
|
||||
it does not change what any other module prints. (BL-028 and the general
|
||||
question of app-wide logging are separate items.)"""
|
||||
if log.handlers or logging.getLogger().handlers:
|
||||
return
|
||||
handler = logging.StreamHandler()
|
||||
handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s"))
|
||||
log.addHandler(handler)
|
||||
log.setLevel(logging.INFO)
|
||||
|
||||
|
||||
_make_logs_visible()
|
||||
|
||||
# Okta's own page maximum for this endpoint. Asking for the maximum keeps a
|
||||
# 20-odd account org to a single call.
|
||||
PAGE_LIMIT = 200
|
||||
# A cut-off list is more dangerous than an error, so the loop is bounded and
|
||||
# raises when the bound is reached. 50 pages of 200 is 10,000 people.
|
||||
MAX_PAGES = 50
|
||||
TIMEOUT_SECONDS = 15.0
|
||||
# The whole fetch, all pages together, must finish inside this. T13.4 relies on it:
|
||||
# a run is always shorter than the shortest allowed interval, so one cycle can
|
||||
# never still be running when the next is due.
|
||||
TOTAL_DEADLINE_SECONDS = 120.0
|
||||
|
||||
# Which Okta *profile attribute* matches each OIDC claim that OKTA_IDENTITY_CLAIM
|
||||
# may name. The sign-in flow reads a claim from the ID token. This API returns
|
||||
# the profile instead. For the default claim, preferred_username, Okta's own
|
||||
# default maps it to profile.login. A claim that is not in this table is read
|
||||
# from profile[<claim>] (for example a custom attribute).
|
||||
# OKTA_SYNC_PROFILE_ATTRIBUTE overrides the whole table. Use it if the tenant
|
||||
# builds the claim from a different attribute.
|
||||
_CLAIM_TO_PROFILE = {
|
||||
"preferred_username": "login",
|
||||
"email": "email",
|
||||
}
|
||||
|
||||
|
||||
class OktaSyncError(Exception):
|
||||
"""The Okta user list could not be fetched with confidence. T13.3 must take no
|
||||
action on any account when it sees this."""
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class OktaUser:
|
||||
identity: str # the value that matches users.username (lower case is NOT applied here)
|
||||
status: str # Okta's status, upper case: ACTIVE, SUSPENDED, DEPROVISIONED, ...
|
||||
|
||||
|
||||
# ── configuration ─────────────────────────────────────────────────────────────
|
||||
def _token() -> str:
|
||||
return os.getenv("OKTA_API_TOKEN", "").strip()
|
||||
|
||||
|
||||
def org_url() -> str:
|
||||
"""The Okta org base URL, for example https://yourorg.okta.com. The Management
|
||||
API lives here, not at the authorization server issuer. OKTA_ORG_URL wins if
|
||||
set. Otherwise it is the scheme and host of OKTA_ISSUER."""
|
||||
explicit = os.getenv("OKTA_ORG_URL", "").strip().rstrip("/")
|
||||
if explicit:
|
||||
return explicit
|
||||
issuer = (okta_auth.ISSUER or "").strip()
|
||||
parsed = urlparse(issuer)
|
||||
if parsed.scheme and parsed.netloc:
|
||||
return f"{parsed.scheme}://{parsed.netloc}"
|
||||
return ""
|
||||
|
||||
|
||||
# D23: a second way to prove who we are to Okta. Okta apps of type "API Services"
|
||||
# use OAuth 2.0 with client authentication private_key_jwt: this app holds a private
|
||||
# key (a JWK), signs a short JWT with it, trades the JWT for a one-hour access token
|
||||
# that carries only the scope okta.users.read, and sends that as a Bearer token. The
|
||||
# private key never leaves this process. An SSWS API token (OKTA_API_TOKEN) still
|
||||
# works and wins if both are set, so a deploy that has only a token behaves as before.
|
||||
OAUTH_SCOPE = "okta.users.read"
|
||||
ASSERTION_LIFETIME_SECONDS = 300
|
||||
|
||||
|
||||
def _client_id() -> str:
|
||||
return os.getenv("OKTA_SYNC_CLIENT_ID", "").strip()
|
||||
|
||||
|
||||
def _key_raw() -> str:
|
||||
return os.getenv("OKTA_SYNC_PRIVATE_KEY", "").strip()
|
||||
|
||||
|
||||
def auth_method() -> str:
|
||||
""""token" (SSWS API token), "oauth" (service app with a private key), or "" (none)."""
|
||||
if _token():
|
||||
return "token"
|
||||
if _client_id() and _key_raw():
|
||||
return "oauth"
|
||||
return ""
|
||||
|
||||
|
||||
def _parse_json_forgiving(text: str):
|
||||
"""Parse the key text as JSON, forgiving what copying does to it. Returns
|
||||
(object, None) or (None, "problem at line L column C: what"). The problem names a
|
||||
position and a rule, never any text of the key. Forgiven: line breaks and other control
|
||||
characters inside a string (a long value wrapped over several lines), curly quote
|
||||
marks, and a comma before a closing brace. Spaces inside the key's own number fields
|
||||
are removed, because those fields are base64 text."""
|
||||
attempts = [text]
|
||||
cleaned = (text.replace("\u201c", '"').replace("\u201d", '"').replace("\u2018", "'").replace("\u2019", "'"))
|
||||
cleaned = re.sub(r",\s*([}\]])", r"\1", cleaned)
|
||||
if cleaned != text:
|
||||
attempts.append(cleaned)
|
||||
last = ""
|
||||
for t in attempts:
|
||||
try:
|
||||
obj = json.loads(t, strict=False)
|
||||
except json.JSONDecodeError as exc:
|
||||
last = f"{exc.msg} at line {exc.lineno} column {exc.colno}"
|
||||
continue
|
||||
except ValueError:
|
||||
last = "unreadable"
|
||||
continue
|
||||
if isinstance(obj, dict):
|
||||
for k in ("n", "e", "d", "p", "q", "dp", "dq", "qi", "x", "y"):
|
||||
if isinstance(obj.get(k), str):
|
||||
obj[k] = re.sub(r"\s+", "", obj[k])
|
||||
return obj, None
|
||||
return None, last
|
||||
|
||||
|
||||
def _load_jwk() -> dict:
|
||||
"""OKTA_SYNC_PRIVATE_KEY is the private key as a JWK. Two forms are accepted:
|
||||
the JSON itself (it starts with "{"), or that JSON encoded as base64. Use base64
|
||||
in a Portainer environment field: it has no quote marks to be mangled.
|
||||
|
||||
The reader forgives the usual copy and paste damage: quote marks around the whole
|
||||
value, spaces and line breaks inside it, missing "=" padding, the URL-safe base64
|
||||
alphabet, a byte-order mark at the start of the file, and a JWK Set
|
||||
({"keys": [one key]}) in place of a single key. Raises OktaSyncError that says
|
||||
which step failed. A message never holds any text of the key, only counts."""
|
||||
raw = _key_raw()
|
||||
if not raw:
|
||||
raise _fail("OKTA_SYNC_PRIVATE_KEY is not set")
|
||||
if len(raw) >= 2 and raw[0] == raw[-1] and raw[0] in "\"'" and raw[0] not in raw[1:-1]:
|
||||
raw = raw[1:-1].strip()
|
||||
if raw.startswith(("{", '"', "-----BEGIN")):
|
||||
text = raw
|
||||
else:
|
||||
compact = re.sub(r"\s+", "", raw)
|
||||
compact += "=" * (-len(compact) % 4)
|
||||
decoded = None
|
||||
for decode in (base64.b64decode, base64.urlsafe_b64decode):
|
||||
try:
|
||||
decoded = decode(compact)
|
||||
break
|
||||
except Exception: # noqa: BLE001
|
||||
continue
|
||||
if decoded is None:
|
||||
raise _fail(f"OKTA_SYNC_PRIVATE_KEY is not valid base64 ({len(compact)} characters after "
|
||||
"removing spaces; it should be one long line of letters, digits, + / =)")
|
||||
try:
|
||||
text = decoded.decode("utf-8-sig")
|
||||
except UnicodeDecodeError:
|
||||
raise _fail("OKTA_SYNC_PRIVATE_KEY decodes from base64 but the result is not text") from None
|
||||
text = text.lstrip("\ufeff").strip()
|
||||
via = "" if raw.startswith(("{", '"', "-----BEGIN")) else " after decoding base64"
|
||||
if text.startswith("-----BEGIN"):
|
||||
raise _fail("OKTA_SYNC_PRIVATE_KEY holds a PEM key (-----BEGIN ...). A JWK is needed: "
|
||||
"JSON with the fields kty, kid, n, e and d")
|
||||
if text.startswith('"'):
|
||||
text = "{" + text.rstrip(",") + "}" # the fields were copied without the outer braces
|
||||
jwk, problem = _parse_json_forgiving(text)
|
||||
if problem is not None and not text.startswith("{"):
|
||||
# One more layer? A value that was encoded twice decodes to base64 text.
|
||||
try:
|
||||
squeezed = re.sub(r"\s+", "", text)
|
||||
again = base64.b64decode(squeezed + "=" * (-len(squeezed) % 4)).decode("utf-8-sig").strip()
|
||||
jwk, problem = _parse_json_forgiving(again)
|
||||
except Exception: # noqa: BLE001
|
||||
pass
|
||||
if problem is not None:
|
||||
shape = ("starts with a brace" if text.startswith("{") else
|
||||
"starts with a letter or digit" if text[:1].isalnum() else "starts with a symbol")
|
||||
raise _fail(f"OKTA_SYNC_PRIVATE_KEY is not JSON{via} ({len(text)} characters, {shape}; {problem})")
|
||||
if isinstance(jwk, dict) and isinstance(jwk.get("keys"), list):
|
||||
if len(jwk["keys"]) != 1:
|
||||
raise _fail("OKTA_SYNC_PRIVATE_KEY is a key set with more than one key; paste one key")
|
||||
jwk = jwk["keys"][0]
|
||||
if not isinstance(jwk, dict) or jwk.get("kty") not in {"RSA", "EC"}:
|
||||
raise _fail("OKTA_SYNC_PRIVATE_KEY is JSON but not an RSA or EC key")
|
||||
if "d" not in jwk:
|
||||
raise _fail("OKTA_SYNC_PRIVATE_KEY holds a PUBLIC key; the private key is needed to sign")
|
||||
return jwk
|
||||
|
||||
|
||||
def _client_assertion(jwk: dict, client_id: str, audience: str) -> str:
|
||||
"""The signed JWT that proves we hold the key. Okta's rules: iss and sub are the
|
||||
client ID, aud is the token endpoint, and the lifetime is at most 60 minutes."""
|
||||
alg = jwk.get("alg") or ("RS256" if jwk["kty"] == "RSA" else "ES256")
|
||||
algorithms = jwt.algorithms.get_default_algorithms()
|
||||
if alg not in algorithms:
|
||||
raise _fail(f"the key names an algorithm that is not supported ({alg})")
|
||||
try:
|
||||
key = algorithms[alg].from_jwk(json.dumps(jwk))
|
||||
now = int(time.time())
|
||||
return jwt.encode(
|
||||
{"iss": client_id, "sub": client_id, "aud": audience, "iat": now,
|
||||
"exp": now + ASSERTION_LIFETIME_SECONDS, "jti": uuid.uuid4().hex},
|
||||
key, algorithm=alg, headers={"kid": jwk["kid"]} if jwk.get("kid") else None)
|
||||
except OktaSyncError:
|
||||
raise
|
||||
except Exception as exc: # noqa: BLE001
|
||||
raise _fail(f"the key could not sign ({type(exc).__name__})") from None
|
||||
|
||||
|
||||
def _dpop_mode() -> str:
|
||||
"""OKTA_SYNC_DPOP (D24): "auto" (the default), "on" or "off".
|
||||
auto: ask for a plain Bearer token first. If Okta answers invalid_dpop_proof, the
|
||||
Okta app requires DPoP, and the sync asks again with DPoP. Nothing to set.
|
||||
on: always use DPoP.
|
||||
off: never use DPoP. A DPoP-bound token is then refused with a clear message."""
|
||||
v = os.getenv("OKTA_SYNC_DPOP", "").strip().lower()
|
||||
if v in {"1", "true", "yes", "on"}:
|
||||
return "on"
|
||||
if v in {"0", "false", "no", "off"}:
|
||||
return "off"
|
||||
return "auto"
|
||||
|
||||
|
||||
def _url_no_query(url: str) -> str:
|
||||
p = urlparse(url)
|
||||
return f"{p.scheme}://{p.netloc}{p.path}"
|
||||
|
||||
|
||||
class _Dpop:
|
||||
"""DPoP (RFC 9449): proof that the caller holds a key, sent with every request, so
|
||||
a stolen access token alone is useless. One throwaway EC key (P-256) is made for each
|
||||
run and is never stored or logged. Each request gets its own signed proof: the method
|
||||
(htm), the URL without its query (htu), a one-time id (jti), the time (iat), the
|
||||
server's latest nonce if it gave one, and for an API call a hash of the access token
|
||||
(ath). The public half of the key travels in the proof header."""
|
||||
|
||||
def __init__(self):
|
||||
self._key = ec.generate_private_key(ec.SECP256R1())
|
||||
pub = json.loads(jwt.algorithms.ECAlgorithm.to_jwk(self._key.public_key()))
|
||||
self._pub = {k: pub[k] for k in ("kty", "crv", "x", "y")}
|
||||
self.nonce: Optional[str] = None
|
||||
|
||||
def proof(self, method: str, url: str, access_token: Optional[str] = None) -> str:
|
||||
claims = {"jti": uuid.uuid4().hex, "htm": method.upper(), "htu": _url_no_query(url),
|
||||
"iat": int(time.time())}
|
||||
if self.nonce:
|
||||
claims["nonce"] = self.nonce
|
||||
if access_token:
|
||||
claims["ath"] = base64.urlsafe_b64encode(
|
||||
hashlib.sha256(access_token.encode()).digest()).rstrip(b"=").decode()
|
||||
return jwt.encode(claims, self._key, algorithm="ES256",
|
||||
headers={"typ": "dpop+jwt", "jwk": self._pub})
|
||||
|
||||
def take_nonce(self, resp: httpx.Response) -> Optional[str]:
|
||||
n = resp.headers.get("DPoP-Nonce")
|
||||
if n:
|
||||
self.nonce = n
|
||||
return n
|
||||
|
||||
|
||||
class _Credential:
|
||||
"""What is sent with each Management API call: an SSWS token, a Bearer token, or a
|
||||
DPoP-bound token with a fresh proof for every request."""
|
||||
|
||||
def __init__(self, scheme: str, token: str, dpop: Optional[_Dpop] = None):
|
||||
self.scheme, self.token, self.dpop = scheme, token, dpop
|
||||
|
||||
def headers(self, method: str, url: str) -> dict:
|
||||
h = {"Accept": "application/json"}
|
||||
if self.dpop is not None:
|
||||
h["Authorization"] = f"DPoP {self.token}"
|
||||
h["DPoP"] = self.dpop.proof(method, url, self.token)
|
||||
else:
|
||||
h["Authorization"] = f"{self.scheme} {self.token}"
|
||||
return h
|
||||
|
||||
|
||||
def _error_code(resp: httpx.Response) -> str:
|
||||
"""Okta's OAuth error code (invalid_client, invalid_scope, invalid_dpop_proof, ...).
|
||||
It tells an admin what to fix and holds no secret. Kept only if it is a short code."""
|
||||
try:
|
||||
c = resp.json().get("error", "")
|
||||
if isinstance(c, str) and re.fullmatch(r"[a-z_]{1,40}", c):
|
||||
return c
|
||||
except Exception: # noqa: BLE001
|
||||
pass
|
||||
return ""
|
||||
|
||||
|
||||
def _access_token(client: httpx.Client, base: str) -> _Credential:
|
||||
"""Trade a signed assertion for an access token. Raises OktaSyncError for every
|
||||
failure. Neither the key, the assertion nor the token is logged or put in a message.
|
||||
Each attempt gets a new assertion, because Okta accepts an assertion id only once."""
|
||||
client_id = _client_id()
|
||||
if not client_id:
|
||||
raise _fail("OKTA_SYNC_CLIENT_ID is not set")
|
||||
endpoint = f"{base}/oauth2/v1/token"
|
||||
jwk = _load_jwk()
|
||||
mode = _dpop_mode()
|
||||
dpop: Optional[_Dpop] = _Dpop() if mode == "on" else None
|
||||
nonce_retries = 0
|
||||
resp = None
|
||||
for _ in range(4):
|
||||
headers = {"Accept": "application/json"}
|
||||
if dpop is not None:
|
||||
headers["DPoP"] = dpop.proof("POST", endpoint)
|
||||
try:
|
||||
resp = client.post(
|
||||
endpoint, headers=headers, follow_redirects=False,
|
||||
data={"grant_type": "client_credentials", "scope": OAUTH_SCOPE,
|
||||
"client_assertion_type": "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
|
||||
"client_assertion": _client_assertion(jwk, client_id, endpoint)})
|
||||
except httpx.HTTPError as exc:
|
||||
raise _fail(f"network error on the token request ({type(exc).__name__})") from None
|
||||
if dpop is not None:
|
||||
dpop.take_nonce(resp)
|
||||
if resp.status_code == 200:
|
||||
break
|
||||
code = _error_code(resp)
|
||||
if mode == "auto" and dpop is None and code == "invalid_dpop_proof":
|
||||
dpop = _Dpop() # the Okta app requires DPoP: ask again with it
|
||||
continue
|
||||
if dpop is not None and code == "use_dpop_nonce" and dpop.nonce and nonce_retries < 2:
|
||||
nonce_retries += 1 # Okta asks for its nonce in the proof: ask again with it
|
||||
continue
|
||||
raise _fail(f"Okta answered HTTP {resp.status_code} to the token request"
|
||||
+ (f", error {code}" if code else "")
|
||||
+ (" (DPoP was used)" if dpop is not None else ""))
|
||||
else:
|
||||
raise _fail("the token request did not succeed after retries")
|
||||
try:
|
||||
body = resp.json()
|
||||
except ValueError:
|
||||
raise _fail("the token response is not JSON") from None
|
||||
tok = body.get("access_token") if isinstance(body, dict) else None
|
||||
if not isinstance(tok, str) or not tok:
|
||||
raise _fail("the token response has no access token")
|
||||
kind = str(body.get("token_type", "")).lower()
|
||||
if kind == "dpop":
|
||||
if dpop is None:
|
||||
raise _fail("the token is DPoP-bound but OKTA_SYNC_DPOP is off")
|
||||
return _Credential("DPoP", tok, dpop)
|
||||
if kind == "bearer":
|
||||
return _Credential("Bearer", tok)
|
||||
raise _fail("the token response is neither a Bearer nor a DPoP token")
|
||||
|
||||
|
||||
def is_configured() -> bool:
|
||||
return bool(auth_method() and org_url().startswith("https://"))
|
||||
|
||||
|
||||
def describe() -> str:
|
||||
"""One line for the startup log. Never contains the token."""
|
||||
method = auth_method()
|
||||
if not method:
|
||||
return ("Okta sync DISABLED - set OKTA_API_TOKEN, or OKTA_SYNC_CLIENT_ID and "
|
||||
"OKTA_SYNC_PRIVATE_KEY. Deprovisioning is not detected.")
|
||||
base = org_url()
|
||||
if not base:
|
||||
return "Okta sync DISABLED - no OKTA_ORG_URL and OKTA_ISSUER has no host. Deprovisioning is not detected."
|
||||
if not base.startswith("https://"):
|
||||
return f"Okta sync DISABLED - org URL {base!r} is not https. The token is never sent over http."
|
||||
mode = "DRY RUN, no account is changed" if dry_run() else "live"
|
||||
how = ("API token" if method == "token"
|
||||
else f"OAuth service app (private_key_jwt), DPoP {_dpop_mode()}")
|
||||
return (f"Okta sync configured ({mode}) - org {base}, credential: {how}, "
|
||||
f"identity claim {okta_auth.IDENTITY_CLAIM!r}")
|
||||
|
||||
|
||||
def dry_run() -> bool:
|
||||
"""OKTA_SYNC_DRY_RUN (D22). When it is 1, true, yes or on, the sync does its whole
|
||||
job except the change: it logs who it WOULD disable and writes nothing. Off by
|
||||
default, so a deploy that does not set it behaves as before. Read at each run,
|
||||
so the value in force is the one the worker started with."""
|
||||
return os.getenv("OKTA_SYNC_DRY_RUN", "").strip().lower() in {"1", "true", "yes", "on"}
|
||||
|
||||
|
||||
def _profile_attribute() -> str:
|
||||
override = os.getenv("OKTA_SYNC_PROFILE_ATTRIBUTE", "").strip()
|
||||
if override:
|
||||
return override
|
||||
claim = okta_auth.IDENTITY_CLAIM
|
||||
return _CLAIM_TO_PROFILE.get(claim, claim)
|
||||
|
||||
|
||||
# ── the client ────────────────────────────────────────────────────────────────
|
||||
def _fail(reason: str) -> "OktaSyncError":
|
||||
# One place to build the error, so no call site can add a URL or a header by
|
||||
# accident.
|
||||
return OktaSyncError(f"Okta user list could not be fetched: {reason}")
|
||||
|
||||
|
||||
def _same_origin(url: str, base: str) -> bool:
|
||||
a, b = urlparse(url), urlparse(base)
|
||||
return (a.scheme, a.netloc) == (b.scheme, b.netloc)
|
||||
|
||||
|
||||
def _parse_user(item, attribute: str) -> OktaUser:
|
||||
if not isinstance(item, dict):
|
||||
raise _fail("a list item is not an object")
|
||||
status = item.get("status")
|
||||
profile = item.get("profile")
|
||||
if not isinstance(status, str) or not status.strip():
|
||||
raise _fail("a user has no status")
|
||||
if not isinstance(profile, dict):
|
||||
raise _fail("a user has no profile")
|
||||
value = profile.get(attribute)
|
||||
if not isinstance(value, str) or not value.strip():
|
||||
raise _fail(f"a user has no value for profile attribute {attribute!r}")
|
||||
return OktaUser(identity=value.strip(), status=status.strip().upper())
|
||||
|
||||
|
||||
def _get_page(client: httpx.Client, cred: _Credential, url: str, params: Optional[dict]) -> httpx.Response:
|
||||
"""One GET, with the credential's headers. With DPoP, each call has its own proof,
|
||||
and if the server answers use_dpop_nonce the call is repeated once with its nonce."""
|
||||
for attempt in (0, 1):
|
||||
resp = client.get(url, params=params, headers=cred.headers("GET", url), follow_redirects=False)
|
||||
if cred.dpop is None:
|
||||
return resp
|
||||
nonce = cred.dpop.take_nonce(resp)
|
||||
asks = resp.status_code in (400, 401) and nonce and (
|
||||
"use_dpop_nonce" in resp.headers.get("WWW-Authenticate", "") or "use_dpop_nonce" in resp.text)
|
||||
if attempt == 0 and asks:
|
||||
continue
|
||||
return resp
|
||||
return resp
|
||||
|
||||
|
||||
def fetch_users(http: Optional[httpx.Client] = None) -> "list[OktaUser]":
|
||||
"""Return every user Okta lists, as OktaUser values. Raises OktaSyncError on
|
||||
any problem; never returns an empty list. `http` is for tests (a client built
|
||||
on httpx.MockTransport). In production it is None and a short-lived client is
|
||||
made here."""
|
||||
method = auth_method()
|
||||
base = org_url()
|
||||
if not method:
|
||||
raise _fail("no Okta credential is set (OKTA_API_TOKEN, or OKTA_SYNC_CLIENT_ID and OKTA_SYNC_PRIVATE_KEY)")
|
||||
if not base.startswith("https://"):
|
||||
raise _fail("the org URL is missing or is not https")
|
||||
attribute = _profile_attribute()
|
||||
|
||||
own_client = http is None
|
||||
client = http if http is not None else httpx.Client(timeout=TIMEOUT_SECONDS)
|
||||
users: "list[OktaUser]" = []
|
||||
started = time.monotonic()
|
||||
try:
|
||||
cred = (_Credential("SSWS", _token()) if method == "token"
|
||||
else _access_token(client, base))
|
||||
url: Optional[str] = f"{base}/api/v1/users"
|
||||
params: Optional[dict] = {"limit": PAGE_LIMIT}
|
||||
for _ in range(MAX_PAGES):
|
||||
if not url:
|
||||
break
|
||||
if time.monotonic() - started > TOTAL_DEADLINE_SECONDS:
|
||||
raise _fail(f"took longer than {int(TOTAL_DEADLINE_SECONDS)} seconds in total")
|
||||
try:
|
||||
resp = _get_page(client, cred, url, params)
|
||||
except httpx.HTTPError as exc:
|
||||
# Class name only. The exception text can carry the URL.
|
||||
raise _fail(f"network error ({type(exc).__name__})") from None
|
||||
if resp.status_code != 200:
|
||||
raise _fail(f"Okta answered HTTP {resp.status_code}")
|
||||
try:
|
||||
body = resp.json()
|
||||
except ValueError:
|
||||
raise _fail("the response is not JSON") from None
|
||||
if not isinstance(body, list):
|
||||
raise _fail("the response is not a list of users")
|
||||
users.extend(_parse_user(item, attribute) for item in body)
|
||||
|
||||
nxt = resp.links.get("next", {}).get("url")
|
||||
if not nxt:
|
||||
url = None
|
||||
break
|
||||
if not _same_origin(nxt, base):
|
||||
# Never send the token to a host that the Link header names.
|
||||
raise _fail("a next-page link points to a different host")
|
||||
url, params = nxt, None # the next link already carries its query
|
||||
else:
|
||||
# The loop used every page and the last one still named a next page.
|
||||
raise _fail(f"more than {MAX_PAGES} pages; the list may be cut off")
|
||||
finally:
|
||||
if own_client:
|
||||
client.close()
|
||||
|
||||
if not users:
|
||||
raise _fail("Okta returned no users, which is not credible for a real org")
|
||||
log.info("Okta user list fetched: %d user(s)%s", len(users),
|
||||
" (with DPoP)" if cred.dpop is not None else "")
|
||||
return users
|
||||
|
||||
|
||||
# ── T13.3: the sync job ───────────────────────────────────────────────────────
|
||||
# Which Okta statuses mean "this person is gone or was blocked on purpose".
|
||||
# Decided by Matt, 2026-10-16: a literal "anything that is not ACTIVE" would also
|
||||
# catch PASSWORD_EXPIRED, LOCKED_OUT and RECOVERY, which people leave on their
|
||||
# own, and because this job never re-enables, one AD password expiry would lock a
|
||||
# person out of this app until an admin stepped in. A user who is MISSING from the
|
||||
# list disables too (Okta's default list leaves out DEPROVISIONED users).
|
||||
DISABLING_STATUSES = frozenset({"SUSPENDED", "DEPROVISIONED"})
|
||||
|
||||
SYNC_ACTOR = "system:okta_sync"
|
||||
SYNC_ACTION = "user_deprovisioned_by_sync"
|
||||
|
||||
|
||||
@dataclass
|
||||
class SyncResult:
|
||||
ok: bool # False = the run took NO action at all
|
||||
error: str = "" # why, when ok is False
|
||||
okta_users: int = 0 # size of the Okta list
|
||||
checked: int = 0 # active local accounts looked at
|
||||
disabled: "list[str]" = field(default_factory=list) # usernames disabled this run
|
||||
# (in a dry run: the ones it WOULD disable)
|
||||
dry_run: bool = False # True = nothing was changed on purpose (D22)
|
||||
|
||||
|
||||
def _gen_id(prefix: str) -> str:
|
||||
# Same shape as server/app.py gen_id. Not imported: app.py will import this
|
||||
# module (T13.4), and the reverse import would be circular.
|
||||
return f"{prefix}_{uuid.uuid4().hex[:12]}"
|
||||
|
||||
|
||||
def run_sync(db: Session, fetch: Optional[Callable[[], "list[OktaUser]"]] = None,
|
||||
dry: Optional[bool] = None) -> SyncResult:
|
||||
"""Compare the Okta user list to local accounts and disable the ones Okta no
|
||||
longer vouches for.
|
||||
|
||||
The rules, none of which may be relaxed (D18):
|
||||
|
||||
1. FAIL CLOSED. If fetching raises ANY exception, or the answer cannot be
|
||||
trusted, this function changes nothing and writes nothing. A failure is
|
||||
never read as "nobody is active". The fetch happens before the database
|
||||
is read, so there is no code path from a failed fetch to a change.
|
||||
2. ONLY DISABLE. It sets is_active to False. No input makes it set True.
|
||||
An account that is already disabled is not touched and gets no new
|
||||
audit row, so a run that finds nothing new writes nothing.
|
||||
3. One AuditLog row per disabled account, actor "system:okta_sync", so it
|
||||
is never confused with an admin's action. Rows and changes are saved in
|
||||
one transaction. If saving fails, none of it is kept.
|
||||
4. Only changes are logged. A person who is still active in Okta and still
|
||||
active here produces no row.
|
||||
|
||||
One guard beyond the rules above, inside decision 4's "anything the sync can't
|
||||
confidently parse": if there are active local accounts and NONE of them
|
||||
matches any Okta user, the identity attribute is almost certainly wrong (see
|
||||
OKTA_SYNC_PROFILE_ATTRIBUTE). That is a setup mistake, not an offboarding of
|
||||
the whole company, so the run is refused and nothing changes.
|
||||
|
||||
A local account matches an Okta user by username or by email, ignoring case,
|
||||
the same two fields auth.find_user() uses. If the Okta list holds the same
|
||||
identity twice with different statuses, the account is disabled only when
|
||||
every one of them is a disabling status.
|
||||
|
||||
DRY RUN (D22, OKTA_SYNC_DRY_RUN): every step above runs, including the fetch and
|
||||
both guards, but the account changes and the audit rows are skipped. The run logs
|
||||
who it would disable. Use it for the first deploy, to see the match count and the
|
||||
list before any account is touched.
|
||||
|
||||
`fetch` and `dry` are for tests. In production fetch is None and fetch_users() is
|
||||
used, and dry is None so the environment decides.
|
||||
"""
|
||||
fetch = fetch or fetch_users
|
||||
dry = dry_run() if dry is None else dry
|
||||
|
||||
# 1. The fetch. Nothing below runs unless it returned a list.
|
||||
try:
|
||||
okta_users = fetch()
|
||||
except Exception as exc: # an error of ANY kind, not only OktaSyncError
|
||||
msg = str(exc) if isinstance(exc, OktaSyncError) else f"unexpected {type(exc).__name__}"
|
||||
log.error("Okta sync SKIPPED, no account was changed: %s", msg)
|
||||
return SyncResult(ok=False, error=msg)
|
||||
if not okta_users:
|
||||
# fetch_users() already raises on an empty list. This covers a stand-in
|
||||
# fetch (and any future change) that returns one: same rule.
|
||||
msg = "the fetch returned no users"
|
||||
log.error("Okta sync SKIPPED, no account was changed: %s", msg)
|
||||
return SyncResult(ok=False, error=msg)
|
||||
|
||||
try:
|
||||
by_identity: "dict[str, list[str]]" = {}
|
||||
for u in okta_users:
|
||||
by_identity.setdefault(u.identity.strip().lower(), []).append(u.status.strip().upper())
|
||||
|
||||
locals_ = list(db.scalars(select(models.User).where(models.User.is_active.is_(True))))
|
||||
plan: "list[tuple[models.User, str]]" = []
|
||||
matched = 0
|
||||
for local in locals_:
|
||||
statuses: "list[str]" = []
|
||||
for key in {(local.username or "").strip().lower(), (local.email or "").strip().lower()}:
|
||||
if key:
|
||||
statuses.extend(by_identity.get(key, []))
|
||||
if statuses:
|
||||
matched += 1
|
||||
if all(st in DISABLING_STATUSES for st in statuses):
|
||||
plan.append((local, sorted(set(statuses))[0]))
|
||||
else:
|
||||
plan.append((local, "MISSING"))
|
||||
|
||||
if locals_ and matched == 0:
|
||||
msg = (f"none of {len(locals_)} active local account(s) matches any of "
|
||||
f"{len(okta_users)} Okta user(s); the identity attribute is probably wrong")
|
||||
log.error("Okta sync SKIPPED, no account was changed: %s", msg)
|
||||
return SyncResult(ok=False, error=msg, okta_users=len(okta_users), checked=len(locals_))
|
||||
|
||||
planned = [(local.username, status) for local, status in plan]
|
||||
checked = len(locals_)
|
||||
if not dry:
|
||||
for local, status in plan:
|
||||
local.is_active = False
|
||||
db.add(models.AuditLog(
|
||||
id=_gen_id("ev"), actor=SYNC_ACTOR, action=SYNC_ACTION,
|
||||
entity_type="user", entity_id=local.id, project_id=None,
|
||||
summary=local.username[:400],
|
||||
detail={"okta_status": status, "via": "okta_sync"},
|
||||
))
|
||||
db.commit()
|
||||
except Exception as exc:
|
||||
db.rollback()
|
||||
log.error("Okta sync FAILED, nothing was kept: unexpected %s", type(exc).__name__)
|
||||
return SyncResult(ok=False, error=f"unexpected {type(exc).__name__}")
|
||||
|
||||
if dry:
|
||||
for name, status in planned:
|
||||
log.warning("Okta sync DRY RUN would disable %r (Okta status: %s)", name, status)
|
||||
log.info("Okta sync DRY RUN done, no account was changed: %d Okta user(s), %d active "
|
||||
"local account(s) checked, %d matched, %d would be disabled",
|
||||
len(okta_users), checked, matched, len(planned))
|
||||
return SyncResult(ok=True, okta_users=len(okta_users), checked=checked,
|
||||
disabled=[name for name, _ in planned], dry_run=True)
|
||||
for name, status in planned:
|
||||
log.warning("Okta sync disabled %r (Okta status: %s)", name, status)
|
||||
log.info("Okta sync done: %d Okta user(s), %d active local account(s) checked, %d disabled",
|
||||
len(okta_users), checked, len(planned))
|
||||
return SyncResult(ok=True, okta_users=len(okta_users), checked=checked,
|
||||
disabled=[name for name, _ in planned])
|
||||
|
||||
|
||||
# ── T13.4: running it on a schedule ───────────────────────────────────────────
|
||||
# The api container runs gunicorn with 2 workers, and a later deploy may run more
|
||||
# than one container. Each worker starts its own timer thread below. Without a
|
||||
# gate, all of them would call Okta every cycle and write overlapping changes.
|
||||
# The gate is one row in app_settings (key "okta_sync"). Its updated_at is "when
|
||||
# the last run was claimed". A worker claims a run with ONE atomic conditional
|
||||
# UPDATE: it succeeds only if the last claim is older than the interval. Exactly
|
||||
# one worker wins each cycle, on SQLite and on Postgres, with no lock held.
|
||||
#
|
||||
# The row survives a restart, so a restart inside the interval does not start a
|
||||
# second run. A slow run cannot overlap the next: a fetch is bounded to
|
||||
# TOTAL_DEADLINE_SECONDS (120) and the interval can never be set below
|
||||
# INTERVAL_MIN_SECONDS (300). Inside one process the timer thread runs one cycle at
|
||||
# a time, so cycles cannot stack there either.
|
||||
CLAIM_KEY = "okta_sync"
|
||||
INTERVAL_DEFAULT_SECONDS = 900 # 15 minutes (D18, proposed, not confirmed)
|
||||
INTERVAL_MIN_SECONDS = 300
|
||||
TICK_SECONDS = 60 # how often each worker checks if a run is due
|
||||
START_DELAY_SECONDS = 10 # let the app finish starting first
|
||||
|
||||
|
||||
def interval_seconds() -> int:
|
||||
"""OKTA_SYNC_INTERVAL_SECONDS, default 900. A value that is not a number uses
|
||||
the default. A value under 300 is raised to 300 (see above for why)."""
|
||||
raw = os.getenv("OKTA_SYNC_INTERVAL_SECONDS", "").strip()
|
||||
if not raw:
|
||||
return INTERVAL_DEFAULT_SECONDS
|
||||
try:
|
||||
value = int(raw)
|
||||
except ValueError:
|
||||
log.warning("OKTA_SYNC_INTERVAL_SECONDS=%r is not a whole number; using %d",
|
||||
raw, INTERVAL_DEFAULT_SECONDS)
|
||||
return INTERVAL_DEFAULT_SECONDS
|
||||
if value < INTERVAL_MIN_SECONDS:
|
||||
log.warning("OKTA_SYNC_INTERVAL_SECONDS=%d is under the minimum; using %d",
|
||||
value, INTERVAL_MIN_SECONDS)
|
||||
return INTERVAL_MIN_SECONDS
|
||||
return value
|
||||
|
||||
|
||||
def claim(db: Session, interval: int, now: Optional[datetime] = None) -> bool:
|
||||
"""Try to claim this cycle. True for exactly one caller per interval."""
|
||||
now = now or datetime.now(timezone.utc)
|
||||
cutoff = now - timedelta(seconds=interval)
|
||||
try:
|
||||
row = db.get(models.AppSetting, CLAIM_KEY)
|
||||
if row is None:
|
||||
db.add(models.AppSetting(key=CLAIM_KEY, value={}, updated_at=now))
|
||||
db.commit() # the insert is the claim
|
||||
return True
|
||||
except IntegrityError:
|
||||
db.rollback() # another worker inserted first: it won
|
||||
return False
|
||||
db.rollback() # end the read; the UPDATE starts fresh
|
||||
result = db.execute(
|
||||
update(models.AppSetting)
|
||||
.where(models.AppSetting.key == CLAIM_KEY, models.AppSetting.updated_at <= cutoff)
|
||||
.values(updated_at=now)
|
||||
)
|
||||
db.commit()
|
||||
return result.rowcount == 1
|
||||
|
||||
|
||||
def run_if_due(session_factory: Callable[[], Session],
|
||||
fetch: Optional[Callable[[], "list[OktaUser]"]] = None,
|
||||
now: Optional[datetime] = None) -> Optional[SyncResult]:
|
||||
"""One timer tick: claim the cycle, and if it is ours, run the sync. Returns
|
||||
the SyncResult, or None when the cycle was not ours (or could not be claimed).
|
||||
Nothing here can turn a failure into a change: run_sync() owns that rule, and
|
||||
anything that goes wrong around it is logged and ends the tick."""
|
||||
try:
|
||||
with session_factory() as db:
|
||||
if not claim(db, interval_seconds(), now=now):
|
||||
return None
|
||||
with session_factory() as db:
|
||||
return run_sync(db, fetch=fetch)
|
||||
except Exception as exc:
|
||||
log.error("Okta sync tick failed, no account was changed: unexpected %s", type(exc).__name__)
|
||||
return None
|
||||
|
||||
|
||||
def start_scheduler(session_factory: Callable[[], Session]) -> "Optional[threading.Event]":
|
||||
"""Start the timer thread. Returns a stop Event (set it to end the thread), or
|
||||
None when the sync is off because OKTA_API_TOKEN or the org URL is missing.
|
||||
Logs one line either way, so an operator can see it in `docker compose logs api`."""
|
||||
log.info(describe())
|
||||
if not is_configured():
|
||||
return None
|
||||
stop = threading.Event()
|
||||
|
||||
def loop():
|
||||
if stop.wait(START_DELAY_SECONDS):
|
||||
return
|
||||
while not stop.is_set():
|
||||
run_if_due(session_factory)
|
||||
if stop.wait(TICK_SECONDS):
|
||||
return
|
||||
|
||||
threading.Thread(target=loop, name="okta-sync", daemon=True).start()
|
||||
log.info("Okta sync scheduled: every %d seconds (checked each %d s by every worker; "
|
||||
"one wins each cycle)", interval_seconds(), TICK_SECONDS)
|
||||
return stop
|
||||
@@ -17,7 +17,9 @@ pymssql==2.3.13 # read-only lookups against the Micron asset DB (SQL
|
||||
# MICRON_DB_URL to mssql+pyodbc://…?driver=ODBC+Driver+18+for+SQL+Server
|
||||
pydantic==2.13.4
|
||||
python-dotenv==1.2.2
|
||||
PyJWT==2.13.0 # signed session tokens
|
||||
PyJWT==2.15.1 # signed session tokens. D21 (2026-10-02): was 2.13.0, which
|
||||
# pip-audit lists 13 advisories for (GHSA-8wjv-2p76-3863 let a
|
||||
# crafted cookie raise past decode_token). 2.15.1: none known.
|
||||
starlette==1.3.1 # pinned transitive (cookie / CORS handling — security-relevant)
|
||||
Authlib==1.7.2 # Okta OIDC authorization-code flow (T10.1, wave 10 / D15)
|
||||
httpx==0.28.1 # Authlib's OIDC client needs an HTTP client; explicit, not transitive
|
||||
|
||||
@@ -241,18 +241,16 @@ def main():
|
||||
# Only what the badge RENDERS counts. The phrase still appears in the
|
||||
# comment explaining why it was changed, and asserting on that would be
|
||||
# asserting that the reason cannot be written down.
|
||||
rendered = [ln for ln in src.split("\n")
|
||||
if ("el.textContent" in ln or "el.innerHTML" in ln)
|
||||
and "All changes saved" in ln]
|
||||
# T14.3: the badge's words now live in one place, badgeModel(), as
|
||||
# `text: '...'` entries. Those are the lines that render.
|
||||
texts = [ln for ln in src.split("\n") if "text: '" in ln and "tone: '" in ln]
|
||||
rendered = [ln for ln in texts if "All changes saved" in ln]
|
||||
chk("the badge no longer renders 'All changes saved'", not rendered, rendered[:1])
|
||||
chk("the badge words were found (a rename must not make this check empty)", len(texts) >= 7,
|
||||
"found %d" % len(texts))
|
||||
chk("...and every state it renders names the project",
|
||||
all("the project" in ln
|
||||
for ln in src.split("\n")
|
||||
if ("el.textContent = '" in ln or "el.innerHTML = '" in ln)
|
||||
and "wp-sync-badge" not in ln and ("✓" in ln or "⚠" in ln or "↻" in ln or "✕" in ln)),
|
||||
[ln.strip()[:70] for ln in src.split("\n")
|
||||
if ("el.textContent = '" in ln or "el.innerHTML = '" in ln)
|
||||
and "the project" not in ln and ("✓" in ln or "⚠" in ln or "↻" in ln or "✕" in ln)])
|
||||
all("the project" in ln for ln in texts),
|
||||
[ln.strip()[:70] for ln in texts if "the project" not in ln])
|
||||
|
||||
print("\n7. autosave's own registration survives the retired analytics tracker")
|
||||
# This section used to also assert the per-browser usage-analytics dwell
|
||||
|
||||
@@ -207,9 +207,12 @@ def run(page, base, tok):
|
||||
chk("auth resolved to the admin account", page.eval("(window.WP_USER||{}).role") == "admin")
|
||||
chk("the directory is visible",
|
||||
page.eval("getComputedStyle(document.getElementById('users-main')).display") != "none")
|
||||
chk("manager table renders 9 columns",
|
||||
page.eval("document.querySelectorAll('#users-table thead th').length") == 9,
|
||||
# 10 since CR-020 (T12.2): the 9 data columns plus the row-selection column.
|
||||
chk("manager table renders 10 columns (9 + row selection)",
|
||||
page.eval("document.querySelectorAll('#users-table thead th').length") == 10,
|
||||
page.eval("document.querySelectorAll('#users-table thead th').length"))
|
||||
chk("...the first is the selection column",
|
||||
page.eval("!!document.querySelector('#users-table thead th.selcell #sel-all')"))
|
||||
chk("an admin sees every account in the fixture (7)",
|
||||
page.eval("document.querySelectorAll('#users-table tbody tr').length") == 7,
|
||||
page.eval("document.querySelectorAll('#users-table tbody tr').length"))
|
||||
@@ -237,15 +240,22 @@ def run(page, base, tok):
|
||||
|
||||
# Your own row: permissions locked so you cannot demote yourself, job function
|
||||
# still editable. Asserted on the two cells, not "no select in the row".
|
||||
# The cells are found by their HEADER, not by position: CR-020's selection
|
||||
# column moved every column one to the right and these asserted r.cells[3]
|
||||
# and [4], which then read the username and the email (T12.6).
|
||||
ROW = ("const r=[...document.querySelectorAll('#users-table tbody tr')]"
|
||||
".find(r=>r.querySelector('.me-tag'));")
|
||||
".find(r=>r.querySelector('.me-tag'));"
|
||||
"const col=n=>[...document.querySelectorAll('#users-table thead th')]"
|
||||
".findIndex(h=>h.textContent.trim().toLowerCase()===n);")
|
||||
def own(q):
|
||||
return "(()=>{" + ROW + "if(!r)return false;const c=r.cells[3];return " + q + "})()"
|
||||
return "(()=>{" + ROW + "if(!r)return false;const c=r.cells[col('permissions')];return " + q + "})()"
|
||||
chk("your own permissions cell is locked, not a dropdown",
|
||||
page.eval(own("!c.querySelector('select') && !!c.querySelector('.tag')")))
|
||||
chk("...and wears the Administrator pill", page.eval(own("!!c.querySelector('.tag.admin')")))
|
||||
chk("...while your job function stays editable",
|
||||
page.eval("(()=>{" + ROW + "return !!r && !!r.cells[4].querySelector('select')})()"))
|
||||
page.eval("(()=>{" + ROW + "return !!r && !!r.cells[col('project role')].querySelector('select')})()"))
|
||||
chk("...and your own row cannot be ticked for a bulk action",
|
||||
page.eval("(()=>{" + ROW + "return !!r && !r.querySelector('input.selbox')})()"))
|
||||
|
||||
page.eval("[...document.querySelectorAll('#users-table tbody button')]"
|
||||
".find(b=>/project/i.test(b.textContent)).click()")
|
||||
|
||||
326
tests/bulk_users_check.py
Normal file
326
tests/bulk_users_check.py
Normal file
@@ -0,0 +1,326 @@
|
||||
#!/usr/bin/env python3
|
||||
"""CR-020 (wave 12): bulk user administration, server side.
|
||||
|
||||
python tests/bulk_users_check.py
|
||||
|
||||
Self-contained and needs NO browser: a throwaway SQLite database in a temp
|
||||
folder, the API driven in-process with FastAPI's TestClient, everything deleted
|
||||
afterward. Your real wpsuite.db is never touched.
|
||||
|
||||
What it proves is the wave's own exit criteria, measured on the server, where
|
||||
they are enforced:
|
||||
- every bulk action works on a list, row by row, with the single-user rules;
|
||||
- the caller's own account is refused on its own row, never the whole batch;
|
||||
- a super user's batch cannot reach an account or project outside their scope;
|
||||
- one AuditLog row per changed account, the same shape the single route writes;
|
||||
- a partial failure is reported row by row, never as a clean success;
|
||||
- project add/remove never touches the user's other projects or their roles;
|
||||
- the single-user routes, now built on the same functions, behave as before.
|
||||
|
||||
Exit codes: 0 all passed, 1 a check failed, 2 the run could not start.
|
||||
"""
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
import warnings
|
||||
|
||||
warnings.filterwarnings("ignore")
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
TMP = tempfile.mkdtemp(prefix="wp-bulk-users-check-")
|
||||
os.environ["DATABASE_URL"] = "sqlite:///" + os.path.join(TMP, "bulk.db").replace("\\", "/")
|
||||
os.environ["AUTH_SECRET_KEY"] = "bulk-users-check-key-bulk-users-check-0000"
|
||||
os.environ.pop("WP_OKTA_FAKE_DIRECTORY", None)
|
||||
sys.path.insert(0, ROOT)
|
||||
|
||||
try:
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import select
|
||||
from server import app as A, auth, models
|
||||
from server.db import SessionLocal
|
||||
except Exception as exc: # noqa: BLE001
|
||||
print("could not start:", exc)
|
||||
sys.exit(2)
|
||||
|
||||
RESULTS = []
|
||||
|
||||
|
||||
def check(name, ok, detail=""):
|
||||
RESULTS.append(bool(ok))
|
||||
print(f" {'PASS' if ok else 'FAIL'} {name}" + (f" ({detail})" if detail and not ok else ""))
|
||||
|
||||
|
||||
db = SessionLocal()
|
||||
|
||||
|
||||
def account(uid, role="project_user"):
|
||||
u = models.User(id=uid, username=uid[5:], role=role, email=uid[5:] + "@example.test")
|
||||
db.add(u)
|
||||
db.commit()
|
||||
return u
|
||||
|
||||
|
||||
def member(uid, pid, role=""):
|
||||
db.add(models.ProjectMember(id=f"pm_{uid}_{pid}", user_id=uid, project_id=pid, role=role))
|
||||
db.commit()
|
||||
|
||||
|
||||
def client(user):
|
||||
c = TestClient(A.app, base_url="http://127.0.0.1", raise_server_exceptions=False)
|
||||
c.cookies.set(auth.COOKIE_NAME, auth.create_token(user))
|
||||
return c
|
||||
|
||||
|
||||
def fresh():
|
||||
s = SessionLocal()
|
||||
s.expire_all()
|
||||
return s
|
||||
|
||||
|
||||
def audit(action, entity_id=None):
|
||||
s = fresh()
|
||||
q = select(models.AuditLog).where(models.AuditLog.action == action)
|
||||
if entity_id:
|
||||
q = q.where(models.AuditLog.entity_id == entity_id)
|
||||
return s.scalars(q.order_by(models.AuditLog.at)).all()
|
||||
|
||||
|
||||
def roles_of(uid):
|
||||
s = fresh()
|
||||
return {m.project_id: (m.role or "") for m in
|
||||
s.scalars(select(models.ProjectMember).where(models.ProjectMember.user_id == uid))}
|
||||
|
||||
|
||||
def by_user(resp):
|
||||
return {r["user_id"]: r for r in resp.json().get("results", [])}
|
||||
|
||||
|
||||
# ── fixture ──────────────────────────────────────────────────────────────────
|
||||
admin = account("user_admin", "admin")
|
||||
admin2 = account("user_admin2", "admin")
|
||||
superu = account("user_super") # super user on proj1 and proj4 by membership
|
||||
pu = account("user_pu") # plain project user: manages nobody
|
||||
for n in range(1, 10):
|
||||
account(f"user_u{n}")
|
||||
for pid in ("proj1", "proj2", "proj3", "proj4"):
|
||||
db.add(models.Project(id=pid, name=pid))
|
||||
db.commit()
|
||||
member("user_super", "proj1", "project_super_user")
|
||||
member("user_super", "proj4", "project_super_user")
|
||||
member("user_pu", "proj1")
|
||||
for n in (1, 2, 3, 4, 5):
|
||||
member(f"user_u{n}", "proj1")
|
||||
member("user_u6", "proj1"); member("user_u6", "proj2", "project_admin") # partly outside super's scope
|
||||
member("user_u7", "proj2") # wholly outside it
|
||||
member("user_u8", "proj1"); member("user_u8", "proj4", "project_super_user") # a role super may not grant
|
||||
ca, cs, cp = client(admin), client(superu), client(pu)
|
||||
|
||||
|
||||
def single_routes():
|
||||
print("\n0. the single-user routes still behave as before (now on the shared functions)")
|
||||
r = ca.post("/api/auth/users/user_admin/role", json={"role": "project_user"})
|
||||
check("changing your own role: 400, same words", r.status_code == 400 and "own role" in r.json()["detail"])
|
||||
r = ca.post("/api/auth/users/user_admin/active", json={"is_active": False})
|
||||
check("disabling yourself: 400, same words", r.status_code == 400 and "disable your own" in r.json()["detail"])
|
||||
r = ca.delete("/api/auth/users/user_admin")
|
||||
check("deleting yourself: 400, same words", r.status_code == 400 and "delete your own" in r.json()["detail"])
|
||||
r = cs.post("/api/auth/users/user_u6/role", json={"role": "project_admin"})
|
||||
check("a super user changing an account outside their scope: 403", r.status_code == 403)
|
||||
r = cs.put("/api/auth/users/user_u3/projects", json={"project_ids": ["proj1", "proj2"]})
|
||||
check("a super user assigning a project they don't run: 403", r.status_code == 403)
|
||||
r = ca.post("/api/auth/users/user_u9/role", json={"role": "project_admin"})
|
||||
check("a single role change still works and returns the directory row",
|
||||
r.status_code == 200 and r.json().get("role") == "project_admin")
|
||||
one = audit("role_changed", "user_u9")
|
||||
check("...and writes one role_changed row", len(one) == 1 and one[0].detail == {"from": "project_user", "to": "project_admin"},
|
||||
str([e.detail for e in one]))
|
||||
ca.post("/api/auth/users/user_u9/role", json={"role": "project_user"})
|
||||
|
||||
|
||||
def bulk_role():
|
||||
print("\n1. bulk permissions role")
|
||||
r = ca.post("/api/auth/bulk-users/role", json={
|
||||
"user_ids": ["user_u1", "user_u2", "user_admin", "user_nobody", "user_u1", "bad id!"],
|
||||
"role": "project_admin"})
|
||||
res = by_user(r)
|
||||
check("200 even though some rows fail (a partial failure is a result, not an error)", r.status_code == 200)
|
||||
check("u1 and u2 changed", res["user_u1"]["status"] == "ok" and res["user_u2"]["status"] == "ok")
|
||||
check("your own row is refused with its reason; the rest still ran",
|
||||
res["user_admin"]["status"] == "failed" and "own role" in res["user_admin"]["reason"])
|
||||
check("an unknown account is refused as 'User not found'",
|
||||
res["user_nobody"]["status"] == "failed" and res["user_nobody"]["reason"] == "User not found")
|
||||
check("a malformed id is refused, not crashed on", res["bad id!"]["status"] == "failed")
|
||||
check("a duplicated id is applied once", r.json()["requested"] == 5)
|
||||
check("counts match the rows", (r.json()["ok"], r.json()["failed"]) == (2, 3), str(r.json()))
|
||||
rows = audit("role_changed", "user_u1")
|
||||
check("one role_changed audit row per changed account, same shape as the single route",
|
||||
len(rows) == 1 and rows[0].detail == {"from": "project_user", "to": "project_admin"}
|
||||
and rows[0].actor == "admin" and rows[0].entity_type == "user", str([e.detail for e in rows]))
|
||||
r = ca.post("/api/auth/bulk-users/role", json={"user_ids": ["user_u1"], "role": "project_admin"})
|
||||
check("a row already in that role is 'unchanged' and writes no audit row",
|
||||
by_user(r)["user_u1"]["status"] == "unchanged" and len(audit("role_changed", "user_u1")) == 1)
|
||||
r = cs.post("/api/auth/bulk-users/role", json={"user_ids": ["user_u3"], "role": "admin"})
|
||||
check("a super user cannot grant admin in bulk either (grantable_roles, whole request 400)",
|
||||
r.status_code == 400)
|
||||
r = cs.post("/api/auth/bulk-users/role", json={"user_ids": ["user_u3", "user_u6", "user_u7"],
|
||||
"role": "project_admin"})
|
||||
res = by_user(r)
|
||||
check("super user: an account wholly inside their projects changes", res["user_u3"]["status"] == "ok")
|
||||
check("super user: an account also on a project they don't run is refused, with the reason",
|
||||
res["user_u6"]["status"] == "failed" and "don't administer" in res["user_u6"]["reason"],
|
||||
res["user_u6"]["reason"])
|
||||
check("super user: an account they cannot see is 'User not found' (no existence leak)",
|
||||
res["user_u7"]["status"] == "failed" and res["user_u7"]["reason"] == "User not found")
|
||||
r = ca.post("/api/auth/bulk-users/role", json={"user_ids": ["user_admin2"], "role": "project_user"})
|
||||
check("demoting another admin works while one active admin remains (the caller)",
|
||||
by_user(r)["user_admin2"]["status"] == "ok")
|
||||
|
||||
|
||||
def bulk_active():
|
||||
print("\n2. bulk activate / disable")
|
||||
r = ca.post("/api/auth/bulk-users/active", json={"user_ids": ["user_u4", "user_admin", "user_u5"],
|
||||
"is_active": False})
|
||||
res = by_user(r)
|
||||
check("u4 and u5 disabled", res["user_u4"]["status"] == "ok" and res["user_u5"]["status"] == "ok"
|
||||
and not fresh().get(models.User, "user_u4").is_active)
|
||||
check("your own account is refused on its own row",
|
||||
res["user_admin"]["status"] == "failed" and "disable your own" in res["user_admin"]["reason"])
|
||||
check("one user_disabled audit row each, same shape as the single route",
|
||||
[e.detail for e in audit("user_disabled", "user_u4")] == [{"is_active": False}])
|
||||
r = ca.post("/api/auth/bulk-users/active", json={"user_ids": ["user_u4", "user_u2"], "is_active": True})
|
||||
res = by_user(r)
|
||||
check("re-enable: u4 ok, u2 (already active) unchanged",
|
||||
res["user_u4"]["status"] == "ok" and res["user_u2"]["status"] == "unchanged")
|
||||
|
||||
|
||||
def bulk_projects():
|
||||
print("\n3. bulk project add / remove")
|
||||
r = cs.post("/api/auth/bulk-users/projects", json={"user_ids": ["user_u1", "user_u2", "user_u8"],
|
||||
"project_id": "proj1", "op": "add", "role": "project_admin"})
|
||||
res = by_user(r)
|
||||
check("add with a per-project role: every row ok", all(res[u]["status"] == "ok" for u in ("user_u1", "user_u2", "user_u8")),
|
||||
str(res))
|
||||
check("the role landed on that project", roles_of("user_u1").get("proj1") == "project_admin")
|
||||
check("the user's OTHER project and its role are untouched - even a role the caller could not grant",
|
||||
roles_of("user_u8").get("proj4") == "project_super_user", str(roles_of("user_u8")))
|
||||
rows = audit("project_access_changed", "user_u1")
|
||||
check("one project_access_changed row per account, in the single route's shape",
|
||||
len(rows) == 1 and set(rows[0].detail) == {"projects", "scope", "overrides"}
|
||||
and rows[0].detail["scope"] == "managed", str([e.detail for e in rows]))
|
||||
r = cs.post("/api/auth/bulk-users/projects", json={"user_ids": ["user_u1"], "project_id": "proj2", "op": "add"})
|
||||
check("a super user cannot add anyone to a project they don't run (403)", r.status_code == 403)
|
||||
r = cs.post("/api/auth/bulk-users/projects", json={"user_ids": ["user_u1"], "project_id": "proj1",
|
||||
"op": "add", "role": "project_super_user"})
|
||||
check("a super user cannot hand out super user on a project (400)", r.status_code == 400)
|
||||
r = ca.post("/api/auth/bulk-users/projects", json={"user_ids": ["user_u6"], "project_id": "proj3", "op": "add"})
|
||||
check("admin adds u6 to proj3; proj1 and proj2 memberships and roles stay",
|
||||
by_user(r)["user_u6"]["status"] == "ok"
|
||||
and roles_of("user_u6") == {"proj1": "", "proj2": "project_admin", "proj3": ""}, str(roles_of("user_u6")))
|
||||
r = cs.post("/api/auth/bulk-users/projects", json={"user_ids": ["user_u2", "user_u9"], "project_id": "proj1", "op": "remove"})
|
||||
res = by_user(r)
|
||||
check("remove: u2 taken off proj1", res["user_u2"]["status"] == "ok" and "proj1" not in roles_of("user_u2"))
|
||||
check("remove: someone not on it is 'User not found' to a super user who cannot see them",
|
||||
res["user_u9"]["status"] == "failed")
|
||||
r = ca.post("/api/auth/bulk-users/projects", json={"user_ids": ["user_u9"], "project_id": "proj1", "op": "remove"})
|
||||
check("remove: an admin removing someone not on the project gets 'unchanged'",
|
||||
by_user(r)["user_u9"]["status"] == "unchanged")
|
||||
r = ca.post("/api/auth/bulk-users/projects", json={"user_ids": ["user_u1"], "project_id": "proj1", "op": "replace"})
|
||||
check("only add and remove exist: no replace-the-whole-list (400)", r.status_code == 400)
|
||||
|
||||
|
||||
def bulk_delete():
|
||||
print("\n4. bulk delete")
|
||||
r = ca.post("/api/auth/bulk-users/delete", json={"user_ids": ["user_u5", "user_admin", "user_gone"]})
|
||||
res = by_user(r)
|
||||
check("u5 deleted", res["user_u5"]["status"] == "ok" and fresh().get(models.User, "user_u5") is None)
|
||||
check("your own account is refused on its own row, the others still ran",
|
||||
res["user_admin"]["status"] == "failed" and "delete your own" in res["user_admin"]["reason"])
|
||||
check("an account that no longer exists is reported, not hidden",
|
||||
res["user_gone"]["status"] == "failed" and res["user_gone"]["reason"] == "User not found")
|
||||
check("one user_deleted audit row, same shape as the single route",
|
||||
[e.detail for e in audit("user_deleted", "user_u5")] == [{"role": "project_user"}])
|
||||
check("the deleted account's memberships went with it", roles_of("user_u5") == {})
|
||||
|
||||
|
||||
def csv_resolve():
|
||||
print("\n6. CSV upload: usernames in, a line-by-line report out (T12.4)")
|
||||
text = ("username\n" # header, skipped
|
||||
"u3\n"
|
||||
"U3\n" # same person, different case: a duplicate
|
||||
"nobody\n"
|
||||
"\n" # blank lines are not rows
|
||||
"u6\n" # visible to the super user, but also on a project they don't run
|
||||
"u7\n" # not visible to the super user at all
|
||||
"super\n" # the caller themself
|
||||
"u8@example.test,extra column\n" # an email address, matched to exactly one account
|
||||
",\n") # an empty first column
|
||||
r = cs.post("/api/auth/bulk-users/resolve", json={"text": text})
|
||||
j = r.json()
|
||||
rows = {x["line"]: x for x in j["rows"]}
|
||||
check("200 with a row for every non-blank line after the header",
|
||||
r.status_code == 200 and sorted(rows) == [2, 3, 4, 6, 7, 8, 9, 10], sorted(rows))
|
||||
check("a valid username is accepted", rows[2]["status"] == "ok" and rows[2]["user_id"] == "user_u3")
|
||||
check("a repeated username is rejected as a duplicate of its first line, not applied twice",
|
||||
rows[3]["status"] == "rejected" and rows[3]["reason"] == "duplicate of line 2")
|
||||
check("an unknown username: 'User not found', with its line number", rows[4]["reason"] == "User not found")
|
||||
check("an account the caller may not change: rejected with the single route's own reason",
|
||||
rows[6]["status"] == "rejected" and "don't administer" in rows[6]["reason"], rows[6]["reason"])
|
||||
check("an account the caller cannot see: 'User not found' (a CSV leaks no more than the table)",
|
||||
rows[7]["reason"] == "User not found")
|
||||
check("the caller's own account is rejected with a clear reason", "your own account" in rows[8]["reason"])
|
||||
check("an email address resolves when exactly one account has it",
|
||||
rows[9]["status"] == "ok" and rows[9]["user_id"] == "user_u8")
|
||||
check("an empty first column is rejected, not skipped", rows[10]["reason"] == "no username in the first column")
|
||||
check("the ids handed back are exactly the accepted rows",
|
||||
j["user_ids"] == ["user_u3", "user_u8"] and (j["accepted"], j["rejected"]) == (2, 6), str(j["user_ids"]))
|
||||
r = cs.post("/api/auth/bulk-users/active", json={"user_ids": j["user_ids"] + ["user_u7"], "is_active": False})
|
||||
res = by_user(r)
|
||||
check("the CSV's ids go through the same bulk route and the same guards: an id slipped in by hand "
|
||||
"is still refused", res["user_u7"]["status"] == "failed" and res["user_u3"]["status"] == "ok")
|
||||
ca.post("/api/auth/bulk-users/active", json={"user_ids": ["user_u3", "user_u8"], "is_active": True})
|
||||
r = cp.post("/api/auth/bulk-users/resolve", json={"text": "u3"})
|
||||
check("a plain project user cannot resolve a list (403)", r.status_code == 403)
|
||||
r = cs.post("/api/auth/bulk-users/resolve", json={"text": "x" * 200_001})
|
||||
check("an oversized upload is refused (422)", r.status_code == 422)
|
||||
s = fresh()
|
||||
s.add(models.User(id="user_twin1", username="twin1", email="shared@example.test"))
|
||||
s.add(models.User(id="user_twin2", username="twin2", email="shared@example.test"))
|
||||
s.commit()
|
||||
r = ca.post("/api/auth/bulk-users/resolve", json={"text": "shared@example.test"})
|
||||
check("an email shared by two accounts matches neither", r.json()["rows"][0]["reason"] == "User not found")
|
||||
|
||||
|
||||
def gates():
|
||||
print("\n7. who may call, and how much")
|
||||
for path, body in (("role", {"role": "project_user"}), ("active", {"is_active": False}),
|
||||
("projects", {"project_id": "proj1", "op": "add"}), ("delete", {})):
|
||||
r = cp.post(f"/api/auth/bulk-users/{path}", json={"user_ids": ["user_u3"], **body})
|
||||
check(f"a plain project user is refused /bulk-users/{path} (403)", r.status_code == 403, str(r.status_code))
|
||||
anon = TestClient(A.app, base_url="http://127.0.0.1", raise_server_exceptions=False)
|
||||
check("no session: 401", anon.post("/api/auth/bulk-users/delete", json={"user_ids": ["user_u3"]}).status_code == 401)
|
||||
r = ca.post("/api/auth/bulk-users/active", json={"user_ids": [f"user_x{i}" for i in range(501)], "is_active": False})
|
||||
check("more than 500 ids in one request is refused (422)", r.status_code == 422, str(r.status_code))
|
||||
|
||||
|
||||
SECTIONS = [single_routes, bulk_role, bulk_active, bulk_projects, bulk_delete, csv_resolve, gates]
|
||||
|
||||
|
||||
def main() -> int:
|
||||
print("Wave 12 bulk user administration - server checks")
|
||||
try:
|
||||
for section in SECTIONS:
|
||||
try:
|
||||
section()
|
||||
except Exception as exc: # noqa: BLE001 - a missing route must read as FAIL, not a traceback
|
||||
check(f"section {section.__name__} ran to the end", False, f"{type(exc).__name__}: {exc}")
|
||||
finally:
|
||||
db.close()
|
||||
A.engine.dispose()
|
||||
shutil.rmtree(TMP, ignore_errors=True)
|
||||
passed = sum(RESULTS)
|
||||
print(f"\n{passed}/{len(RESULTS)} checks passed.")
|
||||
return 0 if passed == len(RESULTS) else 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
197
tests/bulk_users_ui_check.py
Normal file
197
tests/bulk_users_ui_check.py
Normal file
@@ -0,0 +1,197 @@
|
||||
#!/usr/bin/env python3
|
||||
"""CR-020 (wave 12): bulk user administration in the browser, at 1440px and 390px.
|
||||
|
||||
python tests/bulk_users_ui_check.py
|
||||
|
||||
Self-contained like the other probes: a throwaway SQLite database seeded by
|
||||
browser_check.seed(), its own uvicorn, headless Edge or Chrome over CDP. Your
|
||||
real wpsuite.db is never touched. Run it alone, not back to back with another
|
||||
probe (file-map.md: chained runs exhaust the browser's ports).
|
||||
|
||||
The server rules are proven in tests/bulk_users_check.py without a browser.
|
||||
This file proves the page: selection, the toolbar, the CSV path and the typed
|
||||
delete, end to end against the real server, operable by keyboard, and holding
|
||||
together at 390px. It writes the after-screenshots CLAUDE.md asks for to
|
||||
docs/reference/baseline/wave-12/; the before-shots are the existing
|
||||
users-1440.png and after-wave9/users-390.png.
|
||||
|
||||
Exit 0 all passed, 1 a failure, 2 could not run.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import urllib.request
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
import cdp # noqa: E402
|
||||
from browser_check import seed, start_server, chk, _PASS, _FAIL # noqa: E402
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
SHOTS = os.path.join(ROOT, "docs", "reference", "baseline", "wave-12")
|
||||
READY = "!!document.querySelector('#users-table table')"
|
||||
|
||||
|
||||
def api(base, path, token):
|
||||
req = urllib.request.Request(base + path, headers={"Cookie": "wp_session=" + token})
|
||||
with urllib.request.urlopen(req, timeout=10) as r:
|
||||
return json.loads(r.read().decode())
|
||||
|
||||
|
||||
def user(base, token, username):
|
||||
return next((u for u in api(base, "/api/auth/users", token) if u["username"] == username), None)
|
||||
|
||||
|
||||
def press(page, key, code, vk):
|
||||
"""A REAL key event (Input.dispatchKeyEvent), not a synthetic KeyboardEvent:
|
||||
only a real one triggers a checkbox's native Space toggle - the lesson
|
||||
launcher_check.py records."""
|
||||
for t in ("keyDown", "keyUp"):
|
||||
page.ws.call("Input.dispatchKeyEvent", {"type": t, "key": key, "code": code,
|
||||
"windowsVirtualKeyCode": vk, "text": " " if t == "keyDown" else ""})
|
||||
time.sleep(0.3)
|
||||
|
||||
|
||||
def answer(page, ok=True, typed=None):
|
||||
time.sleep(0.4)
|
||||
if typed is not None:
|
||||
page.eval("document.getElementById('wp-dlg-input').value=%s" % json.dumps(typed))
|
||||
page.eval("document.getElementById('%s').click()" % ("wp-dlg-ok" if ok else "wp-dlg-cancel"))
|
||||
time.sleep(1.2)
|
||||
|
||||
|
||||
def tick(page, username):
|
||||
page.eval("(() => { const b=[...document.querySelectorAll('input.selbox')]"
|
||||
".find(x => x.dataset.id==='user_%s'); if(b) b.click(); })()" % username)
|
||||
time.sleep(0.2)
|
||||
|
||||
|
||||
BAR_390 = """(function(){
|
||||
var bar=document.getElementById('bulk-bar'); var out={sw:document.documentElement.scrollWidth,under:[],clipped:[],n:0};
|
||||
bar.querySelectorAll('button, select').forEach(function(el){
|
||||
var r=el.getBoundingClientRect(); if(!r.width||!r.height) return; out.n++;
|
||||
var id=el.tagName+' '+(el.textContent||el.id).trim().slice(0,20)+' '+Math.round(r.width)+'x'+Math.round(r.height);
|
||||
if(Math.min(r.width,r.height)<24) out.under.push(id);
|
||||
if(r.left<-2||r.right>392) out.clipped.push(id);
|
||||
});
|
||||
return JSON.stringify(out);
|
||||
})()"""
|
||||
|
||||
|
||||
def main():
|
||||
exe = cdp.find_browser()
|
||||
if not exe:
|
||||
print("no headless-capable browser found; set WP_BROWSER.")
|
||||
return 2
|
||||
tmpdir = tempfile.mkdtemp(prefix="wpsuite-bulkui-")
|
||||
db_path = os.path.join(tmpdir, "check.db")
|
||||
server = browser = None
|
||||
try:
|
||||
tok = seed(db_path)
|
||||
port = cdp.free_port()
|
||||
base = "http://127.0.0.1:%d" % port
|
||||
server = start_server(port, db_path)
|
||||
if server is None:
|
||||
print("server would not start")
|
||||
return 2
|
||||
browser = cdp.Browser(exe)
|
||||
page = browser.page()
|
||||
|
||||
def visit(who, width, mobile=False):
|
||||
page.clear_cookies()
|
||||
page.set_cookie("wp_session", tok[who])
|
||||
page.viewport(width, 900 if width > 500 else 844, mobile=mobile)
|
||||
page.goto(base + "/users.html", wait_for=READY)
|
||||
time.sleep(0.6)
|
||||
|
||||
print("\n1. the admin at 1440px")
|
||||
visit("root", 1440)
|
||||
page.screenshot(os.path.join(SHOTS, "users-1440.png"))
|
||||
chk("a selection column with select-all", page.eval("!!document.getElementById('sel-all')"))
|
||||
chk("no bulk bar until a row is selected", page.eval("document.getElementById('bulk-bar').hidden"))
|
||||
page.eval("document.querySelector('input.selbox[data-id=\"user_pat\"]').focus()")
|
||||
press(page, " ", "Space", 32)
|
||||
chk("Space on a focused row checkbox selects it (keyboard operable)",
|
||||
page.eval("document.querySelector('input.selbox[data-id=\"user_pat\"]').checked"))
|
||||
chk("the bar appears, reachable by keyboard",
|
||||
page.eval("!document.getElementById('bulk-bar').hidden && document.querySelector('#bulk-bar button').tabIndex >= 0"))
|
||||
tick(page, "legacy")
|
||||
chk("the bar counts the selection", page.eval("document.getElementById('bulk-count').textContent") == "2 selected")
|
||||
page.screenshot(os.path.join(SHOTS, "users-1440-selected.png"))
|
||||
page.eval("document.getElementById('bulk-role').value='project_admin';"
|
||||
"document.querySelector('[onclick=\"bulkRole()\"]').click()")
|
||||
time.sleep(0.4)
|
||||
chk("the confirm names the count", "for 2 users" in (page.eval("document.getElementById('wp-dlg-msg').textContent") or ""))
|
||||
answer(page)
|
||||
chk("both accounts changed on the server",
|
||||
user(base, tok["root"], "pat")["role"] == "project_admin" and user(base, tok["root"], "legacy")["role"] == "project_admin")
|
||||
chk("the result is announced", "2 changed" in page.eval("document.getElementById('bulk-banner').textContent")
|
||||
and page.eval("document.getElementById('bulk-banner').getAttribute('role')") in ("status", "alert"))
|
||||
tick(page, "pat")
|
||||
page.eval("const f=document.getElementById('user-filter'); f.value='active'; f.dispatchEvent(new Event('change'))")
|
||||
time.sleep(0.3)
|
||||
chk("changing the filter clears the selection, and says so",
|
||||
page.eval("document.getElementById('bulk-bar').hidden") and
|
||||
"filter changed" in page.eval("document.getElementById('sel-banner').textContent"))
|
||||
|
||||
print("\n2. the super user, a CSV with good and bad lines")
|
||||
visit("sue", 1440)
|
||||
page.eval("loadCsv(new File(['username\\npat\\nmix\\nbob\\nsue\\npat\\n'], 'list.csv', {type:'text/csv'}))")
|
||||
time.sleep(1.5)
|
||||
rep = page.eval("document.getElementById('csv-banner').textContent") or ""
|
||||
chk("only the account she may change is selected", page.eval("document.getElementById('bulk-count').textContent") == "1 selected", rep)
|
||||
chk("every bad line is reported by number and reason: another job, not found, herself, a duplicate",
|
||||
all(s in rep for s in ("Line 3 (mix)", "Line 4 (bob): User not found", "Line 5 (sue)", "Line 6 (pat): duplicate of line 2")), rep)
|
||||
page.eval("document.getElementById('bulk-project-role').value='project_admin';"
|
||||
"document.querySelector('[onclick=\"bulkProject(\\'add\\')\"]').click()")
|
||||
answer(page)
|
||||
roles = api(base, "/api/auth/users/user_pat/projects", tok["root"])["roles"]
|
||||
chk("add to project with a role landed on the server", roles.get("projA") == "project_admin", roles)
|
||||
|
||||
print("\n3. bulk delete needs the typed word")
|
||||
visit("root", 1440)
|
||||
tick(page, "bob")
|
||||
page.eval("document.querySelector('[onclick=\"bulkDelete()\"]').click()")
|
||||
time.sleep(0.4)
|
||||
chk("the dialog lists the username", "bob" in page.eval("document.getElementById('wp-dlg-msg').textContent"))
|
||||
page.eval("document.getElementById('wp-dlg-input').value='delete'; document.getElementById('wp-dlg-ok').click()")
|
||||
time.sleep(0.4)
|
||||
chk("a wrong word sends nothing", user(base, tok["root"], "bob") is not None
|
||||
and page.eval("document.getElementById('wp-dlg-overlay').classList.contains('open')"))
|
||||
answer(page, typed="DELETE")
|
||||
chk("DELETE deletes, and the report says so", user(base, tok["root"], "bob") is None
|
||||
and "1 changed" in page.eval("document.getElementById('bulk-banner').textContent"))
|
||||
|
||||
print("\n4. the gloved-hands width: 390px")
|
||||
visit("root", 390, mobile=True)
|
||||
page.screenshot(os.path.join(SHOTS, "users-390.png"))
|
||||
tick(page, "pat")
|
||||
m = json.loads(page.eval(BAR_390))
|
||||
page.screenshot(os.path.join(SHOTS, "users-390-selected.png"))
|
||||
chk("390px with the bar open: no sideways scrolling", m["sw"] <= 392, m["sw"])
|
||||
chk("390px: no bar control clipped past the screen", not m["clipped"], m["clipped"])
|
||||
chk("390px: every bar control meets the 24px floor", m["n"] > 0 and not m["under"], m["under"])
|
||||
|
||||
errs = [e for e in page.js_errors() if "beforeunload" not in e]
|
||||
chk("no JavaScript errors in the whole run", not errs, errs[:3])
|
||||
finally:
|
||||
if browser is not None:
|
||||
try:
|
||||
browser.close()
|
||||
except Exception:
|
||||
pass
|
||||
if server is not None:
|
||||
server.terminate()
|
||||
|
||||
print("\n" + "-" * 54)
|
||||
print("%d/%d checks passed." % (len(_PASS), len(_PASS) + len(_FAIL)))
|
||||
for f in _FAIL:
|
||||
print(" - " + f)
|
||||
return 1 if _FAIL else 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
31
tests/cdp.py
31
tests/cdp.py
@@ -190,8 +190,26 @@ class Browser:
|
||||
f"--user-data-dir={self.profile}", "--remote-allow-origins=*",
|
||||
"--no-first-run", "--no-default-browser-check", "--disable-gpu",
|
||||
"--disable-extensions", "--disable-sync",
|
||||
"--window-size=1400,1000", "about:blank"],
|
||||
"--window-size=1400,1000"]
|
||||
# Run from an elevated (Administrator) shell, Edge and Chrome
|
||||
# "de-elevate": they relaunch themselves as a normal user through
|
||||
# Explorer and the launched process exits rc=0. The relaunch does
|
||||
# not keep the debugging port, so the port is never bound (found
|
||||
# 2026-10-02; Edge logs "Edge is running elevated: 1"). This
|
||||
# Chromium switch keeps the process we started. Ignored when the
|
||||
# shell is not elevated, and on other platforms.
|
||||
+ (["--do-not-de-elevate"] if sys.platform == "win32" else [])
|
||||
+ ["about:blank"],
|
||||
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
# On some Windows installs msedge.exe is a launcher: it starts the real
|
||||
# browser and exits rc=0 at once, and the real browser binds the port a
|
||||
# moment later. Giving up the instant the launched process exited
|
||||
# failed every attempt on such a machine although Edge worked when
|
||||
# started by hand (found 2026-10-02 running the wave 16 probes). An
|
||||
# rc=0 exit now gets a grace period to bind; a non-zero exit is still
|
||||
# a failure at once. Teardown is unaffected: close() sweeps by the
|
||||
# unique profile path, which also catches the handed-off process.
|
||||
exited_at = None
|
||||
for _ in range(160):
|
||||
try:
|
||||
with urllib.request.urlopen(
|
||||
@@ -199,9 +217,14 @@ class Browser:
|
||||
json.load(r)
|
||||
return
|
||||
except Exception:
|
||||
if self.proc.poll() is not None:
|
||||
last = f"exited rc={self.proc.returncode} without binding the port"
|
||||
break
|
||||
rc = self.proc.poll()
|
||||
if rc is not None:
|
||||
last = f"exited rc={rc} without binding the port"
|
||||
if rc != 0:
|
||||
break
|
||||
exited_at = exited_at or time.time()
|
||||
if time.time() - exited_at > 15:
|
||||
break
|
||||
time.sleep(0.25)
|
||||
else:
|
||||
last = "never bound the debugging port"
|
||||
|
||||
@@ -112,6 +112,33 @@ def main():
|
||||
chk("cancel means no: bob is still an account",
|
||||
st == 200 and any(u.get("username") == "bob" for u in (users or [])),
|
||||
ascii_([u.get("username") for u in (users or [])]))
|
||||
# CR-020 / T12.5: the bulk delete is a PROMPT-style dialog (typed DELETE),
|
||||
# the kind this file's docstring says belongs back here.
|
||||
print("\n4. bulk delete needs the typed word")
|
||||
page.eval("(() => { const b=[...document.querySelectorAll('input.selbox')]"
|
||||
".find(x => x.dataset.id==='user_bob'); b.click(); })()")
|
||||
time.sleep(0.3)
|
||||
chk("ticking a row shows the bulk bar",
|
||||
page.eval("!document.getElementById('bulk-bar').hidden"))
|
||||
page.eval("document.querySelector('[onclick=\"bulkDelete()\"]').click()")
|
||||
time.sleep(0.4)
|
||||
chk("the bulk delete asks through the kit's prompt, listing the username",
|
||||
page.eval("(() => { const o=document.getElementById('wp-dlg-overlay');"
|
||||
" return o.classList.contains('open')"
|
||||
" && document.getElementById('wp-dlg-input-wrap').style.display !== 'none'"
|
||||
" && /bob/.test(document.getElementById('wp-dlg-msg').textContent); })()"))
|
||||
page.eval("document.getElementById('wp-dlg-input').value='delete';"
|
||||
"document.getElementById('wp-dlg-ok').click()")
|
||||
time.sleep(0.3)
|
||||
chk("the wrong word keeps it open and names what to type",
|
||||
page.eval("document.getElementById('wp-dlg-overlay').classList.contains('open')"
|
||||
" && /DELETE/.test(document.getElementById('wp-dlg-err').textContent)"))
|
||||
page.eval("document.getElementById('wp-dlg-cancel').click()")
|
||||
time.sleep(0.6)
|
||||
st, users = api(base, "/api/auth/users", tok["root"])
|
||||
chk("cancel means no: bob is still an account after the bulk prompt",
|
||||
st == 200 and any(u.get("username") == "bob" for u in (users or [])))
|
||||
|
||||
errs = [e for e in page.js_errors() if "beforeunload" not in e]
|
||||
chk("no JavaScript errors, and no path reached a native dialog (they throw here)",
|
||||
not errs, ascii_(errs[:3]))
|
||||
|
||||
252
tests/okta_sync_check.py
Normal file
252
tests/okta_sync_check.py
Normal file
@@ -0,0 +1,252 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Does the Okta Management API client keep its promises? - D18 / T13.2.
|
||||
|
||||
The one promise that matters: a failure is never an empty list. T13.3 reads an
|
||||
empty list as "nobody is active", so a client that returned [] when Okta was
|
||||
down would disable the whole org. Every failure below must raise OktaSyncError.
|
||||
|
||||
Also checked: the client uses its own credential (never the OIDC client secret),
|
||||
follows pagination, never sends the token to another host, never follows a
|
||||
redirect, and never lets the token reach a log line, an error message, or
|
||||
describe().
|
||||
|
||||
Self-contained: no server, no database, no network, no browser. httpx's
|
||||
MockTransport stands in for Okta.
|
||||
Exit 0 all passed, 1 a failure.
|
||||
"""
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, ROOT)
|
||||
|
||||
import httpx # noqa: E402
|
||||
|
||||
from server import okta_auth, okta_sync # noqa: E402
|
||||
|
||||
TOKEN = "00tok-SECRET-api-token-xyz"
|
||||
CLIENT_SECRET = "oidc-client-SECRET-abc"
|
||||
ORG = "https://example.okta.com"
|
||||
|
||||
fails = 0
|
||||
|
||||
|
||||
def chk(label, ok, extra=""):
|
||||
global fails
|
||||
print(("PASS " if ok else "FAIL ") + label + (f" [{extra}]" if (extra and not ok) else ""))
|
||||
if not ok:
|
||||
fails += 1
|
||||
|
||||
|
||||
class Capture(logging.Handler):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.lines = []
|
||||
|
||||
def emit(self, record):
|
||||
self.lines.append(record.getMessage())
|
||||
|
||||
|
||||
cap = Capture()
|
||||
logging.getLogger("wpsuite").addHandler(cap)
|
||||
logging.getLogger("wpsuite").setLevel(logging.DEBUG)
|
||||
|
||||
|
||||
def reset_env():
|
||||
os.environ["OKTA_API_TOKEN"] = TOKEN
|
||||
os.environ["OKTA_CLIENT_SECRET"] = CLIENT_SECRET
|
||||
os.environ.pop("OKTA_ORG_URL", None)
|
||||
os.environ.pop("OKTA_SYNC_PROFILE_ATTRIBUTE", None)
|
||||
okta_auth.ISSUER = f"{ORG}/oauth2/default"
|
||||
okta_auth.IDENTITY_CLAIM = "preferred_username"
|
||||
|
||||
|
||||
def user(login, status="ACTIVE", **profile):
|
||||
p = {"login": login, "email": login}
|
||||
p.update(profile)
|
||||
return {"id": "00u" + login, "status": status, "profile": p}
|
||||
|
||||
|
||||
def client(handler):
|
||||
return httpx.Client(transport=httpx.MockTransport(handler))
|
||||
|
||||
|
||||
def raises(handler, label, **kw):
|
||||
"""Run fetch_users against `handler`; it must raise OktaSyncError, and the
|
||||
message must not hold the token."""
|
||||
reset_env()
|
||||
try:
|
||||
out = okta_sync.fetch_users(http=client(handler))
|
||||
except okta_sync.OktaSyncError as exc:
|
||||
msg = str(exc)
|
||||
clean = TOKEN not in msg and CLIENT_SECRET not in msg and "example.okta.com" not in msg
|
||||
chk(f"{label}: raises, message has no token or host", clean, msg)
|
||||
return msg
|
||||
except Exception as exc: # the wrong exception type
|
||||
chk(f"{label}: raises OktaSyncError", False, f"{type(exc).__name__}: {exc}")
|
||||
return ""
|
||||
chk(f"{label}: raises (did NOT raise, returned {out!r})", False)
|
||||
return ""
|
||||
|
||||
|
||||
def ok(body):
|
||||
return lambda req: httpx.Response(200, json=body)
|
||||
|
||||
|
||||
# ── 1. a good fetch ───────────────────────────────────────────────────────────
|
||||
print("\n1. a good fetch")
|
||||
reset_env()
|
||||
seen = []
|
||||
|
||||
|
||||
def good(req):
|
||||
seen.append(req)
|
||||
return httpx.Response(200, json=[user("alice", "ACTIVE"), user("bob", "suspended")])
|
||||
|
||||
|
||||
users = okta_sync.fetch_users(http=client(good))
|
||||
chk("returns every user", [u.identity for u in users] == ["alice", "bob"])
|
||||
chk("status is upper case", [u.status for u in users] == ["ACTIVE", "SUSPENDED"])
|
||||
chk("asks the users endpoint of the org", str(seen[0].url).startswith(f"{ORG}/api/v1/users"))
|
||||
chk("asks for the page maximum", seen[0].url.params.get("limit") == "200")
|
||||
chk("authenticates with the API token (SSWS)", seen[0].headers.get("authorization") == f"SSWS {TOKEN}")
|
||||
chk("never sends the OIDC client secret", CLIENT_SECRET not in str(seen[0].headers))
|
||||
|
||||
# ── 2. pagination ─────────────────────────────────────────────────────────────
|
||||
print("\n2. pagination")
|
||||
reset_env()
|
||||
calls = []
|
||||
|
||||
|
||||
def paged(req):
|
||||
calls.append(req)
|
||||
if "after=p2" not in str(req.url):
|
||||
return httpx.Response(200, json=[user("alice")],
|
||||
headers={"Link": f'<{ORG}/api/v1/users?limit=200&after=p2>; rel="next"'})
|
||||
return httpx.Response(200, json=[user("bob")])
|
||||
|
||||
|
||||
users = okta_sync.fetch_users(http=client(paged))
|
||||
chk("follows the next link and returns both pages", [u.identity for u in users] == ["alice", "bob"])
|
||||
chk("two requests, no more", len(calls) == 2)
|
||||
chk("page 2 also carries the token", calls[1].headers.get("authorization") == f"SSWS {TOKEN}")
|
||||
|
||||
# ── 3. the failure rule: an error is never an empty list ──────────────────────
|
||||
print("\n3. every failure raises (never returns an empty list)")
|
||||
for code in (401, 403, 404, 429, 500, 502, 503):
|
||||
raises(lambda req, c=code: httpx.Response(c, json={"errorCode": "x"}), f"HTTP {code}")
|
||||
raises(lambda req: httpx.Response(302, headers={"Location": "https://evil.example/"}), "a redirect (never followed)")
|
||||
raises(lambda req: (_ for _ in ()).throw(httpx.ConnectError("boom to https://example.okta.com/api/v1/users")), "connection error")
|
||||
raises(lambda req: (_ for _ in ()).throw(httpx.ReadTimeout("slow")), "timeout")
|
||||
raises(lambda req: httpx.Response(200, text="<html>not json</html>"), "body is not JSON")
|
||||
raises(ok({"errorCode": "E0000011"}), "body is an object, not a list")
|
||||
raises(ok("a string"), "body is a string")
|
||||
raises(ok([]), "an empty list (not credible for a real org)")
|
||||
raises(ok(["alice"]), "an item that is not an object")
|
||||
raises(ok([{"profile": {"login": "a"}}]), "an item with no status")
|
||||
raises(ok([{"status": "ACTIVE"}]), "an item with no profile")
|
||||
raises(ok([{"status": "ACTIVE", "profile": {}}]), "an item with no login")
|
||||
raises(ok([{"status": "ACTIVE", "profile": {"login": " "}}]), "an item with a blank login")
|
||||
raises(ok([user("alice"), {"status": "ACTIVE", "profile": {}}]), "one bad item among good ones (never skipped)")
|
||||
|
||||
# ── 4. misconfiguration raises ────────────────────────────────────────────────
|
||||
print("\n4. misconfiguration")
|
||||
reset_env()
|
||||
os.environ["OKTA_API_TOKEN"] = ""
|
||||
try:
|
||||
okta_sync.fetch_users(http=client(good))
|
||||
chk("no token raises", False)
|
||||
except okta_sync.OktaSyncError:
|
||||
chk("no token raises", True)
|
||||
reset_env()
|
||||
okta_auth.ISSUER = ""
|
||||
try:
|
||||
okta_sync.fetch_users(http=client(good))
|
||||
chk("no org URL raises", False)
|
||||
except okta_sync.OktaSyncError:
|
||||
chk("no org URL raises", True)
|
||||
reset_env()
|
||||
os.environ["OKTA_ORG_URL"] = "http://example.okta.com"
|
||||
sent = []
|
||||
try:
|
||||
okta_sync.fetch_users(http=client(lambda r: sent.append(r) or httpx.Response(200, json=[user("a")])))
|
||||
chk("an http org URL raises", False)
|
||||
except okta_sync.OktaSyncError:
|
||||
chk("an http org URL raises", True)
|
||||
chk("...and no request was made (token never sent over http)", sent == [])
|
||||
|
||||
# ── 5. never leaks the token to another host, never loops forever ─────────────
|
||||
print("\n5. next-link safety")
|
||||
reset_env()
|
||||
hosts = []
|
||||
|
||||
|
||||
def evil_link(req):
|
||||
hosts.append(req.url.host)
|
||||
return httpx.Response(200, json=[user("alice")],
|
||||
headers={"Link": '<https://evil.example/steal>; rel="next"'})
|
||||
|
||||
|
||||
raises(evil_link, "a next link to another host")
|
||||
chk("...the other host was never contacted", hosts == ["example.okta.com"])
|
||||
|
||||
count = []
|
||||
|
||||
|
||||
def endless(req):
|
||||
count.append(1)
|
||||
return httpx.Response(200, json=[user("alice")],
|
||||
headers={"Link": f'<{ORG}/api/v1/users?after={len(count)}>; rel="next"'})
|
||||
|
||||
|
||||
raises(endless, "an endless next chain (page cap)")
|
||||
chk(f"...stops at the cap ({okta_sync.MAX_PAGES} requests)", len(count) == okta_sync.MAX_PAGES, str(len(count)))
|
||||
|
||||
# ── 6. org URL and identity attribute ─────────────────────────────────────────
|
||||
print("\n6. org URL and identity attribute")
|
||||
reset_env()
|
||||
chk("org URL is the issuer's scheme and host", okta_sync.org_url() == ORG)
|
||||
os.environ["OKTA_ORG_URL"] = "https://corp.okta.com/"
|
||||
chk("OKTA_ORG_URL wins, trailing slash removed", okta_sync.org_url() == "https://corp.okta.com")
|
||||
reset_env()
|
||||
okta_auth.IDENTITY_CLAIM = "email"
|
||||
body = [{"status": "ACTIVE", "profile": {"login": "alice", "email": "alice@x.com"}}]
|
||||
chk("claim 'email' reads profile.email", okta_sync.fetch_users(http=client(ok(body)))[0].identity == "alice@x.com")
|
||||
okta_auth.IDENTITY_CLAIM = "sAMAccountName"
|
||||
body = [{"status": "ACTIVE", "profile": {"login": "alice@x.com", "sAMAccountName": "alice"}}]
|
||||
chk("a custom claim reads profile[<claim>]", okta_sync.fetch_users(http=client(ok(body)))[0].identity == "alice")
|
||||
os.environ["OKTA_SYNC_PROFILE_ATTRIBUTE"] = "login"
|
||||
chk("OKTA_SYNC_PROFILE_ATTRIBUTE overrides", okta_sync.fetch_users(http=client(ok(body)))[0].identity == "alice@x.com")
|
||||
|
||||
# ── 7. the token never leaks ──────────────────────────────────────────────────
|
||||
print("\n7. the token never leaks")
|
||||
reset_env()
|
||||
chk("describe() has no token", TOKEN not in okta_sync.describe() and "configured" in okta_sync.describe())
|
||||
os.environ["OKTA_API_TOKEN"] = ""
|
||||
chk("describe() says DISABLED without a token", "DISABLED" in okta_sync.describe())
|
||||
reset_env()
|
||||
every_log = " ".join(cap.lines)
|
||||
chk("no log line holds the API token", TOKEN not in every_log)
|
||||
chk("no log line holds the client secret", CLIENT_SECRET not in every_log)
|
||||
|
||||
# ── 8. the whole fetch is time-bounded (T13.4 relies on it) ──────────────────
|
||||
print("\n8. the whole fetch is time-bounded")
|
||||
reset_env()
|
||||
saved = okta_sync.TOTAL_DEADLINE_SECONDS
|
||||
okta_sync.TOTAL_DEADLINE_SECONDS = 0.0
|
||||
|
||||
|
||||
def slow_pages(req):
|
||||
return httpx.Response(200, json=[user("alice")],
|
||||
headers={"Link": f'<{ORG}/api/v1/users?after=x>; rel="next"'})
|
||||
|
||||
|
||||
raises(slow_pages, "a fetch that runs past the total deadline")
|
||||
okta_sync.TOTAL_DEADLINE_SECONDS = saved
|
||||
chk("the real deadline is under the shortest allowed interval",
|
||||
okta_sync.TOTAL_DEADLINE_SECONDS < okta_sync.INTERVAL_MIN_SECONDS)
|
||||
|
||||
print()
|
||||
print("ALL PASSED" if not fails else f"{fails} FAILED")
|
||||
sys.exit(0 if not fails else 1)
|
||||
277
tests/okta_sync_dpop_check.py
Normal file
277
tests/okta_sync_dpop_check.py
Normal file
@@ -0,0 +1,277 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Does the Okta sync use DPoP correctly? - D24 / T13.10.
|
||||
|
||||
A strict fake Okta (httpx.MockTransport) checks every DPoP proof the way RFC 9449 says a
|
||||
server must: the header (typ dpop+jwt, ES256, a PUBLIC jwk only), the signature against
|
||||
that jwk, htm, htu (no query), iat, a one-time jti, the nonce when the server asked for
|
||||
one, and on an API call the ath hash of the access token and the same key as at the token
|
||||
request. Also: auto mode finds out that DPoP is required, the nonce dance at the token
|
||||
endpoint and at the API, paging, off mode, bounded retries, and no secret in any message.
|
||||
|
||||
Self-contained: no network, no server, no browser. Exit 0 all passed, 1 a failure.
|
||||
"""
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from urllib.parse import parse_qs
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, ROOT)
|
||||
os.environ["DATABASE_URL"] = "sqlite://"
|
||||
for v in ("OKTA_API_TOKEN", "OKTA_SYNC_PROFILE_ATTRIBUTE", "OKTA_SYNC_DPOP"):
|
||||
os.environ.pop(v, None)
|
||||
os.environ["OKTA_ORG_URL"] = "https://example.okta.com"
|
||||
|
||||
import httpx # noqa: E402
|
||||
import jwt # noqa: E402
|
||||
from cryptography.hazmat.primitives.asymmetric import rsa # noqa: E402
|
||||
|
||||
from server import okta_sync # noqa: E402
|
||||
from server.okta_sync import OktaSyncError # noqa: E402
|
||||
|
||||
fails = 0
|
||||
|
||||
|
||||
def chk(label, ok, extra=""):
|
||||
global fails
|
||||
print(("PASS " if ok else "FAIL ") + label + (f" [{extra}]" if (extra and not ok) else ""))
|
||||
if not ok:
|
||||
fails += 1
|
||||
|
||||
|
||||
pk = rsa.generate_private_key(public_exponent=65537, key_size=2048)
|
||||
CLIENT_JWK = json.loads(jwt.algorithms.RSAAlgorithm.to_jwk(pk))
|
||||
CLIENT_JWK.update({"kid": "k1", "alg": "RS256", "use": "sig"})
|
||||
CLIENT_PUB = jwt.algorithms.RSAAlgorithm.from_jwk(json.dumps({k: CLIENT_JWK[k] for k in ("kty", "n", "e")}))
|
||||
os.environ["OKTA_SYNC_CLIENT_ID"] = "0oaCLIENT"
|
||||
os.environ["OKTA_SYNC_PRIVATE_KEY"] = json.dumps(CLIENT_JWK)
|
||||
|
||||
TOKEN_URL = "https://example.okta.com/oauth2/v1/token"
|
||||
USERS_URL = "https://example.okta.com/api/v1/users"
|
||||
ACCESS = "ACCESS-TOKEN-VALUE-123"
|
||||
|
||||
|
||||
def thumb(jwk):
|
||||
return json.dumps({k: jwk[k] for k in ("crv", "kty", "x", "y")}, sort_keys=True, separators=(",", ":"))
|
||||
|
||||
|
||||
class StrictOkta:
|
||||
def __init__(self, require_dpop=False, token_nonce=False, api_nonce=False, always_bad_proof=False,
|
||||
token_type_if_plain="Bearer", pages=1, endless_nonce=False):
|
||||
self.endless_nonce = endless_nonce
|
||||
self.require_dpop, self.token_nonce, self.api_nonce = require_dpop, token_nonce, api_nonce
|
||||
self.always_bad_proof, self.token_type_if_plain, self.pages = always_bad_proof, token_type_if_plain, pages
|
||||
self.token_calls = 0
|
||||
self.api_calls = 0
|
||||
self.problems = [] # anything the strict checks found wrong
|
||||
self.jtis = set()
|
||||
self.assertion_jtis = set()
|
||||
self.token_key = None # thumbprint of the key used at the token request
|
||||
self.api_keys = set()
|
||||
self.dpop_on_token = 0
|
||||
self.dpop_on_api = 0
|
||||
self.issued_nonce = "nonce-" + os.urandom(3).hex()
|
||||
self.api_seen = []
|
||||
|
||||
def _check_proof(self, req, url, method, ath=None, need_nonce=False):
|
||||
proof = req.headers.get("dpop")
|
||||
if not proof:
|
||||
return None, "no proof"
|
||||
try:
|
||||
hdr = jwt.get_unverified_header(proof)
|
||||
except Exception as e: # noqa: BLE001
|
||||
return None, f"unreadable header {type(e).__name__}"
|
||||
if hdr.get("typ") != "dpop+jwt" or hdr.get("alg") != "ES256":
|
||||
self.problems.append(f"bad header typ/alg {hdr.get('typ')} {hdr.get('alg')}")
|
||||
jwk = hdr.get("jwk")
|
||||
if not isinstance(jwk, dict) or "d" in jwk or jwk.get("kty") != "EC":
|
||||
self.problems.append("proof jwk missing or holds private part")
|
||||
return None, "bad jwk"
|
||||
try:
|
||||
claims = jwt.decode(proof, jwt.algorithms.ECAlgorithm.from_jwk(json.dumps(jwk)),
|
||||
algorithms=["ES256"], options={"verify_iat": False})
|
||||
except Exception as e: # noqa: BLE001
|
||||
self.problems.append(f"signature did not verify: {type(e).__name__}")
|
||||
return None, "bad signature"
|
||||
if claims.get("htm") != method:
|
||||
self.problems.append(f"htm {claims.get('htm')} != {method}")
|
||||
if claims.get("htu") != url:
|
||||
self.problems.append(f"htu {claims.get('htu')} != {url}")
|
||||
if abs(time.time() - int(claims.get("iat", 0))) > 60:
|
||||
self.problems.append("iat not current")
|
||||
if claims.get("jti") in self.jtis or not claims.get("jti"):
|
||||
self.problems.append("jti missing or reused")
|
||||
self.jtis.add(claims.get("jti"))
|
||||
if ath is not None and claims.get("ath") != ath:
|
||||
self.problems.append("ath wrong")
|
||||
if ath is None and "ath" in claims:
|
||||
self.problems.append("ath present on a token request")
|
||||
if need_nonce and claims.get("nonce") != self.issued_nonce:
|
||||
return jwk, "nonce"
|
||||
return jwk, None
|
||||
|
||||
def handler(self, req: httpx.Request):
|
||||
if req.url.path == "/oauth2/v1/token":
|
||||
self.token_calls += 1
|
||||
form = {k: v[0] for k, v in parse_qs(req.content.decode()).items()}
|
||||
try:
|
||||
a = jwt.decode(form["client_assertion"], CLIENT_PUB, algorithms=["RS256"], audience=TOKEN_URL)
|
||||
if a["jti"] in self.assertion_jtis:
|
||||
self.problems.append("client assertion jti reused")
|
||||
self.assertion_jtis.add(a["jti"])
|
||||
except Exception as e: # noqa: BLE001
|
||||
self.problems.append(f"client assertion bad: {type(e).__name__}")
|
||||
has = "dpop" in req.headers
|
||||
if has:
|
||||
self.dpop_on_token += 1
|
||||
if self.always_bad_proof:
|
||||
return httpx.Response(400, json={"error": "invalid_dpop_proof"})
|
||||
if self.require_dpop and not has:
|
||||
return httpx.Response(400, json={"error": "invalid_dpop_proof"})
|
||||
if not has:
|
||||
return httpx.Response(200, json={"token_type": self.token_type_if_plain, "access_token": ACCESS})
|
||||
jwk, bad = self._check_proof(req, TOKEN_URL, "POST", need_nonce=self.token_nonce)
|
||||
if bad == "nonce" or (self.endless_nonce and not bad):
|
||||
return httpx.Response(400, json={"error": "use_dpop_nonce"}, headers={"DPoP-Nonce": self.issued_nonce})
|
||||
if bad:
|
||||
return httpx.Response(400, json={"error": "invalid_dpop_proof"})
|
||||
self.token_key = thumb(jwk)
|
||||
return httpx.Response(200, json={"token_type": "DPoP", "access_token": ACCESS})
|
||||
if req.url.path == "/api/v1/users":
|
||||
self.api_calls += 1
|
||||
auth = req.headers.get("authorization", "")
|
||||
self.api_seen.append(auth.split(" ")[0])
|
||||
if auth.startswith("DPoP "):
|
||||
self.dpop_on_api += 1
|
||||
if auth != f"DPoP {ACCESS}":
|
||||
self.problems.append("wrong DPoP access token")
|
||||
ath = base64.urlsafe_b64encode(hashlib.sha256(ACCESS.encode()).digest()).rstrip(b"=").decode()
|
||||
jwk, bad = self._check_proof(req, USERS_URL, "GET", ath=ath, need_nonce=self.api_nonce)
|
||||
if bad == "nonce":
|
||||
return httpx.Response(401, json={"errorCode": "invalid_token"},
|
||||
headers={"DPoP-Nonce": self.issued_nonce,
|
||||
"WWW-Authenticate": 'DPoP error="use_dpop_nonce"'})
|
||||
if bad:
|
||||
return httpx.Response(401, json={})
|
||||
self.api_keys.add(thumb(jwk))
|
||||
if thumb(jwk) != self.token_key:
|
||||
self.problems.append("API proof key differs from the token request key")
|
||||
elif auth == f"Bearer {ACCESS}":
|
||||
if "dpop" in req.headers:
|
||||
self.problems.append("DPoP header sent with a plain Bearer token")
|
||||
else:
|
||||
return httpx.Response(401, json={})
|
||||
page = int(req.url.params.get("after", "0") or 0)
|
||||
body = [{"status": "ACTIVE", "profile": {"login": f"user{page}"}}]
|
||||
headers = {}
|
||||
if page + 1 < self.pages:
|
||||
headers["Link"] = f'<{USERS_URL}?after={page + 1}&limit=200>; rel="next"'
|
||||
return httpx.Response(200, json=body, headers=headers)
|
||||
return httpx.Response(404)
|
||||
|
||||
def client(self):
|
||||
return httpx.Client(transport=httpx.MockTransport(self.handler))
|
||||
|
||||
|
||||
def run(fake, mode=None):
|
||||
if mode is None:
|
||||
os.environ.pop("OKTA_SYNC_DPOP", None)
|
||||
else:
|
||||
os.environ["OKTA_SYNC_DPOP"] = mode
|
||||
try:
|
||||
return okta_sync.fetch_users(http=fake.client()), None
|
||||
except OktaSyncError as e:
|
||||
return None, str(e)
|
||||
|
||||
|
||||
# 1. auto: a plain app needs no DPoP
|
||||
f = StrictOkta()
|
||||
users, err = run(f)
|
||||
chk("auto, app without DPoP: works with one token call, Bearer", users and f.token_calls == 1 and f.dpop_on_token == 0 and f.api_seen == ["Bearer"], str(err))
|
||||
chk("no problems found by the strict checks", not f.problems, str(f.problems))
|
||||
|
||||
# 2. auto: the app requires DPoP (what your tenant answered)
|
||||
f = StrictOkta(require_dpop=True)
|
||||
users, err = run(f)
|
||||
chk("auto, app requires DPoP: plain try, then DPoP try, then users", users and f.token_calls == 2 and f.dpop_on_token == 1, f"{err} calls={f.token_calls}")
|
||||
chk("API calls carry DPoP and the access token", f.api_calls == 1 and f.dpop_on_api == 1 and f.api_seen == ["DPoP"])
|
||||
chk("API proof used the same key as the token request", len(f.api_keys) == 1 and f.api_keys == {f.token_key})
|
||||
chk("every proof and assertion id is unique, no problems", not f.problems, str(f.problems))
|
||||
|
||||
# 3. nonce at the token endpoint
|
||||
f = StrictOkta(require_dpop=True, token_nonce=True)
|
||||
users, err = run(f)
|
||||
chk("token nonce: plain, DPoP without nonce, DPoP with nonce", users and f.token_calls == 3, f"{err} calls={f.token_calls}")
|
||||
chk("token nonce: no problems", not f.problems, str(f.problems))
|
||||
|
||||
# 4. nonce at the API
|
||||
f = StrictOkta(require_dpop=True, api_nonce=True)
|
||||
users, err = run(f)
|
||||
chk("API nonce: the call is repeated once with the nonce", users and f.api_calls == 2 and f.dpop_on_api == 2, f"{err} calls={f.api_calls}")
|
||||
chk("API nonce: no problems", not f.problems, str(f.problems))
|
||||
|
||||
# 5. paging: a new proof for every page, htu without the query
|
||||
f = StrictOkta(require_dpop=True, pages=3)
|
||||
users, err = run(f)
|
||||
chk("3 pages with DPoP: 3 users, 3 API calls", users and len(users) == 3 and f.api_calls == 3, f"{err} {f.api_calls}")
|
||||
chk("3 pages: htu had no query, jti unique, same key", not f.problems, str(f.problems))
|
||||
|
||||
# 6. mode on goes straight to DPoP
|
||||
f = StrictOkta(require_dpop=True)
|
||||
users, err = run(f, "on")
|
||||
chk("mode on: DPoP from the first token call", users and f.token_calls == 1 and f.dpop_on_token == 1, f"{err} {f.token_calls}")
|
||||
f = StrictOkta()
|
||||
users, err = run(f, "true")
|
||||
chk("mode on with an app that does not need it: still works", users is not None and not f.problems, f"{err} {f.problems}")
|
||||
|
||||
# 7. mode off
|
||||
f = StrictOkta(require_dpop=True)
|
||||
users, err = run(f, "off")
|
||||
chk("mode off, app requires DPoP: stops after one call, names the error", users is None and f.token_calls == 1 and "invalid_dpop_proof" in err, str(err))
|
||||
f = StrictOkta(token_type_if_plain="DPoP")
|
||||
users, err = run(f, "off")
|
||||
chk("mode off, DPoP-bound token: clear refusal, users never called", users is None and "DPoP-bound" in err and f.api_calls == 0, str(err))
|
||||
f = StrictOkta(token_type_if_plain="mac")
|
||||
users, err = run(f)
|
||||
chk("unknown token type refused", users is None and "neither" in err, str(err))
|
||||
|
||||
# 8. bounded retries and a hopeless server
|
||||
f = StrictOkta(always_bad_proof=True)
|
||||
users, err = run(f)
|
||||
chk("a server that always rejects the proof: bounded calls, clear error", users is None and f.token_calls <= 4 and "invalid_dpop_proof" in err and "DPoP was used" in err, f"{err} {f.token_calls}")
|
||||
chk("no users call when the token failed", f.api_calls == 0)
|
||||
|
||||
f = StrictOkta(require_dpop=True, token_nonce=True, endless_nonce=True)
|
||||
users, err = run(f)
|
||||
chk("a server that asks for a nonce forever: bounded calls, clear error", users is None and f.token_calls <= 4 and "use_dpop_nonce" in err, f"{err} {f.token_calls}")
|
||||
|
||||
# 9. a new key on each run
|
||||
f1, f2 = StrictOkta(require_dpop=True), StrictOkta(require_dpop=True)
|
||||
run(f1); run(f2)
|
||||
chk("each run makes its own DPoP key", f1.token_key and f2.token_key and f1.token_key != f2.token_key)
|
||||
|
||||
# 10. secrets and startup line
|
||||
f = StrictOkta(always_bad_proof=True)
|
||||
_, err = run(f)
|
||||
chk("error holds no token, key or assertion", ACCESS not in err and CLIENT_JWK["d"][:12] not in err)
|
||||
line = okta_sync.describe()
|
||||
chk("startup line names the DPoP mode", "DPoP auto" in line, line)
|
||||
os.environ["OKTA_SYNC_DPOP"] = "on"
|
||||
chk("startup line follows the setting", "DPoP on" in okta_sync.describe())
|
||||
os.environ.pop("OKTA_SYNC_DPOP")
|
||||
|
||||
# 11. the SSWS token path is untouched
|
||||
os.environ["OKTA_API_TOKEN"] = "ssws-value"
|
||||
f = StrictOkta()
|
||||
def ssws_handler(req):
|
||||
f.api_seen.append(req.headers.get("authorization", ""))
|
||||
return httpx.Response(200, json=[{"status": "ACTIVE", "profile": {"login": "a"}}])
|
||||
users = okta_sync.fetch_users(http=httpx.Client(transport=httpx.MockTransport(ssws_handler)))
|
||||
chk("SSWS path unchanged: SSWS header, no DPoP", f.api_seen == ["SSWS ssws-value"] and users, str(f.api_seen))
|
||||
os.environ.pop("OKTA_API_TOKEN")
|
||||
|
||||
print("\nDONE" if fails == 0 else f"\n{fails} FAILED")
|
||||
sys.exit(1 if fails else 0)
|
||||
134
tests/okta_sync_dry_run_check.py
Normal file
134
tests/okta_sync_dry_run_check.py
Normal file
@@ -0,0 +1,134 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Does the Okta sync dry run change nothing? - D22 / T13.7.
|
||||
|
||||
With OKTA_SYNC_DRY_RUN on, the sync must fetch, match and apply both guards exactly
|
||||
as in a live run, report who it WOULD disable, and change no account and write no
|
||||
audit row. With it off (the default) behavior is as before.
|
||||
|
||||
Self-contained: in-memory SQLite, no server, no network, no browser.
|
||||
Exit 0 all passed, 1 a failure.
|
||||
"""
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, ROOT)
|
||||
|
||||
from sqlalchemy import create_engine, select # noqa: E402
|
||||
from sqlalchemy.orm import sessionmaker # noqa: E402
|
||||
from sqlalchemy.pool import StaticPool # noqa: E402
|
||||
|
||||
from server import models, okta_sync # noqa: E402
|
||||
from server.okta_sync import OktaSyncError, OktaUser, run_sync # noqa: E402
|
||||
|
||||
fails = 0
|
||||
|
||||
|
||||
def chk(label, ok, extra=""):
|
||||
global fails
|
||||
print(("PASS " if ok else "FAIL ") + label + (f" [{extra}]" if (extra and not ok) else ""))
|
||||
if not ok:
|
||||
fails += 1
|
||||
|
||||
|
||||
engine = create_engine("sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool)
|
||||
models.Base.metadata.create_all(engine)
|
||||
Session = sessionmaker(bind=engine, autoflush=False, future=True)
|
||||
|
||||
|
||||
def seed(names):
|
||||
with Session() as db:
|
||||
db.query(models.AuditLog).delete()
|
||||
db.query(models.User).delete()
|
||||
for n in names:
|
||||
db.add(models.User(id="u_" + n, username=n, is_active=True, email=""))
|
||||
db.commit()
|
||||
|
||||
|
||||
def state():
|
||||
with Session() as db:
|
||||
return ({u.username: u.is_active for u in db.scalars(select(models.User))},
|
||||
db.query(models.AuditLog).count())
|
||||
|
||||
|
||||
def okta(*pairs):
|
||||
return lambda: [OktaUser(identity=i, status=s) for i, s in pairs]
|
||||
|
||||
|
||||
class Capture(logging.Handler):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.lines = []
|
||||
|
||||
def emit(self, r):
|
||||
self.lines.append(r.getMessage())
|
||||
|
||||
|
||||
cap = Capture()
|
||||
logging.getLogger("wpsuite.okta_sync").addHandler(cap)
|
||||
|
||||
# 1. Flag parsing
|
||||
for v, want in [("", False), ("false", False), ("0", False), ("true", True), ("TRUE", True),
|
||||
("1", True), ("yes", True), ("on", True), (" on ", True), ("maybe", False)]:
|
||||
os.environ["OKTA_SYNC_DRY_RUN"] = v
|
||||
chk(f"OKTA_SYNC_DRY_RUN={v!r} -> {want}", okta_sync.dry_run() is want)
|
||||
os.environ.pop("OKTA_SYNC_DRY_RUN")
|
||||
chk("unset -> live", okta_sync.dry_run() is False)
|
||||
|
||||
# 2. Dry run reports the plan and changes nothing
|
||||
seed(["alice", "bob", "carol", "dan"])
|
||||
before = state()
|
||||
with Session() as db:
|
||||
res = run_sync(db, fetch=okta(("alice", "ACTIVE"), ("bob", "SUSPENDED"), ("carol", "DEPROVISIONED")), dry=True)
|
||||
after = state()
|
||||
chk("dry run ok", res.ok and res.dry_run)
|
||||
chk("dry run lists bob, carol and dan (missing)", sorted(res.disabled) == ["bob", "carol", "dan"], str(res.disabled))
|
||||
chk("dry run changed no account", before == after, f"{before} {after}")
|
||||
chk("dry run wrote no audit row", after[1] == 0)
|
||||
chk("log says DRY RUN would disable", any("DRY RUN would disable 'bob'" in l for l in cap.lines))
|
||||
chk("summary line says no account was changed and counts 3 matched, 3 would be disabled",
|
||||
any("DRY RUN done, no account was changed" in l and "3 matched" in l and "3 would be disabled" in l
|
||||
for l in cap.lines))
|
||||
|
||||
# 3. The environment decides when no argument is given
|
||||
os.environ["OKTA_SYNC_DRY_RUN"] = "true"
|
||||
seed(["alice", "bob"])
|
||||
with Session() as db:
|
||||
res = run_sync(db, fetch=okta(("alice", "ACTIVE"), ("bob", "SUSPENDED")))
|
||||
chk("env on -> dry run", res.dry_run and state()[0]["bob"] is True and state()[1] == 0)
|
||||
|
||||
# 4. Guards still apply in a dry run
|
||||
seed(["alice", "bob"])
|
||||
with Session() as db:
|
||||
res = run_sync(db, fetch=okta(("zed", "ACTIVE")))
|
||||
chk("zero-match guard still refuses in a dry run", (not res.ok) and "probably wrong" in res.error)
|
||||
|
||||
|
||||
def boom():
|
||||
raise OktaSyncError("down")
|
||||
|
||||
|
||||
with Session() as db:
|
||||
res = run_sync(db, fetch=boom)
|
||||
chk("fetch failure still fails closed in a dry run", (not res.ok) and state() == ({"alice": True, "bob": True}, 0))
|
||||
|
||||
# 5. Live run (flag off) is unchanged
|
||||
os.environ.pop("OKTA_SYNC_DRY_RUN")
|
||||
seed(["alice", "bob"])
|
||||
with Session() as db:
|
||||
res = run_sync(db, fetch=okta(("alice", "ACTIVE"), ("bob", "SUSPENDED")))
|
||||
st = state()
|
||||
chk("live run disables and logs one audit row", (not res.dry_run) and st[0]["bob"] is False and st[1] == 1)
|
||||
|
||||
# 6. Startup line names the mode
|
||||
os.environ["OKTA_API_TOKEN"] = "tok-secret-9f3a"
|
||||
os.environ["OKTA_ORG_URL"] = "https://example.okta.com"
|
||||
os.environ["OKTA_SYNC_DRY_RUN"] = "true"
|
||||
chk("startup line says DRY RUN", "DRY RUN" in okta_sync.describe())
|
||||
os.environ.pop("OKTA_SYNC_DRY_RUN")
|
||||
chk("startup line says live", "(live)" in okta_sync.describe())
|
||||
chk("startup line never holds the token", "tok-secret-9f3a" not in okta_sync.describe())
|
||||
|
||||
print("\nDONE" if fails == 0 else f"\n{fails} FAILED")
|
||||
sys.exit(1 if fails else 0)
|
||||
218
tests/okta_sync_job_check.py
Normal file
218
tests/okta_sync_job_check.py
Normal file
@@ -0,0 +1,218 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Does the Okta deprovisioning sync fail closed? - D18 / T13.3.
|
||||
|
||||
The rule that must never be skipped or weakened: when Okta cannot be asked, or
|
||||
its answer cannot be trusted, the sync changes NO account and writes NO audit
|
||||
row. Treating "could not reach Okta" as "nobody is active" would lock the whole
|
||||
company out on one bad afternoon.
|
||||
|
||||
Also checked: the sync only ever disables (it never re-enables, under any
|
||||
input), writes one audit row per change with a non-human actor, leaves an
|
||||
already-disabled account alone, logs only changes, and keeps temporary Okta
|
||||
states (PASSWORD_EXPIRED, LOCKED_OUT, RECOVERY) from disabling anyone.
|
||||
|
||||
Self-contained: its own in-memory SQLite, no server, no network, no browser.
|
||||
Exit 0 all passed, 1 a failure.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, ROOT)
|
||||
|
||||
import httpx # noqa: E402
|
||||
from sqlalchemy import create_engine, select # noqa: E402
|
||||
from sqlalchemy.orm import sessionmaker # noqa: E402
|
||||
from sqlalchemy.pool import StaticPool # noqa: E402
|
||||
|
||||
from server import models, okta_auth, okta_sync # noqa: E402
|
||||
from server.okta_sync import OktaSyncError, OktaUser, run_sync # noqa: E402
|
||||
|
||||
fails = 0
|
||||
|
||||
|
||||
def chk(label, ok, extra=""):
|
||||
global fails
|
||||
print(("PASS " if ok else "FAIL ") + label + (f" [{extra}]" if (extra and not ok) else ""))
|
||||
if not ok:
|
||||
fails += 1
|
||||
|
||||
|
||||
engine = create_engine("sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool)
|
||||
models.Base.metadata.create_all(engine)
|
||||
Session = sessionmaker(bind=engine, autoflush=False, future=True)
|
||||
|
||||
|
||||
def seed(rows):
|
||||
"""rows: list of (username, is_active[, email]). Wipes users and audit rows first."""
|
||||
with Session() as db:
|
||||
db.query(models.AuditLog).delete()
|
||||
db.query(models.User).delete()
|
||||
for r in rows:
|
||||
name, active = r[0], r[1]
|
||||
db.add(models.User(id="u_" + name, username=name, is_active=active,
|
||||
email=(r[2] if len(r) > 2 else "")))
|
||||
db.commit()
|
||||
|
||||
|
||||
def state():
|
||||
with Session() as db:
|
||||
users = {u.username: u.is_active for u in db.scalars(select(models.User))}
|
||||
audits = [a.to_dict() for a in db.scalars(select(models.AuditLog))]
|
||||
return users, audits
|
||||
|
||||
|
||||
def fixed(*pairs):
|
||||
return lambda: [OktaUser(identity=i, status=s) for i, s in pairs]
|
||||
|
||||
|
||||
# ── 1. the failure rule ───────────────────────────────────────────────────────
|
||||
print("\n1. FAIL CLOSED: a failed or untrusted fetch changes nothing")
|
||||
BASE = [("alice", True), ("bob", True), ("carol", True)]
|
||||
|
||||
|
||||
def boom(exc):
|
||||
def f():
|
||||
raise exc
|
||||
return f
|
||||
|
||||
|
||||
failing = [
|
||||
("OktaSyncError (auth failure)", boom(OktaSyncError("HTTP 401"))),
|
||||
("RuntimeError", boom(RuntimeError("boom"))),
|
||||
("ConnectionError", boom(ConnectionError("down"))),
|
||||
("TimeoutError", boom(TimeoutError("slow"))),
|
||||
("ValueError", boom(ValueError("bad"))),
|
||||
("KeyError", boom(KeyError("x"))),
|
||||
("fetch returns an empty list", lambda: []),
|
||||
("fetch returns None", lambda: None),
|
||||
("fetch returns garbage (strings)", lambda: ["alice", "bob"]),
|
||||
("fetch returns garbage (dicts)", lambda: [{"identity": "alice"}]),
|
||||
]
|
||||
for label, fetch in failing:
|
||||
seed(BASE)
|
||||
before = state()
|
||||
with Session() as db:
|
||||
res = run_sync(db, fetch=fetch)
|
||||
after = state()
|
||||
chk(f"{label}: ok is False", res.ok is False)
|
||||
chk(f"{label}: zero accounts changed", after[0] == before[0] == {"alice": True, "bob": True, "carol": True})
|
||||
chk(f"{label}: zero audit rows", after[1] == [] and res.disabled == [])
|
||||
|
||||
print("\n ...and through the real client, not a stand-in")
|
||||
okta_auth.ISSUER = "https://example.okta.com/oauth2/default"
|
||||
os.environ["OKTA_API_TOKEN"] = "tok"
|
||||
for code in (401, 403, 429, 500):
|
||||
seed(BASE)
|
||||
c = httpx.Client(transport=httpx.MockTransport(lambda r, k=code: httpx.Response(k, json={})))
|
||||
with Session() as db:
|
||||
res = run_sync(db, fetch=lambda c=c: okta_sync.fetch_users(http=c))
|
||||
u, a = state()
|
||||
chk(f"real client, HTTP {code}: nothing changed, no audit rows",
|
||||
(not res.ok) and all(u.values()) and a == [])
|
||||
seed(BASE)
|
||||
os.environ["OKTA_API_TOKEN"] = ""
|
||||
with Session() as db:
|
||||
res = run_sync(db) # fetch=None: the default path, no token set
|
||||
u, a = state()
|
||||
chk("default fetch with no token: nothing changed, no audit rows", (not res.ok) and all(u.values()) and a == [])
|
||||
|
||||
print("\n ...and when saving fails")
|
||||
seed(BASE)
|
||||
with Session() as db:
|
||||
real_commit = db.commit
|
||||
|
||||
def bad_commit():
|
||||
raise RuntimeError("disk full")
|
||||
|
||||
db.commit = bad_commit
|
||||
res = run_sync(db, fetch=fixed(("alice", "ACTIVE"))) # bob and carol would be disabled
|
||||
u, a = state()
|
||||
chk("a failed commit keeps no change and no audit row", (not res.ok) and all(u.values()) and a == [])
|
||||
|
||||
print("\n ...and when no local account matches any Okta user (wrong identity attribute)")
|
||||
seed(BASE)
|
||||
with Session() as db:
|
||||
res = run_sync(db, fetch=fixed(("zed@x.com", "ACTIVE"), ("yan@x.com", "ACTIVE")))
|
||||
u, a = state()
|
||||
chk("zero matches is refused, not read as 'everyone left'", (not res.ok) and all(u.values()) and a == [])
|
||||
chk("...and the error says why", "identity attribute" in res.error)
|
||||
|
||||
# ── 2. what it does disable ───────────────────────────────────────────────────
|
||||
print("\n2. a good run disables the right accounts")
|
||||
seed([("alice", True), ("bob", True), ("carol", True), ("dave", True),
|
||||
("erin", True), ("frank", True), ("gina", True), ("hank", True)])
|
||||
okta = fixed(("alice", "ACTIVE"), ("bob", "SUSPENDED"),
|
||||
# carol is MISSING from the list
|
||||
("dave", "DEPROVISIONED"), ("erin", "PASSWORD_EXPIRED"),
|
||||
("frank", "LOCKED_OUT"), ("gina", "RECOVERY"), ("hank", "ACTIVE"))
|
||||
with Session() as db:
|
||||
res = run_sync(db, fetch=okta)
|
||||
users, audits = state()
|
||||
chk("run is ok", res.ok, res.error)
|
||||
chk("missing, SUSPENDED and DEPROVISIONED are disabled",
|
||||
sorted(res.disabled) == ["bob", "carol", "dave"] and
|
||||
[users[n] for n in ("bob", "carol", "dave")] == [False, False, False])
|
||||
chk("ACTIVE stays active", users["alice"] is True and users["hank"] is True)
|
||||
chk("PASSWORD_EXPIRED, LOCKED_OUT and RECOVERY do NOT disable (temporary states)",
|
||||
users["erin"] is True and users["frank"] is True and users["gina"] is True)
|
||||
chk("one audit row per disabled account", len(audits) == 3, str(len(audits)))
|
||||
row = next(a for a in audits if a["summary"] == "bob")
|
||||
chk("audit actor is the sync job, not a person", {a["actor"] for a in audits} == {"system:okta_sync"})
|
||||
chk("audit action is user_deprovisioned_by_sync", {a["action"] for a in audits} == {"user_deprovisioned_by_sync"})
|
||||
chk("audit row names the user and the Okta status found",
|
||||
row["entity_type"] == "user" and row["entity_id"] == "u_bob" and row["detail"].get("okta_status") == "SUSPENDED")
|
||||
chk("a missing user is recorded as MISSING",
|
||||
next(a for a in audits if a["summary"] == "carol")["detail"].get("okta_status") == "MISSING")
|
||||
|
||||
print("\n ...run it again: only changes are logged")
|
||||
with Session() as db:
|
||||
res2 = run_sync(db, fetch=okta)
|
||||
users2, audits2 = state()
|
||||
chk("second run is ok, disables nobody", res2.ok and res2.disabled == [])
|
||||
chk("second run writes no new audit rows (no duplicates)", len(audits2) == 3)
|
||||
chk("accounts unchanged by the second run", users2 == users)
|
||||
|
||||
# ── 3. never re-enables, never touches a disabled account ─────────────────────
|
||||
print("\n3. never re-enables, never touches an already-disabled account")
|
||||
seed([("alice", True), ("hank", False), ("gina", False)])
|
||||
with Session() as db:
|
||||
res = run_sync(db, fetch=fixed(("alice", "ACTIVE"), ("hank", "ACTIVE"))) # gina missing
|
||||
users, audits = state()
|
||||
chk("a disabled account that is ACTIVE in Okta stays disabled", users["hank"] is False)
|
||||
chk("a disabled account that is missing from Okta stays as it is", users["gina"] is False)
|
||||
chk("no audit row for either (nothing changed)", audits == [] and res.disabled == [])
|
||||
src = open(os.path.join(ROOT, "server", "okta_sync.py")).read()
|
||||
chk("no assignment of is_active to True anywhere in okta_sync.py",
|
||||
"is_active = True" not in src and "is_active=True" not in src.replace("is_active.is_(True)", ""))
|
||||
|
||||
# ── 4. matching ───────────────────────────────────────────────────────────────
|
||||
print("\n4. matching")
|
||||
seed([("ALICE", True), ("bob", True, "Bob@X.com"), ("carol", True)])
|
||||
with Session() as db:
|
||||
res = run_sync(db, fetch=fixed(("alice", "ACTIVE"), ("bob@x.com", "ACTIVE"), ("carol", "SUSPENDED")))
|
||||
users, _ = state()
|
||||
chk("username match ignores case", users["ALICE"] is True)
|
||||
chk("email match works, ignoring case", users["bob"] is True)
|
||||
chk("a matching SUSPENDED user is disabled", users["carol"] is False)
|
||||
|
||||
seed([("alice", True), ("bob", True), ("carol", True)])
|
||||
with Session() as db:
|
||||
run_sync(db, fetch=fixed(("alice", "ACTIVE"), ("bob", "ACTIVE"), ("bob", "SUSPENDED"),
|
||||
("carol", "SUSPENDED"), ("carol", "DEPROVISIONED")))
|
||||
users, _ = state()
|
||||
chk("an identity listed twice, one entry active: stays active", users["bob"] is True)
|
||||
chk("an identity listed twice, every entry disabling: disabled", users["carol"] is False)
|
||||
|
||||
# ── 5. the secret never reaches the audit row or the result ───────────────────
|
||||
print("\n5. no secret in what is stored or returned")
|
||||
seed(BASE)
|
||||
os.environ["OKTA_API_TOKEN"] = "SECRET-TOKEN-123"
|
||||
c = httpx.Client(transport=httpx.MockTransport(lambda r: httpx.Response(401, json={})))
|
||||
with Session() as db:
|
||||
res = run_sync(db, fetch=lambda: okta_sync.fetch_users(http=c))
|
||||
chk("the failure message has no token", "SECRET-TOKEN-123" not in res.error and "example.okta.com" not in res.error)
|
||||
|
||||
print()
|
||||
print("ALL PASSED" if not fails else f"{fails} FAILED")
|
||||
sys.exit(0 if not fails else 1)
|
||||
288
tests/okta_sync_oauth_check.py
Normal file
288
tests/okta_sync_oauth_check.py
Normal file
@@ -0,0 +1,288 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Does the Okta sync sign in as an OAuth service app (private_key_jwt)? - D23 / T13.9.
|
||||
|
||||
Checks, with a fake Okta (httpx.MockTransport) and freshly made keys:
|
||||
- the token request carries the right form fields and a JWT that VERIFIES against the
|
||||
public key, with iss and sub = client ID, aud = the token endpoint, kid in the header,
|
||||
a short lifetime, and a one-time jti
|
||||
- the users request then uses the Bearer token, not SSWS
|
||||
- every failure raises OktaSyncError and the users endpoint is never called
|
||||
- the key is accepted as JSON and as base64, RSA and EC; a public key is refused
|
||||
- an SSWS API token still works and wins when both are set
|
||||
- the key, the assertion and the token never appear in an error message or the startup line
|
||||
|
||||
Self-contained: no network, no server, no browser. Exit 0 all passed, 1 a failure.
|
||||
"""
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from urllib.parse import parse_qs
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, ROOT)
|
||||
os.environ["DATABASE_URL"] = "sqlite://"
|
||||
for v in ("OKTA_API_TOKEN", "OKTA_SYNC_CLIENT_ID", "OKTA_SYNC_PRIVATE_KEY", "OKTA_SYNC_PROFILE_ATTRIBUTE"):
|
||||
os.environ.pop(v, None)
|
||||
os.environ["OKTA_ORG_URL"] = "https://example.okta.com"
|
||||
|
||||
import httpx # noqa: E402
|
||||
import jwt # noqa: E402
|
||||
from cryptography.hazmat.primitives.asymmetric import ec, rsa # noqa: E402
|
||||
|
||||
from server import okta_sync # noqa: E402
|
||||
from server.okta_sync import OktaSyncError # noqa: E402
|
||||
|
||||
fails = 0
|
||||
|
||||
|
||||
def chk(label, ok, extra=""):
|
||||
global fails
|
||||
print(("PASS " if ok else "FAIL ") + label + (f" [{extra}]" if (extra and not ok) else ""))
|
||||
if not ok:
|
||||
fails += 1
|
||||
|
||||
|
||||
def make_rsa():
|
||||
pk = rsa.generate_private_key(public_exponent=65537, key_size=2048)
|
||||
jwk = json.loads(jwt.algorithms.RSAAlgorithm.to_jwk(pk))
|
||||
jwk.update({"kid": "rsa-kid-1", "use": "sig", "alg": "RS256"})
|
||||
pub = {k: jwk[k] for k in ("kty", "n", "e")}
|
||||
return jwk, jwt.algorithms.RSAAlgorithm.from_jwk(json.dumps(pub)), "RS256"
|
||||
|
||||
|
||||
def make_ec():
|
||||
pk = ec.generate_private_key(ec.SECP256R1())
|
||||
jwk = json.loads(jwt.algorithms.ECAlgorithm.to_jwk(pk))
|
||||
jwk.update({"kid": "ec-kid-1", "use": "sig", "alg": "ES256"})
|
||||
pub = {k: jwk[k] for k in ("kty", "crv", "x", "y")}
|
||||
return jwk, jwt.algorithms.ECAlgorithm.from_jwk(json.dumps(pub)), "ES256"
|
||||
|
||||
|
||||
class FakeOkta:
|
||||
def __init__(self, pub=None, alg="RS256", token_status=200, token_body=None, user_status=200):
|
||||
self.pub, self.alg = pub, alg
|
||||
self.token_status, self.token_body, self.user_status = token_status, token_body, user_status
|
||||
self.token_calls, self.user_calls, self.seen_auth, self.form, self.verified = 0, 0, [], {}, None
|
||||
|
||||
def handler(self, req: httpx.Request):
|
||||
if req.url.path == "/oauth2/v1/token":
|
||||
self.token_calls += 1
|
||||
self.form = {k: v[0] for k, v in parse_qs(req.content.decode()).items()}
|
||||
if self.pub is not None and "client_assertion" in self.form:
|
||||
try:
|
||||
self.verified = jwt.decode(self.form["client_assertion"], self.pub, algorithms=[self.alg],
|
||||
audience="https://example.okta.com/oauth2/v1/token")
|
||||
except Exception as e: # noqa: BLE001
|
||||
self.verified = e
|
||||
body = self.token_body if self.token_body is not None else {
|
||||
"token_type": "Bearer", "access_token": "ACCESS-TOKEN-VALUE", "expires_in": 3600}
|
||||
return httpx.Response(self.token_status, json=body)
|
||||
if req.url.path == "/api/v1/users":
|
||||
self.user_calls += 1
|
||||
self.seen_auth.append(req.headers.get("authorization", ""))
|
||||
return httpx.Response(self.user_status, json=[
|
||||
{"status": "ACTIVE", "profile": {"login": "alice"}}])
|
||||
return httpx.Response(404)
|
||||
|
||||
def client(self):
|
||||
return httpx.Client(transport=httpx.MockTransport(self.handler))
|
||||
|
||||
|
||||
def setenv(jwk=None, as_b64=False, client_id="0oaCLIENT"):
|
||||
os.environ["OKTA_SYNC_CLIENT_ID"] = client_id
|
||||
if jwk is None:
|
||||
os.environ.pop("OKTA_SYNC_PRIVATE_KEY", None)
|
||||
else:
|
||||
raw = json.dumps(jwk)
|
||||
os.environ["OKTA_SYNC_PRIVATE_KEY"] = base64.b64encode(raw.encode()).decode() if as_b64 else raw
|
||||
|
||||
|
||||
def fetch_err(fake):
|
||||
try:
|
||||
okta_sync.fetch_users(http=fake.client())
|
||||
return None
|
||||
except OktaSyncError as e:
|
||||
return str(e)
|
||||
|
||||
|
||||
# 1. Happy path, RSA, JSON form
|
||||
rsa_jwk, rsa_pub, _ = make_rsa()
|
||||
setenv(rsa_jwk)
|
||||
chk("method is oauth", okta_sync.auth_method() == "oauth")
|
||||
chk("is_configured with client id and key", okta_sync.is_configured())
|
||||
f = FakeOkta(rsa_pub, "RS256")
|
||||
users = okta_sync.fetch_users(http=f.client())
|
||||
chk("returns the user", [u.identity for u in users] == ["alice"])
|
||||
chk("token request fields", f.form.get("grant_type") == "client_credentials"
|
||||
and f.form.get("scope") == "okta.users.read"
|
||||
and f.form.get("client_assertion_type") == "urn:ietf:params:oauth:client-assertion-type:jwt-bearer")
|
||||
v = f.verified
|
||||
chk("assertion verifies against the public key", isinstance(v, dict), repr(v))
|
||||
if isinstance(v, dict):
|
||||
chk("iss and sub are the client id", v.get("iss") == "0oaCLIENT" and v.get("sub") == "0oaCLIENT")
|
||||
chk("aud is the token endpoint", v.get("aud") == "https://example.okta.com/oauth2/v1/token")
|
||||
chk("lifetime is at most 5 minutes", 0 < v["exp"] - v["iat"] <= 300)
|
||||
chk("jti present", bool(v.get("jti")))
|
||||
hdr = jwt.get_unverified_header(f.form["client_assertion"])
|
||||
chk("kid and alg in the header", hdr.get("kid") == "rsa-kid-1" and hdr.get("alg") == "RS256")
|
||||
chk("users call uses Bearer, not SSWS", f.seen_auth == ["Bearer ACCESS-TOKEN-VALUE"], str(f.seen_auth))
|
||||
f2 = FakeOkta(rsa_pub)
|
||||
okta_sync.fetch_users(http=f2.client())
|
||||
chk("each assertion has a new jti", f.verified["jti"] != f2.verified["jti"])
|
||||
|
||||
# 2. base64 form and EC key
|
||||
setenv(rsa_jwk, as_b64=True)
|
||||
f = FakeOkta(rsa_pub)
|
||||
chk("base64 form works", [u.identity for u in okta_sync.fetch_users(http=f.client())] == ["alice"])
|
||||
ec_jwk, ec_pub, _ = make_ec()
|
||||
setenv(ec_jwk)
|
||||
f = FakeOkta(ec_pub, "ES256")
|
||||
okta_sync.fetch_users(http=f.client())
|
||||
chk("EC key works and verifies", isinstance(f.verified, dict), repr(f.verified))
|
||||
|
||||
# 3. Failures raise and never reach the users endpoint
|
||||
setenv(rsa_jwk)
|
||||
for label, fake in [
|
||||
("token endpoint 401", FakeOkta(token_status=401, token_body={"error": "invalid_client"})),
|
||||
("token endpoint 400 invalid_scope", FakeOkta(token_status=400, token_body={"error": "invalid_scope"})),
|
||||
("token endpoint 500", FakeOkta(token_status=500, token_body={})),
|
||||
("no access_token in body", FakeOkta(token_body={"token_type": "Bearer"})),
|
||||
("DPoP token type", FakeOkta(token_body={"token_type": "DPoP", "access_token": "x"})),
|
||||
("body is not an object", FakeOkta(token_body=[1, 2])),
|
||||
]:
|
||||
err = fetch_err(fake)
|
||||
chk(f"{label}: raises, users never called", err is not None and fake.user_calls == 0, str(err))
|
||||
e = fetch_err(FakeOkta(token_status=401, token_body={"error": "invalid_client"}))
|
||||
chk("error carries the Okta error code and the status", "401" in e and "invalid_client" in e, e)
|
||||
e = fetch_err(FakeOkta(token_status=400, token_body={"error": "<script>bad value with spaces</script>"}))
|
||||
chk("an unsafe error value is dropped", "script" not in e and "400" in e, e)
|
||||
|
||||
# 4. Bad key material
|
||||
pub_only = {k: rsa_jwk[k] for k in ("kty", "n", "e", "kid")}
|
||||
for label, raw in [("public key only", json.dumps(pub_only)),
|
||||
("not JSON, not base64", "{{{ not json"),
|
||||
("base64 of something else", base64.b64encode(b"hello").decode()),
|
||||
("wrong kty", json.dumps({"kty": "oct", "k": "AAAA"}))]:
|
||||
os.environ["OKTA_SYNC_PRIVATE_KEY"] = raw
|
||||
fake = FakeOkta()
|
||||
err = fetch_err(fake)
|
||||
chk(f"{label}: refused before any call", err is not None and fake.token_calls == 0 and fake.user_calls == 0, str(err))
|
||||
if label == "public key only":
|
||||
chk("public key: the message says it is a PUBLIC key", err is not None and "PUBLIC" in err, str(err))
|
||||
chk(f"{label}: message holds no key text", err is not None and rsa_jwk["d"][:12] not in err and "hello" not in err)
|
||||
setenv(rsa_jwk, client_id="")
|
||||
os.environ["OKTA_SYNC_CLIENT_ID"] = ""
|
||||
chk("no client id: not configured", okta_sync.auth_method() == "" and not okta_sync.is_configured())
|
||||
os.environ["OKTA_SYNC_CLIENT_ID"] = "0oaCLIENT"
|
||||
os.environ.pop("OKTA_SYNC_PRIVATE_KEY")
|
||||
chk("no key: not configured", okta_sync.auth_method() == "")
|
||||
|
||||
# 4b. Copy and paste damage is forgiven, and the messages say which step failed
|
||||
good = json.dumps(rsa_jwk)
|
||||
b64 = base64.b64encode(good.encode()).decode()
|
||||
variants = {
|
||||
"double quotes around base64": '"' + b64 + '"',
|
||||
"single quotes around base64": "'" + b64 + "'",
|
||||
"line breaks every 76 characters": "\n".join(b64[i:i + 76] for i in range(0, len(b64), 76)),
|
||||
"spaces and a trailing newline": " " + b64[:50] + " " + b64[50:] + " \r\n",
|
||||
"padding removed": b64.rstrip("="),
|
||||
"URL-safe alphabet": base64.urlsafe_b64encode(good.encode()).decode(),
|
||||
"byte-order mark in the file": base64.b64encode(b"\xef\xbb\xbf" + good.encode()).decode(),
|
||||
"JWK set with one key (JSON)": json.dumps({"keys": [rsa_jwk]}),
|
||||
"JWK set with one key (base64)": base64.b64encode(json.dumps({"keys": [rsa_jwk]}).encode()).decode(),
|
||||
}
|
||||
for label, raw in variants.items():
|
||||
os.environ["OKTA_SYNC_PRIVATE_KEY"] = raw
|
||||
try:
|
||||
f = FakeOkta(rsa_pub)
|
||||
okta_sync.fetch_users(http=f.client())
|
||||
chk(f"forgiven: {label}", isinstance(f.verified, dict) and f.user_calls == 1)
|
||||
except OktaSyncError as e:
|
||||
chk(f"forgiven: {label}", False, str(e))
|
||||
# fields copied without the outer braces, and a value encoded twice, are forgiven
|
||||
inner = json.dumps(rsa_jwk)[1:-1]
|
||||
for label, raw in [("fields without braces (text)", inner),
|
||||
("fields without braces (base64)", base64.b64encode(inner.encode()).decode()),
|
||||
("encoded twice", base64.b64encode(b64.encode()).decode())]:
|
||||
os.environ["OKTA_SYNC_PRIVATE_KEY"] = raw
|
||||
try:
|
||||
f = FakeOkta(rsa_pub)
|
||||
okta_sync.fetch_users(http=f.client())
|
||||
chk(f"forgiven: {label}", isinstance(f.verified, dict))
|
||||
except OktaSyncError as e:
|
||||
chk(f"forgiven: {label}", False, str(e))
|
||||
pem = "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkq\n-----END PRIVATE KEY-----"
|
||||
for label, raw in [("PEM text", pem), ("PEM in base64", base64.b64encode(pem.encode()).decode())]:
|
||||
os.environ["OKTA_SYNC_PRIVATE_KEY"] = raw
|
||||
err = fetch_err(FakeOkta()) or ""
|
||||
chk(f"PEM is named: {label}", "PEM" in err and "MIIEvQ" not in err, err)
|
||||
# the pretty-printed, hand-copied shapes that Python's strict reader refuses
|
||||
def b64s(t):
|
||||
return base64.b64encode(t.encode()).decode()
|
||||
|
||||
|
||||
pretty = json.dumps(rsa_jwk, indent=2)
|
||||
wrapped = pretty.replace(rsa_jwk["n"], rsa_jwk["n"][:100] + "\n" + rsa_jwk["n"][100:]) # line break inside a value
|
||||
wrapped_sp = pretty.replace(rsa_jwk["d"], rsa_jwk["d"][:60] + " \r\n " + rsa_jwk["d"][60:])
|
||||
curly = pretty.replace('"', "\u201c", 1)
|
||||
curly = curly.replace('": ', "\u201d: ") # curly quotes after the first
|
||||
trailing = pretty[:-2] + ",\n}"
|
||||
for label, text in [("pretty-printed, 36 lines", pretty),
|
||||
("line break inside the n value", wrapped),
|
||||
("space and line break inside the d value", wrapped_sp),
|
||||
("trailing comma before the closing brace", trailing)]:
|
||||
for how, raw in [("base64", b64s(text)), ("plain", text)]:
|
||||
os.environ["OKTA_SYNC_PRIVATE_KEY"] = raw
|
||||
try:
|
||||
f = FakeOkta(rsa_pub)
|
||||
okta_sync.fetch_users(http=f.client())
|
||||
chk(f"forgiven ({how}): {label}", isinstance(f.verified, dict))
|
||||
except OktaSyncError as e:
|
||||
chk(f"forgiven ({how}): {label}", False, str(e))
|
||||
curly_ok = json.dumps(rsa_jwk, indent=2).replace('"kty"', "\u201ckty\u201d")
|
||||
os.environ["OKTA_SYNC_PRIVATE_KEY"] = b64s(curly_ok)
|
||||
try:
|
||||
f = FakeOkta(rsa_pub)
|
||||
okta_sync.fetch_users(http=f.client())
|
||||
chk("forgiven: curly quote marks", isinstance(f.verified, dict))
|
||||
except OktaSyncError as e:
|
||||
chk("forgiven: curly quote marks", False, str(e))
|
||||
broken = pretty.replace('"kty": "RSA",', '"kty": "RSA" "x",', 1)
|
||||
os.environ["OKTA_SYNC_PRIVATE_KEY"] = b64s(broken)
|
||||
err = fetch_err(FakeOkta()) or ""
|
||||
chk("a real syntax error names the line and column", "line " in err and "column " in err and "starts with a brace" in err, err)
|
||||
chk("the position message holds no key text", rsa_jwk["d"][:10] not in err and rsa_jwk["n"][:10] not in err, err)
|
||||
|
||||
os.environ["OKTA_SYNC_PRIVATE_KEY"] = json.dumps({"keys": [rsa_jwk, rsa_jwk]})
|
||||
chk("a key set with two keys is refused", "more than one key" in (fetch_err(FakeOkta()) or ""))
|
||||
for label, raw, want in [
|
||||
("garbage characters", "!!!not*base64!!!", "not valid base64"),
|
||||
("base64 of non-JSON", base64.b64encode(b"hello world").decode(), "not JSON after decoding base64 (11 characters, starts with a letter or digit;"),
|
||||
("base64 of bytes that are not text", base64.b64encode(b"\xff\xfe\xfd\xfc").decode(), "not text"),
|
||||
("broken JSON", "{ not json", "not JSON"),
|
||||
]:
|
||||
os.environ["OKTA_SYNC_PRIVATE_KEY"] = raw
|
||||
err = fetch_err(FakeOkta()) or ""
|
||||
chk(f"clear message: {label}", want in err, err)
|
||||
chk(f"no key text in message: {label}", raw[:8] not in err.replace("OKTA_SYNC_PRIVATE_KEY", ""), err)
|
||||
|
||||
# 5. An SSWS token still works and wins
|
||||
setenv(rsa_jwk)
|
||||
os.environ["OKTA_API_TOKEN"] = "ssws-token-value"
|
||||
chk("method is token when both are set", okta_sync.auth_method() == "token")
|
||||
f = FakeOkta()
|
||||
okta_sync.fetch_users(http=f.client())
|
||||
chk("SSWS used, no token request made", f.seen_auth == ["SSWS ssws-token-value"] and f.token_calls == 0, str(f.seen_auth))
|
||||
os.environ.pop("OKTA_API_TOKEN")
|
||||
|
||||
# 6. Secrets stay out of the startup line
|
||||
setenv(rsa_jwk)
|
||||
line = okta_sync.describe()
|
||||
chk("startup line names the method", "OAuth service app" in line, line)
|
||||
chk("startup line holds no key part", rsa_jwk["d"][:12] not in line and rsa_jwk["n"][:12] not in line)
|
||||
os.environ.pop("OKTA_SYNC_CLIENT_ID"); os.environ.pop("OKTA_SYNC_PRIVATE_KEY")
|
||||
chk("startup line says DISABLED with no credential", "DISABLED" in okta_sync.describe())
|
||||
|
||||
print("\nDONE" if fails == 0 else f"\n{fails} FAILED")
|
||||
sys.exit(1 if fails else 0)
|
||||
242
tests/okta_sync_schedule_check.py
Normal file
242
tests/okta_sync_schedule_check.py
Normal file
@@ -0,0 +1,242 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Does the Okta sync run on a schedule without doubling up? - D18 / T13.4.
|
||||
|
||||
The api container runs 2 gunicorn workers, and each starts its own timer. Checked
|
||||
here: exactly one of them wins each cycle (including a race on a real file
|
||||
database), a restart inside the interval does not start a second run, the
|
||||
interval is configurable and cannot go below the minimum, the timer thread starts
|
||||
and stops, nothing starts when the sync is not configured, and a failure inside
|
||||
the scheduled path still changes nothing.
|
||||
|
||||
Self-contained: a throwaway SQLite file, threads, no server, no network, no browser.
|
||||
Exit 0 all passed, 1 a failure.
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, ROOT)
|
||||
|
||||
from sqlalchemy import create_engine, select # noqa: E402
|
||||
from sqlalchemy.orm import sessionmaker # noqa: E402
|
||||
|
||||
from server import models, okta_auth, okta_sync # noqa: E402
|
||||
from server.okta_sync import OktaSyncError, OktaUser # noqa: E402
|
||||
|
||||
fails = 0
|
||||
|
||||
|
||||
def chk(label, ok, extra=""):
|
||||
global fails
|
||||
print(("PASS " if ok else "FAIL ") + label + (f" [{extra}]" if (extra and not ok) else ""))
|
||||
if not ok:
|
||||
fails += 1
|
||||
|
||||
|
||||
tmp = tempfile.mkdtemp(prefix="oktasync_")
|
||||
engine = create_engine(f"sqlite:///{os.path.join(tmp, 't.db')}",
|
||||
connect_args={"check_same_thread": False, "timeout": 20}, future=True)
|
||||
models.Base.metadata.create_all(engine)
|
||||
Session = sessionmaker(bind=engine, autoflush=False, future=True)
|
||||
INTERVAL = 900
|
||||
os.environ["OKTA_SYNC_INTERVAL_SECONDS"] = str(INTERVAL)
|
||||
T0 = datetime(2026, 10, 16, 12, 0, 0, tzinfo=timezone.utc)
|
||||
|
||||
|
||||
def wipe(users=()):
|
||||
with Session() as db:
|
||||
db.query(models.AuditLog).delete()
|
||||
db.query(models.User).delete()
|
||||
db.query(models.AppSetting).filter(models.AppSetting.key == okta_sync.CLAIM_KEY).delete()
|
||||
for name, active in users:
|
||||
db.add(models.User(id="u_" + name, username=name, is_active=active))
|
||||
db.commit()
|
||||
|
||||
|
||||
def users_state():
|
||||
with Session() as db:
|
||||
return ({u.username: u.is_active for u in db.scalars(select(models.User))},
|
||||
db.query(models.AuditLog).count())
|
||||
|
||||
|
||||
# ── 1. interval ───────────────────────────────────────────────────────────────
|
||||
print("\n1. interval")
|
||||
def iv(v):
|
||||
if v is None:
|
||||
os.environ.pop("OKTA_SYNC_INTERVAL_SECONDS", None)
|
||||
else:
|
||||
os.environ["OKTA_SYNC_INTERVAL_SECONDS"] = v
|
||||
return okta_sync.interval_seconds()
|
||||
|
||||
chk("default is 900 seconds (15 minutes)", iv(None) == 900 and iv("") == 900)
|
||||
chk("an env value is used", iv("600") == 600 and iv("3600") == 3600)
|
||||
chk("not a number: the default", iv("soon") == 900 and iv("1.5") == 900)
|
||||
chk("under the minimum is raised to 300 (a slow cycle can never overlap the next)",
|
||||
iv("60") == 300 and iv("0") == 300 and iv("-5") == 300)
|
||||
chk("a fetch is bounded below the minimum interval",
|
||||
okta_sync.TOTAL_DEADLINE_SECONDS < okta_sync.INTERVAL_MIN_SECONDS)
|
||||
iv(str(INTERVAL))
|
||||
|
||||
# ── 2. the claim ──────────────────────────────────────────────────────────────
|
||||
print("\n2. the claim gate")
|
||||
wipe()
|
||||
with Session() as db:
|
||||
chk("first ever claim wins (the insert is the claim)", okta_sync.claim(db, INTERVAL, now=T0) is True)
|
||||
with Session() as db:
|
||||
chk("a second claim in the same instant loses", okta_sync.claim(db, INTERVAL, now=T0) is False)
|
||||
with Session() as db:
|
||||
chk("one second before the interval is up: loses",
|
||||
okta_sync.claim(db, INTERVAL, now=T0 + timedelta(seconds=INTERVAL - 1)) is False)
|
||||
with Session() as db:
|
||||
chk("when the interval is up: wins",
|
||||
okta_sync.claim(db, INTERVAL, now=T0 + timedelta(seconds=INTERVAL)) is True)
|
||||
with Session() as db:
|
||||
chk("and then the next caller loses again",
|
||||
okta_sync.claim(db, INTERVAL, now=T0 + timedelta(seconds=INTERVAL + 1)) is False)
|
||||
|
||||
# ── 3. the race ───────────────────────────────────────────────────────────────
|
||||
print("\n3. exactly one of many workers wins (a real race on a file database)")
|
||||
|
||||
|
||||
def race(n=8):
|
||||
barrier = threading.Barrier(n)
|
||||
wins, errors = [], []
|
||||
|
||||
def worker():
|
||||
try:
|
||||
with Session() as db:
|
||||
barrier.wait()
|
||||
wins.append(okta_sync.claim(db, INTERVAL, now=datetime.now(timezone.utc)))
|
||||
except Exception as exc: # a lost race may surface as an error
|
||||
errors.append(type(exc).__name__)
|
||||
wins.append(False)
|
||||
|
||||
ts = [threading.Thread(target=worker) for _ in range(n)]
|
||||
[t.start() for t in ts]
|
||||
[t.join() for t in ts]
|
||||
return sum(1 for w in wins if w), errors
|
||||
|
||||
|
||||
wipe()
|
||||
won, errs = race()
|
||||
chk("no row yet, 8 workers at once: exactly 1 wins (the insert race)", won == 1, f"{won} {errs}")
|
||||
bad = []
|
||||
for i in range(15):
|
||||
with Session() as db: # age the claim so the next race is due again
|
||||
db.query(models.AppSetting).filter(models.AppSetting.key == okta_sync.CLAIM_KEY).update(
|
||||
{"updated_at": datetime.now(timezone.utc) - timedelta(seconds=INTERVAL + 5)})
|
||||
db.commit()
|
||||
won, errs = race()
|
||||
if won != 1:
|
||||
bad.append((i, won, errs))
|
||||
chk("row due, 8 workers at once, 15 rounds: exactly 1 wins every round", not bad, str(bad))
|
||||
|
||||
# ── 4. one tick ───────────────────────────────────────────────────────────────
|
||||
print("\n4. a tick runs the sync once per cycle")
|
||||
wipe([("alice", True), ("bob", True)])
|
||||
calls = []
|
||||
|
||||
|
||||
def fetch_ok():
|
||||
calls.append(1)
|
||||
return [OktaUser("alice", "ACTIVE"), OktaUser("bob", "SUSPENDED")]
|
||||
|
||||
|
||||
r1 = okta_sync.run_if_due(Session, fetch=fetch_ok, now=T0)
|
||||
chk("first tick claims and runs", r1 is not None and r1.ok and r1.disabled == ["bob"])
|
||||
r2 = okta_sync.run_if_due(Session, fetch=fetch_ok, now=T0 + timedelta(seconds=30))
|
||||
chk("a tick 30 seconds later does nothing (not due)", r2 is None and len(calls) == 1)
|
||||
r3 = okta_sync.run_if_due(Session, fetch=fetch_ok, now=T0 + timedelta(seconds=INTERVAL))
|
||||
chk("a tick when due runs again", r3 is not None and r3.ok and len(calls) == 2)
|
||||
|
||||
print("\n ...a restart inside the interval does not start a second run")
|
||||
wipe([("alice", True), ("bob", True)])
|
||||
calls.clear()
|
||||
okta_sync.run_if_due(Session, fetch=fetch_ok, now=T0)
|
||||
# "restart": a brand-new caller with nothing in memory, 2 minutes later
|
||||
r = okta_sync.run_if_due(Session, fetch=fetch_ok, now=T0 + timedelta(seconds=120))
|
||||
chk("the claim row outlives the process: no duplicate run", r is None and len(calls) == 1)
|
||||
|
||||
print("\n ...two workers tick in the same instant")
|
||||
wipe([("alice", True), ("bob", True)])
|
||||
calls.clear()
|
||||
results = []
|
||||
barrier = threading.Barrier(2)
|
||||
|
||||
|
||||
def tick():
|
||||
barrier.wait()
|
||||
results.append(okta_sync.run_if_due(Session, fetch=fetch_ok))
|
||||
|
||||
|
||||
ts = [threading.Thread(target=tick) for _ in range(2)]
|
||||
[t.start() for t in ts]
|
||||
[t.join() for t in ts]
|
||||
chk("Okta is asked once, not twice", len(calls) == 1, str(len(calls)))
|
||||
chk("exactly one worker got a result", sum(1 for r in results if r is not None) == 1)
|
||||
_, audits = users_state()
|
||||
chk("one audit row, not two", audits == 1, str(audits))
|
||||
|
||||
# ── 5. fail closed through the scheduled path ─────────────────────────────────
|
||||
print("\n5. FAIL CLOSED through the scheduled wrapper (not just run_sync)")
|
||||
for label, exc in (("OktaSyncError", OktaSyncError("HTTP 500")), ("RuntimeError", RuntimeError("x")),
|
||||
("TimeoutError", TimeoutError("t"))):
|
||||
wipe([("alice", True), ("bob", True)])
|
||||
|
||||
def failing(e=exc):
|
||||
raise e
|
||||
|
||||
r = okta_sync.run_if_due(Session, fetch=failing, now=T0)
|
||||
u, a = users_state()
|
||||
chk(f"{label} in the scheduled path: ok is False, zero changes, zero audit rows",
|
||||
r is not None and r.ok is False and all(u.values()) and a == 0)
|
||||
|
||||
wipe([("alice", True), ("bob", True)])
|
||||
|
||||
|
||||
def broken_factory():
|
||||
raise RuntimeError("database is down")
|
||||
|
||||
|
||||
chk("the database failing at claim time: the tick ends quietly, returns None",
|
||||
okta_sync.run_if_due(broken_factory, fetch=fetch_ok, now=T0) is None)
|
||||
u, a = users_state()
|
||||
chk("...and nothing changed", all(u.values()) and a == 0)
|
||||
|
||||
# ── 6. the thread ─────────────────────────────────────────────────────────────
|
||||
print("\n6. the timer thread")
|
||||
os.environ["OKTA_API_TOKEN"] = ""
|
||||
okta_auth.ISSUER = "https://example.okta.com/oauth2/default"
|
||||
chk("no token: nothing starts", okta_sync.start_scheduler(Session) is None)
|
||||
chk("...no thread named okta-sync", not any(t.name == "okta-sync" for t in threading.enumerate()))
|
||||
|
||||
os.environ["OKTA_API_TOKEN"] = "tok"
|
||||
wipe([("alice", True), ("bob", True)])
|
||||
okta_sync.START_DELAY_SECONDS = 0.05
|
||||
okta_sync.TICK_SECONDS = 0.05
|
||||
ran = []
|
||||
real_fetch = okta_sync.fetch_users
|
||||
okta_sync.fetch_users = lambda http=None: (ran.append(1) or
|
||||
[OktaUser("alice", "ACTIVE"), OktaUser("bob", "DEPROVISIONED")])
|
||||
stop = okta_sync.start_scheduler(Session)
|
||||
chk("configured: a stop Event comes back", stop is not None)
|
||||
time.sleep(1.2) # about 20 ticks
|
||||
u, a = users_state()
|
||||
chk("the thread ran the sync on its own (bob was disabled without a manual call)", u["bob"] is False)
|
||||
chk("many ticks, one run (the claim gate holds)", len(ran) == 1 and a == 1, f"ran={len(ran)} audits={a}")
|
||||
th = [t for t in threading.enumerate() if t.name == "okta-sync"]
|
||||
chk("one timer thread is alive", len(th) == 1 and th[0].is_alive())
|
||||
chk("the thread is a daemon (never blocks shutdown)", th[0].daemon)
|
||||
stop.set()
|
||||
th[0].join(timeout=3)
|
||||
chk("setting the stop Event ends the thread", not th[0].is_alive())
|
||||
okta_sync.fetch_users = real_fetch
|
||||
os.environ["OKTA_API_TOKEN"] = ""
|
||||
|
||||
print()
|
||||
print("ALL PASSED" if not fails else f"{fails} FAILED")
|
||||
sys.exit(0 if not fails else 1)
|
||||
153
tests/okta_sync_ui_check.py
Normal file
153
tests/okta_sync_ui_check.py
Normal file
@@ -0,0 +1,153 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Does an auto-disable read clearly in the admin console? - D18 / T13.5.
|
||||
|
||||
T13.5 builds no new UI. It confirms that an account the Okta sync disabled shows
|
||||
up legibly in the surface admins already use (the Activity log card on
|
||||
admin.html), without special-casing, and that the disabled account is refused on
|
||||
its very next request. This runs the REAL sync against a throwaway database,
|
||||
then reads the real page in a headless browser at 1440px and 390px.
|
||||
|
||||
It does not rely on CR-019's Activity & usage card: only the Activity log card
|
||||
(AuditLog) is read.
|
||||
|
||||
Boots its own throwaway SQLite + uvicorn + headless browser; run it alone.
|
||||
Screenshots go to a folder printed at the end (WP_SHOTS_DIR overrides it).
|
||||
Exit 0 all passed, 1 a failure, 2 could not run.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
import cdp # noqa: E402
|
||||
from browser_check import seed, start_server, chk, _PASS, _FAIL # noqa: E402
|
||||
from qa_gate_check import api # noqa: E402
|
||||
|
||||
|
||||
def ascii_(v, n=300):
|
||||
return re.sub(r"\s+", " ", str(v)).encode("ascii", "replace").decode()[:n]
|
||||
|
||||
|
||||
ROWS_JS = """JSON.stringify([...document.querySelectorAll('#audit-admin tbody tr')].map(
|
||||
tr => [...tr.children].map(td => td.textContent.trim())))"""
|
||||
|
||||
|
||||
def main():
|
||||
exe = cdp.find_browser()
|
||||
if not exe:
|
||||
print("no headless-capable browser found; set WP_BROWSER.")
|
||||
return 2
|
||||
|
||||
tmpdir = tempfile.mkdtemp(prefix="wpsuite-syncui-")
|
||||
shots = os.environ.get("WP_SHOTS_DIR") or os.path.join(tmpdir, "shots")
|
||||
db_path = os.path.join(tmpdir, "check.db")
|
||||
server = browser = None
|
||||
try:
|
||||
tok = seed(db_path)
|
||||
|
||||
print("\n1. run the real sync against the throwaway database")
|
||||
from server.db import SessionLocal
|
||||
from server import okta_sync
|
||||
from server.okta_sync import OktaUser
|
||||
okta = lambda: [OktaUser("root", "ACTIVE"), OktaUser("sue", "ACTIVE"),
|
||||
OktaUser("pat", "SUSPENDED"), OktaUser("mix", "ACTIVE"),
|
||||
OktaUser("sam", "ACTIVE"), OktaUser("legacy", "ACTIVE")] # bob is missing
|
||||
with SessionLocal() as db:
|
||||
res = okta_sync.run_sync(db, fetch=okta)
|
||||
chk("the sync disabled pat (SUSPENDED) and bob (missing)",
|
||||
res.ok and sorted(res.disabled) == ["bob", "pat"], ascii_((res.ok, res.error, res.disabled)))
|
||||
|
||||
port = cdp.free_port()
|
||||
base = "http://127.0.0.1:%d" % port
|
||||
server = start_server(port, db_path)
|
||||
if not server:
|
||||
print("the server did not start")
|
||||
return 2
|
||||
|
||||
print("\n2. a disabled account is refused on its very next request")
|
||||
st, _ = api(base, "/api/auth/me", tok["pat"])
|
||||
chk("pat (auto-disabled) is refused", st in (401, 403), st)
|
||||
st, _ = api(base, "/api/auth/me", tok["bob"])
|
||||
chk("bob (auto-disabled) is refused", st in (401, 403), st)
|
||||
st, _ = api(base, "/api/auth/me", tok["mix"])
|
||||
chk("mix (still active in Okta) still works", st == 200, st)
|
||||
|
||||
print("\n3. the audit rows through the API")
|
||||
st, audit = api(base, "/api/audit?entity_type=user&limit=50", tok["root"])
|
||||
mine = [e for e in (audit or []) if e.get("actor") == "system:okta_sync"]
|
||||
chk("an admin gets both rows", st == 200 and len(mine) == 2, ascii_((st, len(mine))))
|
||||
chk("each row names the action, the user, and the Okta status",
|
||||
{(e["summary"], e["detail"].get("okta_status")) for e in mine} == {("pat", "SUSPENDED"), ("bob", "MISSING")},
|
||||
ascii_([(e["summary"], e["detail"]) for e in mine]))
|
||||
st, audit_sue = api(base, "/api/audit?entity_type=user&limit=50", tok["sue"])
|
||||
chk("a project super user does not see them (they belong to no project)",
|
||||
st == 200 and not [e for e in audit_sue if e.get("actor") == "system:okta_sync"])
|
||||
|
||||
print("\n4. the admin console, as an admin")
|
||||
browser = cdp.Browser(exe)
|
||||
page = browser.page()
|
||||
for width, label in ((1440, "1440"), (390, "390")):
|
||||
page.clear_cookies()
|
||||
page.set_cookie("wp_session", tok["root"])
|
||||
page.viewport(width, 900, mobile=(width < 500))
|
||||
page.goto(base + "/admin.html")
|
||||
time.sleep(2.0)
|
||||
page.eval("void loadAudit()")
|
||||
time.sleep(1.2)
|
||||
rows = json.loads(page.eval(ROWS_JS) or "[]")
|
||||
sync_rows = [r for r in rows if "system:okta_sync" in r]
|
||||
chk("[%s] the Activity log lists both auto-disables" % label, len(sync_rows) == 2,
|
||||
ascii_(rows[:4]))
|
||||
if sync_rows:
|
||||
r = next((x for x in sync_rows if "pat" in x), sync_rows[0])
|
||||
# columns: When, Who, Action, Type, Item, Detail
|
||||
chk("[%s] Who reads 'system:okta_sync', not a person" % label, r[1] == "system:okta_sync", r)
|
||||
chk("[%s] Action reads 'user deprovisioned by sync'" % label,
|
||||
r[2] == "user deprovisioned by sync", r)
|
||||
chk("[%s] Type and Item name the account" % label, r[3] == "user" and r[4] == "pat", r)
|
||||
chk("[%s] Detail gives the Okta status found" % label,
|
||||
"SUSPENDED" in r[5] and "okta" in r[5].lower(), r)
|
||||
miss = next((x for x in sync_rows if "bob" in x), None)
|
||||
chk("[%s] a person missing from Okta reads 'MISSING'" % label,
|
||||
bool(miss) and "MISSING" in miss[5], miss)
|
||||
# filter + search work on it with no special-casing
|
||||
page.eval("document.getElementById('audit-search').value='okta_sync'; renderAudit();")
|
||||
rows = json.loads(page.eval(ROWS_JS) or "[]")
|
||||
chk("[%s] searching 'okta_sync' finds exactly the sync rows" % label,
|
||||
len(rows) == 2 and all("system:okta_sync" in r for r in rows), ascii_(rows))
|
||||
page.eval("document.getElementById('audit-search').value='';"
|
||||
"document.getElementById('audit-type').value='user'; renderAudit();")
|
||||
rows = json.loads(page.eval(ROWS_JS) or "[]")
|
||||
chk("[%s] the 'User accounts' filter keeps them" % label,
|
||||
len([r for r in rows if "system:okta_sync" in r]) == 2)
|
||||
page.eval("document.getElementById('audit-type').value=''; renderAudit();")
|
||||
over = page.eval("document.documentElement.scrollWidth - document.documentElement.clientWidth")
|
||||
chk("[%s] no sideways scroll on the page" % label, (over or 0) <= 0, over)
|
||||
card = page.eval("(()=>{const e=document.getElementById('audit-admin');"
|
||||
"e.scrollIntoView();return e.getBoundingClientRect().width>0})()")
|
||||
chk("[%s] the card is on screen" % label, bool(card))
|
||||
time.sleep(0.4)
|
||||
page.screenshot(os.path.join(shots, "admin-activity-log-%s.png" % label), full_page=False)
|
||||
errs = [e for e in page.js_errors() if "beforeunload" not in e]
|
||||
chk("no JavaScript errors", not errs, ascii_(errs[:3]))
|
||||
print("\n screenshots: " + shots)
|
||||
finally:
|
||||
if browser:
|
||||
browser.close()
|
||||
if server:
|
||||
server.terminate()
|
||||
|
||||
print("\n" + "-" * 54)
|
||||
print("%d/%d checks passed." % (len(_PASS), len(_PASS) + len(_FAIL)))
|
||||
for f in _FAIL:
|
||||
print(" - " + f)
|
||||
return 1 if _FAIL else 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
226
tests/outbox_check.py
Normal file
226
tests/outbox_check.py
Normal file
@@ -0,0 +1,226 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Does a sign-in problem destroy queued work? - D26 / T14.1.
|
||||
|
||||
The outbox in html/project-data.js queues every save and sends it later. It used to
|
||||
treat a 401 (the session ended) like any other 4xx: the change was marked dead and
|
||||
never sent again. It also hid a full device store and sent a change under whoever
|
||||
was signed in. This check drives the real page in a real browser. It replaces
|
||||
window.fetch for the write endpoints only, so no real server state is needed.
|
||||
|
||||
1. a 401 holds the whole queue: nothing dead, no try counted, state is "held"
|
||||
2. online, tab focus and the timer do not release a held queue
|
||||
3. a good sign-in (wp-auth-ready) sends every change, in the original order
|
||||
4. a 403 kills only that change and says why
|
||||
5. a full device store is reported to the caller and the badge; nothing is dropped
|
||||
6. a change is sent only under the person who made it
|
||||
7. auth-guard.js never deletes the outbox
|
||||
|
||||
Exit 0 all passed, 1 a failure, 2 could not run.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
import cdp # noqa: E402
|
||||
from browser_check import seed, start_server, chk, _PASS, _FAIL # noqa: E402
|
||||
|
||||
KEY = "wp_sync_outbox_v1"
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
HTML = os.path.join(os.path.dirname(HERE), "html")
|
||||
|
||||
# Replaces fetch for POST /api/wps, POST /api/sops and DELETE /api/wps/<id> only.
|
||||
# window.__plan maps a work package id to a status; the default is window.__status.
|
||||
STUB = """(() => {
|
||||
if (!window.__realFetch) window.__realFetch = window.fetch.bind(window);
|
||||
window.__calls = []; window.__status = 200; window.__plan = {};
|
||||
window.fetch = function (url, opts) {
|
||||
const m = ((opts && opts.method) || 'GET').toUpperCase();
|
||||
const u = String(url);
|
||||
if ((m === 'POST' && /\\/api\\/(wps|sops)$/.test(u)) || (m === 'DELETE' && /\\/api\\/wps\\//.test(u))) {
|
||||
let id = '';
|
||||
try { id = JSON.parse(opts.body).id; } catch (e) { id = decodeURIComponent(u.split('/').pop()); }
|
||||
window.__calls.push(id);
|
||||
const st = (id in window.__plan) ? window.__plan[id] : window.__status;
|
||||
return Promise.resolve(new Response(JSON.stringify(st >= 400 ? { detail: 'stub ' + st } : { ok: true }),
|
||||
{ status: st, headers: { 'Content-Type': 'application/json' } }));
|
||||
}
|
||||
return window.__realFetch(url, opts);
|
||||
};
|
||||
localStorage.removeItem('wp_sync_outbox_v1');
|
||||
return true;
|
||||
})()"""
|
||||
|
||||
|
||||
def boot(page, base, tok, user="root"):
|
||||
page.clear_cookies()
|
||||
page.set_cookie("wp_session", tok[user])
|
||||
page.goto(base + "/field.html")
|
||||
for _ in range(40):
|
||||
if page.eval("!!(window.WP_USER && window.ProjectData)"):
|
||||
break
|
||||
time.sleep(0.3)
|
||||
time.sleep(0.5)
|
||||
page.eval(STUB)
|
||||
|
||||
|
||||
def queue(page):
|
||||
return json.loads(page.eval("localStorage.getItem(%r) || '[]'" % KEY))
|
||||
|
||||
|
||||
def status(page):
|
||||
return page.eval("ProjectData.syncStatus()")
|
||||
|
||||
|
||||
def push(page, ids):
|
||||
page.eval("(() => { %s; return true; })()" % ";".join(
|
||||
"ProjectData.pushWP({id:%r, name:%r}, 'projA')" % (i, i) for i in ids))
|
||||
|
||||
|
||||
def main():
|
||||
exe = cdp.find_browser()
|
||||
if not exe:
|
||||
print("no headless-capable browser found; set WP_BROWSER.")
|
||||
return 2
|
||||
tmpdir = tempfile.mkdtemp(prefix="wpsuite-outbox-")
|
||||
db_path = os.path.join(tmpdir, "check.db")
|
||||
server = None
|
||||
try:
|
||||
tok = seed(db_path)
|
||||
port = cdp.free_port()
|
||||
base = "http://127.0.0.1:%d" % port
|
||||
server = start_server(port, db_path)
|
||||
if server is None:
|
||||
print("the test server would not start.")
|
||||
return 2
|
||||
print("\nThe outbox keeps every change - D26 / T14.1\nTarget: %s" % base)
|
||||
browser = cdp.Browser(exe)
|
||||
page = browser.page()
|
||||
try:
|
||||
ids = ["wp_o1", "wp_o2", "wp_o3"]
|
||||
|
||||
print("\n1. a 401 holds the whole queue")
|
||||
boot(page, base, tok)
|
||||
page.eval("window.__status = 401")
|
||||
push(page, ids)
|
||||
page.eval("ProjectData.flushSync()")
|
||||
q = queue(page)
|
||||
st = status(page)
|
||||
chk("all 3 changes are still queued", [o["key"] for o in q] == ids, str([o["key"] for o in q]))
|
||||
chk("none is marked dead", not any(o.get("dead") for o in q) and st["dead"] == 0)
|
||||
chk("no try was counted", all((o.get("tries") or 0) == 0 for o in q), str([o.get("tries") for o in q]))
|
||||
chk("the state is held", st["held"] is True, json.dumps(st))
|
||||
chk("the run stopped at the first 401", len(page.eval("window.__calls")) == 1,
|
||||
str(page.eval("window.__calls")))
|
||||
badge = page.eval("document.getElementById('wp-sync-badge').textContent")
|
||||
chk("the badge asks for a new sign-in", "Sign in again" in badge, badge)
|
||||
|
||||
print("\n2. only a good sign-in releases it")
|
||||
page.eval("window.__status = 200")
|
||||
page.eval("window.dispatchEvent(new Event('online'))")
|
||||
page.eval("document.dispatchEvent(new Event('visibilitychange'))")
|
||||
page.eval("ProjectData.flushSync()")
|
||||
time.sleep(1.0)
|
||||
chk("online, focus and a manual flush send nothing while held",
|
||||
len(page.eval("window.__calls")) == 1 and len(queue(page)) == 3,
|
||||
str(page.eval("window.__calls")))
|
||||
|
||||
print("\n3. a good sign-in sends all of them, in order")
|
||||
page.eval("document.dispatchEvent(new CustomEvent('wp-auth-ready', {detail: window.WP_USER}))")
|
||||
for _ in range(30):
|
||||
if not queue(page):
|
||||
break
|
||||
time.sleep(0.3)
|
||||
calls = page.eval("window.__calls")
|
||||
chk("the queue is empty", queue(page) == [], str(queue(page)))
|
||||
chk("the changes arrived in the original order", calls[1:] == ids or calls[-3:] == ids, str(calls))
|
||||
chk("the state is no longer held", status(page)["held"] is False)
|
||||
|
||||
print("\n4. a 403 kills only that change, with a reason")
|
||||
boot(page, base, tok)
|
||||
page.eval("window.__plan = {wp_o2: 403}")
|
||||
push(page, ids)
|
||||
page.eval("ProjectData.flushSync()")
|
||||
q = queue(page)
|
||||
dead = [o for o in q if o.get("dead")]
|
||||
chk("only wp_o2 is dead", [o["key"] for o in dead] == ["wp_o2"], str([o["key"] for o in q]))
|
||||
chk("the others were sent", [o["key"] for o in q if not o.get("dead")] == [])
|
||||
chk("the reason names the cause", dead and "no longer have access" in dead[0].get("lastErr", ""),
|
||||
dead[0].get("lastErr", "") if dead else "")
|
||||
chk("a 403 does not hold the queue", status(page)["held"] is False)
|
||||
|
||||
print("\n5. a full device store is never silent")
|
||||
boot(page, base, tok)
|
||||
page.eval("window.__status = 500") # keep it queued so the badge state stays visible
|
||||
page.eval("""(() => {
|
||||
window.__setItem = Storage.prototype.setItem;
|
||||
Storage.prototype.setItem = function (k, v) {
|
||||
if (k === 'wp_sync_outbox_v1') { const e = new Error('full'); e.name = 'QuotaExceededError'; throw e; }
|
||||
return window.__setItem.call(this, k, v);
|
||||
};
|
||||
return true; })()""")
|
||||
stored = page.eval("ProjectData.pushWP({id:'wp_full', name:'full'}, 'projA')")
|
||||
st = status(page)
|
||||
chk("the caller is told it was not stored", stored is False, repr(stored))
|
||||
chk("the state says device full, 1 unsaved", st["deviceFull"] is True and st["unsaved"] == 1, json.dumps(st))
|
||||
badge = page.eval("document.getElementById('wp-sync-badge').textContent")
|
||||
chk("the badge says it is not saved on this device", "not saved on this device" in badge, badge)
|
||||
page.eval("window.__status = 200")
|
||||
page.eval("ProjectData.flushSync()")
|
||||
chk("the change is still sent from memory", "wp_full" in page.eval("window.__calls"),
|
||||
str(page.eval("window.__calls")))
|
||||
page.eval("Storage.prototype.setItem = window.__setItem")
|
||||
chk("the state clears once it is sent", status(page)["unsaved"] == 0)
|
||||
|
||||
print("\n6. a change is sent only under the person who made it")
|
||||
boot(page, base, tok)
|
||||
page.eval("window.__status = 503") # keep the change queued while we look at it
|
||||
push(page, ["wp_owned"])
|
||||
time.sleep(0.5)
|
||||
page.eval("window.__status = 200")
|
||||
q0 = queue(page)
|
||||
owner = (q0[0].get("owner") if q0 else None)
|
||||
chk("the change records its owner", owner == "root", repr(owner))
|
||||
page.eval("window.__calls = []; window.__me = window.WP_USER; window.WP_USER = {username: 'pat'}")
|
||||
page.eval("ProjectData.flushSync()")
|
||||
st = status(page)
|
||||
chk("person B sends nothing", page.eval("window.__calls") == [], str(page.eval("window.__calls")))
|
||||
chk("the change is kept", [o["key"] for o in queue(page)] == ["wp_owned"])
|
||||
chk("the badge names the owner", st["others"] == 1 and st["otherOwner"] == "root", json.dumps(st))
|
||||
page.eval("window.WP_USER = window.__me")
|
||||
page.eval("ProjectData.flushSync()")
|
||||
chk("it is sent after the owner signs in", page.eval("window.__calls") == ["wp_owned"]
|
||||
and queue(page) == [], str(page.eval("window.__calls")))
|
||||
finally:
|
||||
page.close()
|
||||
browser.close()
|
||||
|
||||
print("\n7. auth-guard.js never deletes the outbox")
|
||||
src = open(os.path.join(HTML, "auth-guard.js"), encoding="utf-8").read()
|
||||
code = re.sub(r"/\*.*?\*/|//[^\n]*", "", src, flags=re.S)
|
||||
chk("no code path touches the outbox key", KEY not in code)
|
||||
chk("nothing clears all of localStorage", "localStorage.clear" not in code)
|
||||
finally:
|
||||
if server is not None:
|
||||
server.kill()
|
||||
try:
|
||||
server.wait(timeout=10)
|
||||
except subprocess.TimeoutExpired:
|
||||
pass
|
||||
try:
|
||||
from server.db import engine
|
||||
engine.dispose()
|
||||
except Exception:
|
||||
pass
|
||||
print("\n%d passed, %d failed" % (len(_PASS), len(_FAIL)))
|
||||
return 1 if _FAIL else 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
270
tests/security_check.py
Normal file
270
tests/security_check.py
Normal file
@@ -0,0 +1,270 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Wave 16 security checks: D19, D20, D21.
|
||||
|
||||
python tests/security_check.py
|
||||
|
||||
Self-contained, like the other probes, but needs NO browser: it builds its own
|
||||
throwaway SQLite database in a temp folder, drives the API in-process with
|
||||
FastAPI's TestClient, and deletes everything afterward. Your real wpsuite.db is
|
||||
never touched, and no network traffic leaves the process.
|
||||
|
||||
Each check tests what was BROKEN, measured by the security review of
|
||||
2026-10-01 (docs/reference/security-review-2026-10-01.md), not only what is
|
||||
easy to assert. Every check that tests a fix FAILED against da1325c, the
|
||||
commit before wave 16; the checks that say "still works" passed there too,
|
||||
and are what proves the fix broke nothing.
|
||||
|
||||
Exit codes: 0 all passed, 1 a check failed, 2 the run could not start.
|
||||
"""
|
||||
import base64
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
import warnings
|
||||
|
||||
warnings.filterwarnings("ignore")
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
TMP = tempfile.mkdtemp(prefix="wp-security-check-")
|
||||
# db.py reads DATABASE_URL at import, so the environment is set first.
|
||||
os.environ["DATABASE_URL"] = "sqlite:///" + os.path.join(TMP, "security.db").replace("\\", "/")
|
||||
os.environ["AUTH_SECRET_KEY"] = "security-check-key-security-check-key-0000"
|
||||
os.environ.pop("WP_OKTA_FAKE_DIRECTORY", None)
|
||||
sys.path.insert(0, ROOT)
|
||||
|
||||
try:
|
||||
from fastapi.testclient import TestClient
|
||||
from server import app as A, auth, models
|
||||
from server.db import SessionLocal
|
||||
except Exception as exc: # noqa: BLE001
|
||||
print("could not start:", exc)
|
||||
sys.exit(2)
|
||||
|
||||
RESULTS = []
|
||||
|
||||
|
||||
def check(item, name, ok, detail=""):
|
||||
RESULTS.append(bool(ok))
|
||||
print(f" {'PASS' if ok else 'FAIL'} {item:<4} {name}" + (f" ({detail})" if detail and not ok else ""))
|
||||
|
||||
|
||||
db = SessionLocal()
|
||||
|
||||
|
||||
def account(uid, role="project_user"):
|
||||
u = models.User(id=uid, username=uid[5:], role=role, email=uid[5:] + "@example.test")
|
||||
db.add(u)
|
||||
db.commit()
|
||||
return u
|
||||
|
||||
|
||||
def client(user=None, token=None):
|
||||
c = TestClient(A.app, base_url="http://127.0.0.1", raise_server_exceptions=False)
|
||||
if user is not None:
|
||||
c.cookies.set(auth.COOKIE_NAME, auth.create_token(user))
|
||||
if token is not None:
|
||||
c.cookies.set(auth.COOKIE_NAME, token)
|
||||
return c
|
||||
|
||||
|
||||
admin = account("user_admin", "admin")
|
||||
member = account("user_member") # project_user on proj_a
|
||||
padmin = account("user_padmin") # project_admin on proj_a
|
||||
as_admin, as_member, as_padmin = client(admin), client(member), client(padmin)
|
||||
for pid in ("proj_a", "proj_b", "proj_c"):
|
||||
as_admin.post("/api/projects", json={"id": pid, "name": pid})
|
||||
for uid, role in (("user_member", ""), ("user_padmin", "project_admin")):
|
||||
for pid in ("proj_a", "proj_b"):
|
||||
db.add(models.ProjectMember(id=f"pm_{uid}_{pid}", user_id=uid, project_id=pid, role=role))
|
||||
db.commit()
|
||||
|
||||
|
||||
def b64url(raw: bytes) -> str:
|
||||
return base64.urlsafe_b64encode(raw).rstrip(b"=").decode()
|
||||
|
||||
|
||||
# ── D21: session decode fails closed ─────────────────────────────────────────
|
||||
def d21():
|
||||
print("\nD21 session decode fails closed")
|
||||
crafted = b64url(b"[" * 1200) + "." + b64url(b"{}") + ".x"
|
||||
r = client(token=crafted).get("/api/projects")
|
||||
check("D21", "a crafted 1.6 KB cookie with no account gets 401, not 500",
|
||||
r.status_code == 401, f"status {r.status_code}")
|
||||
r = client(token="x" * 5000).get("/api/projects")
|
||||
check("D21", "an over-long cookie gets 401", r.status_code == 401, f"status {r.status_code}")
|
||||
no_exp = __import__("jwt").encode({"sub": member.id, "ver": 0}, auth.SECRET_KEY, algorithm="HS256")
|
||||
r = client(token=no_exp).get("/api/projects")
|
||||
check("D21", "a correctly signed token with no exp is refused", r.status_code == 401,
|
||||
f"status {r.status_code}")
|
||||
r = as_member.get("/api/auth/me")
|
||||
check("D21", "a real session still works", r.status_code == 200 and r.json()["user"]["id"] == member.id,
|
||||
f"status {r.status_code}")
|
||||
# D18 sliding refresh must survive the stricter decode: a token near its idle
|
||||
# edge, used now, comes back with a fresh cookie.
|
||||
from datetime import datetime, timedelta, timezone
|
||||
now = datetime.now(timezone.utc)
|
||||
near = __import__("jwt").encode({"sub": member.id, "username": member.username, "role": member.role,
|
||||
"ver": 0, "iat": now - timedelta(minutes=25),
|
||||
"login_at": (now - timedelta(minutes=25)).timestamp(),
|
||||
"exp": now + timedelta(minutes=5)}, auth.SECRET_KEY, algorithm="HS256")
|
||||
r = client(token=near).get("/api/projects")
|
||||
if hasattr(auth, "maybe_refresh_token"):
|
||||
check("D21", "the D18 sliding refresh still reissues the session",
|
||||
r.status_code == 200 and auth.COOKIE_NAME in (r.headers.get("set-cookie") or ""),
|
||||
f"status {r.status_code}")
|
||||
else:
|
||||
# D18 (T13.1) is not on this branch (wave 16 built on main, 2026-10-06):
|
||||
# there is no refresh to keep working, so prove the stricter decode still
|
||||
# accepts a valid token about to expire.
|
||||
check("D21", "a valid token close to its expiry is still accepted (no D18 on this branch)",
|
||||
r.status_code == 200, f"status {r.status_code}")
|
||||
import re
|
||||
pin = re.search(r"(?im)^pyjwt==([0-9.]+)", open(os.path.join(ROOT, "server", "requirements.txt")).read())
|
||||
pinned = tuple(int(x) for x in pin.group(1).split(".")) if pin else (0,)
|
||||
check("D21", "server/requirements.txt pins PyJWT 2.15.1 or later", pinned >= (2, 15, 1),
|
||||
pin.group(1) if pin else "no pin")
|
||||
|
||||
|
||||
# ── D19: uploaded files download safely ──────────────────────────────────────
|
||||
def d19():
|
||||
print("\nD19 uploaded files download safely")
|
||||
r = as_member.post("/api/wps", json={"id": "wp_files", "project_id": "proj_a", "number": "F1"})
|
||||
assert r.status_code == 200, r.text
|
||||
|
||||
def upload(name, mime, data):
|
||||
r = as_member.post("/api/wps/wp_files/files", json={
|
||||
"name": name, "mime": mime, "data_base64": base64.b64encode(data).decode()})
|
||||
return r.status_code, (r.json().get("file") or {}).get("id")
|
||||
|
||||
code, svg = upload("route.svg", "image/svg+xml",
|
||||
b'<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>')
|
||||
check("D19", "an SVG upload is still accepted (decided: accept, download only)", code == 200, f"status {code}")
|
||||
r = as_member.get(f"/api/files/{svg}")
|
||||
disp, csp = r.headers.get("content-disposition", ""), r.headers.get("content-security-policy", "")
|
||||
check("D19", "an SVG downloads as an attachment, never inline", disp.startswith("attachment"), disp)
|
||||
check("D19", "an SVG download carries a sandbox CSP", "sandbox" in csp, csp or "no CSP")
|
||||
check("D19", "file responses carry nosniff", r.headers.get("x-content-type-options") == "nosniff")
|
||||
|
||||
for name, mime, data in (("plan.png", "image/png", b"\x89PNG\r\n\x1a\n"),
|
||||
("photo.jpg", "image/jpeg", b"\xff\xd8\xff"),
|
||||
("sheet.pdf", "application/pdf", b"%PDF-1.4")):
|
||||
_, fid = upload(name, mime, data)
|
||||
r = as_member.get(f"/api/files/{fid}")
|
||||
check("D19", f"{mime} still opens inline, no sandbox",
|
||||
r.status_code == 200 and r.headers.get("content-disposition", "").startswith("inline")
|
||||
and "content-security-policy" not in r.headers, r.headers.get("content-disposition", ""))
|
||||
check("D19", f"{mime} keeps the offline cache header",
|
||||
r.headers.get("cache-control") == "private, max-age=86400", r.headers.get("cache-control", ""))
|
||||
|
||||
_, fid = upload("Plan é – Nivel 2.pdf", "application/pdf", b"%PDF-1.4")
|
||||
r = as_member.get(f"/api/files/{fid}")
|
||||
check("D19", "a drawing with a non-Latin-1 name downloads (was 500)", r.status_code == 200,
|
||||
f"status {r.status_code}")
|
||||
check("D19", "the real name rides in filename*",
|
||||
"filename*=UTF-8''Plan%20%C3%A9" in r.headers.get("content-disposition", ""),
|
||||
r.headers.get("content-disposition", ""))
|
||||
|
||||
_, fid = upload('a\r\nSet-Cookie: x=1;".pdf', "application/pdf", b"%PDF-1.4")
|
||||
r = as_member.get(f"/api/files/{fid}")
|
||||
disp = r.headers.get("content-disposition", "")
|
||||
check("D19", "CR/LF and quotes in a name cannot reach the header",
|
||||
r.status_code == 200 and "\r" not in disp and "\n" not in disp and "set-cookie" not in r.headers,
|
||||
repr(disp))
|
||||
r = client(account("user_outsider")).get(f"/api/files/{svg}")
|
||||
check("D19", "a non-member still gets 403 (access check unchanged)", r.status_code == 403,
|
||||
f"status {r.status_code}")
|
||||
|
||||
|
||||
# ── D20: one SOP per project ─────────────────────────────────────────────────
|
||||
def d20():
|
||||
print("\nD20 one SOP per project")
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
def sop(c, **body):
|
||||
body.setdefault("project_id", "proj_a")
|
||||
return c.post("/api/sops", json=body)
|
||||
|
||||
def team(qa):
|
||||
return {"sop": {"project": {"pmId": qa, "cmId": qa, "qaGroupIds": [qa]}}, "state": {}}
|
||||
|
||||
r = sop(as_member, name="draft", complete=False, data=team("user_padmin"))
|
||||
check("D20", "a SOP posted with no id lands on sop__<project_id>",
|
||||
r.status_code == 200 and r.json().get("id") == "sop__proj_a", f"{r.status_code} {r.json().get('id')}")
|
||||
r = sop(as_member, id="sop__proj_a", name="done", complete=True, data=team("user_padmin"))
|
||||
check("D20", "a project_user still completes the first SOP of a project", r.status_code == 200,
|
||||
f"status {r.status_code}")
|
||||
r = sop(as_member, id="sop_rogue", name="mine", complete=True, data=team("user_member"))
|
||||
check("D20", "a project_user cannot create a second SOP (the SEC-02 attack)", r.status_code == 400,
|
||||
f"status {r.status_code}")
|
||||
r = sop(as_member, id="sop__proj_a", name="edit", complete=True, data=team("user_member"))
|
||||
check("D20", "a project_user still cannot change the completed SOP", r.status_code == 403,
|
||||
f"status {r.status_code}")
|
||||
r = sop(as_member, id="sop__proj_a", name="undo", complete=False, data=team("user_member"))
|
||||
check("D20", "a project_user cannot un-complete it either", r.status_code == 403, f"status {r.status_code}")
|
||||
r = sop(as_padmin, id="sop__proj_a", name="revised", complete=True, data=team("user_padmin"))
|
||||
check("D20", "a Project Admin can still change the completed SOP", r.status_code == 200,
|
||||
f"status {r.status_code}")
|
||||
qa = [u.id for u in A.project_qa_group(SessionLocal(), "proj_a")]
|
||||
check("D20", "QA mail still goes to the group the real SOP names", qa == ["user_padmin"], str(qa))
|
||||
|
||||
# A rogue complete SOP made BEFORE D20 (written straight to the table, newer
|
||||
# than the real one) must not stay the baseline.
|
||||
s = SessionLocal()
|
||||
old, new = datetime.now(timezone.utc) - timedelta(days=2), datetime.now(timezone.utc)
|
||||
s.add(models.Sop(id="sop__proj_b", project_id="proj_b", name="real", complete=True,
|
||||
data=team("user_padmin"), updated_at=old))
|
||||
s.add(models.Sop(id="sop_rogue_b", project_id="proj_b", name="rogue", complete=True,
|
||||
data=team("user_member"), updated_at=new))
|
||||
s.commit()
|
||||
r = as_member.get("/api/sops/latest?complete=true&project_id=proj_b")
|
||||
check("D20", "the page-load baseline is the canonical SOP, not a newer rogue row",
|
||||
r.status_code == 200 and r.json().get("id") == "sop__proj_b", f"{r.status_code} {r.json().get('id')}")
|
||||
qa = [u.id for u in A.project_qa_group(SessionLocal(), "proj_b")]
|
||||
team_ids = A.project_sop_team(SessionLocal(), "proj_b")
|
||||
check("D20", "QA and PM/CM mail follow the canonical SOP", qa == ["user_padmin"] and team_ids == ["user_padmin", "user_padmin"],
|
||||
f"qa={qa} team={team_ids}")
|
||||
r = as_member.get("/api/projects/proj_b/summary")
|
||||
check("D20", "the launcher summary reports the canonical SOP", r.json().get("sop_id") == "sop__proj_b",
|
||||
str(r.json().get("sop_id")))
|
||||
|
||||
# Rows made before D20 under other ids keep working under the old rules.
|
||||
s.add(models.Sop(id="sop_legacy", project_id="proj_c", name="legacy", complete=False, data={}))
|
||||
db.add(models.ProjectMember(id="pm_member_c", user_id="user_member", project_id="proj_c", role=""))
|
||||
db.commit(); s.commit()
|
||||
r = sop(as_member, id="sop_legacy", project_id="proj_c", name="legacy edited", complete=False)
|
||||
check("D20", "an existing non-canonical SOP can still be edited", r.status_code == 200,
|
||||
f"status {r.status_code}")
|
||||
r = sop(as_member, id="sop_legacy", project_id="proj_c", name="legacy done", complete=True)
|
||||
check("D20", "...and completed the first time", r.status_code == 200, f"status {r.status_code}")
|
||||
r = sop(as_member, id="sop__proj_c", project_id="proj_c", name="second", complete=True)
|
||||
check("D20", "while a project has a complete SOP, another complete one needs Project Admin",
|
||||
r.status_code == 403, f"status {r.status_code}")
|
||||
r = sop(as_member, id="sop_legacy", project_id="proj_a", name="moved", complete=False)
|
||||
check("D20", "a SOP cannot be moved into another project under a non-canonical id",
|
||||
r.status_code in (400, 403), f"status {r.status_code}")
|
||||
r = client(account("user_stranger")).get("/api/sops/latest?complete=true&project_id=proj_a")
|
||||
check("D20", "a non-member still gets 404 for the baseline", r.status_code == 404, f"status {r.status_code}")
|
||||
r = as_admin.post("/api/sops", json={"project_id": "proj_c", "name": "x", "complete": False})
|
||||
check("D20", "an id-less write (smoke test, seed_demo, admin diagnostics) targets the canonical row",
|
||||
r.status_code == 200 and r.json().get("id") == "sop__proj_c", f"{r.status_code} {r.json().get('id')}")
|
||||
|
||||
|
||||
SECTIONS = [d21, d19, d20]
|
||||
|
||||
|
||||
def main() -> int:
|
||||
print("Wave 16 security checks")
|
||||
try:
|
||||
for section in SECTIONS:
|
||||
section()
|
||||
finally:
|
||||
db.close()
|
||||
A.engine.dispose()
|
||||
shutil.rmtree(TMP, ignore_errors=True)
|
||||
passed = sum(RESULTS)
|
||||
print(f"\n{passed}/{len(RESULTS)} checks passed.")
|
||||
return 0 if passed == len(RESULTS) else 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
66
tests/session_default_check.py
Normal file
66
tests/session_default_check.py
Normal file
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Is the session limit 8 hours by default? - D25 / T13.11.
|
||||
|
||||
The default of AUTH_SESSION_HOURS is 8. The token expiry and the cookie max-age both
|
||||
follow it, a set value overrides it, and the shipped files name the same default.
|
||||
|
||||
Self-contained: no network, no server, no browser. Exit 0 all passed, 1 a failure.
|
||||
"""
|
||||
import importlib
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from datetime import datetime, timezone
|
||||
from types import SimpleNamespace
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, ROOT)
|
||||
os.environ["DATABASE_URL"] = "sqlite://"
|
||||
os.environ["AUTH_SECRET_KEY"] = "session-default-check-secret-0123456789abcdef"
|
||||
|
||||
fails = 0
|
||||
|
||||
|
||||
def chk(label, ok, extra=""):
|
||||
global fails
|
||||
print(("PASS " if ok else "FAIL ") + label + (f" [{extra}]" if (extra and not ok) else ""))
|
||||
if not ok:
|
||||
fails += 1
|
||||
|
||||
|
||||
def load(value):
|
||||
if value is None:
|
||||
os.environ.pop("AUTH_SESSION_HOURS", None)
|
||||
else:
|
||||
os.environ["AUTH_SESSION_HOURS"] = value
|
||||
from server import auth
|
||||
return importlib.reload(auth)
|
||||
|
||||
|
||||
import jwt # noqa: E402
|
||||
|
||||
user = SimpleNamespace(id="u1", username="a", role="project_user", token_version=0)
|
||||
|
||||
auth = load(None)
|
||||
chk("default is 8 hours", auth.SESSION_HOURS == 8, str(auth.SESSION_HOURS))
|
||||
claims = jwt.decode(auth.create_token(user), auth.SECRET_KEY, algorithms=["HS256"])
|
||||
hours = (datetime.fromtimestamp(claims["exp"], timezone.utc) - datetime.fromtimestamp(claims["iat"], timezone.utc)).total_seconds() / 3600
|
||||
chk("a new token lasts 8 hours", abs(hours - 8) < 0.01, str(hours))
|
||||
|
||||
auth = load("2")
|
||||
chk("AUTH_SESSION_HOURS=2 overrides the default", auth.SESSION_HOURS == 2)
|
||||
auth = load("12")
|
||||
chk("AUTH_SESSION_HOURS=12 overrides the default", auth.SESSION_HOURS == 12)
|
||||
load(None)
|
||||
|
||||
def read(p):
|
||||
return open(os.path.join(ROOT, p), encoding="utf-8").read()
|
||||
|
||||
chk("docker-compose.yml defaults to 8", "AUTH_SESSION_HOURS: ${AUTH_SESSION_HOURS:-8}" in read("docker-compose.yml"))
|
||||
chk(".env.example says 8", re.search(r"^# AUTH_SESSION_HOURS=8$", read("server/.env.example"), re.M) is not None)
|
||||
chk("DEPLOY-login-portal.md says 8", "defaults to 8" in read("DEPLOY-login-portal.md"))
|
||||
for p in ("docker-compose.yml", "server/.env.example", "DEPLOY-login-portal.md", "server/auth.py"):
|
||||
chk(f"no leftover default of 12 in {p}", not re.search(r"AUTH_SESSION_HOURS[^\n]{0,30}\b12\b", read(p)))
|
||||
|
||||
print("\nDONE" if fails == 0 else f"\n{fails} FAILED")
|
||||
sys.exit(1 if fails else 0)
|
||||
220
tests/session_expiry_check.py
Normal file
220
tests/session_expiry_check.py
Normal file
@@ -0,0 +1,220 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Does the offline sign-in window follow the server? - D27 / T14.2.
|
||||
|
||||
auth-guard.js used to let the app open offline for a fixed 12 hours after the last
|
||||
good check, and every page load reset that time. The server session lasts
|
||||
AUTH_SESSION_HOURS (8). This check proves the browser now uses the server's own end
|
||||
time.
|
||||
|
||||
1. /api/auth/me returns session_expires_at and it matches the token's exp
|
||||
2. the browser keeps it, and the fixed 12 hours is gone from the code
|
||||
3. offline before that time: the app opens as normal
|
||||
4. offline after that time: read and queue. No redirect, the mode line shows,
|
||||
a new change is queued and held, not sent
|
||||
5. an older cache with no end time is treated as ended (no hidden 12 hours)
|
||||
6. a page load offline does not extend the cached window
|
||||
7. no cache at all, offline: sign-in page
|
||||
8. online with a dead session: sign-in page with next=, and the outbox is kept
|
||||
9. refreshing the cached user keeps the end of the session
|
||||
|
||||
Exit 0 all passed, 1 a failure, 2 could not run.
|
||||
"""
|
||||
import base64
|
||||
import calendar
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
import cdp # noqa: E402
|
||||
from browser_check import seed, start_server, chk, _PASS, _FAIL # noqa: E402
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
HTML = os.path.join(os.path.dirname(HERE), "html")
|
||||
KEY = "wp_sync_outbox_v1"
|
||||
|
||||
# Makes /api/auth/me fail like a missing network when localStorage.x_me_offline is set.
|
||||
OFFLINE_SHIM = """(() => {
|
||||
const real = window.fetch.bind(window);
|
||||
window.fetch = function (u, o) {
|
||||
let off = false; try { off = !!localStorage.getItem('x_me_offline'); } catch (e) {}
|
||||
if (off && String(u).indexOf('/api/auth/me') !== -1) return Promise.reject(new TypeError('Failed to fetch'));
|
||||
return real(u, o);
|
||||
};
|
||||
})();"""
|
||||
|
||||
STUB = """(() => {
|
||||
if (!window.__realFetch) window.__realFetch = window.fetch.bind(window);
|
||||
window.__calls = [];
|
||||
window.fetch = function (url, opts) {
|
||||
const m = ((opts && opts.method) || 'GET').toUpperCase();
|
||||
if (m === 'POST' && /\\/api\\/(wps|sops)$/.test(String(url))) {
|
||||
window.__calls.push(String(url));
|
||||
return Promise.resolve(new Response('{}', { status: 200, headers: { 'Content-Type': 'application/json' } }));
|
||||
}
|
||||
return window.__realFetch(url, opts);
|
||||
};
|
||||
return true;
|
||||
})()"""
|
||||
|
||||
|
||||
def token_exp(token):
|
||||
body = token.split(".")[1]
|
||||
body += "=" * (-len(body) % 4)
|
||||
return int(json.loads(base64.urlsafe_b64decode(body))["exp"])
|
||||
|
||||
|
||||
def cache(page):
|
||||
return json.loads(page.eval("localStorage.getItem('wp_auth_cache') || 'null'") or "null")
|
||||
|
||||
|
||||
def load(page, base, path="/field.html"):
|
||||
page.goto(base + path)
|
||||
time.sleep(2.0)
|
||||
|
||||
|
||||
def main():
|
||||
exe = cdp.find_browser()
|
||||
if not exe:
|
||||
print("no headless-capable browser found; set WP_BROWSER.")
|
||||
return 2
|
||||
tmpdir = tempfile.mkdtemp(prefix="wpsuite-expiry-")
|
||||
db_path = os.path.join(tmpdir, "check.db")
|
||||
server = None
|
||||
try:
|
||||
tok = seed(db_path)
|
||||
port = cdp.free_port()
|
||||
base = "http://127.0.0.1:%d" % port
|
||||
server = start_server(port, db_path)
|
||||
if server is None:
|
||||
print("the test server would not start.")
|
||||
return 2
|
||||
print("\nThe offline window follows the server - D27 / T14.2\nTarget: %s" % base)
|
||||
browser = cdp.Browser(exe)
|
||||
page = browser.page()
|
||||
try:
|
||||
page.ws.call("Page.addScriptToEvaluateOnNewDocument", {"source": OFFLINE_SHIM})
|
||||
page.clear_cookies()
|
||||
page.set_cookie("wp_session", tok["root"])
|
||||
want = token_exp(tok["root"])
|
||||
|
||||
print("\n1-2. the server sends its own end time and the browser keeps it")
|
||||
load(page, base)
|
||||
me = page.eval("fetch('/api/auth/me').then(r => r.json())")
|
||||
got = (me or {}).get("session_expires_at")
|
||||
chk("/api/auth/me returns session_expires_at", bool(got), json.dumps(me)[:200])
|
||||
chk("it is a UTC time that matches the token exp",
|
||||
bool(got) and bool(re.match(r"^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ$", got)) and
|
||||
abs(calendar.timegm(time.strptime(got, "%Y-%m-%dT%H:%M:%SZ")) - want) <= 1,
|
||||
"%s vs %s" % (got, want))
|
||||
c = cache(page)
|
||||
chk("the cache holds the server's end time",
|
||||
bool(c) and abs((c.get("exp") or 0) / 1000.0 - want) <= 1, json.dumps(c)[:200])
|
||||
src = open(os.path.join(HTML, "auth-guard.js"), encoding="utf-8").read()
|
||||
code = re.sub(r"/\*.*?\*/|//[^\n]*", "", src, flags=re.S)
|
||||
chk("the fixed 12 hours is gone from auth-guard.js", "12 * 3600" not in code and "43200000" not in code)
|
||||
|
||||
user = json.dumps(c["user"]) if c else "{}"
|
||||
now_ms = int(time.time() * 1000)
|
||||
|
||||
def set_cache(obj):
|
||||
page.eval("localStorage.setItem('wp_auth_cache', %s); localStorage.setItem('x_me_offline','1'); true"
|
||||
% json.dumps(json.dumps(obj)))
|
||||
|
||||
print("\n3. offline before the end time: opens as normal")
|
||||
future = now_ms + 3600 * 1000
|
||||
set_cache({"user": c["user"], "exp": future, "at": 111})
|
||||
load(page, base)
|
||||
chk("no redirect", page.eval("location.pathname") == "/field.html", page.eval("location.href"))
|
||||
chk("the user is the cached user", page.eval("window.WP_USER && window.WP_USER.username") == "root")
|
||||
chk("mode is offline, not read-and-queue", page.eval("window.WP_SESSION_MODE") == "offline",
|
||||
page.eval("window.WP_SESSION_MODE"))
|
||||
|
||||
print("\n6. a page load offline does not extend the window")
|
||||
c2 = cache(page)
|
||||
chk("the end time did not move", c2 and c2.get("exp") == future, json.dumps(c2))
|
||||
chk("the cache was not rewritten", c2 and c2.get("at") == 111, json.dumps(c2))
|
||||
|
||||
print("\n4. offline after the end time: read and queue")
|
||||
set_cache({"user": c["user"], "exp": now_ms - 3600 * 1000, "at": now_ms})
|
||||
load(page, base)
|
||||
chk("no redirect to the sign-in page", page.eval("location.pathname") == "/field.html",
|
||||
page.eval("location.href"))
|
||||
chk("the user is the cached user", page.eval("window.WP_USER && window.WP_USER.username") == "root")
|
||||
chk("mode is read and queue", page.eval("window.WP_SESSION_MODE") == "readqueue",
|
||||
page.eval("window.WP_SESSION_MODE"))
|
||||
badge = page.eval("(document.getElementById('wp-sync-badge')||{}).textContent || ''")
|
||||
chk("the mode line shows", "Working offline. Sign in again when you have a signal to send your changes to the project." in badge,
|
||||
badge)
|
||||
chk("the line sits in the live region",
|
||||
page.eval("(document.getElementById('wp-sync-badge')||{getAttribute(){}}).getAttribute('role')") == "status")
|
||||
page.eval(STUB)
|
||||
page.eval("localStorage.removeItem(%r); ProjectData.pushWP({id:'wp_rq', name:'rq'}, 'projA'); true" % KEY)
|
||||
page.eval("ProjectData.flushSync()")
|
||||
time.sleep(0.5)
|
||||
q = json.loads(page.eval("localStorage.getItem(%r) || '[]'" % KEY))
|
||||
chk("a new change is queued", [o.get("key") for o in q] == ["wp_rq"], json.dumps(q)[:200])
|
||||
chk("and held: nothing was sent", page.eval("window.__calls") == [], str(page.eval("window.__calls")))
|
||||
|
||||
print("\n5. an older cache with no end time counts as ended")
|
||||
set_cache({"user": c["user"], "at": now_ms})
|
||||
load(page, base)
|
||||
chk("read and queue, no redirect, no hidden window",
|
||||
page.eval("location.pathname") == "/field.html" and page.eval("window.WP_SESSION_MODE") == "readqueue",
|
||||
page.eval("location.href") + " " + str(page.eval("window.WP_SESSION_MODE")))
|
||||
|
||||
print("\n9. refreshing the cached user keeps the end of the session")
|
||||
set_cache({"user": c["user"], "exp": future, "at": 1})
|
||||
page.eval("window.wpCacheAuth({username: 'renamed'}); true")
|
||||
c3 = cache(page)
|
||||
chk("the user changed", c3 and c3["user"].get("username") == "renamed", json.dumps(c3))
|
||||
chk("the end time did not", c3 and c3.get("exp") == future, json.dumps(c3))
|
||||
|
||||
print("\n7. no cache, offline: the sign-in page")
|
||||
page.eval("localStorage.removeItem('wp_auth_cache'); localStorage.setItem('x_me_offline','1'); true")
|
||||
load(page, base)
|
||||
chk("sent to login.html", page.eval("location.pathname") == "/login.html", page.eval("location.href"))
|
||||
|
||||
print("\n8. online with a dead session: sign-in page and the outbox is kept")
|
||||
page.clear_cookies()
|
||||
page.set_cookie("wp_session", tok["root"])
|
||||
page.goto(base + "/field.html")
|
||||
time.sleep(1.5)
|
||||
page.eval("localStorage.removeItem('x_me_offline'); localStorage.setItem(%r, %s); true" % (
|
||||
KEY, json.dumps(json.dumps([{"kind": "wp", "key": "wp_keep", "opId": "wp:wp_keep:1", "tries": 0,
|
||||
"owner": "root", "body": {"id": "wp_keep"}}]))))
|
||||
page.clear_cookies()
|
||||
page.set_cookie("wp_session", "not-a-valid-token")
|
||||
load(page, base)
|
||||
loc = page.eval("location.pathname + location.search")
|
||||
chk("online with a dead session: redirect to login.html?next=",
|
||||
loc.startswith("/login.html") and "next=" in loc, loc)
|
||||
q = json.loads(page.eval("localStorage.getItem(%r) || '[]'" % KEY))
|
||||
chk("the queued change is still there", [o.get("key") for o in q] == ["wp_keep"], json.dumps(q)[:200])
|
||||
finally:
|
||||
page.close()
|
||||
browser.close()
|
||||
finally:
|
||||
if server is not None:
|
||||
server.kill()
|
||||
try:
|
||||
server.wait(timeout=10)
|
||||
except subprocess.TimeoutExpired:
|
||||
pass
|
||||
try:
|
||||
from server.db import engine
|
||||
engine.dispose()
|
||||
except Exception:
|
||||
pass
|
||||
print("\n%d passed, %d failed" % (len(_PASS), len(_FAIL)))
|
||||
return 1 if _FAIL else 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
236
tests/session_idle_check.py
Normal file
236
tests/session_idle_check.py
Normal file
@@ -0,0 +1,236 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Does the sliding session with a hard ceiling work? - D18 / T13.1 (checked in T13.6).
|
||||
|
||||
T13.1 shipped with its done-when list unchecked and no test of its own; the old
|
||||
session tests had no idea about idle time. This is that test.
|
||||
|
||||
1. Continuous activity keeps a session alive past 30 minutes, but never past the
|
||||
absolute ceiling (AUTH_SESSION_HOURS) measured from the ORIGINAL sign-in.
|
||||
2. A session idle for more than 30 minutes is refused on its next request.
|
||||
3. The cookie is not rewritten on every request (the refresh is throttled).
|
||||
4. A token from before this shipped (no login_at) still gets a hard ceiling,
|
||||
from its iat.
|
||||
5. A refresh carries identity and login_at forward unchanged.
|
||||
6. The two variables are documented, and both defaults are flagged as proposed.
|
||||
|
||||
Self-contained: throwaway SQLite, in-process test client, no network, no browser.
|
||||
Exit 0 all passed, 1 a failure.
|
||||
"""
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
import tempfile
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.insert(0, ROOT)
|
||||
os.environ["DATABASE_URL"] = "sqlite:///" + os.path.join(tempfile.mkdtemp(prefix="wp-idle-"), "t.db").replace("\\", "/")
|
||||
os.environ["AUTH_SECRET_KEY"] = "session-idle-check-secret-not-for-production"
|
||||
os.environ.pop("AUTH_IDLE_MINUTES", None)
|
||||
os.environ.pop("AUTH_SESSION_HOURS", None)
|
||||
|
||||
import jwt # noqa: E402
|
||||
from fastapi.testclient import TestClient # noqa: E402
|
||||
|
||||
from server import app as appmod, auth, models # noqa: E402
|
||||
from server.db import Base, SessionLocal, engine # noqa: E402
|
||||
|
||||
if not hasattr(auth, "maybe_refresh_token"):
|
||||
print("SKIP: T13.1 (sliding session) is not on this branch. It waits for T14.1.")
|
||||
sys.exit(0)
|
||||
|
||||
fails = 0
|
||||
|
||||
|
||||
def chk(label, ok, extra=""):
|
||||
global fails
|
||||
print(("PASS " if ok else "FAIL ") + label + (f" [{extra}]" if (extra and not ok) else ""))
|
||||
if not ok:
|
||||
fails += 1
|
||||
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
with SessionLocal() as db:
|
||||
db.add(models.User(id="u1", username="alice", email="a@x.test", role=auth.ROLE_ADMIN))
|
||||
db.commit()
|
||||
USER = db.get(models.User, "u1")
|
||||
db.expunge(USER)
|
||||
|
||||
# A route the auth gate covers. NOT /api/auth/me: everything under /api/auth/ is
|
||||
# exempt from the gate (auth._EXEMPT_PREFIXES), so no session refresh happens there
|
||||
# and a probe on it would pass without testing anything.
|
||||
PROBE = "/api/projects"
|
||||
IDLE = auth.IDLE_MINUTES
|
||||
CEIL_H = auth.SESSION_HOURS
|
||||
NOW = datetime.now(timezone.utc)
|
||||
|
||||
|
||||
def mint(login_at=None, iat=None, exp=None, **extra):
|
||||
"""A token with exactly the claims given (None = leave the claim out)."""
|
||||
p = {"sub": "u1", "username": "alice", "role": auth.ROLE_ADMIN, "ver": 0}
|
||||
if iat is not None:
|
||||
p["iat"] = iat
|
||||
if login_at is not None:
|
||||
p["login_at"] = login_at.timestamp()
|
||||
if exp is not None:
|
||||
p["exp"] = exp
|
||||
p.update(extra)
|
||||
return jwt.encode(p, auth.SECRET_KEY, algorithm=auth.JWT_ALG)
|
||||
|
||||
|
||||
def raw_claims(tok):
|
||||
return jwt.decode(tok, auth.SECRET_KEY, algorithms=[auth.JWT_ALG], options={"verify_exp": False, "verify_iat": False, "verify_nbf": False})
|
||||
|
||||
|
||||
class FakeClock(datetime):
|
||||
_now = NOW
|
||||
|
||||
@classmethod
|
||||
def now(cls, tz=None):
|
||||
return cls._now
|
||||
|
||||
|
||||
real_dt = auth.datetime
|
||||
|
||||
|
||||
def at(t):
|
||||
FakeClock._now = t
|
||||
auth.datetime = FakeClock
|
||||
|
||||
|
||||
def real():
|
||||
auth.datetime = real_dt
|
||||
|
||||
|
||||
print("defaults: idle %d min, ceiling %d h" % (IDLE, CEIL_H))
|
||||
|
||||
# ── 1. continuous activity, then the ceiling ──────────────────────────────────
|
||||
print("\n1. continuous activity stays alive past 30 minutes, but is cut off at the ceiling")
|
||||
at(NOW)
|
||||
tok = auth.create_token(USER)
|
||||
c = raw_claims(tok)
|
||||
chk("a new token carries login_at", "login_at" in c)
|
||||
chk("a new token expires one idle window out, not at the ceiling",
|
||||
abs(c["exp"] - (NOW + timedelta(minutes=IDLE)).timestamp()) < 2)
|
||||
login_at = NOW
|
||||
cur = c
|
||||
alive_past_30 = False
|
||||
rewrites = 0
|
||||
last_exp = cur["exp"]
|
||||
t = NOW
|
||||
STEP = timedelta(minutes=1) # an active user: a request every minute
|
||||
end = NOW + timedelta(hours=CEIL_H + 2)
|
||||
cut_off_at = None
|
||||
while t < end:
|
||||
t += STEP
|
||||
at(t)
|
||||
if cur["exp"] <= t.timestamp(): # the session already lapsed
|
||||
cut_off_at = t
|
||||
break
|
||||
if t - NOW > timedelta(minutes=IDLE):
|
||||
alive_past_30 = True
|
||||
new = auth.maybe_refresh_token(cur)
|
||||
if new:
|
||||
rewrites += 1
|
||||
cur = raw_claims(new)
|
||||
last_exp = cur["exp"]
|
||||
real()
|
||||
chk("with a request every minute the session is alive well past 30 minutes", alive_past_30)
|
||||
chk("it is never extended past the ceiling (last exp <= login + %d h)" % CEIL_H,
|
||||
last_exp <= (login_at + timedelta(hours=CEIL_H)).timestamp() + 1,
|
||||
str(datetime.fromtimestamp(last_exp, timezone.utc)))
|
||||
chk("it IS cut off once the ceiling is reached, however active the session",
|
||||
cut_off_at is not None and cut_off_at <= login_at + timedelta(hours=CEIL_H) + timedelta(minutes=IDLE) + STEP,
|
||||
str(cut_off_at))
|
||||
chk("the claims after the last refresh still carry the ORIGINAL login_at",
|
||||
abs(cur["login_at"] - login_at.timestamp()) < 1)
|
||||
steps = int((CEIL_H * 60) / 1)
|
||||
chk("the cookie was rewritten far less often than once per request (%d rewrites for ~%d requests)" % (rewrites, steps),
|
||||
0 < rewrites < steps / 5, f"{rewrites}/{steps}")
|
||||
|
||||
# ── 2. idle ───────────────────────────────────────────────────────────────────
|
||||
print("\n2. a session idle for more than %d minutes is refused on its next request" % IDLE)
|
||||
client = TestClient(appmod.app)
|
||||
idle_tok = mint(login_at=NOW - timedelta(minutes=IDLE + 1), iat=NOW - timedelta(minutes=IDLE + 1),
|
||||
exp=NOW - timedelta(minutes=1))
|
||||
client.cookies.set(auth.COOKIE_NAME, idle_tok)
|
||||
r = client.get(PROBE)
|
||||
chk("an idle session gets 401", r.status_code == 401, r.status_code)
|
||||
live_tok = mint(login_at=NOW - timedelta(minutes=5), iat=NOW - timedelta(minutes=5),
|
||||
exp=NOW + timedelta(minutes=IDLE - 5))
|
||||
client.cookies.clear()
|
||||
client.cookies.set(auth.COOKIE_NAME, live_tok)
|
||||
chk("a session inside the window gets 200", client.get(PROBE).status_code == 200)
|
||||
|
||||
# ── 3. throttle ───────────────────────────────────────────────────────────────
|
||||
print("\n3. the cookie is not rewritten on every request")
|
||||
fresh = auth.create_token(USER)
|
||||
client.cookies.clear()
|
||||
client.cookies.set(auth.COOKIE_NAME, fresh)
|
||||
cookies_set = 0
|
||||
for _ in range(20):
|
||||
r = client.get(PROBE)
|
||||
if "set-cookie" in r.headers and auth.COOKIE_NAME in r.headers["set-cookie"]:
|
||||
cookies_set += 1
|
||||
chk("20 rapid requests on a fresh session: 0 cookie rewrites", cookies_set == 0, cookies_set)
|
||||
|
||||
aged = mint(login_at=NOW - timedelta(minutes=20), iat=NOW - timedelta(minutes=20),
|
||||
exp=NOW + timedelta(minutes=2)) # 2 minutes of idle window left
|
||||
client.cookies.clear()
|
||||
client.cookies.set(auth.COOKIE_NAME, aged)
|
||||
r = client.get(PROBE)
|
||||
chk("an aged session gets a refreshed cookie on its next request",
|
||||
r.status_code == 200 and auth.COOKIE_NAME in r.headers.get("set-cookie", ""))
|
||||
m = re.search(auth.COOKIE_NAME + r"=([^;]+)", r.headers.get("set-cookie", ""))
|
||||
newc = raw_claims(m.group(1)) if m else {}
|
||||
chk("the refreshed cookie keeps the original login_at",
|
||||
abs(newc.get("login_at", 0) - (NOW - timedelta(minutes=20)).timestamp()) < 1)
|
||||
client.cookies.clear()
|
||||
client.cookies.set(auth.COOKIE_NAME, m.group(1) if m else "")
|
||||
r2 = client.get(PROBE)
|
||||
chk("...and the very next request does not rewrite it again", auth.COOKIE_NAME not in r2.headers.get("set-cookie", ""))
|
||||
|
||||
# ── 4. tokens from before D18 ─────────────────────────────────────────────────
|
||||
print("\n4. a token with no login_at still gets a hard ceiling (via iat)")
|
||||
old = {"sub": "u1", "username": "alice", "role": auth.ROLE_ADMIN, "ver": 0,
|
||||
"iat": (NOW - timedelta(hours=CEIL_H + 1)).timestamp(),
|
||||
"exp": (NOW + timedelta(hours=3)).timestamp()} # the old flat 12 h exp, still valid
|
||||
chk("past its iat-based ceiling: no refresh, the old exp is not extended", auth.maybe_refresh_token(old) is None)
|
||||
young = {"sub": "u1", "username": "alice", "role": auth.ROLE_ADMIN, "ver": 0,
|
||||
"iat": (NOW - timedelta(hours=1)).timestamp(),
|
||||
"exp": (NOW + timedelta(minutes=2)).timestamp()}
|
||||
ref = auth.maybe_refresh_token(young)
|
||||
rc = raw_claims(ref) if ref else {}
|
||||
chk("inside the ceiling: refreshed, and login_at is taken from iat (not 'now')",
|
||||
bool(ref) and abs(rc["login_at"] - young["iat"]) < 1)
|
||||
chk("the refreshed token cannot outlive iat + ceiling",
|
||||
bool(ref) and rc["exp"] <= young["iat"] + CEIL_H * 3600 + 1)
|
||||
nothing = {"sub": "u1", "username": "alice", "role": auth.ROLE_ADMIN, "ver": 0,
|
||||
"exp": (NOW + timedelta(minutes=2)).timestamp()}
|
||||
chk("no login_at and no iat: no refresh, and no crash", auth.maybe_refresh_token(nothing) is None)
|
||||
junk = dict(young, login_at="not-a-number")
|
||||
chk("an unparseable login_at falls back to iat, no crash", auth.maybe_refresh_token(junk) is not None)
|
||||
|
||||
# ── 5. identity carried forward ───────────────────────────────────────────────
|
||||
print("\n5. a refresh carries the identity forward unchanged")
|
||||
src = {"sub": "u9", "username": "zed", "role": "project_admin", "ver": 7,
|
||||
"iat": (NOW - timedelta(minutes=20)).timestamp(),
|
||||
"login_at": (NOW - timedelta(minutes=20)).timestamp(),
|
||||
"exp": (NOW + timedelta(minutes=2)).timestamp()}
|
||||
rc = raw_claims(auth.maybe_refresh_token(src))
|
||||
chk("sub, username, role, ver are unchanged",
|
||||
(rc["sub"], rc["username"], rc["role"], rc["ver"]) == ("u9", "zed", "project_admin", 7))
|
||||
|
||||
# ── 6. documentation ──────────────────────────────────────────────────────────
|
||||
print("\n6. the two variables are documented, defaults flagged as proposed")
|
||||
env = open(os.path.join(ROOT, "server", ".env.example"), encoding="utf-8").read()
|
||||
dep = open(os.path.join(ROOT, "DEPLOYMENT.md"), encoding="utf-8").read()
|
||||
for name in ("AUTH_IDLE_MINUTES", "AUTH_SESSION_HOURS"):
|
||||
chk(f"{name} is in .env.example", name in env)
|
||||
chk(f"{name} is in DEPLOYMENT.md", name in dep)
|
||||
chk(".env.example calls the defaults proposed, not confirmed", re.search(r"propos", env, re.I) is not None)
|
||||
chk("DEPLOYMENT.md calls the defaults proposed, not confirmed", re.search(r"propos", dep, re.I) is not None)
|
||||
|
||||
print()
|
||||
print("ALL PASSED" if not fails else f"{fails} FAILED")
|
||||
sys.exit(0 if not fails else 1)
|
||||
332
tests/sync_badge_check.py
Normal file
332
tests/sync_badge_check.py
Normal file
@@ -0,0 +1,332 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Does the sync badge tell the truth, work by keyboard and touch, and stay out of
|
||||
the way? - D26 and BL-061 / T14.3.
|
||||
|
||||
1. every state shows, in words that name the PROJECT
|
||||
2. Sign in and Dismiss are real <a> and <button> elements: focusable, at least
|
||||
44px, with a visible focus ring. Sign in goes to login.html?next=
|
||||
3. Dismiss removes only the changes the project refused
|
||||
4. text contrast is 4.5:1 or better in every state
|
||||
5. "Everything sent" follows real work and then hides; a page with nothing to
|
||||
send shows no badge
|
||||
6. the console pages (Admin, User Directory) never show it
|
||||
7. at 390 and 1440 the badge covers no visible control on any page that shows it
|
||||
|
||||
Exit 0 all passed, 1 a failure, 2 could not run.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
import cdp # noqa: E402
|
||||
from browser_check import seed, start_server, chk, _PASS, _FAIL # noqa: E402
|
||||
|
||||
KEY = "wp_sync_outbox_v1"
|
||||
|
||||
STUB = """(() => {
|
||||
if (!window.__realFetch) window.__realFetch = window.fetch.bind(window);
|
||||
window.__calls = []; window.__status = 200; window.__never = false;
|
||||
window.fetch = function (url, opts) {
|
||||
const m = ((opts && opts.method) || 'GET').toUpperCase();
|
||||
const u = String(url);
|
||||
if ((m === 'POST' && /\\/api\\/(wps|sops)$/.test(u)) || (m === 'DELETE' && /\\/api\\/wps\\//.test(u))) {
|
||||
window.__calls.push(u);
|
||||
if (window.__never) return new Promise(() => {});
|
||||
const st = window.__status;
|
||||
return Promise.resolve(new Response(JSON.stringify(st >= 400 ? { detail: 'The project is archived. Ask an admin to unarchive it.' } : { ok: true }),
|
||||
{ status: st, headers: { 'Content-Type': 'application/json' } }));
|
||||
}
|
||||
return window.__realFetch(url, opts);
|
||||
};
|
||||
localStorage.removeItem('wp_sync_outbox_v1');
|
||||
return true;
|
||||
})()"""
|
||||
|
||||
QUOTA = """(() => {
|
||||
window.__setItem = Storage.prototype.setItem;
|
||||
Storage.prototype.setItem = function (k, v) {
|
||||
if (k === 'wp_sync_outbox_v1') { const e = new Error('full'); e.name = 'QuotaExceededError'; throw e; }
|
||||
return window.__setItem.call(this, k, v);
|
||||
};
|
||||
return true;
|
||||
})()"""
|
||||
|
||||
BADGE = """(() => {
|
||||
const b = document.getElementById('wp-sync-badge');
|
||||
if (!b) return JSON.stringify({exists: false});
|
||||
const cs = getComputedStyle(b);
|
||||
return JSON.stringify({exists: true, shown: cs.display !== 'none', text: b.textContent,
|
||||
role: b.getAttribute('role'), ctl: Array.from(b.querySelectorAll('.wp-sb-ctl')).map(e => e.tagName + ':' + e.textContent)});
|
||||
})()"""
|
||||
|
||||
# Contrast of the badge text against its own background (alpha composited on the page).
|
||||
CONTRAST = """(() => {
|
||||
const b = document.getElementById('wp-sync-badge');
|
||||
const parse = s => { const m = s.match(/rgba?\\(([^)]+)\\)/); if (!m) return null;
|
||||
const p = m[1].split(',').map(x => parseFloat(x)); return {r: p[0], g: p[1], b: p[2], a: p.length > 3 ? p[3] : 1}; };
|
||||
const over = (f, back) => ({r: f.r * f.a + back.r * (1 - f.a), g: f.g * f.a + back.g * (1 - f.a), b: f.b * f.a + back.b * (1 - f.a), a: 1});
|
||||
const lum = c => { const f = v => { v /= 255; return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4); };
|
||||
return 0.2126 * f(c.r) + 0.7152 * f(c.g) + 0.0722 * f(c.b); };
|
||||
let page = parse(getComputedStyle(document.body).backgroundColor) || {r: 255, g: 255, b: 255, a: 1};
|
||||
if (page.a < 1) page = over(page, {r: 255, g: 255, b: 255});
|
||||
const bg = over(parse(getComputedStyle(b).backgroundColor), page);
|
||||
const fg = over(parse(getComputedStyle(b).color), bg);
|
||||
const l1 = lum(fg), l2 = lum(bg);
|
||||
return (Math.max(l1, l2) + 0.05) / (Math.min(l1, l2) + 0.05);
|
||||
})()"""
|
||||
|
||||
# How many visible interactive elements the badge covers.
|
||||
COVER = """(() => {
|
||||
const b = document.getElementById('wp-sync-badge');
|
||||
const r = b.getBoundingClientRect(); const hits = [];
|
||||
document.querySelectorAll('a[href],button,input,select,textarea,summary,[role="button"],[tabindex]:not([tabindex="-1"])').forEach(o => {
|
||||
if (o === b || b.contains(o)) return;
|
||||
const q = o.getBoundingClientRect();
|
||||
if (!q.width || !q.height || !o.getClientRects().length) return;
|
||||
const cs = getComputedStyle(o); if (cs.visibility === 'hidden' || cs.display === 'none') return;
|
||||
if (q.left < r.right && q.right > r.left && q.top < r.bottom && q.bottom > r.top)
|
||||
hits.push((o.id || o.className || o.tagName).toString().slice(0, 30) + ' ' + (o.textContent || '').trim().slice(0, 20));
|
||||
});
|
||||
// Is there ANY spot on this screen where a badge this size covers nothing? Scanned on
|
||||
// a finer grid than the page code uses, so "the badge missed a free spot" can fail.
|
||||
const rects = [];
|
||||
document.querySelectorAll('a[href],button,input,select,textarea,summary,[role="button"],[tabindex]:not([tabindex="-1"])').forEach(o => {
|
||||
if (o === b || b.contains(o)) return;
|
||||
const q = o.getBoundingClientRect();
|
||||
if (!q.width || !q.height || !o.getClientRects().length) return;
|
||||
const cs = getComputedStyle(o); if (cs.visibility === 'hidden' || cs.display === 'none') return;
|
||||
rects.push(q);
|
||||
});
|
||||
const w = r.width, h = r.height, vw = document.documentElement.clientWidth, vh = window.innerHeight;
|
||||
let free = false;
|
||||
for (let y = 0; y + h <= vh && !free; y += 4) {
|
||||
for (let x = 0; x + w <= vw && !free; x += 4) {
|
||||
if (!rects.some(q => q.left < x + w && q.right > x && q.top < y + h && q.bottom > y)) free = true;
|
||||
}
|
||||
}
|
||||
return JSON.stringify({hits: hits, free: free, rect: [r.left, r.top, r.right, r.bottom].map(Math.round)});
|
||||
})()"""
|
||||
|
||||
|
||||
def info(page):
|
||||
return json.loads(page.eval(BADGE))
|
||||
|
||||
|
||||
def queue(page):
|
||||
return json.loads(page.eval("localStorage.getItem(%r) || '[]'" % KEY))
|
||||
|
||||
|
||||
def boot(page, base, tok, path="/field.html", user="root", width=1440):
|
||||
page.viewport(width, 844 if width < 800 else 900, mobile=width < 800)
|
||||
page.clear_cookies()
|
||||
page.set_cookie("wp_session", tok[user])
|
||||
page.goto(base + path)
|
||||
for _ in range(40):
|
||||
if page.eval("!!(window.WP_USER && window.ProjectData)"):
|
||||
break
|
||||
time.sleep(0.3)
|
||||
time.sleep(0.6)
|
||||
page.eval(STUB)
|
||||
|
||||
|
||||
def push(page, wid="wp_b1"):
|
||||
page.eval("ProjectData.pushWP({id:%r, name:%r}, 'projA'); true" % (wid, wid))
|
||||
time.sleep(0.7)
|
||||
|
||||
|
||||
def drive(page, state):
|
||||
"""Put the page into one badge state by using the real outbox."""
|
||||
if state == "sending":
|
||||
page.eval("window.__never = true")
|
||||
push(page)
|
||||
elif state == "retrying":
|
||||
page.eval("localStorage.setItem(%r, %s); window.__status = 500; true" % (
|
||||
KEY, json.dumps(json.dumps([{"kind": "wp", "key": "wp_r", "opId": "wp:wp_r:1", "tries": 3,
|
||||
"owner": "root", "body": {"id": "wp_r"}}]))))
|
||||
page.eval("ProjectData.flushSync()")
|
||||
time.sleep(0.5)
|
||||
elif state == "held":
|
||||
page.eval("window.__status = 401")
|
||||
push(page)
|
||||
elif state == "unsaved":
|
||||
page.eval(QUOTA)
|
||||
page.eval("window.__never = true")
|
||||
push(page)
|
||||
elif state == "rejected":
|
||||
page.eval("window.__status = 409")
|
||||
push(page)
|
||||
elif state == "readqueue":
|
||||
page.eval("window.WP_SESSION_MODE = 'readqueue'; document.dispatchEvent(new CustomEvent('wp-session-mode')); true")
|
||||
time.sleep(0.4)
|
||||
elif state == "sent":
|
||||
push(page)
|
||||
time.sleep(0.3)
|
||||
|
||||
|
||||
def main():
|
||||
exe = cdp.find_browser()
|
||||
if not exe:
|
||||
print("no headless-capable browser found; set WP_BROWSER.")
|
||||
return 2
|
||||
tmpdir = tempfile.mkdtemp(prefix="wpsuite-badge-")
|
||||
db_path = os.path.join(tmpdir, "check.db")
|
||||
server = None
|
||||
try:
|
||||
tok = seed(db_path)
|
||||
port = cdp.free_port()
|
||||
base = "http://127.0.0.1:%d" % port
|
||||
server = start_server(port, db_path)
|
||||
if server is None:
|
||||
print("the test server would not start.")
|
||||
return 2
|
||||
print("\nThe sync badge - D26 and BL-061 / T14.3\nTarget: %s" % base)
|
||||
browser = cdp.Browser(exe)
|
||||
page = browser.page()
|
||||
try:
|
||||
page.ws.call("Emulation.setFocusEmulationEnabled", {"enabled": True})
|
||||
|
||||
print("\n1. every state shows, in words that name the project")
|
||||
expect = {
|
||||
"sending": "Sending",
|
||||
"retrying": "Retrying",
|
||||
"held": "Sign in again",
|
||||
"unsaved": "not saved on this device",
|
||||
"rejected": "rejected by the project",
|
||||
"readqueue": "Working offline",
|
||||
}
|
||||
for state, words in expect.items():
|
||||
boot(page, base, tok)
|
||||
drive(page, state)
|
||||
b = info(page)
|
||||
chk("%-9s shows: %r" % (state, words), b.get("shown") and words in b.get("text", ""), b.get("text", "")[:100])
|
||||
chk("%-9s names the project" % state, "project" in b.get("text", ""), b.get("text", "")[:100])
|
||||
chk("%-9s is in a polite live region" % state, b.get("role") == "status")
|
||||
ratio = page.eval(CONTRAST)
|
||||
chk("%-9s contrast %.1f:1 is at least 4.5:1" % (state, ratio), ratio >= 4.5, "%.2f" % ratio)
|
||||
|
||||
print("\n2. Sign in and Dismiss are real, focusable, 44px controls")
|
||||
for state in ("held", "readqueue"):
|
||||
boot(page, base, tok)
|
||||
drive(page, state)
|
||||
r = json.loads(page.eval("""(() => {
|
||||
const a = document.querySelector('#wp-sync-badge a.wp-sb-ctl');
|
||||
if (!a) return JSON.stringify({none: true});
|
||||
a.focus(); const q = a.getBoundingClientRect(); const cs = getComputedStyle(a);
|
||||
return JSON.stringify({tag: a.tagName, href: a.getAttribute('href'), w: q.width, h: q.height,
|
||||
focused: document.activeElement === a, ring: cs.outlineStyle !== 'none' && parseFloat(cs.outlineWidth) >= 2,
|
||||
pe: getComputedStyle(document.getElementById('wp-sync-badge')).pointerEvents});
|
||||
})()"""))
|
||||
chk("%-9s has a Sign in link" % state, r.get("tag") == "A", json.dumps(r))
|
||||
chk("%-9s link goes to login.html?next=" % state,
|
||||
(r.get("href") or "").startswith("login.html?next=%2F"), r.get("href"))
|
||||
chk("%-9s link is at least 44px both ways" % state, r.get("w", 0) >= 44 and r.get("h", 0) >= 44,
|
||||
"%sx%s" % (r.get("w"), r.get("h")))
|
||||
chk("%-9s link takes focus and shows a ring" % state, r.get("focused") and r.get("ring"), json.dumps(r))
|
||||
chk("%-9s the badge takes clicks (no pointer-events:none)" % state, r.get("pe") == "auto", r.get("pe"))
|
||||
|
||||
print("\n3. Dismiss removes only what the project refused")
|
||||
boot(page, base, tok)
|
||||
page.eval("window.__status = 200")
|
||||
page.eval("localStorage.setItem(%r, %s); true" % (KEY, json.dumps(json.dumps([
|
||||
{"kind": "wp", "key": "wp_dead", "opId": "wp:wp_dead:1", "tries": 0, "owner": "root", "dead": True,
|
||||
"lastErr": "The project is archived. Ask an admin to unarchive it.", "body": {"id": "wp_dead"}},
|
||||
{"kind": "wp", "key": "wp_live", "opId": "wp:wp_live:1", "tries": 0, "owner": "root", "body": {"id": "wp_live"}}]))))
|
||||
page.eval("window.__never = true; ProjectData.flushSync(); true")
|
||||
time.sleep(0.5)
|
||||
b = info(page)
|
||||
chk("the reason is shown", "archived" in b.get("text", ""), b.get("text", "")[:120])
|
||||
r = json.loads(page.eval("""(() => {
|
||||
const d = document.querySelector('#wp-sync-badge button.wp-sb-ctl');
|
||||
if (!d) return JSON.stringify({none: true});
|
||||
d.focus(); const q = d.getBoundingClientRect(); const cs = getComputedStyle(d);
|
||||
return JSON.stringify({tag: d.tagName, type: d.type, w: q.width, h: q.height, focused: document.activeElement === d,
|
||||
ring: cs.outlineStyle !== 'none' && parseFloat(cs.outlineWidth) >= 2});
|
||||
})()"""))
|
||||
chk("Dismiss is a <button type=button>", r.get("tag") == "BUTTON" and r.get("type") == "button", json.dumps(r))
|
||||
chk("Dismiss is at least 44px, takes focus, shows a ring",
|
||||
r.get("w", 0) >= 44 and r.get("h", 0) >= 44 and r.get("focused") and r.get("ring"), json.dumps(r))
|
||||
page.eval("document.querySelector('#wp-sync-badge button.wp-sb-ctl').click(); true")
|
||||
time.sleep(0.4)
|
||||
keys = [o["key"] for o in queue(page)]
|
||||
chk("the refused change is gone", "wp_dead" not in keys, str(keys))
|
||||
chk("the waiting change is kept", "wp_live" in keys, str(keys))
|
||||
|
||||
print("\n5. 'Everything sent' follows real work, then hides")
|
||||
boot(page, base, tok)
|
||||
time.sleep(2.5)
|
||||
chk("a page with nothing to send shows no badge", not info(page).get("shown"))
|
||||
drive(page, "sent")
|
||||
seen = False
|
||||
for _ in range(10):
|
||||
if "Everything sent to the project" in info(page).get("text", "") and info(page).get("shown"):
|
||||
seen = True
|
||||
break
|
||||
time.sleep(0.2)
|
||||
chk("after real work it says everything was sent to the project", seen, info(page).get("text", ""))
|
||||
time.sleep(2.6)
|
||||
chk("and then hides", not info(page).get("shown"))
|
||||
page.eval("ProjectData.flushSync(); true")
|
||||
time.sleep(0.6)
|
||||
chk("an idle flush does not flash it again", not info(page).get("shown"))
|
||||
|
||||
print("\n6. the console pages never show it")
|
||||
for path in ("/admin.html", "/users.html"):
|
||||
boot(page, base, tok, path)
|
||||
has = page.eval("document.body.hasAttribute('data-wp-no-sync-badge')")
|
||||
page.eval("window.__status = 401")
|
||||
push(page, "wp_console")
|
||||
b = info(page)
|
||||
chk("%s opts out" % path, has is True)
|
||||
chk("%s shows no badge even with a held queue" % path, not b.get("shown"), b.get("text", "")[:80])
|
||||
|
||||
print("\n7. the badge covers no control, at 390 and 1440")
|
||||
pages = [("/field.html", "field"),
|
||||
("/index.html", "launcher"),
|
||||
("/work-package-suite.html?project=projA", "wizard"),
|
||||
("/wp-creation-index.html?project=projA&wp=wpA1", "creator")]
|
||||
for width in (390, 1440):
|
||||
for path, label in pages:
|
||||
for state in ("sending", "held", "rejected", "unsaved", "readqueue"):
|
||||
boot(page, base, tok, path, width=width)
|
||||
drive(page, state)
|
||||
b = info(page)
|
||||
if not b.get("shown"):
|
||||
chk("%4d %-8s %-9s badge shows" % (width, label, state), False, "hidden")
|
||||
continue
|
||||
cov = json.loads(page.eval(COVER))
|
||||
r = cov["rect"]
|
||||
inside = r[0] >= 0 and r[2] <= width + 1
|
||||
# The rule: the badge covers no control whenever the screen has
|
||||
# room for it. A screen with no free gap of that size (BL-072)
|
||||
# cannot satisfy it, and is reported as such, not hidden.
|
||||
ok = (not cov["hits"]) or (not cov["free"])
|
||||
note = "" if not cov["hits"] else " (no free gap on this screen, BL-072)"
|
||||
chk("%4d %-8s %-9s covers no control, fits the screen%s" % (width, label, state, note),
|
||||
ok and inside, "%s rect=%s free=%s" % (cov["hits"][:2], r, cov["free"]))
|
||||
finally:
|
||||
page.close()
|
||||
browser.close()
|
||||
finally:
|
||||
if server is not None:
|
||||
server.kill()
|
||||
try:
|
||||
server.wait(timeout=10)
|
||||
except subprocess.TimeoutExpired:
|
||||
pass
|
||||
try:
|
||||
from server.db import engine
|
||||
engine.dispose()
|
||||
except Exception:
|
||||
pass
|
||||
print("\n%d passed, %d failed" % (len(_PASS), len(_FAIL)))
|
||||
return 1 if _FAIL else 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user