Work project · Identity & access

Centralised Auth Service

One sign-in and one access policy for a company's internal apps

TypeScriptCloudflare WorkersHonoWorkers KVNestJSSQLiteDrizzleReactAWSGitHub Actions

Why I built it

Every internal app at the company had its own login and its own idea of permissions. One used database roles, one used shared API keys and one had no auth at all. Nobody could answer "what can this person see?" in one place, and removing someone meant visiting every app.

The service gives the company one sign-in for all of its apps and one policy that says what each person may see inside each of them.

What it does

How it works

Sign-in happens on one login host. A one-time handoff code, tied to the browser that started the sign-in, carries the session back to the app's own domain. On every request the Cloudflare Worker checks the session and whether the person may use that app at all, then mints a 5-minute ES256 JWT and proxies the request to the app through a Cloudflare Tunnel. A firewall rule admits only the Worker.

The app verifies the token against a published key set and asks the SDK what the person may do. The SDK answers from the compiled policy file it holds locally, returning the limits that apply, such as which territories or records, and the app applies them to its own queries. No permission check calls the gateway.

Services use app tokens. A deploy token lets CI send an app's list of capabilities, and a runtime token lets a running app poll for new policy versions with ETags, a hash check and a version floor, so it can never fall back to an older policy.

Why build auth instead of buying Auth0, Okta or Cloudflare Access?

Sign-in can be bought, and my first write-up on this recommended exactly that: buy single sign-on and keep permissions in each app's database. The company's requirements went further. They wanted one policy for what every person may see inside every app, down to the records, which each app can check locally without calling a vendor.

Identity products handle the login well, but the policy model, the compiler and the SDK were the real work, and the gateway was the smaller part. The design kept the lesson from that first write-up: the edge only decides whether someone may reach an app, and record-level decisions stay inside the app.

Isn't a homemade auth system a security risk?

It would be if it invented its own cryptography. It doesn't. Tokens are standard ES256 JWTs signed and checked with a well-known library, sessions live in Cloudflare's key-value store, and apps can only be reached through tunnels that admit the gateway alone.

The custom parts are the policy and the plumbing, and they were built defensively. Unknown email addresses get a sign-in nobody can complete, with the same storage writes and timing as a real account, so nobody can probe which addresses exist. The gateway overwrites the identity header on every request so an app can't be fed a forged one, and the gateway's tests run inside Cloudflare's own Workers runtime.

Where it stands

Deployed, with four internal apps behind it. Changes go in by pull request, and merging to main deploys the gateway, the API and the admin portal. A later round of work cut the time to request a sign-in email from 6–8 seconds to under 2.