@pablo2410/core-server 0.17.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/auth.d.ts CHANGED
@@ -100,6 +100,27 @@ interface DbAdapter<TUser extends BaseUser = BaseUser> {
100
100
  * to the subdomain's local role enum value.
101
101
  */
102
102
  type RoleMapper = (portalRole: string) => string;
103
+ /** report: log who would be refused and let them through. enforce: refuse. */
104
+ type AppAccessMode = "report" | "enforce";
105
+ /** What a refused person is told; each app shows it as a page. */
106
+ declare const APP_NO_ACCESS_MESSAGE = "You don't have access to this app. Ask your administrator.";
107
+ interface AppAccessConfig {
108
+ /** This app's slug in the Portal's app list, e.g. "sqdcp". */
109
+ serviceSlug: string;
110
+ /**
111
+ * "report" (default): log every request that would be refused, let it
112
+ * through. "enforce": refuse with a 403, code APP_NO_ACCESS, and refuse
113
+ * too when the Portal can't be reached. Anything else counts as "report".
114
+ */
115
+ mode?: AppAccessMode;
116
+ }
117
+ interface AppAccessResult {
118
+ allowed: boolean;
119
+ mode: AppAccessMode;
120
+ /** Report mode only: this person would be refused in enforce mode. */
121
+ wouldRefuse?: boolean;
122
+ reason?: "no-grant" | "portal-unavailable";
123
+ }
103
124
  /**
104
125
  * Where the authenticated user is resolved from.
105
126
  * - "db" (default, legacy): a local users table is the working copy; the SDK
@@ -162,6 +183,15 @@ interface SubdomainAuthConfig<TUser extends BaseUser = BaseUser> {
162
183
  * session (fail open), in ms. Default 2s.
163
184
  */
164
185
  sessionCheckTimeoutMs?: number;
186
+ /**
187
+ * Check the person's app grants at the door (ops#691). Off when omitted.
188
+ * authenticateRequest then throws a 403 with `code: "APP_NO_ACCESS"` in
189
+ * enforce mode; the app's context should catch that code (it is not a
190
+ * sign-out) and show a no-access page.
191
+ */
192
+ appAccess?: AppAccessConfig;
193
+ /** How long a person's app list is reused, in ms. Default 30s. 0 = never. */
194
+ appAccessCacheTtlMs?: number;
165
195
  }
166
196
  /** The auth SDK instance returned by createSubdomainAuth. */
