MMOframework
Pourquoi l'IA Playground Bibliothèques Développeurs Agence
FR EN
Créer un projet en direct
Index
# Security of the accounts — mmo.oauth What mmo.oauth does for the accounts of an application, since 2026-11 (ISO/IEC 27001:2022 compliance of the MMOFramework projects, board `iso-27001` of my.oviepro, tickets #17 to #21). Every new behaviour is configured on `MMO\oauth\controllers\AccessController` or `MMO\oauth\Crypt`; what is **on by default** says so. | Topic | Default | Measures | |---|---|---| | Password policy: 12 characters, the 10 000 most common refused | **on** | A.5.17, A.8.5 | | Session id regenerated at a login, a switch of account, a logout | **on** | A.8.5 | | Password reset link: 30 minutes, used once, opens the reset form only | **on** | A.5.17 | | An archived account's tokens refused | **on** | A.5.18 | | A mail at each change of password or second factor | off | A.5.17 | | Second factor (TOTP) | off (enrolment of one's own will possible) | A.8.5, A.8.2 | | Session ended after N minutes without a request, warned before | off | A.8.5 | | « Changer de compte » limited to 1 hour, then back to one's own session | **on** (1 hour) | A.8.2 | | Logout of every device, revocation of the sessions of an account or of all | **on** (routes, command) | A.5.18, A.5.26 | | Last login kept, review of the accesses (`user/access-review`) | **on** (administrators) | A.5.15, A.5.18 | | Accounts without a login for N days archived | off | A.5.18, A.8.2 | | Account created, changed, archived: logged with its author | **on** | A.5.16, A.8.15 | | Tokens naming their key (`kid`), key rotation keeping the old key for verification | **on** (`rotate-keys` by hand) | A.8.24 | ## Passwords (`Crypt`) - `passwordLength` (12): characters, not bytes. `passwordLowcase`, `passwordUpcase`, `passwordDigit`, `passwordSpecial` as before. `passwordBlocklist` (`{MMO\oauth}/data/common-passwords.txt`, null to drop it): the 10 000 most common passwords of zxcvbn's list (MIT), refused whatever their case, or with digits and symbols around them (`Azerty123!`). `allowAnyPassword()` drops it too. - Checked when a password is set only: an existing password stays valid until its next change. Nothing forces a change. - `generatePassword()` makes at least 12 characters, never a common one. - `passwordCost` (bcrypt), null by default: benchmarked to ~50 ms on the first password hashed, not when `Crypt` is built -- it was on every request, ~0.12 s each (my.oviepro-deploy #471). Set it to skip the benchmark. ## Sessions - `\MMO\oauth\regenerateSession()`: a new session id, the data kept, the old file deleted, at each login (password, second factor, `login?token=`, SSO, Google), at « Changer de compte » and its return, at a logout. - `logout` ends the token too (a Bearer one, and its refresh tokens: one left valid renewed it); `logout/all` ends every session of the account (`revokeTokens`). - A password is read from the body of a POST only: `GET login.json` and a `password` in any login url answer 405, unchecked -- a url is written in the access logs. - `idleTimeout` (null: never; my.oviepro sets `PT30M` in its `configs/config.json`, null in its `dev.json`): a session (the cookie, not an `Authorization` header) without a request for that long ends at the next one (`IDLE session` in the log). `idleWarning` (`PT2M`): `src/js/idle.js` warns that long before, `MMO.app.confirm()` offering to stay logged in (a request to `user/config.json`); at the end the page reloads, onto the login form. Every request of the page counts (a `fetch()` resets the count). - `switchAccountTTL` (`PT1H`, null: as long as a session): a session of « Changer de compte » ends after it; its actor is then back in their own session, until the end a session opened at the switch would have had (`SWITCH ACCOUNT ... is back`), if they may still switch. - A token whose account is archived opens nothing (`ClientRepository::loadAccessToken`). - A login route (`login`, `login?token=`, `login/mfa`) ignores a session that ended, instead of answering the login form first. ## External sign-ins (SSO, Google) - An SSO sign-in (`CodeAuthentifier`) carries a `state` and a PKCE verifier of its own, kept in the session (`beginExternalLogin`): a code coming back without this browser's state, used twice or older than ten minutes is refused before the provider is asked (« invalid SSO state »). The session has one at most, the same for every tab while it waits (`pendingExternalLogin`), a new one once used or after ten minutes: showing the login page again no longer drops the older tabs' link among the five sign-ins waiting (`EXTERNAL_LOGINS_MAX`). - Google's authorization code (`google?code=`): `google` sends the browser to Google with the session's `state` (`GoogleController::STATE`, shared as the SSO's), which must come back with the code, once, within ten minutes -- else Google is not asked and nothing opens (400, « invalid Google state », `REFUSED Google code` in the log): a link with an attacker's own code does not sign the victim in as him (login CSRF). With it, the S256 challenge of the sign-in's PKCE verifier (`code_challenge`, `CodeAuthentifier::CodeChallenge`, as the SSO's); the code is exchanged with the verifier (`GoogleAPI::askToken`'s `code_verifier`), which never left the session: a code caught on its way back is useless to anyone else (my.oviepro #455). - Google's button (`google?token=`): the ID token opens a session only signed by one of Google's keys, issued by Google, for our client id, not expired, and carrying a nonce this session asked for -- the button's script asks it (`POST google/nonce.json`, `GoogleController::nonce`) before `google.accounts.id.initialize({nonce})`, `verifyIdToken` uses it up. The session has one at most, the same for every tab while it waits (`GoogleAPI::nonce`): showing the page again no longer drops the older tabs' among the five sign-ins waiting (`EXTERNAL_LOGINS_MAX`). A token without it, with another session's or used twice answers 401 (`REFUSED Google ID token` in the log): an attacker's own valid token, handed to a victim's browser, does not sign the victim in as him (login CSRF). A page showing the button without the nonce no longer signs anyone in. ## Password reset `POST login/forgot` mails a link of `resetTokenTTL` (`PT30M`): a token whose `purpose` is `reset`, which opens the login routes only (elsewhere the request goes on without it), and `POST login/reset` only, once: the password changed, the token expires, every session of the account ends (`revokeTokens`), and the user logs in with the new password. A normal session can no longer change its password through `login/reset`. ## Mails of a security change `securityMails` (false) and `securityMailTpl` (`security-change.phtml`, a file of mmo.core's `MailController::$templateFolder`, which the project writes): mailed after the commit, with `$_VARS['change']` (`password`, `mfa`, `mfa-reset`, `deactivated`), `$_VARS['user']`, `$_VARS['date']` (and `$_VARS['days']` for `deactivated`). Each change is logged whatever (`SECURITY CHANGE [password] of [...] by [...]`). ## Second factor (TOTP, `AccessController::$mfa`) ```json "MMO\\oauth\\controllers\\AccessController": { "mfa": {"required": ["admin", "sudo"], "issuer": "Oviepro", "keyFile": "{configs}/mfa.key"} } ``` - `required`: the scopes whose holders must enrol one (none by default). An account that enrolled one is asked its code whatever `required` says. Options: `window` (1 step of 30 s before and after), `recoveryCodes` (10), `pendingTTL` (300 s between the password and the code). - The login form (`POST login`) checks the password, then goes to `login/mfa`: the code, or for an account not enrolled that must be, a QR code (an SVG made on the server: `helpers/QrCode.php`, nothing sent to a third party), the secret to type, a code to confirm, then 10 recovery codes shown once. `login.json` takes the code as `otp` (401 without it; an account not enrolled enrols from the form first). - A logged-in account enrols one of its own will at `login/mfa?enrol=1` (before it is required). - RFC 6238 (HMAC-SHA1, 6 digits, 30 s), a code accepted once (the step of the last one is kept: no replay), a wrong code counts as a failed login (`maxLoginFailures`: 429 after 10 in 15 minutes). Recovery codes: hashed with the key, each used once (`MFA ... logged in with a recovery code: N left`). - The account keeps `mfa` (`@hidden`: never exported): its secret encrypted (`Crypt::encryptHash`, AES-256-CTR + HMAC) with the key of `keyFile`, written (32 random bytes) at the first enrolment if missing. **Keep it out of git, put it in each server's shared configuration and back it up**: a key lost loses every enrolment (each account then has its second factor reset by an administrator). - `GET user/mfa.json`: the account's own state. `POST user/mfa/reset.json?uid=`: an administrator (`rightsScope`) removes an account's second factor (`MFA RESET of [...] by [...]`, mailed). - Google and SSO sign-ins ask no code: the identity provider's second factor applies (a decision per project). ## Review of the accesses - `lastLogin` (`tUser`): set at each login (password, second factor, SSO, Google), not by `login?token=`. - `GET user/access-review.json|csv|html` (`rightsScope` only, logged): every account, its status, roles, the scopes it holds, `rights`/`super`, its perimeter (`reviewPerimeter`: the paths of an account giving it, my.oviepro `groupes`), creation, last login, last change of password, second factor. - `inactiveAccountDays` (0: never): `php admin.php inactive-accounts [--days=] [--dry-run]` (or the job `MMO\oauth\jobs\InactiveAccountsJob`) archives the active accounts without a login for that long — its last login, else its last session, else its creation — never the last holder of `rightsScope`. Each is logged (`INACTIVE ACCOUNT`) and mailed (`deactivated`) when `securityMails`. Run `--dry-run` first: on a base whose logins were never kept, most accounts count from their creation. A daily crontab line runs it. - Every commit of an account logs `ACCOUNT CREATED`, `ACCOUNT STATUS`, `ACCOUNT RIGHTS` (roles, scopes, perimeter), `ACCOUNT CHANGED` (the fields, not their values), `ACCOUNT DELETED`, with the author (the actor of a switch first). A login logs none of them: its last login, and the `mtime` every save moves, are no change of the account. ## The API console: dry runs, `api.live` mmo.client's API console (``) marks its calls `X-MMO-Console: 1` and makes them as dry runs (mmo.core's DryRun, `X-MMO-Dry-Run: 1`: the answer is real, nothing is changed). A call of the console made live is refused (403) to whoever does not hold `AccessController::$liveScope` (`api.live`, in the scopes catalog; `null` checks nothing): a client's developer tries the API on production without changing it, and uses it for real from their own code. A dry run still authenticates: a session's token refreshed is saved (`DryRun::Outside`), and `login.json`, which mints tokens, refuses a dry run (`dryRun: false`). ## Calling the API as another account - By its password: the console posts `login.json` (the token answered, the browser's session untouched), the second factor's code with it when the account has one. - As an account, for an administrator: `POST user/api-token.json?uid=...` (the session's `X-CSRF-Token`, as every account route) gives a token of `AccessController::$apiTokenTTL` (15 minutes), never refreshed, naming its actor (the JWT's `act`, the stored token's), for an account the administrator could manage (`canManage`) and holding the right of « Changer de compte » (`sudo.switchAccount`); a warning of the log says who called as whom until when, each refusal too. It ends with the account's sessions (`user/tokens/revoke.json`). ## API clients A credential of an account for its own code (`entities\ApiClient`), kept where `AccessController::$apiClients` says -- off unless a site's configuration sets it (a site without it has none: its routes answer 404). An application's guide says where it is turned on (my.oviepro: `docs/api-clients.md`). - **Made by an administrator** (`$rightsScope`, checked by `POST user/api-clients.json`) -- or, by any other account, **one of its own only, with the rights of `$apiClientOwnScopes` it holds** (the MCP server's `mcp.data`, `mcp.internals`: a developer's token for their AI assistant; empty, administrators only) --, in their own session, never a switched one, **for an account** (`account`: a username or a uid; theirs without one) that is active and that they manage (`canManage`). Its secret is random (32 bytes), stored hashed, shown once; the session's `X-CSRF-Token` is asked. The client is guarded by these routes (`iGuarded`): no `data.*` scope is asked of its maker, and the generic data api refuses it to whoever lacks them. - **Its rights** are those given among the account's **and** the administrator's: `getScopes()` keeps each rule given that the owner holds, and the owner's refusals beneath it -- never more than the owner, re-read at each request (a right the owner loses, the client loses). - **The account it acts for** is its owner: `\MMO\oauth\account()` -- what an application reads beyond the rights (its groups, its perimeter) it reads there. Rights are read on `\MMO\oauth\user()` (the client), the perimeter and the authorship of a journal on `account()` (my.oviepro: the journal's `utilisateur` is the owner, its `origin` the client). - **Its token**: `POST oauth/token` (`grant_type=client_credentials`, the id and secret in the body or a Basic header) gives an access token of `$apiClientTokenTTL` (1 hour), never refreshed; its failures count as a login's (`$maxLoginFailures`). - **Made on the server's command line** too, for a machine of the team (a development machine pulling the developer inbox): `php admin.php api-clients-create --account= --scopes=dev.inbox --label='...' [--expires=]` -- the account's rights only, no maker's to check: whoever runs it is the operator, named in the journal (`shell:`). Its secret is printed once; the owner is mailed and the administrators told as from the console. - **It lasts** `$apiClientMaxTTL` at most (90 days), its maker choosing an earlier date; **renewed** (`POST user/api-clients/renew.json`, by its owner or an administrator managing it) with a new secret shown once and a new expiry, the old secret and its tokens ended. Its owner is told `$apiClientExpiryNotice` days before it expires (7), once until renewed: `php admin.php api-clients-expiring`, a daily crontab line (`--dry-run` lists them). - **It ends** at its expiry, when revoked (`POST user/api-clients/revoke.json`: by its owner or an administrator managing it, its tokens ended), or when its owner is archived: its token is re-checked at each request (401). - **Who sees it**: `GET user/api-clients.json` answers each account its own clients (`clients`); an administrator asking `?all=1` gets apart (`others`, « All the clients » of ``) those of the accounts they manage (`canManage`) -- never those of an account holding rights they have not, which they neither renew (the new secret would be theirs) nor revoke (403). No answer holds a secret nor its hash (`describeApiClient`; my.oviepro #463). - **Who is told**: its owner by mail when it is made, renewed, revoked or about to expire (`$securityMails`, the template's `change`: `api-client`, `api-client-renewed`, `api-client-revoked`, `api-client-expiring`, with `label`, `clientId`, `expires`, `by`); the administrators (`usersAllowed($rightsScope)`) in mmo.client's notification centre when it is made, renewed or revoked. Each is a line of the log, as its tokens are. ## Declared rights, and their explanation - A route declares the scopes it needs: `#[APIRoute('GET|reports.json', options: ['scopes' => 'reports.read'])]`. Every one is checked in `onRoute`, before the route runs: 401 without an account, 403 (`eInvalidScope`) without the scope. A Bearer token that is none (not a JWT, its encoding or signature broken) is a 401 too, never a 500. `a|b` is held with any of them, and `{scope}` is the controller's configuration (`APIRoute::Scopes()`): mmo.admin's boards and checklists, mmo.core's jobs, the DUERP of my.oviepro declare theirs. mmo.oauth says it checks them (`APIRoute::SetEnforcer`, while AccessController routes): without it a route declaring a scope is refused. The routes checking by hand keep doing it, and move to a declaration one by one. - `api.json` says it (`AccessController::onApiDocument`): the schemes (`bearer`, the `session`'s cookie), each operation's security (none for an `anonymous` one), its `x-scopes`, and `x-allowed` for the caller of the document. - `GET access/explain.json?method=PUT&path=data/main/orders.json` says why an account may call an operation, or not, without calling it: the scopes of its route; for the data api the scope of its collection and action (`data.main.orders#edit`), what carries rights (`$rightsScope`, `$tokensScope`), a private collection; each with the rule deciding (`data.main`, the closest) and what gives it (a role, the account's own rights). An application adds what only it checks through the event `AccessExplain` (my.oviepro: the perimeter). The caller's own rights; another account's (`uid`) for a holder of `$rightsScope` only. - `GET access/scopes.json?scope=duerp.edit` says it the other way round, what a rule held alone opens: the operations whose declared scopes it gives (`a|b` with one of them; `duerp` opens what `duerp.edit` needs, not the reverse), and the collections of the data api it reaches, with their actions (`data.main.orders#show`: `show` only); a collection its module checks itself is said so (`module`). For a logged-in account; the API console's « My access » asks it. - The API console's saved calls (`ApiCall`, `AccessController::$apiCalls`: off without it, the console keeps them in the browser): what makes a call, never a token nor an answer. `GET user/api-calls.json` gives the account's own and those the others shared with every account; `POST` saves one, `PATCH user/api-calls/{id}.json` renames or shares it (its owner), `DELETE` removes it (its owner, a holder of `$rightsScope` for a shared one); another account's own is a 404. Its writes run as a dry run; the collection is `iGuarded`, kept out of the data api (`privateCollections`: `none`). ## Keys and tokens - A token's header names its key: `kid`, the start of the SHA-256 of the public key's PEM (`\MMO\oauth\keyId`), the same on every machine, no configuration. `AccessController::encodeJWT()` / `decodeJWT()`: a token with a `kid` is verified with that key among `verificationKeys()` (the current one, and the retired ones of `keyFiles['retired']`), a token without one (before 2026-11) with the current key. - `php admin.php rotate-keys [--dry-run]`: the public key goes to `keyFiles['retired']/{kid}.public.key`, a new pair is written over `keyFiles['private']` and `['public']` (written aside then renamed, keeping their rights), the retired keys older than a token's life are deleted. Sessions go on (they are stored on the server: a key only signs the links and the `Authorization` headers). Run it as the user serving the site, on each server, then back up the new pair. - `php admin.php revoke-tokens --user= | --all [--dry-run]`, `POST user/tokens/revoke.json?uid=` (an administrator): every token still valid of the account, and those it opened as another, expires now — checked at each request at no extra cost (the token is read from the base anyway). ### Procedure: a key compromised, an account taken over 1. `php admin.php revoke-tokens --user=` (an account) or `--all` (a key, the server). 2. A key: `php admin.php rotate-keys` on each server, then `revoke-tokens --all` (a stolen key signs tokens of its own choosing until its retired copy is deleted: delete `configs/keys/.public.key` at once in that case). 3. An account: reset its password (`login/forgot`), its second factor (`user/mfa/reset.json`), review its rights (`user/access-review`). 4. Log what was done in the incident's record.