Sign-in and the gate client
@exo/kit/signin#
Exports: createExoSignIn, fromPg, fromPostgres, signinMigrations, qualifySigninSql. A stand issuer for end-to-end tests is under @exo/kit/signin/stand.
This module lets a product offer "Sign in with your exo account". The exo sign-in service is the identity provider (OpenID Connect). The product keeps its own sessions in its own database.
import { createExoSignIn, fromPg } from '@exo/kit/signin';
const signIn = createExoSignIn({
issuer: env.exo.issuer,
clientId: env.exo.clientId,
clientSecret: () => env.exo.clientSecret,
secret: () => env.sessionSecret,
baseUrl: env.siteUrl,
cookieName: 'myapp',
secureCookie: env.secureCookie,
db: fromPg(pool), // or fromPostgres(sql)
extend: async (base) => ({ ...base, plan: await plans.of(base.userId) }), // optional
});
app.register(signIn.fastifyPlugin); // Fastify
export const { GET, POST } = signIn.routeHandlers; // Next: app/api/account/[...all]/route.ts, Node runtime
const who = await signIn.getPrincipal(headers);
const { principal, signIn: state } = await signIn.resolve(headers);
Construction reads nothing over the network and throws on a missing option. A product can start before the sign-in service and works as soon as it answers.
Routes#
Under /api/account: POST /sign-in/social, GET /sign-in/exo, GET /callback/exo, POST /sign-out, POST /exo/logout, GET /list-sessions, POST /revoke-session and POST /revoke-other-sessions. Browser POSTs must come from baseUrl and carry JSON. /api/account/me is a route your product provides.
Sessions#
- The cookie is
__Host-<cookieName>and holds 32 random bytes. The database stores only its hash. - Every 10 minutes a request re-checks the session with the sign-in service. If the account was revoked, all of the person's sessions end. If the service is unreachable, sessions are kept, and after 12 hours without a successful check they answer
unavailable. - The absolute lifetime is
sessionDays, 30 by default.
Users and levels#
Users are linked by users.exo_sub. On the first sign-in the module looks the person up by that id, then by a verified email (linkByEmail, on by default), and otherwise creates a row (createUsers, on by default). The principal carries a level of owner, admin or member. Return null from extend to deny access.
Health#
signIn.checks adds a required signin check and an optional whitegate check:
createHealth({ version, checks: { db, ...signIn.checks }, required: ['db', 'signin'] });
SQL#
The tables the module needs (account_sessions, users.exo_sub) ship as SQL. Vendor them with exo-kit-migrations sync --signin. See Migrations and templates.
@exo/kit/gate#
The gate is the single sign-in in front of admin panels. A service whose port can be reached without the reverse proxy must not trust the X-Exo-* headers, because any container on the network can write them. It asks the gate instead, with the browser's cookie.
import { createGateClient } from '@exo/kit/gate';
const gate = createGateClient({ verifyUrl: 'http://<gate-host>/api/gate/verify' });
const who = await gate.check(request.headers.cookie); // null = refused, also when the gate is down
whois{ user, email, sub, level }.levelisowner,adminormember; anything missing counts asmember.gate.verify(cookie)answers{ ok: true, identity }or{ ok: false, why: 'none' | 'down' }, so you can answer 503 when the gate is down.createGateClient({ verifyUrl, product })passes the product id, so people approved for that product are let in, not only the owner.
The module also exports the gate's server side (createGateFastifyPlugin, decideGate). Products use the client.