// Copyright 2025-2026 The ThunderID Authors // SPDX-License-Identifier: Apache-2.0 import { ThunderIDNodeClient, ThunderIDAuthException, ExtendedAuthorizeRequestUrlParams, FlowSessionStore, ThunderIDNodeManagementClient, ThunderIDRuntimeError, Storage, TokenResponse, User, CookieConfig as NodeCookieConfig, } from '@thunderid/node'; import express from 'express'; import {v4 as uuidv4} from 'uuid'; import CookieConfig from './constants/CookieConfig'; import {ExpressClientConfig} from './models/config'; import hasErrorInURL from './utils/expressUtils'; class ThunderIDExpressClient extends ThunderIDNodeClient { private _expressConfig: ExpressClientConfig | undefined; public constructor() { super(); } public override async initialize(config: T, storage?: Storage): Promise { this._expressConfig = config; return super.initialize(config, storage); } public get expressConfig(): ExpressClientConfig | undefined { return this._expressConfig; } /** * Where an embedded flow's SSO sessions wait between its steps: the client's storage, one entry per flow, so * flows of different users never share one, and instances sharing a store share them too. */ public get flowSessionStore(): FlowSessionStore { return this.getStorageManager(); } /** * Resolves the session cookie name for this client instance, honoring an * explicit `sessionCookie.name` override before falling back to the * `vendor`-derived default (which itself defaults to `'thunderid'`). */ public getSessionCookieName(): string { return NodeCookieConfig.resolveSessionCookieName( this._expressConfig?.vendor, this._expressConfig?.sessionCookie?.name, ); } public async getUserFromRequest(req: express.Request): Promise { const cookies = req.cookies as Record | undefined; const sessionId: string | undefined = cookies?.[this.getSessionCookieName()]; return this.getUser(sessionId); } /** * Returns the management operations bound to the session named by the request's session cookie, * so a route handler can administer users, groups, roles, and agents as the signed-in user. The * session's access token stays on the server and is read on every call. * * ```ts * app.get('/admin/users', protect(), async (req, res) => { * const admin = req.thunderIDAuth.managementFromRequest(req); * res.json(await admin.users.list({limit: 20})); * }); * ``` * * @throws {ThunderIDRuntimeError} `EXPRESS-CLIENT-MFR-NF01` when the request carries no session cookie. */ public managementFromRequest(req: express.Request): ThunderIDNodeManagementClient { const cookies = req.cookies as Record | undefined; const sessionId: string | undefined = cookies?.[this.getSessionCookieName()]; if (!sessionId) { throw new ThunderIDRuntimeError('No session cookie found in the request.', 'EXPRESS-CLIENT-MFR-NF01', 'express'); } return this.management(sessionId); } public async updateUserCredentialsFromRequest(req: express.Request, payload: Record): Promise { const cookies = req.cookies as Record | undefined; const sessionId: string | undefined = cookies?.[this.getSessionCookieName()]; return this.updateUserCredentials(payload, sessionId); } public override async signIn( req: express.Request, res: express.Response, next?: express.NextFunction, signInConfig?: ExtendedAuthorizeRequestUrlParams, ): Promise { if (hasErrorInURL(req.originalUrl)) { return Promise.reject( new ThunderIDAuthException( 'EXPRESS-CLIENT-SI-IV01', 'Invalid login request URL', 'Login request contains an error query parameter in the URL', ), ); } const cookieName = this.getSessionCookieName(); let userId: string = req.cookies?.[cookieName]; if (!userId) { userId = uuidv4(); } const sc = this._expressConfig?.sessionCookie; const authRedirectCallback = (url: string): void => { if (!url) return; res.cookie(cookieName, userId, { httpOnly: sc?.httpOnly ?? CookieConfig.defaultHttpOnly, maxAge: (sc?.expiryTime ?? CookieConfig.defaultExpirySeconds) * 1000, sameSite: (sc?.sameSite ?? CookieConfig.defaultSameSite) as any, secure: sc?.secure ?? CookieConfig.defaultSecure, }); // The redirect is the response, so the request ends here rather than going on to later routes. res.redirect(url); }; // `?ouId=` bounds the sign-in to an organization unit, as a backend's sign-in endpoint does. On the // callback the unit is already recorded with the request, so the parameter is not needed there. const ouId: unknown = req.query['ouId']; const resolvedSignInConfig: ExtendedAuthorizeRequestUrlParams | undefined = typeof ouId === 'string' && ouId !== '' && !req.query['code'] ? {...signInConfig, organizationUnitId: ouId} : signInConfig; const authResponse: TokenResponse = (await super.signIn( authRedirectCallback, userId, req.query['code'] as string | undefined, req.query['state'] as string | undefined, resolvedSignInConfig, )) as unknown as TokenResponse; if (authResponse.accessToken || authResponse.idToken) { return authResponse; } return { accessToken: '', createdAt: 0, expiresIn: '', idToken: '', refreshToken: '', scope: '', tokenType: '', }; } public override async signOut(userId?: string): Promise { return super.signOut(userId); } } export default ThunderIDExpressClient;