← Back to all projects
🔐

Open source · vishwajit-vm Designed · Phase 2

Strutvane Auth

Identity, RBAC/ABAC compiled to SQL, multi-tenancy & audit

@strutvane/auth handles who you are, what you may do and which tenant you belong to. Authentication plugs in through adapters (Better Auth by default, or OIDC/SAML providers); authorization, tenancy and audit are native. Policies are written once and compile to SQL, so the database only ever returns rows a user is allowed to see. It works inside Strutvane or standalone in Hono, Express, Fastify, Nest and Next.js apps.

policies → SQLPostgres RLS3 spikes · 156 tests

Built for: B2B backend teams that need SSO, roles, tenant isolation and an audit trail.

Packages on npm

↓ Scroll to explore the problem, architecture and stack

😩

The problem

Permission checks scattered through controllers drift out of sync, “filter in memory” leaks data at scale, and multi-tenant apps are one forgotten WHERE clause away from showing one customer another customer’s data.

💡

Why it matters

Writing a rule once and enforcing it in the database — with row-level security as a second wall — makes data leaks structurally hard, and makes every decision explainable with auth:explain.

What you can do with it

01Sign in with email and password, magic links, social login, MFA (TOTP and passkeys), API keys or service accounts.
02Connect enterprise identity (Microsoft Entra ID, Okta, Keycloak) through OIDC or SAML.
03Write allow/deny policies with definePolicy, including field-level rules, and see why a decision was made with auth:explain.
04Isolate tenants with a shared schema and row-level security, a schema per tenant, or a database per tenant.
05Keep an audit log written in the same transaction, with redaction and an optional hash chain.
06Use ready-made auth API routes and screens; SCIM 2.0 provisioning is planned for Phase 4.

How it’s built

A simplified view of the architecture — follow the dots as a request moves down the layers.

Identity
IdentityEngine adaptersBetter Auth · OIDC · SAML
Authorization
Policy enginedefinePolicy → QueryFilter
SQL compilerNULL-safe
Tenancy
Tenant resolverre-checked every request
Postgres RLS5 mandatory rules
Audit
Audit sinksame transaction · redacted

A request, step by step

  1. A request hook turns a session, JWT or API key into an Actor.
  2. The tenant resolver produces a TenantContext, re-checked against membership on every request.
  3. Before the query runs, the policy engine adds a filter describing exactly which rows the actor may see.
  4. That filter compiles to NULL-safe SQL, and row-level security enforces the tenant inside the database.
  5. Fields the actor may not read are stripped from the response; the audit entry is written in the same transaction.

Tech stack

Security
🛡️RBAC🔑OAuth / OIDCKeycloakMicrosoft Entra IDJWT
Data
PostgreSQLRow-level security
Libraries
Better Authjoseargon2idWebAuthn
Runtime
Node.jsTypeScript

What I built

  • Designed the auth package: identity adapters, the policy engine, tenancy resolution and the audit sink.
  • Proved policy-to-SQL compilation in spike 02 (70 tests, 43,930 property-based comparisons, 0 mismatches) and tenant isolation in spike 03 (37 tests, 2,400 concurrent transactions, 0 cross-tenant rows).
  • Proved the hardened auth adapter in spike 07 (49 tests on PostgreSQL 18; 3 planted bugs caught; a second engine passes the same contract suite).
  • Wrote five mandatory row-level-security rules (ADR-0007) after finding that a session-level SET leaked the tenant to another client through PgBouncer 40 times out of 40.
  • Defined a hardened adapter (ADR-0013): shared rate-limit storage, trusted proxies only, sessions revoked on password reset, constant-time resets and a 12-character minimum password.

Hard problems & how I solved them

❓ The same rule in memory and in SQL.

✅ Two-valued, NULL-safe semantics (IS DISTINCT FROM, COALESCE(…, FALSE)) behind one validation gate; a null actor never matches — which closed a privilege-escalation trap the spike found (ADR-0008).

❓ Row-level security behind a connection pool.

✅ RESTRICTIVE policies, fail-closed settings, transaction-local set_config only, a runtime role that can’t bypass RLS, and a helper that destroys broken connections (ADR-0007).

❓ Trusting a third-party auth library.

✅ A mandatory hardened adapter fixes what the spike exposed: sessions surviving a password reset, timing that revealed which emails exist (419 ms vs 12 ms) and X-Forwarded-For spoofing (ADR-0013).