Simon Axelsson admin
  • https://siax.io
  • Process optimisation, operational improvement and automation. Founder of SIAX Technology AB. Business workflows, data, BI and system integration.

  • Joined on 2026-07-02

@siax/auth-bff (0.2.0)

Published 2026-11-11 16:14:25 +00:00 by admin

Installation

@siax:registry=https://git.siax.app/api/packages/admin/npm/
npm install @siax/[email protected]
"@siax/auth-bff": "0.2.0"

About this package

Canonical provider-neutral BFF authentication primitives for SIAX: PKCE, OIDC authorization/token/logout requests, encrypted session cookies and refresh-token renewal with fail-closed semantics. Zero runtime dependencies, Node >= 20. See standard/SIAX-AUTH-BFF-STANDARD.md and ADR-034.

@siax/auth-bff

Canonical, provider-neutral primitives for a server-side BFF authentication session in the SIAX estate. Extracted from the VehIQ public app and operator console, where the same login/session/refresh contract was implemented twice.

See standard/SIAX-AUTH-BFF-STANDARD.md and ADR-034. The identity owner is CL0UD (identity-and-access, ADR-025/ADR-008).

What this package is

  • createPkcePair() — S256 verifier/challenge (RFC 7636).
  • createStateNonce() — state and nonce for the authorization request.
  • buildAuthorizeUrl(config, …) — the OIDC authorization-code request.
  • exchangeCode(config, …) / refreshTokens(config, …) — token endpoint requests with client_secret_basic and a hard timeout.
  • buildLogoutUrl(config, …) — RP-initiated logout.
  • createSessionCodec(secret) — AES-256-GCM sealed session value with a versioned envelope and HKDF-derived key.
  • ensureFreshSession({ … }) — the one refresh-token renewal contract: renew inside a skew window, preserve a rotated refresh token, never sign out a visitor whose access token is still valid, and return the new sealed cookie for the response.

Zero runtime dependencies. Node >= 20. No Next.js, React, Express, Keycloak or Zitadel import: the caller supplies the endpoints and a fetch implementation.

What this package deliberately is NOT

  • Not a provider adapter. Endpoint URLs come from the caller. Keycloak, Zitadel or another IdP is a configuration, not a dependency.
  • Not a UI or framework middleware. Cookie attributes, route handlers and redirects belong to the app; this package only produces the values and the refresh decision.
  • Not an authorization layer. Roles, scopes and capabilities belong to @siax/auth-core + CL0UD policy. This package never derives a role from a missing claim.
  • Not a JWKS/ID-token verifier. The consumer verifies the ID token with its provider adapter (or a future @siax/auth-oidc verifier) before it seals a session. The package never trusts an unverified token.

Adoption contract

A repo that has a BFF login must:

  1. Use this package for PKCE, the token requests, the session envelope and ensureFreshSession. No app-local AES-GCM/session-crypto copy.
  2. Seal only verified claims: sub and tenant_id come from the verified ID token, never from a request header or a query parameter.
  3. Use an encrypted, HttpOnly, Secure (except localhost), SameSite=Lax cookie; never put an access/refresh token in localStorage, a URL or a log.
  4. Request offline_access, renew within 60 s of expiry, and write the sealed cookie returned by ensureFreshSession on the response.
  5. Use one OIDC client per surface with exact redirect URIs and an explicit API audience; never a wildcard redirect.
  6. Keep logout local-first and then call buildLogoutUrl with the ID token hint.
  7. Run the auth smoke test from the repo's runbook (authorize redirect, callback rejection, logout, API 401, client-secret probe) in CI or release evidence.

Usage

import {
  assertBffConfig, buildAuthorizeUrl, createPkcePair, createSessionCodec,
  createStateNonce, ensureFreshSession, exchangeCode, refreshTokens,
} from "@siax/auth-bff";

const config = assertBffConfig({
  issuer: process.env.OIDC_ISSUER,
  clientId: process.env.OIDC_CLIENT_ID,
  clientSecret: process.env.OIDC_CLIENT_SECRET,
  redirectUri: process.env.OIDC_REDIRECT_URI,
  authorizationEndpoint: process.env.OIDC_AUTHORIZATION_ENDPOINT,
  tokenEndpoint: process.env.OIDC_TOKEN_ENDPOINT,
  endSessionEndpoint: process.env.OIDC_END_SESSION_ENDPOINT,
  sessionSecret: process.env.SESSION_SECRET,
}, { production: process.env.NODE_ENV === "production" });

const { verifier, challenge } = createPkcePair();
const { state, nonce } = createStateNonce();
const url = buildAuthorizeUrl(config, { state, nonce, codeChallenge: challenge });

const codec = createSessionCodec(config.sessionSecret);
const tokens = await exchangeCode(config, { code, codeVerifier: verifier });
const session = {
  access_token: tokens.access_token,
  refresh_token: tokens.refresh_token,
  id_token: tokens.id_token,
  expires_at: Math.floor(Date.now() / 1000) + (tokens.expires_in ?? 300),
  sub: verifiedClaims.sub,
  email: verifiedClaims.email,
  tenant_id: verifiedClaims.tenant_id,
};
const cookie = codec.seal(session);

const fresh = await ensureFreshSession({
  session: codec.open(requestCookie),
  codec,
  refresh: (refreshToken) => refreshTokens(config, refreshToken),
});

The response writer sets fresh.sealed as the session cookie when it is not null, and uses fresh.session.access_token for the server-side call.

Tests

node --test test/*.test.mjs

The suite covers PKCE (including the RFC 7636 vector), the authorization and logout URLs, the token requests, the sealed-session round trip/tamper cases, refresh rotation, fallback lifetime, and the failure semantics before and after expiry.

Details
npm
2026-11-11 16:14:25 +00:00
0
UNLICENSED
latest
6.3 KiB
Assets (1)
Versions (1) View all
0.2.0 2026-11-11