167
197
  interface SubdomainAuth<TUser extends BaseUser = BaseUser> {
@@ -174,6 +204,11 @@ interface SubdomainAuth<TUser extends BaseUser = BaseUser> {
174
204
  * Portal throws a 403 with `code: "SESSION_ENDED"`.
175
205
  */
176
206
  authenticateRequest(req: Request): Promise<TUser>;
207
+ /**
208
+ * The app check on its own (for apps that build their own context). Always
209
+ * allowed when `appAccess` isn't configured or in report mode.
210
+ */
211
+ checkAppAccess(user: TUser): Promise<AppAccessResult>;
177
212
  /**
178
213
  * Sign-out (#163): end the Portal session behind this request's cookie, so
179
214
  * the device is signed out of the Portal and every app, not just this one.
@@ -213,5 +248,5 @@ interface SubdomainAuth<TUser extends BaseUser = BaseUser> {
213
248
 
214
249
  declare function createSubdomainAuth<TUser extends BaseUser = BaseUser>(config: SubdomainAuthConfig<TUser>): SubdomainAuth<TUser>;
215
250
 
216
- export { createSubdomainAuth };
217
- export type { AuthEnv, BaseUser, DbAdapter, PortalUser, RoleMapper, SessionPayload, SubdomainAuth, SubdomainAuthConfig, UpsertUserPayload };
251
+ export { APP_NO_ACCESS_MESSAGE, createSubdomainAuth };
252
+ export type { AppAccessConfig, AppAccessMode, AppAccessResult, AuthEnv, BaseUser, DbAdapter, PortalUser, RoleMapper, SessionPayload, SubdomainAuth, SubdomainAuthConfig, UpsertUserPayload };
package/dist/auth.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import {
2
+ APP_NO_ACCESS_MESSAGE,
2
3
  createSubdomainAuth
3
- } from "./chunk-URYEVWI6.js";
4
+ } from "./chunk-4XNPHXO7.js";
4
5
  export {
6
+ APP_NO_ACCESS_MESSAGE,
5
7
  createSubdomainAuth
6
8
  };
7
9
  //# sourceMappingURL=auth.js.map
@@ -1,3 +1,10 @@
1
+ // src/auth/types.ts
2
+ var APP_ACCESS_ADMIN_ROLES = /* @__PURE__ */ new Set([
3
+ "enterprise_admin",
4
+ "platform_admin"
5
+ ]);
6
+ var APP_NO_ACCESS_MESSAGE = "You don't have access to this app. Ask your administrator.";
7
+
1
8
  // src/sandbox.ts
2
9
  var PRODUCTION_PORTAL_ORIGIN = "https://portal.oplyticsdigital.net";
3
10
  var PRODUCTION_MARKETING_ORIGIN = "https://oplytics.digital";
@@ -52,7 +59,9 @@ function createSubdomainAuth(config) {
52
59
  portalUserCacheTtlMs = 3e4,
53
60
  sessionCheck = true,
54
61
  sessionCheckCacheTtlMs = 3e4,
55
- sessionCheckTimeoutMs = 2e3
62
+ sessionCheckTimeoutMs = 2e3,
63
+ appAccess,
64
+ appAccessCacheTtlMs = 3e4
56
65
  } = config;
57
66
  const tag = `[${serviceName} Auth]`;
58
67
  const sandbox = isSandboxEnv();
@@ -400,7 +409,83 @@ function createSubdomainAuth(config) {
400
409
  return null;
401
410
  }
402
411
  }
412
+ const appAccessMode = appAccess?.mode === "enforce" ? "enforce" : "report";
413
+ if (appAccess?.mode && appAccess.mode !== appAccessMode) {
414
+ console.warn(
415
+ `${tag} Unknown appAccess mode "${String(appAccess.mode)}" \u2014 using report`
416
+ );
417
+ }
418
+ const appAccessCache = /* @__PURE__ */ new Map();
419
+ async function getEffectiveApps(openId) {
420
+ const hit = appAccessCache.get(openId);
421
+ if (appAccessCacheTtlMs > 0 && hit && Date.now() - hit.at < appAccessCacheTtlMs) {
422
+ return hit.slugs;
423
+ }
424
+ try {
425
+ const client = await getPortalClientAsync();
426
+ if (!client) return null;
427
+ const { data } = await client.get(
428
+ `/api/service/users/by-openid/${encodeURIComponent(openId)}/services`,
429
+ {
430
+ timeout: sessionCheckTimeoutMs,
431
+ signal: AbortSignal.timeout(sessionCheckTimeoutMs)
432
+ }
433
+ );
434
+ if (!Array.isArray(data?.services)) return null;
435
+ const slugs = data.services.filter(
436
+ (s) => typeof s === "string"
437
+ );
438
+ if (appAccessCacheTtlMs > 0) {
439
+ appAccessCache.delete(openId);
440
+ appAccessCache.set(openId, { slugs, at: Date.now() });
441
+ if (appAccessCache.size > PORTAL_USER_CACHE_MAX) {
442
+ const oldest = appAccessCache.keys().next().value;
443
+ if (oldest !== void 0) appAccessCache.delete(oldest);
444
+ }
445
+ }
446
+ return slugs;
447
+ } catch (err) {
448
+ const status = err?.response?.status;
449
+ console.warn(
450
+ `${tag} Portal app list failed for a user (${status ?? err.message})`
451
+ );
452
+ return null;
453
+ }
454
+ }
455
+ async function checkAppAccess(user) {
456
+ if (!appAccess) return { allowed: true, mode: appAccessMode };
457
+ const slug = appAccess.serviceSlug;
458
+ const mode = appAccessMode;
459
+ const who = `user=${user.portalUserId ?? user.id} company=${user.enterpriseId ?? "none"}`;
460
+ if (APP_ACCESS_ADMIN_ROLES.has(user.role)) {
461
+ return { allowed: true, mode };
462
+ }
463
+ const slugs = await getEffectiveApps(user.openId);
464
+ if (slugs?.includes(slug)) return { allowed: true, mode };
465
+ const reason = slugs ? "no-grant" : "portal-unavailable";
466
+ if (mode === "report") {
467
+ console.warn(
468
+ `${tag} APP_ACCESS_REPORT would refuse app=${slug} ${who} reason=${reason}`
469
+ );
470
+ return { allowed: true, mode, wouldRefuse: true, reason };
471
+ }
472
+ console.warn(
473
+ `${tag} APP_ACCESS_ENFORCE refused app=${slug} ${who} reason=${reason}`
474
+ );
475
+ return { allowed: false, mode, reason };
476
+ }
403
477
  async function authenticateRequest(req) {
478
+ const user = await authenticateUser(req);
479
+ const access = await checkAppAccess(user);
480
+ if (!access.allowed) {
481
+ throw Object.assign(new Error(APP_NO_ACCESS_MESSAGE), {
482
+ statusCode: 403,
483
+ code: "APP_NO_ACCESS"
484
+ });
485
+ }
486
+ return user;
487
+ }
488
+ async function authenticateUser(req) {
404
489
  const cookie = await getCookie();
405
490
  const cookies = req.headers.cookie ? new Map(Object.entries(cookie.parseCookie(req.headers.cookie))) : /* @__PURE__ */ new Map();
406
491
  const sessionCookie = cookies.get(cookieName);
@@ -498,6 +583,7 @@ function createSubdomainAuth(config) {
498
583
  return {
499
584
  verifySession,
500
585
  authenticateRequest,
586
+ checkAppAccess,
501
587
  endPortalSession,
502
588
  getPortalUser,
503
589
  getSessionCookieOptions,
@@ -511,6 +597,7 @@ export {
511
597
  isSandboxEnv,
512
598
  sandboxPortalOrigin,
513
599
  resolveMarketingOrigin,
600
+ APP_NO_ACCESS_MESSAGE,
514
601
  createSubdomainAuth
515
602
  };
516
- //# sourceMappingURL=chunk-URYEVWI6.js.map
603
+ //# sourceMappingURL=chunk-4XNPHXO7.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/auth/types.ts","../src/sandbox.ts","../src/auth/createSubdomainAuth.ts"],"sourcesContent":["/**\n * Types for the createSubdomainAuth factory.\n *\n * These types define the contract between the shared auth SDK and each\n * subdomain's local database, role mapping, and portal enrichment logic.\n */\n\nimport type { Request, CookieOptions } from \"express\";\n\n// ─── Session & JWT ──────────────────────────────────────────────────\n\n/** Payload stored inside the session JWT. */\nexport interface SessionPayload {\n openId: string;\n appId: string;\n name: string;\n}\n\n// ─── Portal User (from portal API enrichment) ──────────────────────\n\n/**\n * Minimal portal user shape returned by the portal API\n * (GET /users/by-openid/:openId). The portal exposes the enterprise as\n * `enterpriseId`; the legacy `companyId` alias is kept optional only for\n * backwards-compatibility with older portal builds.\n */\nexport interface PortalUser {\n id: number;\n openId: string;\n name: string | null;\n email: string | null;\n role: string;\n enterpriseId?: number | null;\n /** @deprecated legacy alias for enterpriseId — older portal builds only */\n companyId?: number;\n siteId?: number;\n /** Profile picture as a data URL, or null when the user has none (portal#518). */\n avatarUrl?: string | null;\n /**\n * True for a read-only account (the live check's login, ops#246): the Portal\n * says so in this payload and every app refuses its writes. Absent or false\n * for everyone else.\n */\n readOnly?: boolean;\n}\n\n// ─── Auth Environment ──────────────────────────────────────────────\n\n/** Environment variables required by the auth SDK. */\nexport interface AuthEnv {\n /** Shared portal JWT secret for cross-subdomain SSO (PORTAL_JWT_SECRET) */\n portalJwtSecret: string;\n /** Cookie domain for cross-subdomain sharing, e.g. \".oplyticsdigital.net\" */\n cookieDomain: string;\n /** Owner's openId for auto-admin assignment */\n ownerOpenId: string;\n /** Portal API base URL for user enrichment */\n portalApiUrl: string;\n /** Portal API key sent as X-Service-Key header */\n portalApiKey: string;\n}\n\n// ─── DB Adapter (subdomain provides) ───────────────────────────────\n\n/**\n * Minimal user shape that the auth SDK works with.\n * Subdomains extend this with their own fields.\n */\nexport interface BaseUser {\n id: number;\n openId: string;\n name: string | null;\n email: string | null;\n role: string;\n enterpriseId: number | null;\n portalUserId: number | null;\n /** Set (true) only for a read-only account; see PortalUser.readOnly. */\n readOnly?: boolean;\n}\n\n/**\n * Minimal upsert payload for creating/updating users.\n * Subdomains can extend with additional fields.\n */\nexport interface UpsertUserPayload {\n openId: string;\n name?: string | null;\n email?: string | null;\n loginMethod?: string | null;\n role?: string;\n lastSignedIn?: Date;\n enterpriseId?: number;\n portalUserId?: number;\n}\n\n/**\n * Database adapter that each subdomain must provide.\n * This decouples the auth SDK from any specific ORM or schema.\n */\nexport interface DbAdapter<TUser extends BaseUser = BaseUser> {\n /** Find a user by their openId. Returns null/undefined if not found. */\n getUserByOpenId(openId: string): Promise<TUser | null | undefined>;\n\n /** Create or update a user record. */\n upsertUser(payload: UpsertUserPayload): Promise<void>;\n\n /**\n * Update specific fields on an existing user by their local DB id.\n * Used for enterprise backfill after portal enrichment.\n */\n updateUserFields(\n userId: number,\n fields: Partial<Pick<BaseUser, \"enterpriseId\" | \"portalUserId\">>\n ): Promise<void>;\n}\n\n// ─── Role Mapper (subdomain provides) ──────────────────────────────\n\n/**\n * Maps a portal role string (e.g. \"platform_admin\", \"enterprise_admin\")\n * to the subdomain's local role enum value.\n */\nexport type RoleMapper = (portalRole: string) => string;\n\n// ─── App access (ops#691) ──────────────────────────────────────────\n\n/** report: log who would be refused and let them through. enforce: refuse. */\nexport type AppAccessMode = \"report\" | \"enforce\";\n\n/** Roles that pass the app check without a grant (the fleet admin rule). */\nexport const APP_ACCESS_ADMIN_ROLES: ReadonlySet<string> = new Set([\n \"enterprise_admin\",\n \"platform_admin\",\n]);\n\n/** What a refused person is told; each app shows it as a page. */\nexport const APP_NO_ACCESS_MESSAGE =\n \"You don't have access to this app. Ask your administrator.\";\n\nexport interface AppAccessConfig {\n /** This app's slug in the Portal's app list, e.g. \"sqdcp\". */\n serviceSlug: string;\n /**\n * \"report\" (default): log every request that would be refused, let it\n * through. \"enforce\": refuse with a 403, code APP_NO_ACCESS, and refuse\n * too when the Portal can't be reached. Anything else counts as \"report\".\n */\n mode?: AppAccessMode;\n}\n\nexport interface AppAccessResult {\n allowed: boolean;\n mode: AppAccessMode;\n /** Report mode only: this person would be refused in enforce mode. */\n wouldRefuse?: boolean;\n reason?: \"no-grant\" | \"portal-unavailable\";\n}\n\n// ─── Factory Config ────────────────────────────────────────────────\n\n/**\n * Where the authenticated user is resolved from.\n * - \"db\" (default, legacy): a local users table is the working copy; the SDK\n * upserts/enriches it from the Portal. Keeps a per-subdomain mirror.\n * - \"portal\": the Portal is the single source of truth. ctx.user is built\n * fresh from the Portal user record on each request (cached); nothing is\n * persisted locally and no DbAdapter is required. ctx.user.id is the PORTAL\n * user id. This is the target architecture — no per-subdomain user mirror.\n */\nexport type UserSource = \"db\" | \"portal\";\n\n/** Configuration object passed to createSubdomainAuth. */\nexport interface SubdomainAuthConfig<TUser extends BaseUser = BaseUser> {\n /** Environment variables for auth. */\n env: AuthEnv;\n\n /**\n * Where to resolve the user from. Defaults to \"db\" for backward compat.\n * Set \"portal\" to drop the local users mirror (single source of truth).\n */\n userSource?: UserSource;\n\n /**\n * Database adapter for user CRUD. Required when userSource is \"db\";\n * ignored (and may be omitted) when userSource is \"portal\".\n */\n db?: DbAdapter<TUser>;\n\n /**\n * Maps portal roles to local roles. Only used in \"db\" mode. In \"portal\"\n * mode roles are taken from the Portal verbatim (no mapping).\n */\n mapRole?: RoleMapper;\n\n /** Cookie name (defaults to \"app_session_id\"). */\n cookieName?: string;\n\n /**\n * @deprecated Has no effect. The Portal issues every session cookie (for\n * one year); this factory never signs one.\n */\n sessionExpiryMs?: number;\n\n /** Service name for logging (e.g. \"SQDCP\", \"OEE Manager\"). */\n serviceName?: string;\n\n /**\n * Portal-user cache TTL in ms (portal mode only). Avoids a Portal round-trip\n * on every request. Default 30s. Set 0 to disable caching.\n */\n portalUserCacheTtlMs?: number;\n\n /**\n * Ask the Portal whether the session behind the cookie is still live\n * (POST /api/service/sessions/check), so a device signed out in the Portal\n * is refused here too (core-server#77). Default true. Only an explicit\n * `{ active: false }` from the Portal ends a session: if the Portal is down,\n * slow or answers with an error, the session is kept (fail open).\n */\n sessionCheck?: boolean;\n\n /**\n * How long a session check answer is reused, in ms. Default 30s, which is\n * how long a signed-out device can keep working here. Set 0 to check on\n * every request.\n */\n sessionCheckCacheTtlMs?: number;\n\n /**\n * How long to wait for the Portal's session check before keeping the\n * session (fail open), in ms. Default 2s.\n */\n sessionCheckTimeoutMs?: number;\n\n /**\n * Check the person's app grants at the door (ops#691). Off when omitted.\n * authenticateRequest then throws a 403 with `code: \"APP_NO_ACCESS\"` in\n * enforce mode; the app's context should catch that code (it is not a\n * sign-out) and show a no-access page.\n */\n appAccess?: AppAccessConfig;\n\n /** How long a person's app list is reused, in ms. Default 30s. 0 = never. */\n appAccessCacheTtlMs?: number;\n}\n\n// ─── Return Type ───────────────────────────────────────────────────\n\n/** The auth SDK instance returned by createSubdomainAuth. */\nexport interface SubdomainAuth<TUser extends BaseUser = BaseUser> {\n /** Verify a session cookie value. Returns payload or null. */\n verifySession(\n cookieValue: string | undefined | null\n ): Promise<SessionPayload | null>;\n\n /**\n * Full authentication flow: verify cookie → check the session is still\n * live in the Portal → find/create user → enrich from portal → backfill\n * enterprise. Returns the local user record. A session signed out in the\n * Portal throws a 403 with `code: \"SESSION_ENDED\"`.\n */\n authenticateRequest(req: Request): Promise<TUser>;\n\n /**\n * The app check on its own (for apps that build their own context). Always\n * allowed when `appAccess` isn't configured or in report mode.\n */\n checkAppAccess(user: TUser): Promise<AppAccessResult>;\n\n /**\n * Sign-out (#163): end the Portal session behind this request's cookie, so\n * the device is signed out of the Portal and every app, not just this one.\n * Call it from `auth.logout` before clearing the cookie. Never throws, and\n * waits at most `sessionCheckTimeoutMs`. This app refuses the cookie from\n * then on. Resolves to whether the Portal ended a live session (false when\n * there was none to end), or null when the Portal gave no answer.\n */\n endPortalSession(req: Request): Promise<boolean | null>;\n\n /** Fetch a user from the portal API by openId. */\n getPortalUser(openId: string): Promise<PortalUser | null>;\n\n /** Get cookie options for setting session cookies. */\n getSessionCookieOptions(\n req: Request\n ): Pick<\n CookieOptions,\n \"domain\" | \"httpOnly\" | \"path\" | \"sameSite\" | \"secure\"\n >;\n\n /**\n * Validate a returnTo URL to prevent open-redirect attacks: a relative path,\n * or https on oplyticsdigital.net / *.oplyticsdigital.net. In the local\n * sandbox (INFISICAL_ENV=staging) also http://localhost or 127.0.0.1 on\n * ports 3000-3009.\n */\n isValidReturnUrl(url: string): boolean;\n}\n","/**\n * Production and local-sandbox addresses, and how to tell the two apart\n * (#86, part of oplytics-portal#261: no production links inside the sandbox).\n *\n * Same rule as Portal (oplytics-portal#373) and Policy Deployment (PD#329):\n * the sandbox is INFISICAL_ENV=staging; production is INFISICAL_ENV=prod.\n * NODE_ENV is \"production\" in both, so it can't tell them apart. Production\n * addresses are the defaults and never change.\n *\n * The sandbox ports mirror the sandbox compose file (portal 3000, apps\n * 3001-3006, marketing-site 3007) and shared-ui's SANDBOX_LOCALHOST_PORTS.\n */\n\nexport const PRODUCTION_PORTAL_ORIGIN = \"https://portal.oplyticsdigital.net\";\nexport const PRODUCTION_MARKETING_ORIGIN = \"https://oplytics.digital\";\nexport const SANDBOX_PORTAL_ORIGIN = \"http://localhost:3000\";\nexport const SANDBOX_MARKETING_ORIGIN = \"http://localhost:3007\";\n\nconst env = (name: string): string | undefined =>\n typeof process === \"undefined\" ? undefined : process.env[name];\n\n/** True when this process is the local sandbox (INFISICAL_ENV=staging). */\nexport function isSandboxEnv(\n infisicalEnv: string | undefined = env(\"INFISICAL_ENV\")\n): boolean {\n return infisicalEnv === \"staging\";\n}\n\n/**\n * A sandbox app's own address: plain http on localhost or 127.0.0.1, ports\n * 3000-3009 (the rule Portal's cors.ts and returnUrl.ts use).\n */\nexport function isLocalSandboxUrl(url: URL): boolean {\n return (\n url.protocol === \"http:\" &&\n (url.hostname === \"localhost\" || url.hostname === \"127.0.0.1\") &&\n /^300\\d$/.test(url.port)\n );\n}\n\n/** The origin of a configured URL, or null if it is unset or not a URL. */\nfunction originOf(value: string | undefined): string | null {\n const trimmed = value?.trim();\n if (!trimmed) return null;\n try {\n return new URL(trimmed).origin;\n } catch {\n return null;\n }\n}\n\n/**\n * The Portal origin the browser talks to in the sandbox: the app's own\n * PORTAL_LOGIN_URL (set by the tunnel variant, e.g.\n * https://sandbox-portal.oplyticsdigital.net), else the local sandbox Portal.\n */\nexport function sandboxPortalOrigin(\n portalLoginUrl: string | undefined = env(\"PORTAL_LOGIN_URL\")\n): string {\n return originOf(portalLoginUrl) ?? SANDBOX_PORTAL_ORIGIN;\n}\n\n/**\n * The marketing site's origin, for links Opi cites. As Portal's\n * resolveMarketingOrigin: an explicit MARKETING_ORIGIN wins; otherwise the\n * sandbox's marketing port when running as the sandbox, else production.\n */\nexport function resolveMarketingOrigin(\n configured: string | undefined = env(\"MARKETING_ORIGIN\"),\n infisicalEnv: string | undefined = env(\"INFISICAL_ENV\")\n): string {\n return (\n originOf(configured) ??\n (isSandboxEnv(infisicalEnv)\n ? SANDBOX_MARKETING_ORIGIN\n : PRODUCTION_MARKETING_ORIGIN)\n );\n}\n","/**\n * createSubdomainAuth — shared SSO auth factory for Oplytics.digital subdomains.\n *\n * Encapsulates:\n * - JWT session verification (PORTAL_JWT_SECRET first, fallback to JWT_SECRET)\n * - Cookie domain handling via COOKIE_DOMAIN env var\n * - Portal API user enrichment with X-Service-Key header\n * - Auto-create user from JWT payload if not in local DB\n * - Enterprise backfill from portal API\n * - Portal session check: a device signed out in the Portal is refused here\n * too, cached for 30s, failing open when the Portal can't answer (#77)\n *\n * Based on SQDCP's battle-tested sdk.ts + cookies.ts.\n */\n\nimport type { Request, CookieOptions } from \"express\";\nimport {\n APP_ACCESS_ADMIN_ROLES,\n APP_NO_ACCESS_MESSAGE,\n type AppAccessMode,\n type AppAccessResult,\n} from \"./types\";\nimport type {\n BaseUser,\n PortalUser,\n SessionPayload,\n SubdomainAuth,\n SubdomainAuthConfig,\n} from \"./types\";\nimport { isLocalSandboxUrl, isSandboxEnv } from \"../sandbox\";\n\n// ─── Constants ──────────────────────────────────────────────────────\n\nconst DEFAULT_COOKIE_NAME = \"app_session_id\";\n/** The Portal issues every session cookie for one year (portal#237). */\nconst PORTAL_SESSION_SECONDS = 31_536_000;\n/** Most session check answers kept at once; the oldest is dropped first. */\nconst SESSION_CHECK_CACHE_MAX = 10_000;\n/** Most Portal users cached at once (each can hold a picture); the oldest is dropped first. */\nconst PORTAL_USER_CACHE_MAX = 1_000;\n\n// ─── Helpers ────────────────────────────────────────────────────────\n\nconst isNonEmptyString = (value: unknown): value is string =>\n typeof value === \"string\" && value.length > 0;\n\n/**\n * The cookie's issue time in seconds, as the Portal's session check needs it.\n * Cookies from before portal#237 have no iat; every Portal session cookie is\n * issued for one year, so exp minus a year is when it was issued. Never\n * guessed as 0: the Portal would read that as issued before any sign-out\n * cutoff and end a live session.\n */\nfunction issuedAtSeconds(iat: unknown, exp: unknown): number | null {\n const seconds =\n typeof iat === \"number\"\n ? iat\n : typeof exp === \"number\"\n ? exp - PORTAL_SESSION_SECONDS\n : null;\n if (seconds === null || !Number.isFinite(seconds) || seconds <= 0) {\n return null;\n }\n return Math.floor(seconds);\n}\n\n/** A verified cookie: the public payload, plus what the session check needs. */\ninterface VerifiedCookie {\n session: SessionPayload;\n /** The Portal session id. Absent on cookies issued before portal#237. */\n sid?: string;\n /** Issue time in seconds; null when the cookie has neither iat nor exp. */\n iat: number | null;\n}\n\n// ─── Factory ────────────────────────────────────────────────────────\n\nexport function createSubdomainAuth<TUser extends BaseUser = BaseUser>(\n config: SubdomainAuthConfig<TUser>\n): SubdomainAuth<TUser> {\n const {\n env,\n db,\n // Default to identity (no role translation). Subdomains should adopt the\n // Portal's canonical roles verbatim; a custom mapper is legacy.\n mapRole = (role: string) => role,\n userSource = \"db\",\n cookieName = DEFAULT_COOKIE_NAME,\n serviceName = \"Subdomain\",\n portalUserCacheTtlMs = 30_000,\n sessionCheck = true,\n sessionCheckCacheTtlMs = 30_000,\n sessionCheckTimeoutMs = 2_000,\n appAccess,\n appAccessCacheTtlMs = 30_000,\n } = config;\n\n const tag = `[${serviceName} Auth]`;\n // The local sandbox (INFISICAL_ENV=staging) also accepts its own\n // http://localhost:300x addresses as a returnTo (#86).\n const sandbox = isSandboxEnv();\n\n if (userSource === \"db\" && !db) {\n throw new Error(\n `${tag} userSource \"db\" requires a db adapter in createSubdomainAuth config`\n );\n }\n\n // Short-TTL cache of Portal users (portal mode), keyed by openId, so we don't\n // hit the Portal on every request.\n const portalUserCache = new Map<string, { user: PortalUser; at: number }>();\n\n // ── Lazy-loaded dependencies (jose, cookie, axios) ──────────────\n // We use dynamic imports so consumers don't need to install these\n // as peer deps if they only use other core-server features.\n\n let _jose: typeof import(\"jose\") | null = null;\n async function getJose() {\n if (!_jose) _jose = await import(\"jose\");\n return _jose;\n }\n\n let _cookie: typeof import(\"cookie\") | null = null;\n async function getCookie() {\n if (!_cookie) _cookie = await import(\"cookie\");\n return _cookie;\n }\n\n let _axios: typeof import(\"axios\").default | null = null;\n async function getAxios() {\n if (!_axios) {\n const mod = await import(\"axios\");\n _axios = mod.default;\n }\n return _axios;\n }\n\n // ── Portal API client ──────────────────────────────────────────\n\n let portalClient: import(\"axios\").AxiosInstance | null = null;\n\n async function getPortalClientAsync() {\n if (portalClient) return portalClient;\n if (!env.portalApiUrl) return null;\n const axios = await getAxios();\n portalClient = axios.create({\n baseURL: env.portalApiUrl,\n timeout: 10_000,\n headers: {\n \"Content-Type\": \"application/json\",\n ...(env.portalApiKey ? { \"X-Service-Key\": env.portalApiKey } : {}),\n },\n });\n return portalClient;\n }\n\n // ── JWT Secret ─────────────────────────────────────────────────\n\n function getPortalSecret() {\n const secret = env.portalJwtSecret;\n if (!secret) throw new Error(\"PORTAL_JWT_SECRET is not configured\");\n return new TextEncoder().encode(secret);\n }\n\n // ── Cookie Helpers ─────────────────────────────────────────────\n\n function getCookieDomain(): string | undefined {\n return env.cookieDomain || undefined;\n }\n\n function isSecureRequest(req: Request): boolean {\n if (req.protocol === \"https\") return true;\n const forwardedProto = req.headers[\"x-forwarded-proto\"];\n if (!forwardedProto) return false;\n const protoList = Array.isArray(forwardedProto)\n ? forwardedProto\n : forwardedProto.split(\",\");\n return protoList.some(p => p.trim().toLowerCase() === \"https\");\n }\n\n function getSessionCookieOptions(\n req: Request\n ): Pick<\n CookieOptions,\n \"domain\" | \"httpOnly\" | \"path\" | \"sameSite\" | \"secure\"\n > {\n const secure = isSecureRequest(req);\n const domain = getCookieDomain();\n return {\n domain,\n httpOnly: true,\n path: \"/\",\n sameSite: \"lax\",\n secure,\n };\n }\n\n function isValidReturnUrl(url: string): boolean {\n if (url.startsWith(\"/\") && !url.startsWith(\"//\")) return true;\n try {\n const parsed = new URL(url);\n if (sandbox && isLocalSandboxUrl(parsed)) return true;\n if (parsed.protocol !== \"https:\") return false;\n return (\n parsed.hostname === \"oplyticsdigital.net\" ||\n parsed.hostname.endsWith(\".oplyticsdigital.net\")\n );\n } catch {\n return false;\n }\n }\n\n // ── Session Verification ───────────────────────────────────────\n\n /**\n * Verify session cookie against PORTAL_JWT_SECRET (cross-subdomain SSO).\n *\n * Portal is the only issuer of session cookies for subdomains using this\n * factory — the old Manus OAuth login path (which signed its own cookies\n * with a local JWT_SECRET) was removed. A cookie that fails portal\n * verification is invalid, full stop.\n */\n async function verifyCookie(\n cookieValue: string | undefined | null\n ): Promise<VerifiedCookie | null> {\n if (!cookieValue) {\n console.warn(`${tag} Missing session cookie`);\n return null;\n }\n\n const jose = await getJose();\n\n try {\n const portalKey = getPortalSecret();\n const { payload } = await jose.jwtVerify(cookieValue, portalKey, {\n algorithms: [\"HS256\"],\n });\n const { openId, appId, name, email, sid, iat, exp } = payload as Record<\n string,\n unknown\n >;\n // Identity is openId; the display name is cosmetic. The Portal signs\n // `name: user.name || \"\"`, so a user with no display name has an empty\n // one, and must not be locked out for it (#82).\n if (!isNonEmptyString(openId) || !isNonEmptyString(appId)) {\n console.warn(`${tag} Portal JWT payload missing required fields`);\n return null;\n }\n return {\n session: {\n openId,\n appId,\n name: isNonEmptyString(name)\n ? name\n : isNonEmptyString(email)\n ? email\n : \"\",\n },\n ...(isNonEmptyString(sid) ? { sid } : {}),\n iat: issuedAtSeconds(iat, exp),\n };\n } catch (error) {\n console.warn(\n `${tag} Session verification failed:`,\n (error as Error).message\n );\n return null;\n }\n }\n\n async function verifySession(\n cookieValue: string | undefined | null\n ): Promise<SessionPayload | null> {\n return (await verifyCookie(cookieValue))?.session ?? null;\n }\n\n // ── Portal API ─────────────────────────────────────────────────\n\n // ops#246: who the Portal last said is read-only. Used only when the Portal\n // cannot be reached, so a Portal blip never lifts read-only from a login we\n // already know (the live check). Someone never seen stays fail-open.\n const knownReadOnly = new Set<string>();\n\n async function getPortalUser(openId: string): Promise<PortalUser | null> {\n try {\n const client = await getPortalClientAsync();\n if (!client) return null;\n // The Portal Service API is under /api/service (base URL is the origin,\n // e.g. https://portal.oplyticsdigital.net). Must include the /api/service\n // prefix — omitting it 404s and silently strands enterprise resolution.\n // Deliberately names no company (no X-Enterprise-Id): this is the\n // sign-in lookup that finds the user's company. The Portal must keep\n // allowing it with the master key (#91, subdomains#1037).\n const { data } = await client.get<PortalUser>(\n `/api/service/users/by-openid/${encodeURIComponent(openId)}`\n );\n if (data) {\n if (data.readOnly === true) knownReadOnly.add(openId);\n else knownReadOnly.delete(openId);\n }\n return data ?? null;\n } catch (err: unknown) {\n const status = (err as any)?.response?.status;\n if (status === 401) {\n console.error(\n `${tag} Portal API key revoked (401) — enrichment disabled`\n );\n } else if (status === 404) {\n // User not found in portal — normal for first-time users\n } else {\n console.warn(\n `${tag} Portal user lookup failed:`,\n (err as Error).message\n );\n }\n return null;\n }\n }\n\n /** Portal user lookup with a short-TTL cache (portal mode). */\n async function getPortalUserCached(\n openId: string\n ): Promise<PortalUser | null> {\n if (portalUserCacheTtlMs > 0) {\n const hit = portalUserCache.get(openId);\n if (hit && Date.now() - hit.at < portalUserCacheTtlMs) {\n return hit.user;\n }\n }\n const user = await getPortalUser(openId);\n if (user && portalUserCacheTtlMs > 0) {\n // Re-insert so Map order stays oldest-first for eviction.\n portalUserCache.delete(openId);\n portalUserCache.set(openId, { user, at: Date.now() });\n if (portalUserCache.size > PORTAL_USER_CACHE_MAX) {\n const oldest = portalUserCache.keys().next().value;\n if (oldest !== undefined) portalUserCache.delete(oldest);\n }\n }\n return user;\n }\n\n /**\n * Build ctx.user entirely from the Portal record — no local persistence.\n * The single source of truth. ctx.user.id is the PORTAL user id. Roles and\n * enterprise come from the Portal verbatim (no mapping).\n *\n * On a genuine Portal outage (the by-openid lookup returns null) we keep the\n * user authenticated using their verified session claims, with role/enterprise\n * unresolved, rather than throwing. Throwing here would make createContext (in\n * every subdomain) swallow the error into `user = null`, which the AuthGate\n * renders as the Sign In screen — i.e. a transient Portal blip would log the\n * user out. Their session cookie is already valid; keep them in, degraded,\n * until the Portal recovers. (Enterprise-scoped procedures will 403 during the\n * outage; client array-guards keep pages from crashing.)\n */\n async function resolvePortalUser(\n openId: string,\n session: SessionPayload\n ): Promise<TUser> {\n const portalUser = await getPortalUserCached(openId);\n\n if (!portalUser) {\n console.warn(\n `${tag} Portal user unavailable for ${openId} — keeping session (role/enterprise unresolved until Portal recovers)`\n );\n return {\n id: 0,\n openId,\n name: session.name || null,\n email: null,\n role: \"\",\n enterpriseId: null,\n portalUserId: null,\n // A known read-only login stays read-only through a Portal outage.\n ...(knownReadOnly.has(openId) ? { readOnly: true } : {}),\n } as unknown as TUser;\n }\n\n const enterpriseId =\n portalUser.enterpriseId ?? portalUser.companyId ?? null;\n return {\n id: portalUser.id,\n openId: portalUser.openId ?? openId,\n name: portalUser.name || session.name || null,\n email: portalUser.email ?? null,\n role: portalUser.role,\n enterpriseId,\n portalUserId: portalUser.id,\n avatarUrl: portalUser.avatarUrl ?? null,\n // Only set for a read-only account (ops#246); the apps refuse its writes.\n ...(portalUser.readOnly === true ? { readOnly: true } : {}),\n } as unknown as TUser;\n }\n\n // ── Portal Session Check ───────────────────────────────────────\n //\n // Fails open: only an explicit `{ active: false }` from the Portal ends a\n // session. The check runs on every request to every app, so failing closed\n // would turn a Portal outage, a slow deploy or a key problem into everyone\n // being signed out of everything at once (and nobody could sign back in\n // while the Portal is down). The cost: while the Portal can't answer, a\n // device signed out after its last answer keeps working here until the\n // Portal is back. A session the Portal has already called ended stays\n // ended through an outage.\n\n // Answers keyed by openId + sid + iat, because the Portal judges all three:\n // an old copy of a cookie keeps its sid but its earlier iat is cut off.\n const sessionCheckCache = new Map<string, { active: boolean; at: number }>();\n const sessionCheckInflight = new Map<string, Promise<boolean>>();\n const warned = new Set<string>();\n\n function warnOnce(key: string, message: string) {\n if (warned.has(key)) return;\n warned.add(key);\n console.warn(message);\n }\n\n function rememberSessionCheck(key: string, active: boolean) {\n // Re-insert so Map order stays oldest-first for eviction.\n sessionCheckCache.delete(key);\n sessionCheckCache.set(key, { active, at: Date.now() });\n if (sessionCheckCache.size > SESSION_CHECK_CACHE_MAX) {\n const oldest = sessionCheckCache.keys().next().value;\n if (oldest !== undefined) sessionCheckCache.delete(oldest);\n }\n }\n\n /**\n * One call to POST /api/service/sessions/check. Returns the Portal's\n * answer, or null when it gave none (unreachable, slow, error, odd reply).\n * Like the by-openid lookup it names no company: it runs before the user's\n * company is known, and answers only about the cookie (#91).\n */\n async function askPortalSessionActive(body: {\n openId: string;\n sid?: string;\n iat: number;\n }): Promise<boolean | null> {\n try {\n const client = await getPortalClientAsync();\n if (!client) return null;\n const { data } = await client.post<{ active?: unknown }>(\n \"/api/service/sessions/check\",\n body,\n {\n // axios's timeout only covers an idle socket; the signal caps the\n // whole call, so a Portal that answers slowly can't stall requests.\n timeout: sessionCheckTimeoutMs,\n signal: AbortSignal.timeout(sessionCheckTimeoutMs),\n }\n );\n if (typeof data?.active === \"boolean\") return data.active;\n console.warn(\n `${tag} Portal session check gave no answer for ${body.openId} — keeping session`\n );\n return null;\n } catch (err: unknown) {\n const status = (err as any)?.response?.status;\n if (status === 404) {\n warnOnce(\n \"check-404\",\n `${tag} Portal has no session check yet (404) — sessions signed out in the Portal are not refused until it ships`\n );\n } else if (status === 401) {\n console.error(\n `${tag} Portal API key refused (401) on session check — keeping session`\n );\n } else {\n console.warn(\n `${tag} Portal session check failed for ${body.openId} (${status ?? (err as Error).message}) — keeping session`\n );\n }\n return null;\n }\n }\n\n /** Is the session behind this cookie still live in the Portal? */\n async function isSessionActive(cookie: VerifiedCookie): Promise<boolean> {\n if (!sessionCheck) return true;\n if (!env.portalApiUrl) {\n warnOnce(\n \"no-portal-url\",\n `${tag} No Portal API URL — sessions signed out in the Portal are not refused`\n );\n return true;\n }\n const { openId } = cookie.session;\n if (cookie.iat === null) {\n warnOnce(\n \"no-iat\",\n `${tag} Session cookie for ${openId} has neither iat nor exp — not checked with the Portal`\n );\n return true;\n }\n\n const key = JSON.stringify([openId, cookie.sid ?? null, cookie.iat]);\n const hit = sessionCheckCache.get(key);\n if (hit && Date.now() - hit.at < sessionCheckCacheTtlMs) return hit.active;\n\n // Parallel requests from one page load share a single Portal call.\n let pending = sessionCheckInflight.get(key);\n if (!pending) {\n pending = askPortalSessionActive({\n openId,\n ...(cookie.sid ? { sid: cookie.sid } : {}),\n iat: cookie.iat,\n })\n .then(answer => {\n // No answer: keep an earlier \"ended\", otherwise let it through.\n // Remembered either way, so a down Portal isn't asked every request.\n const active = answer ?? hit?.active ?? true;\n rememberSessionCheck(key, active);\n return active;\n })\n .finally(() => sessionCheckInflight.delete(key));\n sessionCheckInflight.set(key, pending);\n }\n return pending;\n }\n\n // ── Sign-out ───────────────────────────────────────────────────\n\n /** The first non-empty value of a header, cut to the Portal's limit. */\n function headerValue(req: Request, name: string, max: number) {\n const raw = req.headers[name];\n const value = (Array.isArray(raw) ? raw[0] : raw)?.trim();\n return value ? value.slice(0, max) : undefined;\n }\n\n /**\n * Ends the Portal session behind this request's cookie (#163), through the\n * Portal's own endSession: POST /api/service/sessions/end with the cookie\n * itself, which the Portal verifies. Signing out of this app then signs the\n * device out of the Portal and every other app, not just this one. Call it\n * from auth.logout before clearing the cookie.\n *\n * Best-effort: it never throws, and gives up after sessionCheckTimeoutMs,\n * so a slow or missing Portal never holds up a sign-out. Whatever the\n * Portal answers, this app refuses the cookie from now on. Returns whether\n * the Portal ended a live session, or null when it gave no answer.\n */\n async function endPortalSession(req: Request): Promise<boolean | null> {\n let openId: string | undefined;\n try {\n const cookie = await getCookie();\n const value = req.headers.cookie\n ? cookie.parseCookie(req.headers.cookie)[cookieName]\n : undefined;\n if (!value) return false;\n const verified = await verifyCookie(value);\n // A cookie from before portal#237 has no session id: nothing to end.\n if (!verified?.sid) return false;\n openId = verified.session.openId;\n if (verified.iat !== null) {\n rememberSessionCheck(\n JSON.stringify([openId, verified.sid, verified.iat]),\n false\n );\n }\n if (!env.portalApiUrl) {\n warnOnce(\n \"no-portal-url-end\",\n `${tag} No Portal API URL — signing out here leaves the Portal session live`\n );\n return null;\n }\n const ip =\n headerValue(req, \"cf-connecting-ip\", 45) ??\n (req.ip ? String(req.ip).slice(0, 45) : undefined);\n const userAgent = headerValue(req, \"user-agent\", 255);\n const client = await getPortalClientAsync();\n if (!client) return null;\n const { data } = await client.post<{ ended?: unknown }>(\n \"/api/service/sessions/end\",\n {\n cookie: value,\n app: serviceName.slice(0, 64),\n ...(ip ? { ip } : {}),\n ...(userAgent ? { userAgent } : {}),\n },\n {\n timeout: sessionCheckTimeoutMs,\n signal: AbortSignal.timeout(sessionCheckTimeoutMs),\n }\n );\n if (typeof data?.ended === \"boolean\") return data.ended;\n console.warn(\n `${tag} Portal gave no answer to ending the session for ${openId}`\n );\n return null;\n } catch (err: unknown) {\n const status = (err as { response?: { status?: number } } | null)\n ?.response?.status;\n if (status === 404) {\n warnOnce(\n \"end-404\",\n `${tag} Portal can't end sessions yet (404) — signing out here leaves the Portal session live until it ships`\n );\n } else {\n console.warn(\n `${tag} Portal could not end the session for ${openId ?? \"a user\"} (${status ?? (err as Error).message}) — signed out here only`\n );\n }\n return null;\n }\n }\n\n // ── App Access (ops#691) ───────────────────────────────────────\n //\n // Does this person's effective app list (Portal: role's apps plus their own\n // grants) include this app? Platform and enterprise admins always pass.\n // report mode (default) logs who would be refused and lets them through;\n // enforce mode refuses, and fails closed when the Portal can't answer.\n\n const appAccessMode: AppAccessMode =\n appAccess?.mode === \"enforce\" ? \"enforce\" : \"report\";\n if (appAccess?.mode && appAccess.mode !== appAccessMode) {\n console.warn(\n `${tag} Unknown appAccess mode \"${String(appAccess.mode)}\" — using report`\n );\n }\n const appAccessCache = new Map<string, { slugs: string[]; at: number }>();\n\n /** The person's effective app slugs from the Portal, or null if it gave none. */\n async function getEffectiveApps(openId: string): Promise<string[] | null> {\n const hit = appAccessCache.get(openId);\n if (\n appAccessCacheTtlMs > 0 &&\n hit &&\n Date.now() - hit.at < appAccessCacheTtlMs\n ) {\n return hit.slugs;\n }\n try {\n const client = await getPortalClientAsync();\n if (!client) return null;\n const { data } = await client.get<{ services?: unknown }>(\n `/api/service/users/by-openid/${encodeURIComponent(openId)}/services`,\n {\n timeout: sessionCheckTimeoutMs,\n signal: AbortSignal.timeout(sessionCheckTimeoutMs),\n }\n );\n if (!Array.isArray(data?.services)) return null;\n const slugs = data.services.filter(\n (s): s is string => typeof s === \"string\"\n );\n if (appAccessCacheTtlMs > 0) {\n appAccessCache.delete(openId);\n appAccessCache.set(openId, { slugs, at: Date.now() });\n if (appAccessCache.size > PORTAL_USER_CACHE_MAX) {\n const oldest = appAccessCache.keys().next().value;\n if (oldest !== undefined) appAccessCache.delete(oldest);\n }\n }\n return slugs;\n } catch (err: unknown) {\n const status = (err as { response?: { status?: number } } | null)\n ?.response?.status;\n console.warn(\n `${tag} Portal app list failed for a user (${status ?? (err as Error).message})`\n );\n return null;\n }\n }\n\n async function checkAppAccess(user: TUser): Promise<AppAccessResult> {\n if (!appAccess) return { allowed: true, mode: appAccessMode };\n const slug = appAccess.serviceSlug;\n const mode = appAccessMode;\n const who = `user=${user.portalUserId ?? user.id} company=${user.enterpriseId ?? \"none\"}`;\n if (APP_ACCESS_ADMIN_ROLES.has(user.role)) {\n return { allowed: true, mode };\n }\n const slugs = await getEffectiveApps(user.openId);\n if (slugs?.includes(slug)) return { allowed: true, mode };\n\n const reason = slugs ? \"no-grant\" : \"portal-unavailable\";\n if (mode === \"report\") {\n console.warn(\n `${tag} APP_ACCESS_REPORT would refuse app=${slug} ${who} reason=${reason}`\n );\n return { allowed: true, mode, wouldRefuse: true, reason };\n }\n console.warn(\n `${tag} APP_ACCESS_ENFORCE refused app=${slug} ${who} reason=${reason}`\n );\n return { allowed: false, mode, reason };\n }\n\n // ── Authenticate Request ───────────────────────────────────────\n\n async function authenticateRequest(req: Request): Promise<TUser> {\n const user = await authenticateUser(req);\n const access = await checkAppAccess(user);\n if (!access.allowed) {\n throw Object.assign(new Error(APP_NO_ACCESS_MESSAGE), {\n statusCode: 403,\n code: \"APP_NO_ACCESS\",\n });\n }\n return user;\n }\n\n async function authenticateUser(req: Request): Promise<TUser> {\n const cookie = await getCookie();\n const cookies = req.headers.cookie\n ? new Map(Object.entries(cookie.parseCookie(req.headers.cookie)))\n : new Map<string, string | undefined>();\n\n const sessionCookie = cookies.get(cookieName);\n const verified = await verifyCookie(sessionCookie);\n\n if (!verified) {\n throw Object.assign(new Error(\"Invalid session cookie\"), {\n statusCode: 403,\n });\n }\n\n // A device signed out in the Portal is refused here too (#77).\n if (!(await isSessionActive(verified))) {\n throw Object.assign(new Error(\"Session ended\"), {\n statusCode: 403,\n code: \"SESSION_ENDED\",\n });\n }\n\n const { session } = verified;\n\n const sessionUserId = session.openId;\n const signedInAt = new Date();\n\n // ── Portal mode: single source of truth, no local mirror ────\n if (userSource === \"portal\") {\n return await resolvePortalUser(sessionUserId, session);\n }\n\n let user = (await db!.getUserByOpenId(sessionUserId)) ?? null;\n\n // ── Auto-create user from verified JWT ──────────────────────\n if (!user) {\n try {\n // Try portal API first for richer user info\n const portalUser = await getPortalUser(sessionUserId);\n\n if (portalUser) {\n console.log(\n `${tag} Cross-subdomain SSO: creating user from portal (${portalUser.email || sessionUserId})`\n );\n // Portal returns `enterpriseId`; `companyId` is a legacy alias.\n const portalEnterpriseId =\n portalUser.enterpriseId ?? portalUser.companyId ?? null;\n await db!.upsertUser({\n openId: sessionUserId,\n name: portalUser.name || session.name || null,\n email: portalUser.email ?? null,\n loginMethod: \"portal-sso\",\n role: mapRole(portalUser.role),\n lastSignedIn: signedInAt,\n ...(portalEnterpriseId != null\n ? {\n enterpriseId: portalEnterpriseId,\n portalUserId: portalUser.id,\n }\n : {}),\n });\n user = (await db!.getUserByOpenId(sessionUserId)) ?? null;\n } else {\n // Portal unavailable — create from JWT payload\n console.log(\n `${tag} Cross-subdomain SSO: creating user from JWT payload (${sessionUserId})`\n );\n await db!.upsertUser({\n openId: sessionUserId,\n name: session.name || null,\n loginMethod: \"portal-sso\",\n lastSignedIn: signedInAt,\n });\n user = (await db!.getUserByOpenId(sessionUserId)) ?? null;\n }\n } catch (error) {\n console.error(`${tag} Failed to sync user info:`, error);\n throw Object.assign(new Error(\"Failed to sync user info\"), {\n statusCode: 403,\n });\n }\n }\n\n if (!user) {\n throw Object.assign(new Error(\"User not found\"), { statusCode: 403 });\n }\n\n // ── Enterprise backfill ─────────────────────────────────────\n if (user.enterpriseId === null) {\n try {\n const portalUser = await getPortalUser(user.openId);\n // Portal returns `enterpriseId`; `companyId` is a legacy alias.\n const portalEnterpriseId =\n portalUser?.enterpriseId ?? portalUser?.companyId ?? null;\n if (portalEnterpriseId != null) {\n console.log(\n `${tag} Backfilling enterpriseId=${portalEnterpriseId} for user ${user.id}`\n );\n await db!.updateUserFields(user.id, {\n enterpriseId: portalEnterpriseId,\n ...(portalUser?.id ? { portalUserId: portalUser.id } : {}),\n });\n user = (await db!.getUserByOpenId(user.openId)) ?? user;\n }\n } catch (backfillError) {\n console.warn(\n `${tag} Failed to backfill enterpriseId for user ${user.id}:`,\n backfillError\n );\n // Non-fatal — user can still proceed\n }\n }\n\n // Touch lastSignedIn\n await db!.upsertUser({\n openId: user.openId,\n lastSignedIn: signedInAt,\n });\n\n // A read-only account (ops#246) is marked on the user the apps see. The\n // Portal answer is cached like portal mode's; an unreachable Portal leaves\n // the user as it is (the same fail-open as the session check).\n const portalSays = await getPortalUserCached(user.openId);\n const readOnly = portalSays\n ? portalSays.readOnly === true\n : knownReadOnly.has(user.openId);\n return readOnly ? { ...user, readOnly: true } : user;\n }\n\n // ── Return public API ──────────────────────────────────────────\n\n return {\n verifySession,\n authenticateRequest,\n checkAppAccess,\n endPortalSession,\n getPortalUser,\n getSessionCookieOptions,\n isValidReturnUrl,\n };\n}\n"],"mappings":";AAkIO,IAAM,yBAA8C,oBAAI,IAAI;AAAA,EACjE;AAAA,EACA;AACF,CAAC;AAGM,IAAM,wBACX;;;AC5HK,IAAM,2BAA2B;AACjC,IAAM,8BAA8B;AACpC,IAAM,wBAAwB;AAC9B,IAAM,2BAA2B;AAExC,IAAM,MAAM,CAAC,SACX,OAAO,YAAY,cAAc,SAAY,QAAQ,IAAI,IAAI;AAGxD,SAAS,aACd,eAAmC,IAAI,eAAe,GAC7C;AACT,SAAO,iBAAiB;AAC1B;AAMO,SAAS,kBAAkB,KAAmB;AACnD,SACE,IAAI,aAAa,YAChB,IAAI,aAAa,eAAe,IAAI,aAAa,gBAClD,UAAU,KAAK,IAAI,IAAI;AAE3B;AAGA,SAAS,SAAS,OAA0C;AAC1D,QAAM,UAAU,OAAO,KAAK;AAC5B,MAAI,CAAC,QAAS,QAAO;AACrB,MAAI;AACF,WAAO,IAAI,IAAI,OAAO,EAAE;AAAA,EAC1B,QAAQ;AACN,WAAO;AAAA,EACT;AACF;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sandboxPortalOrigin
8
- } from "./chunk-URYEVWI6.js";
8
+ } from "./chunk-4XNPHXO7.js";
9
9
  import {
10
10
  HierarchyRefusedError,
11
11
  createHierarchyService
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pablo2410/core-server",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "description": "Shared server utilities for Oplytics.digital subdomains",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/sandbox.ts","../src/auth/createSubdomainAuth.ts"],"sourcesContent":["/**\n * Production and local-sandbox addresses, and how to tell the two apart\n * (#86, part of oplytics-portal#261: no production links inside the sandbox).\n *\n * Same rule as Portal (oplytics-portal#373) and Policy Deployment (PD#329):\n * the sandbox is INFISICAL_ENV=staging; production is INFISICAL_ENV=prod.\n * NODE_ENV is \"production\" in both, so it can't tell them apart. Production\n * addresses are the defaults and never change.\n *\n * The sandbox ports mirror the sandbox compose file (portal 3000, apps\n * 3001-3006, marketing-site 3007) and shared-ui's SANDBOX_LOCALHOST_PORTS.\n */\n\nexport const PRODUCTION_PORTAL_ORIGIN = \"https://portal.oplyticsdigital.net\";\nexport const PRODUCTION_MARKETING_ORIGIN = \"https://oplytics.digital\";\nexport const SANDBOX_PORTAL_ORIGIN = \"http://localhost:3000\";\nexport const SANDBOX_MARKETING_ORIGIN = \"http://localhost:3007\";\n\nconst env = (name: string): string | undefined =>\n typeof process === \"undefined\" ? undefined : process.env[name];\n\n/** True when this process is the local sandbox (INFISICAL_ENV=staging). */\nexport function isSandboxEnv(\n infisicalEnv: string | undefined = env(\"INFISICAL_ENV\")\n): boolean {\n return infisicalEnv === \"staging\";\n}\n\n/**\n * A sandbox app's own address: plain http on localhost or 127.0.0.1, ports\n * 3000-3009 (the rule Portal's cors.ts and returnUrl.ts use).\n */\nexport function isLocalSandboxUrl(url: URL): boolean {\n return (\n url.protocol === \"http:\" &&\n (url.hostname === \"localhost\" || url.hostname === \"127.0.0.1\") &&\n /^300\\d$/.test(url.port)\n );\n}\n\n/** The origin of a configured URL, or null if it is unset or not a URL. */\nfunction originOf(value: string | undefined): string | null {\n const trimmed = value?.trim();\n if (!trimmed) return null;\n try {\n return new URL(trimmed).origin;\n } catch {\n return null;\n }\n}\n\n/**\n * The Portal origin the browser talks to in the sandbox: the app's own\n * PORTAL_LOGIN_URL (set by the tunnel variant, e.g.\n * https://sandbox-portal.oplyticsdigital.net), else the local sandbox Portal.\n */\nexport function sandboxPortalOrigin(\n portalLoginUrl: string | undefined = env(\"PORTAL_LOGIN_URL\")\n): string {\n return originOf(portalLoginUrl) ?? SANDBOX_PORTAL_ORIGIN;\n}\n\n/**\n * The marketing site's origin, for links Opi cites. As Portal's\n * resolveMarketingOrigin: an explicit MARKETING_ORIGIN wins; otherwise the\n * sandbox's marketing port when running as the sandbox, else production.\n */\nexport function resolveMarketingOrigin(\n configured: string | undefined = env(\"MARKETING_ORIGIN\"),\n infisicalEnv: string | undefined = env(\"INFISICAL_ENV\")\n): string {\n return (\n originOf(configured) ??\n (isSandboxEnv(infisicalEnv)\n ? SANDBOX_MARKETING_ORIGIN\n : PRODUCTION_MARKETING_ORIGIN)\n );\n}\n","/**\n * createSubdomainAuth — shared SSO auth factory for Oplytics.digital subdomains.\n *\n * Encapsulates:\n * - JWT session verification (PORTAL_JWT_SECRET first, fallback to JWT_SECRET)\n * - Cookie domain handling via COOKIE_DOMAIN env var\n * - Portal API user enrichment with X-Service-Key header\n * - Auto-create user from JWT payload if not in local DB\n * - Enterprise backfill from portal API\n * - Portal session check: a device signed out in the Portal is refused here\n * too, cached for 30s, failing open when the Portal can't answer (#77)\n *\n * Based on SQDCP's battle-tested sdk.ts + cookies.ts.\n */\n\nimport type { Request, CookieOptions } from \"express\";\nimport type {\n BaseUser,\n PortalUser,\n SessionPayload,\n SubdomainAuth,\n SubdomainAuthConfig,\n} from \"./types\";\nimport { isLocalSandboxUrl, isSandboxEnv } from \"../sandbox\";\n\n// ─── Constants ──────────────────────────────────────────────────────\n\nconst DEFAULT_COOKIE_NAME = \"app_session_id\";\n/** The Portal issues every session cookie for one year (portal#237). */\nconst PORTAL_SESSION_SECONDS = 31_536_000;\n/** Most session check answers kept at once; the oldest is dropped first. */\nconst SESSION_CHECK_CACHE_MAX = 10_000;\n/** Most Portal users cached at once (each can hold a picture); the oldest is dropped first. */\nconst PORTAL_USER_CACHE_MAX = 1_000;\n\n// ─── Helpers ────────────────────────────────────────────────────────\n\nconst isNonEmptyString = (value: unknown): value is string =>\n typeof value === \"string\" && value.length > 0;\n\n/**\n * The cookie's issue time in seconds, as the Portal's session check needs it.\n * Cookies from before portal#237 have no iat; every Portal session cookie is\n * issued for one year, so exp minus a year is when it was issued. Never\n * guessed as 0: the Portal would read that as issued before any sign-out\n * cutoff and end a live session.\n */\nfunction issuedAtSeconds(iat: unknown, exp: unknown): number | null {\n const seconds =\n typeof iat === \"number\"\n ? iat\n : typeof exp === \"number\"\n ? exp - PORTAL_SESSION_SECONDS\n : null;\n if (seconds === null || !Number.isFinite(seconds) || seconds <= 0) {\n return null;\n }\n return Math.floor(seconds);\n}\n\n/** A verified cookie: the public payload, plus what the session check needs. */\ninterface VerifiedCookie {\n session: SessionPayload;\n /** The Portal session id. Absent on cookies issued before portal#237. */\n sid?: string;\n /** Issue time in seconds; null when the cookie has neither iat nor exp. */\n iat: number | null;\n}\n\n// ─── Factory ────────────────────────────────────────────────────────\n\nexport function createSubdomainAuth<TUser extends BaseUser = BaseUser>(\n config: SubdomainAuthConfig<TUser>\n): SubdomainAuth<TUser> {\n const {\n env,\n db,\n // Default to identity (no role translation). Subdomains should adopt the\n // Portal's canonical roles verbatim; a custom mapper is legacy.\n mapRole = (role: string) => role,\n userSource = \"db\",\n cookieName = DEFAULT_COOKIE_NAME,\n serviceName = \"Subdomain\",\n portalUserCacheTtlMs = 30_000,\n sessionCheck = true,\n sessionCheckCacheTtlMs = 30_000,\n sessionCheckTimeoutMs = 2_000,\n } = config;\n\n const tag = `[${serviceName} Auth]`;\n // The local sandbox (INFISICAL_ENV=staging) also accepts its own\n // http://localhost:300x addresses as a returnTo (#86).\n const sandbox = isSandboxEnv();\n\n if (userSource === \"db\" && !db) {\n throw new Error(\n `${tag} userSource \"db\" requires a db adapter in createSubdomainAuth config`\n );\n }\n\n // Short-TTL cache of Portal users (portal mode), keyed by openId, so we don't\n // hit the Portal on every request.\n const portalUserCache = new Map<string, { user: PortalUser; at: number }>();\n\n // ── Lazy-loaded dependencies (jose, cookie, axios) ──────────────\n // We use dynamic imports so consumers don't need to install these\n // as peer deps if they only use other core-server features.\n\n let _jose: typeof import(\"jose\") | null = null;\n async function getJose() {\n if (!_jose) _jose = await import(\"jose\");\n return _jose;\n }\n\n let _cookie: typeof import(\"cookie\") | null = null;\n async function getCookie() {\n if (!_cookie) _cookie = await import(\"cookie\");\n return _cookie;\n }\n\n let _axios: typeof import(\"axios\").default | null = null;\n async function getAxios() {\n if (!_axios) {\n const mod = await import(\"axios\");\n _axios = mod.default;\n }\n return _axios;\n }\n\n // ── Portal API client ──────────────────────────────────────────\n\n let portalClient: import(\"axios\").AxiosInstance | null = null;\n\n async function getPortalClientAsync() {\n if (portalClient) return portalClient;\n if (!env.portalApiUrl) return null;\n const axios = await getAxios();\n portalClient = axios.create({\n baseURL: env.portalApiUrl,\n timeout: 10_000,\n headers: {\n \"Content-Type\": \"application/json\",\n ...(env.portalApiKey ? { \"X-Service-Key\": env.portalApiKey } : {}),\n },\n });\n return portalClient;\n }\n\n // ── JWT Secret ─────────────────────────────────────────────────\n\n function getPortalSecret() {\n const secret = env.portalJwtSecret;\n if (!secret) throw new Error(\"PORTAL_JWT_SECRET is not configured\");\n return new TextEncoder().encode(secret);\n }\n\n // ── Cookie Helpers ─────────────────────────────────────────────\n\n function getCookieDomain(): string | undefined {\n return env.cookieDomain || undefined;\n }\n\n function isSecureRequest(req: Request): boolean {\n if (req.protocol === \"https\") return true;\n const forwardedProto = req.headers[\"x-forwarded-proto\"];\n if (!forwardedProto) return false;\n const protoList = Array.isArray(forwardedProto)\n ? forwardedProto\n : forwardedProto.split(\",\");\n return protoList.some(p => p.trim().toLowerCase() === \"https\");\n }\n\n function getSessionCookieOptions(\n req: Request\n ): Pick<\n CookieOptions,\n \"domain\" | \"httpOnly\" | \"path\" | \"sameSite\" | \"secure\"\n > {\n const secure = isSecureRequest(req);\n const domain = getCookieDomain();\n return {\n domain,\n httpOnly: true,\n path: \"/\",\n sameSite: \"lax\",\n secure,\n };\n }\n\n function isValidReturnUrl(url: string): boolean {\n if (url.startsWith(\"/\") && !url.startsWith(\"//\")) return true;\n try {\n const parsed = new URL(url);\n if (sandbox && isLocalSandboxUrl(parsed)) return true;\n if (parsed.protocol !== \"https:\") return false;\n return (\n parsed.hostname === \"oplyticsdigital.net\" ||\n parsed.hostname.endsWith(\".oplyticsdigital.net\")\n );\n } catch {\n return false;\n }\n }\n\n // ── Session Verification ───────────────────────────────────────\n\n /**\n * Verify session cookie against PORTAL_JWT_SECRET (cross-subdomain SSO).\n *\n * Portal is the only issuer of session cookies for subdomains using this\n * factory — the old Manus OAuth login path (which signed its own cookies\n * with a local JWT_SECRET) was removed. A cookie that fails portal\n * verification is invalid, full stop.\n */\n async function verifyCookie(\n cookieValue: string | undefined | null\n ): Promise<VerifiedCookie | null> {\n if (!cookieValue) {\n console.warn(`${tag} Missing session cookie`);\n return null;\n }\n\n const jose = await getJose();\n\n try {\n const portalKey = getPortalSecret();\n const { payload } = await jose.jwtVerify(cookieValue, portalKey, {\n algorithms: [\"HS256\"],\n });\n const { openId, appId, name, email, sid, iat, exp } = payload as Record<\n string,\n unknown\n >;\n // Identity is openId; the display name is cosmetic. The Portal signs\n // `name: user.name || \"\"`, so a user with no display name has an empty\n // one, and must not be locked out for it (#82).\n if (!isNonEmptyString(openId) || !isNonEmptyString(appId)) {\n console.warn(`${tag} Portal JWT payload missing required fields`);\n return null;\n }\n return {\n session: {\n openId,\n appId,\n name: isNonEmptyString(name)\n ? name\n : isNonEmptyString(email)\n ? email\n : \"\",\n },\n ...(isNonEmptyString(sid) ? { sid } : {}),\n iat: issuedAtSeconds(iat, exp),\n };\n } catch (error) {\n console.warn(\n `${tag} Session verification failed:`,\n (error as Error).message\n );\n return null;\n }\n }\n\n async function verifySession(\n cookieValue: string | undefined | null\n ): Promise<SessionPayload | null> {\n return (await verifyCookie(cookieValue))?.session ?? null;\n }\n\n // ── Portal API ─────────────────────────────────────────────────\n\n // ops#246: who the Portal last said is read-only. Used only when the Portal\n // cannot be reached, so a Portal blip never lifts read-only from a login we\n // already know (the live check). Someone never seen stays fail-open.\n const knownReadOnly = new Set<string>();\n\n async function getPortalUser(openId: string): Promise<PortalUser | null> {\n try {\n const client = await getPortalClientAsync();\n if (!client) return null;\n // The Portal Service API is under /api/service (base URL is the origin,\n // e.g. https://portal.oplyticsdigital.net). Must include the /api/service\n // prefix — omitting it 404s and silently strands enterprise resolution.\n // Deliberately names no company (no X-Enterprise-Id): this is the\n // sign-in lookup that finds the user's company. The Portal must keep\n // allowing it with the master key (#91, subdomains#1037).\n const { data } = await client.get<PortalUser>(\n `/api/service/users/by-openid/${encodeURIComponent(openId)}`\n );\n if (data) {\n if (data.readOnly === true) knownReadOnly.add(openId);\n else knownReadOnly.delete(openId);\n }\n return data ?? null;\n } catch (err: unknown) {\n const status = (err as any)?.response?.status;\n if (status === 401) {\n console.error(\n `${tag} Portal API key revoked (401) — enrichment disabled`\n );\n } else if (status === 404) {\n // User not found in portal — normal for first-time users\n } else {\n console.warn(\n `${tag} Portal user lookup failed:`,\n (err as Error).message\n );\n }\n return null;\n }\n }\n\n /** Portal user lookup with a short-TTL cache (portal mode). */\n async function getPortalUserCached(\n openId: string\n ): Promise<PortalUser | null> {\n if (portalUserCacheTtlMs > 0) {\n const hit = portalUserCache.get(openId);\n if (hit && Date.now() - hit.at < portalUserCacheTtlMs) {\n return hit.user;\n }\n }\n const user = await getPortalUser(openId);\n if (user && portalUserCacheTtlMs > 0) {\n // Re-insert so Map order stays oldest-first for eviction.\n portalUserCache.delete(openId);\n portalUserCache.set(openId, { user, at: Date.now() });\n if (portalUserCache.size > PORTAL_USER_CACHE_MAX) {\n const oldest = portalUserCache.keys().next().value;\n if (oldest !== undefined) portalUserCache.delete(oldest);\n }\n }\n return user;\n }\n\n /**\n * Build ctx.user entirely from the Portal record — no local persistence.\n * The single source of truth. ctx.user.id is the PORTAL user id. Roles and\n * enterprise come from the Portal verbatim (no mapping).\n *\n * On a genuine Portal outage (the by-openid lookup returns null) we keep the\n * user authenticated using their verified session claims, with role/enterprise\n * unresolved, rather than throwing. Throwing here would make createContext (in\n * every subdomain) swallow the error into `user = null`, which the AuthGate\n * renders as the Sign In screen — i.e. a transient Portal blip would log the\n * user out. Their session cookie is already valid; keep them in, degraded,\n * until the Portal recovers. (Enterprise-scoped procedures will 403 during the\n * outage; client array-guards keep pages from crashing.)\n */\n async function resolvePortalUser(\n openId: string,\n session: SessionPayload\n ): Promise<TUser> {\n const portalUser = await getPortalUserCached(openId);\n\n if (!portalUser) {\n console.warn(\n `${tag} Portal user unavailable for ${openId} — keeping session (role/enterprise unresolved until Portal recovers)`\n );\n return {\n id: 0,\n openId,\n name: session.name || null,\n email: null,\n role: \"\",\n enterpriseId: null,\n portalUserId: null,\n // A known read-only login stays read-only through a Portal outage.\n ...(knownReadOnly.has(openId) ? { readOnly: true } : {}),\n } as unknown as TUser;\n }\n\n const enterpriseId =\n portalUser.enterpriseId ?? portalUser.companyId ?? null;\n return {\n id: portalUser.id,\n openId: portalUser.openId ?? openId,\n name: portalUser.name || session.name || null,\n email: portalUser.email ?? null,\n role: portalUser.role,\n enterpriseId,\n portalUserId: portalUser.id,\n avatarUrl: portalUser.avatarUrl ?? null,\n // Only set for a read-only account (ops#246); the apps refuse its writes.\n ...(portalUser.readOnly === true ? { readOnly: true } : {}),\n } as unknown as TUser;\n }\n\n // ── Portal Session Check ───────────────────────────────────────\n //\n // Fails open: only an explicit `{ active: false }` from the Portal ends a\n // session. The check runs on every request to every app, so failing closed\n // would turn a Portal outage, a slow deploy or a key problem into everyone\n // being signed out of everything at once (and nobody could sign back in\n // while the Portal is down). The cost: while the Portal can't answer, a\n // device signed out after its last answer keeps working here until the\n // Portal is back. A session the Portal has already called ended stays\n // ended through an outage.\n\n // Answers keyed by openId + sid + iat, because the Portal judges all three:\n // an old copy of a cookie keeps its sid but its earlier iat is cut off.\n const sessionCheckCache = new Map<string, { active: boolean; at: number }>();\n const sessionCheckInflight = new Map<string, Promise<boolean>>();\n const warned = new Set<string>();\n\n function warnOnce(key: string, message: string) {\n if (warned.has(key)) return;\n warned.add(key);\n console.warn(message);\n }\n\n function rememberSessionCheck(key: string, active: boolean) {\n // Re-insert so Map order stays oldest-first for eviction.\n sessionCheckCache.delete(key);\n sessionCheckCache.set(key, { active, at: Date.now() });\n if (sessionCheckCache.size > SESSION_CHECK_CACHE_MAX) {\n const oldest = sessionCheckCache.keys().next().value;\n if (oldest !== undefined) sessionCheckCache.delete(oldest);\n }\n }\n\n /**\n * One call to POST /api/service/sessions/check. Returns the Portal's\n * answer, or null when it gave none (unreachable, slow, error, odd reply).\n * Like the by-openid lookup it names no company: it runs before the user's\n * company is known, and answers only about the cookie (#91).\n */\n async function askPortalSessionActive(body: {\n openId: string;\n sid?: string;\n iat: number;\n }): Promise<boolean | null> {\n try {\n const client = await getPortalClientAsync();\n if (!client) return null;\n const { data } = await client.post<{ active?: unknown }>(\n \"/api/service/sessions/check\",\n body,\n {\n // axios's timeout only covers an idle socket; the signal caps the\n // whole call, so a Portal that answers slowly can't stall requests.\n timeout: sessionCheckTimeoutMs,\n signal: AbortSignal.timeout(sessionCheckTimeoutMs),\n }\n );\n if (typeof data?.active === \"boolean\") return data.active;\n console.warn(\n `${tag} Portal session check gave no answer for ${body.openId} — keeping session`\n );\n return null;\n } catch (err: unknown) {\n const status = (err as any)?.response?.status;\n if (status === 404) {\n warnOnce(\n \"check-404\",\n `${tag} Portal has no session check yet (404) — sessions signed out in the Portal are not refused until it ships`\n );\n } else if (status === 401) {\n console.error(\n `${tag} Portal API key refused (401) on session check — keeping session`\n );\n } else {\n console.warn(\n `${tag} Portal session check failed for ${body.openId} (${status ?? (err as Error).message}) — keeping session`\n );\n }\n return null;\n }\n }\n\n /** Is the session behind this cookie still live in the Portal? */\n async function isSessionActive(cookie: VerifiedCookie): Promise<boolean> {\n if (!sessionCheck) return true;\n if (!env.portalApiUrl) {\n warnOnce(\n \"no-portal-url\",\n `${tag} No Portal API URL — sessions signed out in the Portal are not refused`\n );\n return true;\n }\n const { openId } = cookie.session;\n if (cookie.iat === null) {\n warnOnce(\n \"no-iat\",\n `${tag} Session cookie for ${openId} has neither iat nor exp — not checked with the Portal`\n );\n return true;\n }\n\n const key = JSON.stringify([openId, cookie.sid ?? null, cookie.iat]);\n const hit = sessionCheckCache.get(key);\n if (hit && Date.now() - hit.at < sessionCheckCacheTtlMs) return hit.active;\n\n // Parallel requests from one page load share a single Portal call.\n let pending = sessionCheckInflight.get(key);\n if (!pending) {\n pending = askPortalSessionActive({\n openId,\n ...(cookie.sid ? { sid: cookie.sid } : {}),\n iat: cookie.iat,\n })\n .then(answer => {\n // No answer: keep an earlier \"ended\", otherwise let it through.\n // Remembered either way, so a down Portal isn't asked every request.\n const active = answer ?? hit?.active ?? true;\n rememberSessionCheck(key, active);\n return active;\n })\n .finally(() => sessionCheckInflight.delete(key));\n sessionCheckInflight.set(key, pending);\n }\n return pending;\n }\n\n // ── Sign-out ───────────────────────────────────────────────────\n\n /** The first non-empty value of a header, cut to the Portal's limit. */\n function headerValue(req: Request, name: string, max: number) {\n const raw = req.headers[name];\n const value = (Array.isArray(raw) ? raw[0] : raw)?.trim();\n return value ? value.slice(0, max) : undefined;\n }\n\n /**\n * Ends the Portal session behind this request's cookie (#163), through the\n * Portal's own endSession: POST /api/service/sessions/end with the cookie\n * itself, which the Portal verifies. Signing out of this app then signs the\n * device out of the Portal and every other app, not just this one. Call it\n * from auth.logout before clearing the cookie.\n *\n * Best-effort: it never throws, and gives up after sessionCheckTimeoutMs,\n * so a slow or missing Portal never holds up a sign-out. Whatever the\n * Portal answers, this app refuses the cookie from now on. Returns whether\n * the Portal ended a live session, or null when it gave no answer.\n */\n async function endPortalSession(req: Request): Promise<boolean | null> {\n let openId: string | undefined;\n try {\n const cookie = await getCookie();\n const value = req.headers.cookie\n ? cookie.parseCookie(req.headers.cookie)[cookieName]\n : undefined;\n if (!value) return false;\n const verified = await verifyCookie(value);\n // A cookie from before portal#237 has no session id: nothing to end.\n if (!verified?.sid) return false;\n openId = verified.session.openId;\n if (verified.iat !== null) {\n rememberSessionCheck(\n JSON.stringify([openId, verified.sid, verified.iat]),\n false\n );\n }\n if (!env.portalApiUrl) {\n warnOnce(\n \"no-portal-url-end\",\n `${tag} No Portal API URL — signing out here leaves the Portal session live`\n );\n return null;\n }\n const ip =\n headerValue(req, \"cf-connecting-ip\", 45) ??\n (req.ip ? String(req.ip).slice(0, 45) : undefined);\n const userAgent = headerValue(req, \"user-agent\", 255);\n const client = await getPortalClientAsync();\n if (!client) return null;\n const { data } = await client.post<{ ended?: unknown }>(\n \"/api/service/sessions/end\",\n {\n cookie: value,\n app: serviceName.slice(0, 64),\n ...(ip ? { ip } : {}),\n ...(userAgent ? { userAgent } : {}),\n },\n {\n timeout: sessionCheckTimeoutMs,\n signal: AbortSignal.timeout(sessionCheckTimeoutMs),\n }\n );\n if (typeof data?.ended === \"boolean\") return data.ended;\n console.warn(\n `${tag} Portal gave no answer to ending the session for ${openId}`\n );\n return null;\n } catch (err: unknown) {\n const status = (err as { response?: { status?: number } } | null)\n ?.response?.status;\n if (status === 404) {\n warnOnce(\n \"end-404\",\n `${tag} Portal can't end sessions yet (404) — signing out here leaves the Portal session live until it ships`\n );\n } else {\n console.warn(\n `${tag} Portal could not end the session for ${openId ?? \"a user\"} (${status ?? (err as Error).message}) — signed out here only`\n );\n }\n return null;\n }\n }\n\n // ── Authenticate Request ───────────────────────────────────────\n\n async function authenticateRequest(req: Request): Promise<TUser> {\n const cookie = await getCookie();\n const cookies = req.headers.cookie\n ? new Map(Object.entries(cookie.parseCookie(req.headers.cookie)))\n : new Map<string, string | undefined>();\n\n const sessionCookie = cookies.get(cookieName);\n const verified = await verifyCookie(sessionCookie);\n\n if (!verified) {\n throw Object.assign(new Error(\"Invalid session cookie\"), {\n statusCode: 403,\n });\n }\n\n // A device signed out in the Portal is refused here too (#77).\n if (!(await isSessionActive(verified))) {\n throw Object.assign(new Error(\"Session ended\"), {\n statusCode: 403,\n code: \"SESSION_ENDED\",\n });\n }\n\n const { session } = verified;\n\n const sessionUserId = session.openId;\n const signedInAt = new Date();\n\n // ── Portal mode: single source of truth, no local mirror ────\n if (userSource === \"portal\") {\n return await resolvePortalUser(sessionUserId, session);\n }\n\n let user = (await db!.getUserByOpenId(sessionUserId)) ?? null;\n\n // ── Auto-create user from verified JWT ──────────────────────\n if (!user) {\n try {\n // Try portal API first for richer user info\n const portalUser = await getPortalUser(sessionUserId);\n\n if (portalUser) {\n console.log(\n `${tag} Cross-subdomain SSO: creating user from portal (${portalUser.email || sessionUserId})`\n );\n // Portal returns `enterpriseId`; `companyId` is a legacy alias.\n const portalEnterpriseId =\n portalUser.enterpriseId ?? portalUser.companyId ?? null;\n await db!.upsertUser({\n openId: sessionUserId,\n name: portalUser.name || session.name || null,\n email: portalUser.email ?? null,\n loginMethod: \"portal-sso\",\n role: mapRole(portalUser.role),\n lastSignedIn: signedInAt,\n ...(portalEnterpriseId != null\n ? {\n enterpriseId: portalEnterpriseId,\n portalUserId: portalUser.id,\n }\n : {}),\n });\n user = (await db!.getUserByOpenId(sessionUserId)) ?? null;\n } else {\n // Portal unavailable — create from JWT payload\n console.log(\n `${tag} Cross-subdomain SSO: creating user from JWT payload (${sessionUserId})`\n );\n await db!.upsertUser({\n openId: sessionUserId,\n name: session.name || null,\n loginMethod: \"portal-sso\",\n lastSignedIn: signedInAt,\n });\n user = (await db!.getUserByOpenId(sessionUserId)) ?? null;\n }\n } catch (error) {\n console.error(`${tag} Failed to sync user info:`, error);\n throw Object.assign(new Error(\"Failed to sync user info\"), {\n statusCode: 403,\n });\n }\n }\n\n if (!user) {\n throw Object.assign(new Error(\"User not found\"), { statusCode: 403 });\n }\n\n // ── Enterprise backfill ─────────────────────────────────────\n if (user.enterpriseId === null) {\n try {\n const portalUser = await getPortalUser(user.openId);\n // Portal returns `enterpriseId`; `companyId` is a legacy alias.\n const portalEnterpriseId =\n portalUser?.enterpriseId ?? portalUser?.companyId ?? null;\n if (portalEnterpriseId != null) {\n console.log(\n `${tag} Backfilling enterpriseId=${portalEnterpriseId} for user ${user.id}`\n );\n await db!.updateUserFields(user.id, {\n enterpriseId: portalEnterpriseId,\n ...(portalUser?.id ? { portalUserId: portalUser.id } : {}),\n });\n user = (await db!.getUserByOpenId(user.openId)) ?? user;\n }\n } catch (backfillError) {\n console.warn(\n `${tag} Failed to backfill enterpriseId for user ${user.id}:`,\n backfillError\n );\n // Non-fatal — user can still proceed\n }\n }\n\n // Touch lastSignedIn\n await db!.upsertUser({\n openId: user.openId,\n lastSignedIn: signedInAt,\n });\n\n // A read-only account (ops#246) is marked on the user the apps see. The\n // Portal answer is cached like portal mode's; an unreachable Portal leaves\n // the user as it is (the same fail-open as the session check).\n const portalSays = await getPortalUserCached(user.openId);\n const readOnly = portalSays\n ? portalSays.readOnly === true\n : knownReadOnly.has(user.openId);\n return readOnly ? { ...user, readOnly: true } : user;\n }\n\n // ── Return public API ──────────────────────────────────────────\n\n return {\n verifySession,\n authenticateRequest,\n endPortalSession,\n getPortalUser,\n getSessionCookieOptions,\n isValidReturnUrl,\n };\n}\n"],"mappings":";AAaO,IAAM,2BAA2B;AACjC,IAAM,8BAA8B;AACpC,IAAM,wBAAwB;AAC9B,IAAM,2BAA2B;AAExC,IAAM,MAAM,CAAC,SACX,OAAO,YAAY,cAAc,SAAY,QAAQ,IAAI,IAAI;AAGxD,SAAS,aACd,eAAmC,IAAI,eAAe,GAC7C;AACT,SAAO,iBAAiB;AAC1B;AAMO,SAAS,kBAAkB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