AETHERLABS

Client package

Install @aetherlabs.agency/oauth and the callback route, the PKCE flow and the environment entries are scaffolded into your app.

Client package

@aetherlabs.agency/oauth implements the OAuth integration as a package. Installing it writes the callback route, the flow behind it and the environment entries into your app, so you are not hand-rolling PKCE.

npm install @aetherlabs.agency/oauth

It has zero runtime dependencies, and the id_token is verified with node:crypto alone.

It scaffolds on install

The postinstall hook detects your framework and writes the routes. If npm scripts are disabled, or you would rather run it deliberately, use npx aetherlabs-oauth init. Set AETHERLABS_OAUTH_AUTOINIT=0 to switch the automatic pass off entirely.

What it writes

For a Next.js App Router project (either app/ or src/app/):

app/auth/login/route.ts      GET  — starts the flow
app/auth/callback/route.ts   GET  — finishes it (this is your redirect URI)
app/auth/logout/route.ts     POST — clears the session, revokes the token
app/auth/session/route.ts    GET  — the signed-in user as JSON

Each file is a one-liner, so the flow lives in the package:

export { callback as GET } from '@aetherlabs.agency/oauth/next'
export const dynamic = 'force-dynamic'

For an Express project it writes src/aetherlabs-auth.ts, ready to mount:

import { Router } from 'express'
import { createAuthRouter } from '@aetherlabs.agency/oauth/express'

app.use('/auth', createAuthRouter({ Router }))

Scaffolding is idempotent. It never overwrites a file it did not generate unless you pass --force.

Register the client

The redirect URI must exist on the identity service before sign-in works:

npx aetherlabs-oauth register --name "Your Product" \
  --admin-token <an admin access token>

That reads the redirect URI init wrote, creates the OAuth client, and puts the generated client_id and client_secret into your env file. The secret is returned once and cannot be read back.

npx aetherlabs-oauth doctor prints the resolved configuration and what is still missing.

Configuration

VariableMeaning
AETHERLABS_ISSUERIdentity service. Defaults to https://sso.aetherlabs.agency
AETHERLABS_CLIENT_IDFrom register
AETHERLABS_CLIENT_SECRETFrom register. Server-side only
AETHERLABS_REDIRECT_URIMust match the registered redirect URI exactly
AETHERLABS_SESSION_SECRETSigns the session cookie. openssl rand -hex 32
AETHERLABS_POST_LOGIN_REDIRECTWhere to land after sign-in. Defaults to /
AETHERLABS_ADMIN_TOKENOnly for register

Anything can be overridden in code by passing options to the handlers or to resolveConfig(). A whitespace-only value counts as unset, because a copy-pasted variable often carries a stray non-breaking space.

Reading the session

Sign in by linking to /auth/login, optionally with a return path:

<a href="/auth/login?next=/dashboard">Sign in</a>

Only same-origin paths are honoured, so next cannot be used as an open redirect. In a server component:

import { cookies } from 'next/headers'
import { sessionFromCookieHeader } from '@aetherlabs.agency/oauth'

const session = sessionFromCookieHeader((await cookies()).toString())
if (!session) redirect('/auth/login')

Or from the browser, which returns the profile and never the tokens:

const { user } = await fetch('/auth/session').then(r => r.json())

Using the flow directly

Every step is exported as a plain function if you would rather wire it up yourself:

import {
  buildAuthorizeUrl, codeChallengeFor, createCodeVerifier, createState,
  completeLogin, sessionCookie, transactionCookie,
} from '@aetherlabs.agency/oauth'

completeLogin performs the exchange and verifies the id_token — the RS256 signature against the JWKS, then iss, aud, exp and nonce. An unverified id_token is only attacker-supplied JSON, so do not skip it.

Security notes

  • The session cookie is HttpOnly and SameSite=Lax. Strict would withhold it on the redirect back from the identity server, so it is deliberately not used. It is Secure when NODE_ENV=production.
  • The cookie is signed, not encrypted: it is tamper-proof, but a user can read their own claims out of it. Tokens never cross the wire to the browser.
  • PKCE state and the verifier live in a separate short-lived cookie and are compared in constant time.
  • verifyIdToken refuses alg: none and HS256 outright.

A browser-only app cannot use this directly

The token endpoint requires a client secret. Put the four routes on a server — Next.js route handlers count — and let a single-page app ask /auth/session. Shipping the secret to a browser gives it away.

For AI agents

Integrate Aether Labs SSO into this project using the official package.

1. npm install @aetherlabs.agency/oauth  (its postinstall scaffolds the routes;
   if scripts are disabled, run: npx aetherlabs-oauth init).
2. Register the client so the redirect URI exists:
     npx aetherlabs-oauth register --name "<app name>" --admin-token <token>
3. Fill AETHERLABS_CLIENT_ID and AETHERLABS_CLIENT_SECRET in the env file, and
   check with: npx aetherlabs-oauth doctor
4. Link sign-in to /auth/login; read the user from /auth/session.
5. In server components use sessionFromCookieHeader((await cookies()).toString()).
6. Never put the client secret in frontend code or a NEXT_PUBLIC_ variable.
7. Do not hand-roll PKCE — completeLogin() does the exchange and verifies the
   id_token signature, issuer, audience, expiry and nonce.

Next steps

On this page