- 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)
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
@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 withclient_secret_basicand 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-oidcverifier) before it seals a session. The package never trusts an unverified token.
Adoption contract
A repo that has a BFF login must:
- Use this package for PKCE, the token requests, the session envelope and
ensureFreshSession. No app-local AES-GCM/session-crypto copy. - Seal only verified claims:
subandtenant_idcome from the verified ID token, never from a request header or a query parameter. - Use an encrypted, HttpOnly, Secure (except localhost), SameSite=Lax cookie;
never put an access/refresh token in
localStorage, a URL or a log. - Request
offline_access, renew within 60 s of expiry, and write the sealed cookie returned byensureFreshSessionon the response. - Use one OIDC client per surface with exact redirect URIs and an explicit API audience; never a wildcard redirect.
- Keep logout local-first and then call
buildLogoutUrlwith the ID token hint. - 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.