← Todos los artículos

OneHi multi-tenant SSO with OIDC/EdDSA and a shared npm client

A single-tenant TOTP SSO was rebuilt as a multi-tenant OIDC identity provider with per-app consent and a published client package

Publicado originalmente en octa.page ↗

Problem and context

The previous SSO (Alohomora) was a single-tenant hub. It had TOTP sign-in, a long-lived hub session, and short-lived Ed25519-signed tickets that each app verified with only a public key. It worked, but it could not model organizations, several sign-in methods, or per-app consent. Each consuming app also carried a copy of the client code.

The goal was a private, inter-organization SSO. Requirements:

  • multi-tenancy (organizations, invitations, roles);
  • several sign-in methods (email code, passkeys, TOTP, Google, GitHub);
  • OIDC for the apps;
  • migration of every existing app.

Design

  • Storage: MySQL. Throttling counters live in the database so they survive restarts. Tokens are stored as SHA-256 hashes, and secrets are sealed with AES-GCM under a master key.
  • Domain model:
    • Roles are owner, admin, member and guest. An organization can never be left without an owner.
    • An identity is global. Membership is per organization.
    • Each app belongs to exactly one organization. An outside person gets access by being invited to the owning organization as a guest.
  • Protocol:
    • Authorization code with PKCE (S256) and exact redirect_uri matching.
    • Codes are single-use inside a transaction.
    • ID tokens are signed with EdDSA.
    • Cookies use the __Host- prefix, HttpOnly and SameSite=Lax.
    • POSTs are checked against cross-site requests with Sec-Fetch-Site and Origin.
  • Consent: the first time a person enters an app, OneHi (not the app's script) shows what the app will receive (name, email, organization, role). The decision is remembered per app, can be withdrawn, and is asked again if the app requests new data. Showing it from the app's own script would let the app skip it.
  • Keys: per-app API keys, checked through an endpoint that needs no client secret. A revoked key stops working shortly after revocation, and a previously valid key survives a provider outage only for a bounded period.

Decision: EdDSA over RS256

EdDSA (chosen) RS256
Compatibility All in-house apps. Some third-party libraries reject it. Accepted everywhere, and the only algorithm OIDC mandates.
Signature size ~86 characters ~342 characters
Configuration surface Minimal Larger, with a history of implementation errors
Cost None. It was already in use and ships with Node. Larger keys to generate and store

The only real consequence is third-party tools that require RS256. If one is ever connected, a second RS256 flow is added alongside, so nothing is rewritten.

Hardening findings (all fixed)

  • Account-existence oracle by timing. The login form answered identically for known and unknown emails, but response latency could reveal whether an account existed. Lookup and send now run after the response, which makes both cases take the same time. The cost is that a send failure can no longer be shown on screen. It goes to the log, and "send a new code" retries.
  • Impersonation through the operator CLI. The invite command printed a sign-in link. The link is now printed only for new emails. For existing accounts it goes only to the person's mailbox.
  • Secrets in logs. In production (HTTPS), a missing mail API key now makes sending fail rather than falling back to printing codes and links.
  • Open redirect through control characters in next. The shared safeNext function now rejects control characters and non-string values.
  • ReDoS in the email regex. The length bound is now part of the shared regex.
  • Sensitive account changes. Adding or removing a passkey or TOTP, connecting an identity provider, changing the email, and deleting the account now require a recent sign-in. Otherwise the user goes through a "confirm it's you" step. Adding a sign-in method sends a notification, and email-code sign-in can be disabled per person under conditions that prevent a mailbox-only attacker from locking the owner out.
  • CSP regression. form-action blocked the redirect back to the app after the consent and sign-in forms, so it was removed.
  • Time zones. Every MySQL connection runs in UTC, and values previously stored in the server's local time were converted.

Process issue: a development .env pointing at production

During phase 1, the local .env pointed at the production database. Migrations and end-to-end tests ran there. The tests clean up after themselves, and no real data existed yet. One signing key encrypted with the wrong master key was left behind and was deleted. A separate development database now exists. CI runs the end-to-end suite against a disposable MySQL 8.4 service with a random master key.

Distribution: @onehi/client

The client had been copied into at least six apps. Cloning the three that were not local showed that all five Node apps carried an outdated safeNext, so the open redirect above existed in every one of them. This motivated publishing the client:

  • Single source: client/onehi.ts stays the source. The server still serves it, and tsc compiles it into the package.
  • Package contents: the tarball was inspected and contains only compiled JS, type declarations, README, license and package.json. Nothing from the server, migrations, tests or env files is included. The license is MIT for that directory only.
  • Name: @onehi/client over @onehi/node and @onehi/gate. "Client" is the OIDC term, matches existing file names and docs, and an engines field plus the package description cover the "is this for browsers?" ambiguity.
  • Rollout: five apps were migrated to the package and local copies were deleted. pnpm needed a minimum-release-age exception for the @onehi/* scope. Typecheck and tests passed in all five.
  • Deploy fix: the deploy failed with ERR_PNPM_ABORTED_REMOVE_MODULES_DIR_NO_TTY because pnpm wanted to confirm a node_modules purge without a TTY. A workspace setting now disables the prompt.
  • CI: the deploy job now depends on a verification job (typecheck, tests, audit). GitHub Actions are pinned by commit SHA, and Dependabot is limited to security updates.