Skip to content

Authentication ​

Feature draft security authentication cookie token saml xyz

How the XYZ API identifies a user from a cookie, token, API key or authorization header, and how login providers (built-in, SAML, custom) set the cookie; use when changing login or adding a provider.

Resource and sources

Behaviour ​

Every request passes validateRequestAuth, which calls auth to find the user.[1] A request without a user is anonymous; on a PRIVATE instance it is redirected to the login, ${DIR}${AUTH_PATH}/login (default /api/user/login).

auth takes the first of:[2]

  1. An Authorization header: email and password checked against the ACL.
  2. A token request param, else the cookie named after TITLE. The JWT is verified with SECRET. A token param with an api claim is an API key and is checked against the key stored in the ACL.
  3. With USER_SESSION, the token's session must match the ACL, so a new login ends earlier sessions. Without it a cookie stays valid until it expires.

For a token or cookie user the roles are expanded: europe.france adds europe, and every such user has *. A user from an Authorization header (HTTP Basic credentials) is returned before this expansion and has only the roles stored in the ACL. Users from a token or API key never have admin rights. /api/user/token issues a token, 8 hours by default; /api/user/key an API key for accounts with the api field set.

Login providers ​

All providers hand a user to mod/user/redirect.js, which looks the email up in the ACL for its roles, signs the JWT cookie and redirects.[3]

  • Built-in: /api/user/login; see user accounts.
  • SAML: apps/saml mounts /saml/login, /saml/acs, /saml/logout, /saml/logout/callback and /saml/metadata; see the SAML variables in environment. The SAML email must have an ACL record. redirect.js copies admin and roles from that record, so a SAML user is an admin when the ACL says so.
  • Custom: apps/auth is a template provider with /custom/login, /custom/verify and /custom/logout routes; AUTH_PATH points the login links and the PRIVATE redirect at it.[4]

Cookies use COOKIE_PROPS, by default Secure; HttpOnly; SameSite=Strict; Path=<DIR or />, and live COOKIE_TTL seconds. A provider setting or revoking a cookie must use the same properties.

Revocation ​

Cookies, tokens and API keys are signed JWTs. Blocking or unapproving an account stops its logins and cookie renewals, but a cookie or token issued before stays valid until it expires. Keep COOKIE_TTL short. USER_SESSION ends a user's earlier sessions when they log in again. Rotating SECRET or SECRET_KEY invalidates every cookie, token and API key at once; do this when an account must lose access immediately.

Flow ​

validateRequestAuth → user/auth.js → fromACL.js (header) or jsonwebtoken (token, cookie) → checkParamToken, checkSession. Login: provider → user/redirect.js → Set-Cookie and 302.

Where to change ​

  • Token and session checks: apps/xyz/mod/user/auth.js.
  • Cookie signing and claims: redirect.js, cookie.js, jwtClaims.js.
  • A new provider: copy apps/auth, call redirect with { email, lookup: true }.

Tests ​

apps/xyz/tests/mod/user/ covers auth, cookies, tokens, keys and redirects.

Pitfalls ​

  • apps/auth is a template. Its /custom/verify route signs in any ACL account by the posted username, without a password or other credential. Replace the verification before deploying it; never deploy it unchanged (#3166).
  • apps/auth/providerRequestSignature.js verifies signed provider URLs but is not mounted by the default server; mount it where signed URLs are used.
  • A deployment over plain HTTP must set COOKIE_PROPS without Secure.

  1. middleware/validateRequestAuth.js ↩︎

  2. user/auth.js ↩︎

  3. user/redirect.js ↩︎

  4. apps/auth/server.js ↩︎

XYZ v5.1.0. Generated from knowledge/ at dd7a6fa27736502733499c1bf485f15659b10338. Concepts marked draft are not reviewed.