@open-mercato/enterprise 0.10.1-develop.7768.1.90fa052c11 → 0.10.1-develop.7769.1.0e6458bb99

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.
Files changed (36) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/modules/security/api/interceptors.js +2 -1
  3. package/dist/modules/security/api/interceptors.js.map +2 -2
  4. package/dist/modules/security/data/validators.js +6 -0
  5. package/dist/modules/security/data/validators.js.map +2 -2
  6. package/dist/modules/security/events.js +1 -0
  7. package/dist/modules/security/events.js.map +2 -2
  8. package/dist/modules/security/i18n/de.json +2 -0
  9. package/dist/modules/security/i18n/en.json +2 -0
  10. package/dist/modules/security/i18n/es.json +2 -0
  11. package/dist/modules/security/i18n/ko.json +2 -0
  12. package/dist/modules/security/i18n/pl.json +2 -0
  13. package/dist/modules/security/lib/enforcement-redirect.js +4 -1
  14. package/dist/modules/security/lib/enforcement-redirect.js.map +2 -2
  15. package/dist/modules/security/lib/security-config.js +29 -0
  16. package/dist/modules/security/lib/security-config.js.map +2 -2
  17. package/dist/modules/security/notifications.js +12 -0
  18. package/dist/modules/security/notifications.js.map +2 -2
  19. package/dist/modules/security/services/SudoChallengeService.js +7 -3
  20. package/dist/modules/security/services/SudoChallengeService.js.map +2 -2
  21. package/dist/modules/security/subscribers/mfa-emergency-bypass-notification.js +52 -0
  22. package/dist/modules/security/subscribers/mfa-emergency-bypass-notification.js.map +7 -0
  23. package/package.json +9 -9
  24. package/src/modules/security/api/interceptors.ts +2 -1
  25. package/src/modules/security/data/validators.ts +6 -0
  26. package/src/modules/security/events.ts +1 -0
  27. package/src/modules/security/i18n/de.json +2 -0
  28. package/src/modules/security/i18n/en.json +2 -0
  29. package/src/modules/security/i18n/es.json +2 -0
  30. package/src/modules/security/i18n/ko.json +2 -0
  31. package/src/modules/security/i18n/pl.json +2 -0
  32. package/src/modules/security/lib/enforcement-redirect.ts +4 -1
  33. package/src/modules/security/lib/security-config.ts +31 -0
  34. package/src/modules/security/notifications.ts +12 -0
  35. package/src/modules/security/services/SudoChallengeService.ts +9 -4
  36. package/src/modules/security/subscribers/mfa-emergency-bypass-notification.ts +68 -0
@@ -1,2 +1,2 @@
1
- [build:enterprise] found 697 entry points
1
+ [build:enterprise] found 699 entry points
2
2
  [build:enterprise] built successfully
@@ -1,5 +1,5 @@
1
1
  import { signJwt, verifyJwt } from "@open-mercato/shared/lib/auth/jwt";
2
- import { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig } from "../lib/security-config.js";
2
+ import { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig, requestMfaEmergencyBypassNotification } from "../lib/security-config.js";
3
3
  function readClaims(token) {
4
4
  const payload = verifyJwt(token);
5
5
  if (!payload || typeof payload !== "object") return null;
@@ -41,6 +41,7 @@ const interceptors = [
41
41
  if (!mfaVerificationService) return {};
42
42
  if (readSecurityModuleConfig().mfa.emergencyBypass) {
43
43
  emitMfaEmergencyBypassActiveWarning("login MFA challenge bypassed", { userId: claims.sub });
44
+ await requestMfaEmergencyBypassNotification({ tenantId: claims.tenantId, organizationId: claims.orgId });
44
45
  return {};
45
46
  }
46
47
  try {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/security/api/interceptors.ts"],
4
- "sourcesContent": ["import type { ApiInterceptor } from '@open-mercato/shared/lib/crud/api-interceptor'\nimport { signJwt, verifyJwt } from '@open-mercato/shared/lib/auth/jwt'\nimport { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig } from '../lib/security-config'\n\ntype JwtClaims = {\n sub: string\n sid?: string | null\n tenantId?: string | null\n orgId?: string | null\n email?: string | null\n roles?: string[]\n}\n\ntype MfaVerificationServiceLike = {\n createChallenge: (userId: string) => Promise<{\n challengeId: string\n availableMethods: Array<{ type: string; label: string; icon: string }>\n }>\n}\n\nfunction readClaims(token: string): JwtClaims | null {\n const payload = verifyJwt(token)\n if (!payload || typeof payload !== 'object') return null\n const sub = typeof payload.sub === 'string' ? payload.sub : null\n if (!sub) return null\n return {\n sub,\n sid: typeof payload.sid === 'string' ? payload.sid : null,\n tenantId: typeof payload.tenantId === 'string' ? payload.tenantId : null,\n orgId: typeof payload.orgId === 'string' ? payload.orgId : null,\n email: typeof payload.email === 'string' ? payload.email : null,\n roles: Array.isArray(payload.roles) ? payload.roles.filter((value: unknown): value is string => typeof value === 'string') : [],\n }\n}\n\nfunction resolveMfaVerificationService(container: { resolve: (name: string) => unknown }): MfaVerificationServiceLike | null {\n try {\n const resolved = container.resolve('mfaVerificationService')\n if (\n !resolved\n || typeof resolved !== 'object'\n || typeof (resolved as { createChallenge?: unknown }).createChallenge !== 'function'\n ) {\n return null\n }\n return resolved as MfaVerificationServiceLike\n } catch {\n return null\n }\n}\n\nexport const interceptors: ApiInterceptor[] = [\n {\n id: 'security.auth.login.mfa-challenge',\n targetRoute: 'auth/login',\n methods: ['POST'],\n priority: 50,\n async after(_request, response, context) {\n if (response.statusCode !== 200) return {}\n if (response.body.ok !== true || response.body.mfa_required === true) return {}\n if (typeof response.body.token !== 'string' || response.body.token.length === 0) return {}\n\n const claims = readClaims(response.body.token)\n if (!claims) return {}\n\n const mfaVerificationService = resolveMfaVerificationService(context.container as { resolve: (name: string) => unknown })\n if (!mfaVerificationService) return {}\n\n if (readSecurityModuleConfig().mfa.emergencyBypass) {\n emitMfaEmergencyBypassActiveWarning('login MFA challenge bypassed', { userId: claims.sub })\n return {}\n }\n\n try {\n const challenge = await mfaVerificationService.createChallenge(claims.sub)\n const pendingToken = signJwt(\n {\n sub: claims.sub,\n sid: claims.sid ?? undefined,\n tenantId: claims.tenantId ?? null,\n orgId: claims.orgId ?? null,\n email: claims.email ?? null,\n roles: claims.roles ?? [],\n mfa_pending: true,\n mfa_verified: false,\n },\n undefined,\n 60 * 10,\n )\n return {\n replace: {\n ok: true,\n mfa_required: true,\n challenge_id: challenge.challengeId,\n available_methods: challenge.availableMethods,\n token: pendingToken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SAAS,KAAK,MAAM,WAAW,EAAG,QAAO,CAAC;AAEzF,YAAM,SAAS,WAAW,SAAS,KAAK,KAAK;AAC7C,UAAI,CAAC,OAAQ,QAAO,CAAC;AAErB,YAAM,yBAAyB,8BAA8B,QAAQ,SAAmD;AACxH,UAAI,CAAC,uBAAwB,QAAO,CAAC;AAErC,UAAI,yBAAyB,EAAE,IAAI,iBAAiB;AAClD,4CAAoC,gCAAgC,EAAE,QAAQ,OAAO,IAAI,CAAC;AAC1F,eAAO,CAAC;AAAA,MACV;AAEA,UAAI;AACF,cAAM,YAAY,MAAM,uBAAuB,gBAAgB,OAAO,GAAG;AACzE,cAAM,eAAe;AAAA,UACnB;AAAA,YACE,KAAK,OAAO;AAAA,YACZ,KAAK,OAAO,OAAO;AAAA,YACnB,UAAU,OAAO,YAAY;AAAA,YAC7B,OAAO,OAAO,SAAS;AAAA,YACvB,OAAO,OAAO,SAAS;AAAA,YACvB,OAAO,OAAO,SAAS,CAAC;AAAA,YACxB,aAAa;AAAA,YACb,cAAc;AAAA,UAChB;AAAA,UACA;AAAA,UACA,KAAK;AAAA,QACP;AACA,eAAO;AAAA,UACL,SAAS;AAAA,YACP,IAAI;AAAA,YACJ,cAAc;AAAA,YACd,cAAc,UAAU;AAAA,YACxB,mBAAmB,UAAU;AAAA,YAC7B,OAAO;AAAA,UACT;AAAA,QACF;AAAA,MACF,QAAQ;AACN,eAAO,CAAC;AAAA,MACV;AAAA,IACF;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import type { ApiInterceptor } from '@open-mercato/shared/lib/crud/api-interceptor'\nimport { signJwt, verifyJwt } from '@open-mercato/shared/lib/auth/jwt'\nimport { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig, requestMfaEmergencyBypassNotification } from '../lib/security-config'\n\ntype JwtClaims = {\n sub: string\n sid?: string | null\n tenantId?: string | null\n orgId?: string | null\n email?: string | null\n roles?: string[]\n}\n\ntype MfaVerificationServiceLike = {\n createChallenge: (userId: string) => Promise<{\n challengeId: string\n availableMethods: Array<{ type: string; label: string; icon: string }>\n }>\n}\n\nfunction readClaims(token: string): JwtClaims | null {\n const payload = verifyJwt(token)\n if (!payload || typeof payload !== 'object') return null\n const sub = typeof payload.sub === 'string' ? payload.sub : null\n if (!sub) return null\n return {\n sub,\n sid: typeof payload.sid === 'string' ? payload.sid : null,\n tenantId: typeof payload.tenantId === 'string' ? payload.tenantId : null,\n orgId: typeof payload.orgId === 'string' ? payload.orgId : null,\n email: typeof payload.email === 'string' ? payload.email : null,\n roles: Array.isArray(payload.roles) ? payload.roles.filter((value: unknown): value is string => typeof value === 'string') : [],\n }\n}\n\nfunction resolveMfaVerificationService(container: { resolve: (name: string) => unknown }): MfaVerificationServiceLike | null {\n try {\n const resolved = container.resolve('mfaVerificationService')\n if (\n !resolved\n || typeof resolved !== 'object'\n || typeof (resolved as { createChallenge?: unknown }).createChallenge !== 'function'\n ) {\n return null\n }\n return resolved as MfaVerificationServiceLike\n } catch {\n return null\n }\n}\n\nexport const interceptors: ApiInterceptor[] = [\n {\n id: 'security.auth.login.mfa-challenge',\n targetRoute: 'auth/login',\n methods: ['POST'],\n priority: 50,\n async after(_request, response, context) {\n if (response.statusCode !== 200) return {}\n if (response.body.ok !== true || response.body.mfa_required === true) return {}\n if (typeof response.body.token !== 'string' || response.body.token.length === 0) return {}\n\n const claims = readClaims(response.body.token)\n if (!claims) return {}\n\n const mfaVerificationService = resolveMfaVerificationService(context.container as { resolve: (name: string) => unknown })\n if (!mfaVerificationService) return {}\n\n if (readSecurityModuleConfig().mfa.emergencyBypass) {\n emitMfaEmergencyBypassActiveWarning('login MFA challenge bypassed', { userId: claims.sub })\n await requestMfaEmergencyBypassNotification({ tenantId: claims.tenantId, organizationId: claims.orgId })\n return {}\n }\n\n try {\n const challenge = await mfaVerificationService.createChallenge(claims.sub)\n const pendingToken = signJwt(\n {\n sub: claims.sub,\n sid: claims.sid ?? undefined,\n tenantId: claims.tenantId ?? null,\n orgId: claims.orgId ?? null,\n email: claims.email ?? null,\n roles: claims.roles ?? [],\n mfa_pending: true,\n mfa_verified: false,\n },\n undefined,\n 60 * 10,\n )\n return {\n replace: {\n ok: true,\n mfa_required: true,\n challenge_id: challenge.challengeId,\n available_methods: challenge.availableMethods,\n token: pendingToken,\n },\n }\n } catch {\n return {}\n }\n },\n },\n]\n"],
5
+ "mappings": "AACA,SAAS,SAAS,iBAAiB;AACnC,SAAS,qCAAqC,0BAA0B,6CAA6C;AAkBrH,SAAS,WAAW,OAAiC;AACnD,QAAM,UAAU,UAAU,KAAK;AAC/B,MAAI,CAAC,WAAW,OAAO,YAAY,SAAU,QAAO;AACpD,QAAM,MAAM,OAAO,QAAQ,QAAQ,WAAW,QAAQ,MAAM;AAC5D,MAAI,CAAC,IAAK,QAAO;AACjB,SAAO;AAAA,IACL;AAAA,IACA,KAAK,OAAO,QAAQ,QAAQ,WAAW,QAAQ,MAAM;AAAA,IACrD,UAAU,OAAO,QAAQ,aAAa,WAAW,QAAQ,WAAW;AAAA,IACpE,OAAO,OAAO,QAAQ,UAAU,WAAW,QAAQ,QAAQ;AAAA,IAC3D,OAAO,OAAO,QAAQ,UAAU,WAAW,QAAQ,QAAQ;AAAA,IAC3D,OAAO,MAAM,QAAQ,QAAQ,KAAK,IAAI,QAAQ,MAAM,OAAO,CAAC,UAAoC,OAAO,UAAU,QAAQ,IAAI,CAAC;AAAA,EAChI;AACF;AAEA,SAAS,8BAA8B,WAAsF;AAC3H,MAAI;AACF,UAAM,WAAW,UAAU,QAAQ,wBAAwB;AAC3D,QACE,CAAC,YACE,OAAO,aAAa,YACpB,OAAQ,SAA2C,oBAAoB,YAC1E;AACA,aAAO;AAAA,IACT;AACA,WAAO;AAAA,EACT,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEO,MAAM,eAAiC;AAAA,EAC5C;AAAA,IACE,IAAI;AAAA,IACJ,aAAa;AAAA,IACb,SAAS,CAAC,MAAM;AAAA,IAChB,UAAU;AAAA,IACV,MAAM,MAAM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from "./entities.js";
4
4
  import { readSecurityModuleConfig } from "../lib/security-config.js";
5
5
  const passwordSchema = buildPasswordSchema();
6
6
  const securityConfig = readSecurityModuleConfig();
7
+ const mfaEmergencyBypassEventSchema = z.object({
8
+ tenantId: z.string().uuid(),
9
+ organizationId: z.string().uuid(),
10
+ observedAt: z.string().datetime()
11
+ });
7
12
  const changePasswordSchema = z.object({
8
13
  currentPassword: z.string().min(1),
9
14
  newPassword: passwordSchema
@@ -52,6 +57,7 @@ const sudoConfigUpdateSchema = sudoConfigSchema.partial();
52
57
  export {
53
58
  changePasswordSchema,
54
59
  enforcementPolicySchema,
60
+ mfaEmergencyBypassEventSchema,
55
61
  mfaVerifySchema,
56
62
  regenerateRecoveryCodesSchema,
57
63
  removeMfaMethodSchema,
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/security/data/validators.ts"],
4
- "sourcesContent": ["import { z } from 'zod'\nimport { buildPasswordSchema } from '@open-mercato/shared/lib/auth/passwordPolicy'\nimport { ChallengeMethod, EnforcementScope } from './entities'\nimport { readSecurityModuleConfig } from '../lib/security-config'\n\nconst passwordSchema = buildPasswordSchema()\nconst securityConfig = readSecurityModuleConfig()\n\nexport const changePasswordSchema = z.object({\n currentPassword: z.string().min(1),\n newPassword: passwordSchema,\n})\n\nexport const mfaVerifySchema = z.object({\n challengeId: z.string().min(1),\n methodType: z.string().min(1),\n payload: z.record(z.string(), z.unknown()).default({}),\n})\n\nexport const removeMfaMethodSchema = z.object({\n id: z.string().uuid(),\n})\n\nexport const regenerateRecoveryCodesSchema = z.object({})\n\nexport const enforcementPolicySchema = z.object({\n scope: z.nativeEnum(EnforcementScope),\n tenantId: z.string().uuid().nullable().optional(),\n organizationId: z.string().uuid().nullable().optional(),\n isEnforced: z.boolean().default(true),\n allowedMethods: z.array(z.string().min(1)).nullable().optional(),\n enforcementDeadline: z.coerce.date().nullable().optional(),\n})\n\nexport const updateEnforcementPolicySchema = enforcementPolicySchema.partial()\n\nexport const sudoChallengeInitSchema = z.object({\n targetIdentifier: z.string().min(1),\n})\n\nexport const sudoChallengePrepareSchema = z.object({\n sessionId: z.string().uuid(),\n methodType: z.string().min(1),\n})\n\nexport const sudoChallengeVerifySchema = z.object({\n sessionId: z.string().uuid(),\n targetIdentifier: z.string().min(1),\n methodType: z.string().min(1),\n payload: z.record(z.string(), z.unknown()).default({}),\n})\n\nexport const sudoConfigSchema = z.object({\n tenantId: z.string().uuid().nullable().optional(),\n organizationId: z.string().uuid().nullable().optional(),\n label: z.string().max(200).nullable().optional(),\n targetIdentifier: z.string().min(1),\n isEnabled: z.boolean().default(true),\n ttlSeconds: z.coerce.number()\n .int()\n .min(securityConfig.sudo.minTtlSeconds)\n .max(securityConfig.sudo.maxTtlSeconds)\n .default(securityConfig.sudo.defaultTtlSeconds),\n challengeMethod: z.nativeEnum(ChallengeMethod).default(ChallengeMethod.AUTO),\n})\n\nexport const sudoConfigUpdateSchema = sudoConfigSchema.partial()\n\nexport type ChangePasswordInput = z.infer<typeof changePasswordSchema>\nexport type MfaVerifyInput = z.infer<typeof mfaVerifySchema>\nexport type RemoveMfaMethodInput = z.infer<typeof removeMfaMethodSchema>\nexport type RegenerateRecoveryCodesInput = z.infer<typeof regenerateRecoveryCodesSchema>\nexport type EnforcementPolicyInput = z.infer<typeof enforcementPolicySchema>\nexport type UpdateEnforcementPolicyInput = z.infer<typeof updateEnforcementPolicySchema>\nexport type SudoChallengeInitInput = z.infer<typeof sudoChallengeInitSchema>\nexport type SudoChallengePrepareInput = z.infer<typeof sudoChallengePrepareSchema>\nexport type SudoChallengeVerifyInput = z.infer<typeof sudoChallengeVerifySchema>\nexport type SudoConfigInput = z.infer<typeof sudoConfigSchema>\nexport type SudoConfigUpdateInput = z.infer<typeof sudoConfigUpdateSchema>\n"],
5
- "mappings": "AAAA,SAAS,SAAS;AAClB,SAAS,2BAA2B;AACpC,SAAS,iBAAiB,wBAAwB;AAClD,SAAS,gCAAgC;AAEzC,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C;AAEM,MAAM,4BAA4B,EAAE,OAAO;AAAA,EAChD,WAAW,EAAE,OAAO,EAAE,KAAK;AAAA,EAC3B,kBAAkB,EAAE,OAAO,EAAE,IAAI,CAAC;AAAA,EAClC,YAAY,EAAE,OAAO,EAAE,IAAI,CAAC;AAAA,EAC5B,SAAS,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,QAAQ,CAAC,EAAE,QAAQ,CAAC,CAAC;AACvD,CAAC;AAEM,MAAM,mBAAmB,EAAE,OAAO;AAAA,EACvC,UAAU,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS;AAAA,EAChD,gBAAgB,EAAE,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,SAAS;AAAA,EACtD,OAAO,EAAE,OAAO,EAAE,IAAI,GAAG,EAAE,SAAS,EAAE,SAAS;AAAA,EAC/C,kBAAkB,EAAE,OAAO,EAAE,IAAI,CAAC;AAAA,EAClC,WAAW,EAAE,QAAQ,EAAE,QAAQ,IAAI;AAAA,EACnC,YAAY,EAAE,OAAO,OAAO,EACzB,IAAI,EACJ,IAAI,eAAe,KAAK,aAAa,EACrC,IAAI,eAAe,KAAK,aAAa,EACrC,QAAQ,eAAe,KAAK,iBAAiB;AAAA,EAChD,iBAAiB,EAAE,WAAW,eAAe,EAAE,QAAQ,gBAAgB,IAAI;AAC7E,CAAC;AAEM,MAAM,yBAAyB,iBAAiB,QAAQ;",
4
+ "sourcesContent": ["import { z } from 'zod'\nimport { buildPasswordSchema } from '@open-mercato/shared/lib/auth/passwordPolicy'\nimport { ChallengeMethod, EnforcementScope } from './entities'\nimport { readSecurityModuleConfig } from '../lib/security-config'\n\nconst passwordSchema = buildPasswordSchema()\nconst securityConfig = readSecurityModuleConfig()\n\nexport const mfaEmergencyBypassEventSchema = z.object({\n tenantId: z.string().uuid(),\n organizationId: z.string().uuid(),\n observedAt: z.string().datetime(),\n})\n\nexport const changePasswordSchema = z.object({\n currentPassword: z.string().min(1),\n newPassword: passwordSchema,\n})\n\nexport const mfaVerifySchema = z.object({\n challengeId: z.string().min(1),\n methodType: z.string().min(1),\n payload: z.record(z.string(), z.unknown()).default({}),\n})\n\nexport const removeMfaMethodSchema = z.object({\n id: z.string().uuid(),\n})\n\nexport const regenerateRecoveryCodesSchema = z.object({})\n\nexport const enforcementPolicySchema = z.object({\n scope: z.nativeEnum(EnforcementScope),\n tenantId: z.string().uuid().nullable().optional(),\n organizationId: z.string().uuid().nullable().optional(),\n isEnforced: z.boolean().default(true),\n allowedMethods: z.array(z.string().min(1)).nullable().optional(),\n enforcementDeadline: z.coerce.date().nullable().optional(),\n})\n\nexport const updateEnforcementPolicySchema = enforcementPolicySchema.partial()\n\nexport const sudoChallengeInitSchema = z.object({\n targetIdentifier: z.string().min(1),\n})\n\nexport const sudoChallengePrepareSchema = z.object({\n sessionId: z.string().uuid(),\n methodType: z.string().min(1),\n})\n\nexport const sudoChallengeVerifySchema = z.object({\n sessionId: z.string().uuid(),\n targetIdentifier: z.string().min(1),\n methodType: z.string().min(1),\n payload: z.record(z.string(), z.unknown()).default({}),\n})\n\nexport const sudoConfigSchema = z.object({\n tenantId: z.string().uuid().nullable().optional(),\n organizationId: z.string().uuid().nullable().optional(),\n label: z.string().max(200).nullable().optional(),\n targetIdentifier: z.string().min(1),\n isEnabled: z.boolean().default(true),\n ttlSeconds: z.coerce.number()\n .int()\n .min(securityConfig.sudo.minTtlSeconds)\n .max(securityConfig.sudo.maxTtlSeconds)\n .default(securityConfig.sudo.defaultTtlSeconds),\n challengeMethod: z.nativeEnum(ChallengeMethod).default(ChallengeMethod.AUTO),\n})\n\nexport const sudoConfigUpdateSchema = sudoConfigSchema.partial()\n\nexport type ChangePasswordInput = z.infer<typeof changePasswordSchema>\nexport type MfaVerifyInput = z.infer<typeof mfaVerifySchema>\nexport type RemoveMfaMethodInput = z.infer<typeof removeMfaMethodSchema>\nexport type RegenerateRecoveryCodesInput = z.infer<typeof regenerateRecoveryCodesSchema>\nexport type EnforcementPolicyInput = z.infer<typeof enforcementPolicySchema>\nexport type UpdateEnforcementPolicyInput = z.infer<typeof updateEnforcementPolicySchema>\nexport type SudoChallengeInitInput = z.infer<typeof sudoChallengeInitSchema>\nexport type SudoChallengePrepareInput = z.infer<typeof sudoChallengePrepareSchema>\nexport type SudoChallengeVerifyInput = z.infer<typeof sudoChallengeVerifySchema>\nexport type SudoConfigInput = z.infer<typeof sudoConfigSchema>\nexport type SudoConfigUpdateInput = z.infer<typeof sudoConfigUpdateSchema>\n"],
5
+ "mappings": "AAAA,SAAS,SAAS;AAClB,SAAS,2BAA2B;AACpC,SAAS,iBAAiB,wBAAwB;AAClD,SAAS,gCAAgC;AAEzC,MAAM,iBAAiB,oBAAoB;AAC3C,MAAM,iBAAiB,yBAAyB;AAEzC,MAAM,gCAAgC,EAAE,OAAO;AAAA,EACpD,UAAU,EAAE,OAAO,EAAE,KAAK;AAAA,EAC1B,gBAAgB,EAAE,OAAO,EAAE,KAAK;AAAA,EAChC,YAAY,EAAE,OAAO,EAAE,SAAS;AAClC,CAAC;AAEM,MAAM,uBAAuB,EAAE,OAAO;AAAA,EAC3C,iBAAiB,EAAE,OAAO,EAAE,IAAI,CAAC;AAAA,EACjC,aAAa;AACf,CAAC;AAEM,MAAM,kBAAkB,EAAE,OAAO;AAAA,EACtC,aAAa,EAAE,OAAO,EAAE,IAAI,CAAC;AAAA,EAC7B,YAAY,EAAE,OAAO,EAAE,IAAI,CAAC;AAAA,EAC5B,SAAS,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,QAAQ,CAAC,EAAE,QAAQ,CAAC,CAAC;AACvD,CAAC;AAEM,MAAM,wBAAwB,EAAE,OAAO;AAAA,EAC5C,IAAI,EAAE,OAAO,EAAE,KAAK;AACtB,CAAC;AAEM,MAAM,gCAAgC,EAAE,OAAO,CAAC,CAAC;AAEjD,MAAM,0BAA0B,EAAE,OAAO;AAAA,EAC9C,OAAO,EAAE,WAAW,gBAAgB;AAAA,EACpC,UAAU,EAAE,OAA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YAAY,EAAE,OAAO,OAAO,EACzB,IAAI,EACJ,IAAI,eAAe,KAAK,aAAa,EACrC,IAAI,eAAe,KAAK,aAAa,EACrC,QAAQ,eAAe,KAAK,iBAAiB;AAAA,EAChD,iBAAiB,EAAE,WAAW,eAAe,EAAE,QAAQ,gBAAgB,IAAI;AAC7E,CAAC;AAEM,MAAM,yBAAyB,iBAAiB,QAAQ;",
6
6
  "names": []
7
7
  }
@@ -22,6 +22,7 @@ const events = [
22
22
  { id: "security.mfa.verified", label: "MFA Verified", category: "lifecycle" },
23
23
  { id: "security.mfa.otp.sent", label: "MFA OTP Sent", category: "lifecycle" },
24
24
  { id: "security.mfa.reset", label: "MFA Reset", category: "lifecycle" },
25
+ { id: "security.mfa.emergency_bypass_detected", label: "MFA Emergency Bypass Active", category: "system", excludeFromTriggers: true },
25
26
  { id: "security.recovery.regenerated", label: "Recovery Codes Regenerated", category: "lifecycle" },
26
27
  { id: "security.recovery.used", label: "Recovery Code Used", category: "lifecycle" },
27
28
  { id: "security.enforcement.created", label: "MFA Enforcement Created", category: "lifecycle" },
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/modules/security/events.ts"],
4
- "sourcesContent": ["import { createModuleEvents } from '@open-mercato/shared/modules/events'\n\nconst events = [\n {\n id: 'security.password.changed',\n label: 'Password Changed',\n entity: 'password',\n category: 'lifecycle',\n description: 'Emitted after a user successfully changes their password.',\n },\n {\n id: 'security.password.notification_requested',\n label: 'Password Change Notification Requested',\n entity: 'password',\n category: 'lifecycle',\n description: 'Requests creation of a user-facing password change notification.',\n excludeFromTriggers: true,\n },\n { id: 'security.mfa.method.added', label: 'MFA Method Added', category: 'lifecycle' },\n { id: 'security.mfa.method.removed', label: 'MFA Method Removed', category: 'lifecycle' },\n { id: 'security.mfa.enrolled', label: 'MFA Enrolled', category: 'lifecycle' },\n { id: 'security.mfa.removed', label: 'MFA Removed', category: 'lifecycle' },\n { id: 'security.mfa.verified', label: 'MFA Verified', category: 'lifecycle' },\n { id: 'security.mfa.otp.sent', label: 'MFA OTP Sent', category: 'lifecycle' },\n { id: 'security.mfa.reset', label: 'MFA Reset', category: 'lifecycle' },\n { id: 'security.recovery.regenerated', label: 'Recovery Codes Regenerated', category: 'lifecycle' },\n { id: 'security.recovery.used', label: 'Recovery Code Used', category: 'lifecycle' },\n { id: 'security.enforcement.created', label: 'MFA Enforcement Created', category: 'lifecycle' },\n { id: 'security.enforcement.updated', label: 'MFA Enforcement Updated', category: 'lifecycle' },\n { id: 'security.enforcement.deadline_reminder_requested', label: 'MFA Enforcement Deadline Reminder Requested', category: 'lifecycle' },\n { id: 'security.sudo.challenged', label: 'Sudo Challenged', category: 'lifecycle' },\n { id: 'security.sudo.verified', label: 'Sudo Verified', category: 'lifecycle' },\n { id: 'security.sudo.failed', label: 'Sudo Failed', category: 'lifecycle' },\n { id: 'security.sudo.config.created', label: 'Sudo Config Created', category: 'lifecycle' },\n { id: 'security.sudo.config.updated', label: 'Sudo Config Updated', category: 'lifecycle' },\n { id: 'security.sudo.config.deleted', label: 'Sudo Config Deleted', category: 'lifecycle' },\n] as const\n\nexport const eventsConfig = createModuleEvents({ moduleId: 'security', events })\nexport const emitSecurityEvent = eventsConfig.emit\nexport type SecurityEventId = typeof events[number]['id']\nexport default eventsConfig\n"],
5
- "mappings": "AAAA,SAAS,0BAA0B;AAEnC,MAAM,SAAS;AAAA,EACb;AAAA,IACE,IAAI;AAAA,IACJ,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,aAAa;AAAA,EACf;AAAA,EACA;AAAA,IACE,IAAI;AAAA,IACJ,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,aAAa;AAAA,IACb,qBAAqB;AAAA,EACvB;AAAA,EACA,EAAE,IAAI,6BAA6B,OAAO,oBAAoB,UAAU,YAAY;AAAA,EACpF,EAAE,IAAI,+BAA+B,OAAO,sBAAsB,UAAU,YAAY;AAAA,EACxF,EAAE,IAAI,yBAAyB,OAAO,gBAAgB,UAAU,YAAY;AAAA,EAC5E,EAAE,IAAI,wBAAwB,OAAO,eAAe,UAAU,YAAY;AAAA,EAC1E,EAAE,IAAI,yBAAyB,OAAO,gBAAgB,UAAU,YAAY;AAAA,EAC5E,EAAE,IAAI,yBAAyB,OAAO,gBAAgB,UAAU,YAAY;AAAA,EAC5E,EAAE,IAAI,sBAAsB,OAAO,aAAa,UAAU,YAAY;AAAA,EACtE,EAAE,IAAI,iCAAiC,OAAO,8BAA8B,UAAU,YAAY;AAAA,EAClG,EAAE,IAAI,0BAA0B,OAAO,sBAAsB,UAAU,YAAY;AAAA,EACnF,EAAE,IAAI,gCAAgC,OAAO,2BAA2B,UAAU,YAAY;AAAA,EAC9F,EAAE,IAAI,gCAAgC,OAAO,2BAA2B,UAAU,YAAY;AAAA,EAC9F,EAAE,IAAI,oDAAoD,OAAO,+CAA+C,UAAU,YAAY;AAAA,EACtI,EAAE,IAAI,4BAA4B,OAAO,mBAAmB,UAAU,YAAY;AAAA,EAClF,EAAE,IAAI,0BAA0B,OAAO,iBAAiB,UAAU,YAAY;AAAA,EAC9E,EAAE,IAAI,wBAAwB,OAAO,eAAe,UAAU,YAAY;AAAA,EAC1E,EAAE,IAAI,gCAAgC,OAAO,uBAAuB,UAAU,YAAY;AAAA,EAC1F,EAAE,IAAI,gCAAgC,OAAO,uBAAuB,UAAU,YAAY;AAAA,EAC1F,EAAE,IAAI,gCAAgC,OAAO,uBAAuB,UAAU,YAAY;AAC5F;AAEO,MAAM,eAAe,mBAAmB,EAAE,UAAU,YAAY,OAAO,CAAC;AACxE,MAAM,oBAAoB,aAAa;AAE9C,IAAO,iBAAQ;",
4
+ "sourcesContent": ["import { createModuleEvents } from '@open-mercato/shared/modules/events'\n\nconst events = [\n {\n id: 'security.password.changed',\n label: 'Password Changed',\n entity: 'password',\n category: 'lifecycle',\n description: 'Emitted after a user successfully changes their password.',\n },\n {\n id: 'security.password.notification_requested',\n label: 'Password Change Notification Requested',\n entity: 'password',\n category: 'lifecycle',\n description: 'Requests creation of a user-facing password change notification.',\n excludeFromTriggers: true,\n },\n { id: 'security.mfa.method.added', label: 'MFA Method Added', category: 'lifecycle' },\n { id: 'security.mfa.method.removed', label: 'MFA Method Removed', category: 'lifecycle' },\n { id: 'security.mfa.enrolled', label: 'MFA Enrolled', category: 'lifecycle' },\n { id: 'security.mfa.removed', label: 'MFA Removed', category: 'lifecycle' },\n { id: 'security.mfa.verified', label: 'MFA Verified', category: 'lifecycle' },\n { id: 'security.mfa.otp.sent', label: 'MFA OTP Sent', category: 'lifecycle' },\n { id: 'security.mfa.reset', label: 'MFA Reset', category: 'lifecycle' },\n { id: 'security.mfa.emergency_bypass_detected', label: 'MFA Emergency Bypass Active', category: 'system', excludeFromTriggers: true },\n { id: 'security.recovery.regenerated', label: 'Recovery Codes Regenerated', category: 'lifecycle' },\n { id: 'security.recovery.used', label: 'Recovery Code Used', category: 'lifecycle' },\n { id: 'security.enforcement.created', label: 'MFA Enforcement Created', category: 'lifecycle' },\n { id: 'security.enforcement.updated', label: 'MFA Enforcement Updated', category: 'lifecycle' },\n { id: 'security.enforcement.deadline_reminder_requested', label: 'MFA Enforcement Deadline Reminder Requested', category: 'lifecycle' },\n { id: 'security.sudo.challenged', label: 'Sudo Challenged', category: 'lifecycle' },\n { id: 'security.sudo.verified', label: 'Sudo Verified', category: 'lifecycle' },\n { id: 'security.sudo.failed', label: 'Sudo Failed', category: 'lifecycle' },\n { id: 'security.sudo.config.created', label: 'Sudo Config Created', category: 'lifecycle' },\n { id: 'security.sudo.config.updated', label: 'Sudo Config Updated', category: 'lifecycle' },\n { id: 'security.sudo.config.deleted', label: 'Sudo Config Deleted', category: 'lifecycle' },\n] as const\n\nexport const eventsConfig = createModuleEvents({ moduleId: 'security', events })\nexport const emitSecurityEvent = eventsConfig.emit\nexport type SecurityEventId = typeof events[number]['id']\nexport default eventsConfig\n"],
5
+ "mappings": "AAAA,SAAS,0BAA0B;AAE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AI,gCAAgC,OAAO,uBAAuB,UAAU,YAAY;AAAA,EAC1F,EAAE,IAAI,gCAAgC,OAAO,uBAAuB,UAAU,YAAY;AAAA,EAC1F,EAAE,IAAI,gCAAgC,OAAO,uBAAuB,UAAU,YAAY;AAC5F;AAEO,MAAM,eAAe,mBAAmB,EAAE,UAAU,YAAY,OAAO,CAAC;AACxE,MAAM,oBAAoB,aAAa;AAE9C,IAAO,iBAAQ;",
6
6
  "names": []
7
7
  }
@@ -329,6 +329,8 @@
329
329
  "security.notifications.enforcementDeadline.bodyOverdue": "Deine MFA-Registrierungsfrist ist am {deadline} abgelaufen. Richte MFA sofort ein, um den Kontozugriff zu erhalten.",
330
330
  "security.notifications.enforcementDeadline.bodyWithDeadline": "Deine Organisation erfordert jetzt MFA. Richte es innerhalb von {days} Tag(en) vor {deadline} ein, um den Kontozugriff zu erhalten.",
331
331
  "security.notifications.enforcementDeadline.title": "MFA-Registrierung erforderlich",
332
+ "security.notifications.mfaEmergencyBypass.body": "In Ihrer Organisation wurde eine MFA-Notfallumgehung erkannt. Der MFA-Schutz ist möglicherweise deaktiviert. Bitten Sie Ihren Systembetreiber, OM_SECURITY_MFA_EMERGENCY_BYPASS nach dem Notfall zu deaktivieren.",
333
+ "security.notifications.mfaEmergencyBypass.title": "MFA-Notfallumgehung erkannt",
332
334
  "security.notifications.mfaEnrolled.body": "{method} wurde für dein Konto aktiviert.",
333
335
  "security.notifications.mfaEnrolled.title": "MFA-Methode aktiviert",
334
336
  "security.notifications.mfaReset.body": "Ein Administrator hat deine MFA-Methoden zurückgesetzt. Grund: {reason}",
@@ -329,6 +329,8 @@
329
329
  "security.notifications.enforcementDeadline.bodyOverdue": "Your MFA enrollment deadline passed on {deadline}. Set up MFA immediately to keep account access.",
330
330
  "security.notifications.enforcementDeadline.bodyWithDeadline": "Your organization now requires MFA. Set it up within {days} day(s) before {deadline} to keep account access.",
331
331
  "security.notifications.enforcementDeadline.title": "MFA enrollment required",
332
+ "security.notifications.mfaEmergencyBypass.body": "MFA emergency bypass was detected in your organization. MFA protections may be disabled. Ask your system operator to disable OM_SECURITY_MFA_EMERGENCY_BYPASS after the emergency.",
333
+ "security.notifications.mfaEmergencyBypass.title": "MFA emergency bypass detected",
332
334
  "security.notifications.mfaEnrolled.body": "{method} was enabled for your account.",
333
335
  "security.notifications.mfaEnrolled.title": "MFA method enabled",
334
336
  "security.notifications.mfaReset.body": "An administrator reset your MFA methods. Reason: {reason}",
@@ -329,6 +329,8 @@
329
329
  "security.notifications.enforcementDeadline.bodyOverdue": "Tu plazo de inscripción en MFA venció el {deadline}. Configura MFA de inmediato para mantener el acceso a tu cuenta.",
330
330
  "security.notifications.enforcementDeadline.bodyWithDeadline": "Tu organización ahora requiere MFA. Configúralo dentro de {days} día(s) antes del {deadline} para mantener el acceso a tu cuenta.",
331
331
  "security.notifications.enforcementDeadline.title": "Se requiere inscripción en MFA",
332
+ "security.notifications.mfaEmergencyBypass.body": "Se detectó una omisión de emergencia de MFA en tu organización. La protección MFA puede estar desactivada. Pide al operador del sistema que desactive OM_SECURITY_MFA_EMERGENCY_BYPASS después de la emergencia.",
333
+ "security.notifications.mfaEmergencyBypass.title": "Se detectó una omisión de emergencia de MFA",
332
334
  "security.notifications.mfaEnrolled.body": "{method} fue habilitado para tu cuenta.",
333
335
  "security.notifications.mfaEnrolled.title": "Método MFA habilitado",
334
336
  "security.notifications.mfaReset.body": "Un administrador restableció tus métodos MFA. Motivo: {reason}",
@@ -329,6 +329,8 @@
329
329
  "security.notifications.enforcementDeadline.bodyOverdue": "MFA 등록 마감일인 {deadline}이(가) 지났습니다. 계정 접근을 유지하려면 즉시 MFA를 설정하세요.",
330
330
  "security.notifications.enforcementDeadline.bodyWithDeadline": "이제 조직에서 MFA를 요구합니다. 계정 접근을 유지하려면 {deadline} 전까지 {days}일 이내에 설정하세요.",
331
331
  "security.notifications.enforcementDeadline.title": "MFA 등록이 필요합니다",
332
+ "security.notifications.mfaEmergencyBypass.body": "조직에서 MFA 긴급 우회가 감지되었습니다. MFA 보호가 비활성화되어 있을 수 있습니다. 긴급 상황이 끝나면 시스템 운영자에게 OM_SECURITY_MFA_EMERGENCY_BYPASS를 비활성화하도록 요청하세요.",
333
+ "security.notifications.mfaEmergencyBypass.title": "MFA 긴급 우회 감지",
332
334
  "security.notifications.mfaEnrolled.body": "계정에 {method}이(가) 활성화되었습니다.",
333
335
  "security.notifications.mfaEnrolled.title": "MFA 방법이 활성화됨",
334
336
  "security.notifications.mfaReset.body": "관리자가 회원님의 MFA 방법을 재설정했습니다. 사유: {reason}",
@@ -329,6 +329,8 @@
329
329
  "security.notifications.enforcementDeadline.bodyOverdue": "Twój termin rejestracji MFA minął {deadline}. Skonfiguruj MFA natychmiast, aby zachować dostęp do konta.",
330
330
  "security.notifications.enforcementDeadline.bodyWithDeadline": "Twoja organizacja wymaga teraz MFA. Skonfiguruj je w ciągu {days} dnia/dni przed {deadline}, aby zachować dostęp do konta.",
331
331
  "security.notifications.enforcementDeadline.title": "Wymagana rejestracja MFA",
332
+ "security.notifications.mfaEmergencyBypass.body": "W Twojej organizacji wykryto awaryjne pominięcie MFA. Ochrona MFA może być wyłączona. Poproś operatora systemu o wyłączenie OM_SECURITY_MFA_EMERGENCY_BYPASS po zakończeniu sytuacji awaryjnej.",
333
+ "security.notifications.mfaEmergencyBypass.title": "Wykryto awaryjne pominięcie MFA",
332
334
  "security.notifications.mfaEnrolled.body": "Metoda {method} została włączona dla Twojego konta.",
333
335
  "security.notifications.mfaEnrolled.title": "Metoda MFA włączona",
334
336
  "security.notifications.mfaReset.body": "Administrator zresetował Twoje metody MFA. Powód: {reason}",
@@ -1,6 +1,6 @@
1
1
  import { createLogger } from "@open-mercato/shared/lib/logger";
2
2
  import { isEnforcementDeadlineOverdue } from "../services/MfaEnforcementService.js";
3
- import { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig } from "./security-config.js";
3
+ import { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig, requestMfaEmergencyBypassNotification } from "./security-config.js";
4
4
  const MFA_ENROLLMENT_PATH = "/backend/profile/security/mfa";
5
5
  const logger = createLogger("security").child({ component: "mfa-enforcement-redirect" });
6
6
  function resolveDeadlineRedirectState(deadline) {
@@ -56,6 +56,7 @@ async function resolveMfaEnrollmentRedirect(args) {
56
56
  pathname,
57
57
  reason: "enforcement-service-unavailable"
58
58
  });
59
+ void requestMfaEmergencyBypassNotification({ tenantId: auth.tenantId, organizationId: auth.orgId });
59
60
  return null;
60
61
  }
61
62
  logger.error("Unable to resolve MFA enforcement service; redirecting to enrollment", {
@@ -76,6 +77,7 @@ async function resolveMfaEnrollmentRedirect(args) {
76
77
  compliant: compliance.compliant,
77
78
  overdue: deadlineState.overdue
78
79
  });
80
+ void requestMfaEmergencyBypassNotification({ tenantId: auth.tenantId, organizationId: auth.orgId });
79
81
  return null;
80
82
  }
81
83
  return createEnrollmentRedirect(pathname, deadlineState.overdue);
@@ -87,6 +89,7 @@ async function resolveMfaEnrollmentRedirect(args) {
87
89
  pathname,
88
90
  reason: "compliance-check-failed"
89
91
  });
92
+ void requestMfaEmergencyBypassNotification({ tenantId: auth.tenantId, organizationId: auth.orgId });
90
93
  return null;
91
94
  }
92
95
  logger.error("Unable to verify MFA enforcement compliance; redirecting to enrollment", { err: error });
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/security/lib/enforcement-redirect.ts"],
4
- "sourcesContent": ["import type { AuthContext } from '@open-mercato/shared/lib/auth/server'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport { isEnforcementDeadlineOverdue } from '../services/MfaEnforcementService'\nimport { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig } from './security-config'\n\nconst MFA_ENROLLMENT_PATH = '/backend/profile/security/mfa'\nconst logger = createLogger('security').child({ component: 'mfa-enforcement-redirect' })\n\ntype MfaComplianceResult = {\n compliant: boolean\n enforced: boolean\n deadline?: Date\n}\n\ntype MfaEnforcementServiceLike = {\n checkUserCompliance: (userId: string) => Promise<MfaComplianceResult>\n}\n\ntype ServiceContainerLike = {\n resolve: (name: string) => unknown\n}\n\ntype EnforcementServiceResolution = {\n error?: unknown\n service: MfaEnforcementServiceLike | null\n}\n\nfunction resolveDeadlineRedirectState(deadline?: Date): {\n overdue: boolean\n shouldRedirect: boolean\n} {\n if (!(deadline instanceof Date) || Number.isNaN(deadline.getTime())) {\n return { overdue: false, shouldRedirect: true }\n }\n\n const overdue = isEnforcementDeadlineOverdue(deadline)\n return { overdue, shouldRedirect: overdue }\n}\n\nfunction isExemptPath(pathname: string): boolean {\n return pathname.startsWith('/backend/profile/security')\n}\n\nfunction createEnrollmentRedirect(pathname: string, overdue = false): string {\n const searchParams = new URLSearchParams({\n redirect: pathname,\n reason: 'mfa_enrollment_required',\n })\n if (overdue) {\n searchParams.set('overdue', '1')\n }\n return `${MFA_ENROLLMENT_PATH}?${searchParams.toString()}`\n}\n\nfunction canFailClosed(auth: NonNullable<AuthContext>): boolean {\n return typeof auth.tenantId === 'string' && auth.tenantId.length > 0\n}\n\nfunction resolveEnforcementService(\n container: ServiceContainerLike,\n): EnforcementServiceResolution {\n try {\n const resolved = container.resolve('mfaEnforcementService')\n if (\n !resolved\n || typeof resolved !== 'object'\n || typeof (resolved as { checkUserCompliance?: unknown }).checkUserCompliance !== 'function'\n ) {\n const receivedShape = resolved === null\n ? 'null'\n : Array.isArray(resolved)\n ? 'array'\n : typeof resolved === 'object'\n ? `object with keys: ${Object.keys(resolved).sort((left, right) => left.localeCompare(right)).join(', ') || '(none)'}`\n : typeof resolved\n return {\n error: new TypeError(`[internal] Malformed MFA enforcement service (${receivedShape}); expected checkUserCompliance()`),\n service: null,\n }\n }\n return { service: resolved as MfaEnforcementServiceLike }\n } catch (error) {\n return { error, service: null }\n }\n}\n\nexport async function resolveMfaEnrollmentRedirect(args: {\n auth: AuthContext\n pathname: string\n container: ServiceContainerLike\n}): Promise<string | null> {\n const { auth, pathname, container } = args\n if (!auth || typeof auth.sub !== 'string' || auth.sub.length === 0) return null\n if (auth.mfa_pending === true) return null\n if (isExemptPath(pathname)) return null\n\n const emergencyBypass = readSecurityModuleConfig().mfa.emergencyBypass\n\n const enforcementService = resolveEnforcementService(container)\n if (!enforcementService.service) {\n if (!canFailClosed(auth)) return null\n if (emergencyBypass) {\n emitMfaEmergencyBypassActiveWarning('mfa-enrollment-redirect bypassed', {\n userId: auth.sub,\n pathname,\n reason: 'enforcement-service-unavailable',\n })\n return null\n }\n logger.error('Unable to resolve MFA enforcement service; redirecting to enrollment', {\n err: enforcementService.error,\n })\n return createEnrollmentRedirect(pathname)\n }\n\n try {\n const compliance = await enforcementService.service.checkUserCompliance(auth.sub)\n if (!compliance.enforced || compliance.compliant) return null\n\n const deadlineState = resolveDeadlineRedirectState(compliance.deadline)\n if (!deadlineState.shouldRedirect) return null\n\n if (emergencyBypass) {\n emitMfaEmergencyBypassActiveWarning('mfa-enrollment-redirect bypassed', {\n userId: auth.sub,\n pathname,\n enforced: compliance.enforced,\n compliant: compliance.compliant,\n overdue: deadlineState.overdue,\n })\n return null\n }\n\n return createEnrollmentRedirect(pathname, deadlineState.overdue)\n } catch (error) {\n if (canFailClosed(auth)) {\n if (emergencyBypass) {\n emitMfaEmergencyBypassActiveWarning('mfa-enrollment-redirect bypassed', {\n userId: auth.sub,\n pathname,\n reason: 'compliance-check-failed',\n })\n return null\n }\n logger.error('Unable to verify MFA enforcement compliance; redirecting to enrollment', { err: error })\n return createEnrollmentRedirect(pathname)\n }\n return null\n }\n}\n\nexport default resolveMfaEnrollmentRedirect\n"],
5
- "mappings": "AACA,SAAS,oBAAoB;AAC7B,SAAS,oCAAoC;AAC7C,SAAS,qCAAqC,gCAAgC;AAE9E,MAAM,sBAAsB;AAC5B,MAAM,SAAS,aAAa,UAAU,EAAE,MAAM,EAAE,WAAW,2BAA2B,CAAC;AAqBvF,SAAS,6BAA6B,UAGpC;AACA,MAAI,EAAE,oBAAoB,SAAS,OAAO,MAAM,SAAS,QAAQ,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A,EACT;AACF;AAEA,IAAO,+BAAQ;",
4
+ "sourcesContent": ["import type { AuthContext } from '@open-mercato/shared/lib/auth/server'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport { isEnforcementDeadlineOverdue } from '../services/MfaEnforcementService'\nimport { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig, requestMfaEmergencyBypassNotification } from './security-config'\n\nconst MFA_ENROLLMENT_PATH = '/backend/profile/security/mfa'\nconst logger = createLogger('security').child({ component: 'mfa-enforcement-redirect' })\n\ntype MfaComplianceResult = {\n compliant: boolean\n enforced: boolean\n deadline?: Date\n}\n\ntype MfaEnforcementServiceLike = {\n checkUserCompliance: (userId: string) => Promise<MfaComplianceResult>\n}\n\ntype ServiceContainerLike = {\n resolve: (name: string) => unknown\n}\n\ntype EnforcementServiceResolution = {\n error?: unknown\n service: MfaEnforcementServiceLike | null\n}\n\nfunction resolveDeadlineRedirectState(deadline?: Date): {\n overdue: boolean\n shouldRedirect: boolean\n} {\n if (!(deadline instanceof Date) || Number.isNaN(deadline.getTime())) {\n return { overdue: false, shouldRedirect: true }\n }\n\n const overdue = isEnforcementDeadlineOverdue(deadline)\n return { overdue, shouldRedirect: overdue }\n}\n\nfunction isExemptPath(pathname: string): boolean {\n return pathname.startsWith('/backend/profile/security')\n}\n\nfunction createEnrollmentRedirect(pathname: string, overdue = false): string {\n const searchParams = new URLSearchParams({\n redirect: pathname,\n reason: 'mfa_enrollment_required',\n })\n if (overdue) {\n searchParams.set('overdue', '1')\n }\n return `${MFA_ENROLLMENT_PATH}?${searchParams.toString()}`\n}\n\nfunction canFailClosed(auth: NonNullable<AuthContext>): boolean {\n return typeof auth.tenantId === 'string' && auth.tenantId.length > 0\n}\n\nfunction resolveEnforcementService(\n container: ServiceContainerLike,\n): EnforcementServiceResolution {\n try {\n const resolved = container.resolve('mfaEnforcementService')\n if (\n !resolved\n || typeof resolved !== 'object'\n || typeof (resolved as { checkUserCompliance?: unknown }).checkUserCompliance !== 'function'\n ) {\n const receivedShape = resolved === null\n ? 'null'\n : Array.isArray(resolved)\n ? 'array'\n : typeof resolved === 'object'\n ? `object with keys: ${Object.keys(resolved).sort((left, right) => left.localeCompare(right)).join(', ') || '(none)'}`\n : typeof resolved\n return {\n error: new TypeError(`[internal] Malformed MFA enforcement service (${receivedShape}); expected checkUserCompliance()`),\n service: null,\n }\n }\n return { service: resolved as MfaEnforcementServiceLike }\n } catch (error) {\n return { error, service: null }\n }\n}\n\nexport async function resolveMfaEnrollmentRedirect(args: {\n auth: AuthContext\n pathname: string\n container: ServiceContainerLike\n}): Promise<string | null> {\n const { auth, pathname, container } = args\n if (!auth || typeof auth.sub !== 'string' || auth.sub.length === 0) return null\n if (auth.mfa_pending === true) return null\n if (isExemptPath(pathname)) return null\n\n const emergencyBypass = readSecurityModuleConfig().mfa.emergencyBypass\n\n const enforcementService = resolveEnforcementService(container)\n if (!enforcementService.service) {\n if (!canFailClosed(auth)) return null\n if (emergencyBypass) {\n emitMfaEmergencyBypassActiveWarning('mfa-enrollment-redirect bypassed', {\n userId: auth.sub,\n pathname,\n reason: 'enforcement-service-unavailable',\n })\n void requestMfaEmergencyBypassNotification({ tenantId: auth.tenantId, organizationId: auth.orgId })\n return null\n }\n logger.error('Unable to resolve MFA enforcement service; redirecting to enrollment', {\n err: enforcementService.error,\n })\n return createEnrollmentRedirect(pathname)\n }\n\n try {\n const compliance = await enforcementService.service.checkUserCompliance(auth.sub)\n if (!compliance.enforced || compliance.compliant) return null\n\n const deadlineState = resolveDeadlineRedirectState(compliance.deadline)\n if (!deadlineState.shouldRedirect) return null\n\n if (emergencyBypass) {\n emitMfaEmergencyBypassActiveWarning('mfa-enrollment-redirect bypassed', {\n userId: auth.sub,\n pathname,\n enforced: compliance.enforced,\n compliant: compliance.compliant,\n overdue: deadlineState.overdue,\n })\n void requestMfaEmergencyBypassNotification({ tenantId: auth.tenantId, organizationId: auth.orgId })\n return null\n }\n\n return createEnrollmentRedirect(pathname, deadlineState.overdue)\n } catch (error) {\n if (canFailClosed(auth)) {\n if (emergencyBypass) {\n emitMfaEmergencyBypassActiveWarning('mfa-enrollment-redirect bypassed', {\n userId: auth.sub,\n pathname,\n reason: 'compliance-check-failed',\n })\n void requestMfaEmergencyBypassNotification({ tenantId: auth.tenantId, organizationId: auth.orgId })\n return null\n }\n logger.error('Unable to verify MFA enforcement compliance; redirecting to enrollment', { err: error })\n return createEnrollmentRedirect(pathname)\n }\n return null\n }\n}\n\nexport default resolveMfaEnrollmentRedirect\n"],
5
+ "mappings": "AACA,SAAS,oBAAoB;AAC7B,SAAS,oCAAoC;AAC7C,SAAS,qCAAqC,0BAA0B,6CAA6C;AAErH,MAAM,sBAAsB;AAC5B,MAAM,SAAS,aAAa,UAAU,EAAE,MAAM,EAAE,WAAW,2BAA2B,CAAC;AAqBvF,SAAS,6BAA6B,UAGpC;AACA,MAAI,EAAE,oBAAoB,SAAS,OAAO,MAAM,SAAS,QAAQ,CAAC,GAAG;AACnE,WAAO,EAAE,SAAS,OAAO,gBAAgB,KAAK;AAAA,EAChD;AAEA,QAAM,UAAU,6BAA6B,QAAQ;AACrD,SAAO,EAAE,SAAS,gBAAgB,QAAQ;AAC5C;AAEA,SAAS,aAAa,UAA2B;AAC/C,SAAO,SAAS,WAAW,2BAA2B;AACxD;AAEA,SAAS,yBAAyB,UAAkB,UAAU,OAAe;AAC3E,QAAM,eAAe,IAAI,gBAAgB;AAAA,IACvC,UAAU;AAAA,IACV,QAAQ;AAAA,EACV,CAAC;AACD,MAAI,SAAS;AACX,iBAAa,IAAI,WAAW,GAAG;AAAA,EACjC;AACA,SAAO,GAAG,mBAAmB,IAAI,aAAa,SAAS,CAAC;AAC1D;AAEA,SAAS,cAAc,MAAyC;AAC9D,SAAO,OAAO,KAAK,aAAa,YAAY,KAAK,SAAS,SAAS;AACrE;AAEA,SAAS,0BACP,WAC8B;AAC9B,MAAI;AACF,UAAM,WAAW,UAAU,QAAQ,uBAAuB;AAC1D,QACE,CAAC,YACE,OAAO,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G;AAChF,QAAI,CAAC,WAAW,YAAY,WAAW,UAAW,QAAO;AAEzD,UAAM,gBAAgB,6BAA6B,WAAW,QAAQ;AACtE,QAAI,CAAC,cAAc,eAAgB,QAAO;AAE1C,QAAI,iBAAiB;AACnB,0CAAoC,oCAAoC;AAAA,QACtE,QAAQ,KAAK;AAAA,QACb;AAAA,QACA,UAAU,WAAW;AAAA,QACrB,WAAW,WAAW;AAAA,QACtB,SAAS,cAAc;AAAA,MACzB,CAAC;AACD,WAAK,sCAAsC,EAAE,UAAU,KAAK,UAAU,gBAAgB,KAAK,MAAM,CAAC;AAClG,aAAO;AAAA,IACT;AAEA,WAAO,yBAAyB,UAAU,cAAc,OAAO;AAAA,EACjE,SAAS,OAAO;AACd,QAAI,cAAc,IAAI,GAAG;AACvB,UAAI,iBAAiB;AACnB,4CAAoC,oCAAoC;AAAA,UACtE,QAAQ,KAAK;AAAA,UACb;AAAA,UACA,QAAQ;AAAA,QACV,CAAC;AACD,aAAK,sCAAsC,EAAE,UAAU,KAAK,UAAU,gBAAgB,KAAK,MAAM,CAAC;AAClG,eAAO;AAAA,MACT;AACA,aAAO,MAAM,0EAA0E,EAAE,KAAK,MAAM,CAAC;AACrG,aAAO,yBAAyB,QAAQ;AAAA,IAC1C;AACA,WAAO;AAAA,EACT;AACF;AAEA,IAAO,+BAAQ;",
6
6
  "names": []
7
7
  }
@@ -1,11 +1,14 @@
1
1
  import { parseBooleanWithDefault } from "@open-mercato/shared/lib/boolean";
2
2
  import { createLogger } from "@open-mercato/shared/lib/logger";
3
+ import { getTelemetryRuntime } from "@open-mercato/shared/lib/telemetry/runtime";
4
+ import { emitSecurityEvent } from "../events.js";
3
5
  const TOTP_PERIOD_SECONDS = 30;
4
6
  const TOTP_DIGITS = 6;
5
7
  const DEFAULT_SETUP_TTL_MS = 10 * 60 * 1e3;
6
8
  const DEFAULT_WEBAUTHN_CHALLENGE_TTL_MS = 5 * 60 * 1e3;
7
9
  const DEFAULT_SUDO_PENDING_CHALLENGE_TTL_MS = 10 * 60 * 1e3;
8
10
  const MIN_SUDO_TTL_SECONDS = 30;
11
+ const MFA_BYPASS_NOTIFICATION_ENQUEUE_TIMEOUT_MS = 2e3;
9
12
  const securityBypassLogger = createLogger("security").child({ component: "mfa-emergency-bypass" });
10
13
  let startupBypassWarningEmitted = false;
11
14
  const defaultSecurityModuleConfig = {
@@ -120,6 +123,31 @@ function emitMfaEmergencyBypassActiveWarning(context, extra) {
120
123
  ...extra
121
124
  });
122
125
  }
126
+ async function requestMfaEmergencyBypassNotification(scope) {
127
+ if (!scope.tenantId || !scope.organizationId) return;
128
+ const { tenantId, organizationId } = scope;
129
+ let timeout;
130
+ try {
131
+ await Promise.race([
132
+ emitSecurityEvent("security.mfa.emergency_bypass_detected", {
133
+ tenantId,
134
+ organizationId,
135
+ observedAt: (/* @__PURE__ */ new Date()).toISOString()
136
+ }, { persistent: true, tenantId, organizationId }),
137
+ new Promise((_resolve, reject) => {
138
+ timeout = setTimeout(() => reject(new Error("[internal] MFA bypass notification enqueue timed out")), MFA_BYPASS_NOTIFICATION_ENQUEUE_TIMEOUT_MS);
139
+ })
140
+ ]);
141
+ } catch (error) {
142
+ try {
143
+ securityBypassLogger.error("Unable to enqueue MFA emergency bypass notification", { err: error });
144
+ getTelemetryRuntime()?.reportError(error, { module: "security", code: "security.mfa_bypass_notification_failed" });
145
+ } catch {
146
+ }
147
+ } finally {
148
+ if (timeout) clearTimeout(timeout);
149
+ }
150
+ }
123
151
  function emitMfaEmergencyBypassStartupWarningIfNeeded(env = process.env) {
124
152
  if (startupBypassWarningEmitted) return;
125
153
  const bypass = parseBooleanWithDefault(
@@ -238,6 +266,7 @@ export {
238
266
  emitMfaEmergencyBypassStartupWarningIfNeeded,
239
267
  readSecurityModuleConfig,
240
268
  readSecuritySetupTokenSecret,
269
+ requestMfaEmergencyBypassNotification,
241
270
  resetMfaEmergencyBypassStartupWarningForTests,
242
271
  resolveSecurityModuleConfigForRequest
243
272
  };
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/security/lib/security-config.ts"],
4
- "sourcesContent": ["import { parseBooleanWithDefault } from '@open-mercato/shared/lib/boolean'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\n\nconst TOTP_PERIOD_SECONDS = 30\nconst TOTP_DIGITS = 6\nconst DEFAULT_SETUP_TTL_MS = 10 * 60 * 1000\nconst DEFAULT_WEBAUTHN_CHALLENGE_TTL_MS = 5 * 60 * 1000\nconst DEFAULT_SUDO_PENDING_CHALLENGE_TTL_MS = 10 * 60 * 1000\nconst MIN_SUDO_TTL_SECONDS = 30\n\nconst securityBypassLogger = createLogger('security').child({ component: 'mfa-emergency-bypass' })\nlet startupBypassWarningEmitted = false\n\nexport type SecurityModuleConfig = {\n totp: {\n issuer: string\n window: number\n digits: number\n periodSeconds: number\n setupTtlMs: number\n }\n otpEmail: {\n expirySeconds: number\n maxAttempts: number\n setupTtlMs: number\n challengeTtlMs: number\n subject: string\n }\n mfa: {\n challengeTtlMs: number\n maxAttempts: number\n emergencyBypass: boolean\n }\n sudo: {\n minTtlSeconds: number\n defaultTtlSeconds: number\n maxTtlSeconds: number\n pendingChallengeTtlMs: number\n }\n webauthn: {\n rpName: string\n rpId: string\n expectedOrigins: string[]\n setupTtlMs: number\n challengeTtlMs: number\n }\n recoveryCodes: {\n count: number\n bcryptCost: number\n }\n}\n\nexport const defaultSecurityModuleConfig: SecurityModuleConfig = {\n totp: {\n issuer: 'Open Mercato',\n window: 1,\n digits: TOTP_DIGITS,\n periodSeconds: TOTP_PERIOD_SECONDS,\n setupTtlMs: DEFAULT_SETUP_TTL_MS,\n },\n otpEmail: {\n expirySeconds: 600,\n maxAttempts: 5,\n setupTtlMs: DEFAULT_SETUP_TTL_MS,\n challengeTtlMs: 600 * 1000,\n subject: 'Your Open Mercato verification code',\n },\n mfa: {\n challengeTtlMs: 600 * 1000,\n maxAttempts: 5,\n emergencyBypass: false,\n },\n sudo: {\n minTtlSeconds: MIN_SUDO_TTL_SECONDS,\n defaultTtlSeconds: 300,\n maxTtlSeconds: 1800,\n pendingChallengeTtlMs: DEFAULT_SUDO_PENDING_CHALLENGE_TTL_MS,\n },\n webauthn: {\n rpName: 'Open Mercato',\n rpId: 'localhost',\n expectedOrigins: ['http://localhost:3000'],\n setupTtlMs: DEFAULT_SETUP_TTL_MS,\n challengeTtlMs: DEFAULT_WEBAUTHN_CHALLENGE_TTL_MS,\n },\n recoveryCodes: {\n count: 10,\n bcryptCost: 10,\n },\n}\n\nexport function readSecurityModuleConfig(\n env: NodeJS.ProcessEnv = process.env,\n): SecurityModuleConfig {\n const otpExpirySeconds = parsePositiveInteger(env.OM_SECURITY_OTP_EXPIRY_SECONDS)\n ?? defaultSecurityModuleConfig.otpEmail.expirySeconds\n const otpMaxAttempts = parsePositiveInteger(env.OM_SECURITY_OTP_MAX_ATTEMPTS)\n ?? defaultSecurityModuleConfig.otpEmail.maxAttempts\n const sudoMaxTtlSeconds = Math.max(\n parsePositiveInteger(env.OM_SECURITY_SUDO_MAX_TTL) ?? defaultSecurityModuleConfig.sudo.maxTtlSeconds,\n defaultSecurityModuleConfig.sudo.minTtlSeconds,\n )\n const sudoDefaultTtlSeconds = clamp(\n parsePositiveInteger(env.OM_SECURITY_SUDO_DEFAULT_TTL) ?? defaultSecurityModuleConfig.sudo.defaultTtlSeconds,\n defaultSecurityModuleConfig.sudo.minTtlSeconds,\n sudoMaxTtlSeconds,\n )\n const webauthnRpId = readText(env.OM_SECURITY_WEBAUTHN_RP_ID)\n ?? readHostname(env.APP_URL)\n ?? readHostname(env.NEXT_PUBLIC_APP_URL)\n ?? defaultSecurityModuleConfig.webauthn.rpId\n\n return {\n totp: {\n ...defaultSecurityModuleConfig.totp,\n issuer: readText(env.OM_SECURITY_TOTP_ISSUER) ?? defaultSecurityModuleConfig.totp.issuer,\n window: parseNonNegativeInteger(env.OM_SECURITY_TOTP_WINDOW) ?? defaultSecurityModuleConfig.totp.window,\n },\n otpEmail: {\n ...defaultSecurityModuleConfig.otpEmail,\n expirySeconds: otpExpirySeconds,\n maxAttempts: otpMaxAttempts,\n challengeTtlMs: otpExpirySeconds * 1000,\n },\n mfa: {\n challengeTtlMs: otpExpirySeconds * 1000,\n maxAttempts: otpMaxAttempts,\n emergencyBypass: parseBooleanWithDefault(\n env.OM_SECURITY_MFA_EMERGENCY_BYPASS,\n defaultSecurityModuleConfig.mfa.emergencyBypass,\n ),\n },\n sudo: {\n ...defaultSecurityModuleConfig.sudo,\n defaultTtlSeconds: sudoDefaultTtlSeconds,\n maxTtlSeconds: sudoMaxTtlSeconds,\n },\n webauthn: {\n ...defaultSecurityModuleConfig.webauthn,\n rpName: readText(env.OM_SECURITY_WEBAUTHN_RP_NAME) ?? defaultSecurityModuleConfig.webauthn.rpName,\n rpId: webauthnRpId,\n expectedOrigins: readWebAuthnOrigins(env, webauthnRpId),\n },\n recoveryCodes: {\n ...defaultSecurityModuleConfig.recoveryCodes,\n count: parsePositiveInteger(env.OM_SECURITY_RECOVERY_CODE_COUNT)\n ?? parsePositiveInteger(env.OM_SECURITY_RECOVERY_CODES_COUNT)\n ?? defaultSecurityModuleConfig.recoveryCodes.count,\n },\n }\n}\n\nexport function resolveSecurityModuleConfigForRequest(\n baseConfig: SecurityModuleConfig,\n request?: Request,\n env: NodeJS.ProcessEnv = process.env,\n): SecurityModuleConfig {\n const explicitRpId = readText(env.OM_SECURITY_WEBAUTHN_RP_ID)\n const requestRpId = readRequestHostname(request)\n const rpId = explicitRpId ?? requestRpId ?? baseConfig.webauthn.rpId\n const expectedOrigins = readWebAuthnOrigins(env, rpId, request, baseConfig.webauthn.expectedOrigins)\n\n if (rpId === baseConfig.webauthn.rpId && areOriginsEqual(expectedOrigins, baseConfig.webauthn.expectedOrigins)) {\n return baseConfig\n }\n\n return {\n ...baseConfig,\n webauthn: {\n ...baseConfig.webauthn,\n rpId,\n expectedOrigins,\n },\n }\n}\n\nexport function emitMfaEmergencyBypassActiveWarning(context: string, extra?: Record<string, unknown>): void {\n securityBypassLogger.warn('MFA emergency bypass is active', {\n emergencyBypass: true,\n context,\n ...extra,\n })\n}\n\nexport function emitMfaEmergencyBypassStartupWarningIfNeeded(env: NodeJS.ProcessEnv = process.env): void {\n if (startupBypassWarningEmitted) return\n const bypass = parseBooleanWithDefault(\n env.OM_SECURITY_MFA_EMERGENCY_BYPASS,\n defaultSecurityModuleConfig.mfa.emergencyBypass,\n )\n if (!bypass) return\n startupBypassWarningEmitted = true\n emitMfaEmergencyBypassActiveWarning('startup', { startup: true })\n}\n\nexport function resetMfaEmergencyBypassStartupWarningForTests(): void {\n startupBypassWarningEmitted = false\n}\n\nemitMfaEmergencyBypassStartupWarningIfNeeded()\n\nexport function readSecuritySetupTokenSecret(env: NodeJS.ProcessEnv = process.env): string {\n const secret = readText(env.OM_SECURITY_MFA_SETUP_SECRET)\n ?? readText(env.AUTH_JWT_SECRET)\n ?? readText(env.AUTH_SECRET)\n ?? readText(env.JWT_SECRET)\n\n if (secret) {\n return secret\n }\n\n throw new Error(\n 'Security MFA setup tokens require OM_SECURITY_MFA_SETUP_SECRET, AUTH_JWT_SECRET, AUTH_SECRET, or JWT_SECRET.',\n )\n}\n\nfunction readWebAuthnOrigins(\n env: NodeJS.ProcessEnv,\n rpId: string,\n request?: Request,\n fallbackOrigins?: string[],\n): string[] {\n const fromList = (env.OM_SECURITY_WEBAUTHN_ORIGINS ?? '')\n .split(',')\n .map((value) => value.trim())\n .filter((value) => value.length > 0)\n const explicitOrigin = readText(env.OM_SECURITY_WEBAUTHN_ORIGIN)\n\n if (fromList.length > 0 || explicitOrigin) {\n return [...new Set([...fromList, ...(explicitOrigin ? [explicitOrigin] : [])])]\n }\n\n const requestOrigin = readRequestOrigin(request)\n const singletons = [\n requestOrigin,\n env.APP_URL,\n env.NEXT_PUBLIC_APP_URL,\n ]\n .map((value) => readText(value))\n .filter((value): value is string => typeof value === 'string')\n\n const origins = [...new Set([...fromList, ...singletons])]\n if (origins.length > 0) {\n return origins\n }\n\n if (fallbackOrigins && fallbackOrigins.length > 0) {\n return [...fallbackOrigins]\n }\n\n if (rpId === 'localhost') {\n return [...defaultSecurityModuleConfig.webauthn.expectedOrigins]\n }\n\n return [`https://${rpId}`]\n}\n\nfunction readText(value: string | undefined): string | undefined {\n const trimmed = value?.trim()\n return trimmed ? trimmed : undefined\n}\n\nfunction readHostname(value: string | undefined): string | undefined {\n const trimmed = readText(value)\n if (!trimmed) return undefined\n\n try {\n const url = new URL(trimmed)\n return readText(url.hostname)\n } catch {\n return undefined\n }\n}\n\nfunction readRequestHostname(request: Request | undefined): string | undefined {\n const url = readRequestUrl(request)\n return url ? readText(url.hostname) : undefined\n}\n\nfunction readRequestOrigin(request: Request | undefined): string | undefined {\n const url = readRequestUrl(request)\n return url ? readText(url.origin) : undefined\n}\n\nfunction readRequestUrl(request: Request | undefined): URL | null {\n if (!request) return null\n\n const forwardedHost = readForwardedHeader(request.headers.get('x-forwarded-host'))\n const forwardedProto = readForwardedHeader(request.headers.get('x-forwarded-proto'))\n if (forwardedHost) {\n const protocol = forwardedProto ?? 'https'\n try {\n return new URL(`${protocol}://${forwardedHost}`)\n } catch {\n return null\n }\n }\n\n try {\n return new URL(request.url)\n } catch {\n return null\n }\n}\n\nfunction readForwardedHeader(value: string | null): string | undefined {\n const trimmed = readText(value ?? undefined)\n if (!trimmed) return undefined\n const [first] = trimmed.split(',')\n return readText(first)\n}\n\nfunction areOriginsEqual(left: string[], right: string[]): boolean {\n if (left.length !== right.length) return false\n return left.every((value, index) => value === right[index])\n}\n\nfunction parsePositiveInteger(value: string | undefined): number | undefined {\n if (!value) return undefined\n const parsed = Number(value)\n if (!Number.isFinite(parsed) || parsed <= 0) return undefined\n return Math.trunc(parsed)\n}\n\nfunction parseNonNegativeInteger(value: string | undefined): number | undefined {\n if (!value) return undefined\n const parsed = Number(value)\n if (!Number.isFinite(parsed) || parsed < 0) return undefined\n return Math.trunc(parsed)\n}\n\nfunction clamp(value: number, min: number, max: number): number {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AAI,KAAK,IAAI,OAAO,GAAG,GAAG,GAAG;AAC3C;",
4
+ "sourcesContent": ["import { parseBooleanWithDefault } from '@open-mercato/shared/lib/boolean'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport { getTelemetryRuntime } from '@open-mercato/shared/lib/telemetry/runtime'\nimport { emitSecurityEvent } from '../events'\n\nconst TOTP_PERIOD_SECONDS = 30\nconst TOTP_DIGITS = 6\nconst DEFAULT_SETUP_TTL_MS = 10 * 60 * 1000\nconst DEFAULT_WEBAUTHN_CHALLENGE_TTL_MS = 5 * 60 * 1000\nconst DEFAULT_SUDO_PENDING_CHALLENGE_TTL_MS = 10 * 60 * 1000\nconst MIN_SUDO_TTL_SECONDS = 30\nconst MFA_BYPASS_NOTIFICATION_ENQUEUE_TIMEOUT_MS = 2_000\n\nconst securityBypassLogger = createLogger('security').child({ component: 'mfa-emergency-bypass' })\nlet startupBypassWarningEmitted = false\n\nexport type SecurityModuleConfig = {\n totp: {\n issuer: string\n window: number\n digits: number\n periodSeconds: number\n setupTtlMs: number\n }\n otpEmail: {\n expirySeconds: number\n maxAttempts: number\n setupTtlMs: number\n challengeTtlMs: number\n subject: string\n }\n mfa: {\n challengeTtlMs: number\n maxAttempts: number\n emergencyBypass: boolean\n }\n sudo: {\n minTtlSeconds: number\n defaultTtlSeconds: number\n maxTtlSeconds: number\n pendingChallengeTtlMs: number\n }\n webauthn: {\n rpName: string\n rpId: string\n expectedOrigins: string[]\n setupTtlMs: number\n challengeTtlMs: number\n }\n recoveryCodes: {\n count: number\n bcryptCost: number\n }\n}\n\nexport const defaultSecurityModuleConfig: SecurityModuleConfig = {\n totp: {\n issuer: 'Open Mercato',\n window: 1,\n digits: TOTP_DIGITS,\n periodSeconds: TOTP_PERIOD_SECONDS,\n setupTtlMs: DEFAULT_SETUP_TTL_MS,\n },\n otpEmail: {\n expirySeconds: 600,\n maxAttempts: 5,\n setupTtlMs: DEFAULT_SETUP_TTL_MS,\n challengeTtlMs: 600 * 1000,\n subject: 'Your Open Mercato verification code',\n },\n mfa: {\n challengeTtlMs: 600 * 1000,\n maxAttempts: 5,\n emergencyBypass: false,\n },\n sudo: {\n minTtlSeconds: MIN_SUDO_TTL_SECONDS,\n defaultTtlSeconds: 300,\n maxTtlSeconds: 1800,\n pendingChallengeTtlMs: DEFAULT_SUDO_PENDING_CHALLENGE_TTL_MS,\n },\n webauthn: {\n rpName: 'Open Mercato',\n rpId: 'localhost',\n expectedOrigins: ['http://localhost:3000'],\n setupTtlMs: DEFAULT_SETUP_TTL_MS,\n challengeTtlMs: DEFAULT_WEBAUTHN_CHALLENGE_TTL_MS,\n },\n recoveryCodes: {\n count: 10,\n bcryptCost: 10,\n },\n}\n\nexport function readSecurityModuleConfig(\n env: NodeJS.ProcessEnv = process.env,\n): SecurityModuleConfig {\n const otpExpirySeconds = parsePositiveInteger(env.OM_SECURITY_OTP_EXPIRY_SECONDS)\n ?? defaultSecurityModuleConfig.otpEmail.expirySeconds\n const otpMaxAttempts = parsePositiveInteger(env.OM_SECURITY_OTP_MAX_ATTEMPTS)\n ?? defaultSecurityModuleConfig.otpEmail.maxAttempts\n const sudoMaxTtlSeconds = Math.max(\n parsePositiveInteger(env.OM_SECURITY_SUDO_MAX_TTL) ?? defaultSecurityModuleConfig.sudo.maxTtlSeconds,\n defaultSecurityModuleConfig.sudo.minTtlSeconds,\n )\n const sudoDefaultTtlSeconds = clamp(\n parsePositiveInteger(env.OM_SECURITY_SUDO_DEFAULT_TTL) ?? defaultSecurityModuleConfig.sudo.defaultTtlSeconds,\n defaultSecurityModuleConfig.sudo.minTtlSeconds,\n sudoMaxTtlSeconds,\n )\n const webauthnRpId = readText(env.OM_SECURITY_WEBAUTHN_RP_ID)\n ?? readHostname(env.APP_URL)\n ?? readHostname(env.NEXT_PUBLIC_APP_URL)\n ?? defaultSecurityModuleConfig.webauthn.rpId\n\n return {\n totp: {\n ...defaultSecurityModuleConfig.totp,\n issuer: readText(env.OM_SECURITY_TOTP_ISSUER) ?? defaultSecurityModuleConfig.totp.issuer,\n window: parseNonNegativeInteger(env.OM_SECURITY_TOTP_WINDOW) ?? defaultSecurityModuleConfig.totp.window,\n },\n otpEmail: {\n ...defaultSecurityModuleConfig.otpEmail,\n expirySeconds: otpExpirySeconds,\n maxAttempts: otpMaxAttempts,\n challengeTtlMs: otpExpirySeconds * 1000,\n },\n mfa: {\n challengeTtlMs: otpExpirySeconds * 1000,\n maxAttempts: otpMaxAttempts,\n emergencyBypass: parseBooleanWithDefault(\n env.OM_SECURITY_MFA_EMERGENCY_BYPASS,\n defaultSecurityModuleConfig.mfa.emergencyBypass,\n ),\n },\n sudo: {\n ...defaultSecurityModuleConfig.sudo,\n defaultTtlSeconds: sudoDefaultTtlSeconds,\n maxTtlSeconds: sudoMaxTtlSeconds,\n },\n webauthn: {\n ...defaultSecurityModuleConfig.webauthn,\n rpName: readText(env.OM_SECURITY_WEBAUTHN_RP_NAME) ?? defaultSecurityModuleConfig.webauthn.rpName,\n rpId: webauthnRpId,\n expectedOrigins: readWebAuthnOrigins(env, webauthnRpId),\n },\n recoveryCodes: {\n ...defaultSecurityModuleConfig.recoveryCodes,\n count: parsePositiveInteger(env.OM_SECURITY_RECOVERY_CODE_COUNT)\n ?? parsePositiveInteger(env.OM_SECURITY_RECOVERY_CODES_COUNT)\n ?? defaultSecurityModuleConfig.recoveryCodes.count,\n },\n }\n}\n\nexport function resolveSecurityModuleConfigForRequest(\n baseConfig: SecurityModuleConfig,\n request?: Request,\n env: NodeJS.ProcessEnv = process.env,\n): SecurityModuleConfig {\n const explicitRpId = readText(env.OM_SECURITY_WEBAUTHN_RP_ID)\n const requestRpId = readRequestHostname(request)\n const rpId = explicitRpId ?? requestRpId ?? baseConfig.webauthn.rpId\n const expectedOrigins = readWebAuthnOrigins(env, rpId, request, baseConfig.webauthn.expectedOrigins)\n\n if (rpId === baseConfig.webauthn.rpId && areOriginsEqual(expectedOrigins, baseConfig.webauthn.expectedOrigins)) {\n return baseConfig\n }\n\n return {\n ...baseConfig,\n webauthn: {\n ...baseConfig.webauthn,\n rpId,\n expectedOrigins,\n },\n }\n}\n\nexport function emitMfaEmergencyBypassActiveWarning(context: string, extra?: Record<string, unknown>): void {\n securityBypassLogger.warn('MFA emergency bypass is active', {\n emergencyBypass: true,\n context,\n ...extra,\n })\n}\n\nexport async function requestMfaEmergencyBypassNotification(scope: {\n tenantId?: string | null\n organizationId?: string | null\n}): Promise<void> {\n if (!scope.tenantId || !scope.organizationId) return\n const { tenantId, organizationId } = scope\n let timeout: ReturnType<typeof setTimeout> | undefined\n try {\n await Promise.race([\n emitSecurityEvent('security.mfa.emergency_bypass_detected', {\n tenantId,\n organizationId,\n observedAt: new Date().toISOString(),\n }, { persistent: true, tenantId, organizationId }),\n new Promise<never>((_resolve, reject) => {\n timeout = setTimeout(() => reject(new Error('[internal] MFA bypass notification enqueue timed out')), MFA_BYPASS_NOTIFICATION_ENQUEUE_TIMEOUT_MS)\n }),\n ])\n } catch (error) {\n try {\n securityBypassLogger.error('Unable to enqueue MFA emergency bypass notification', { err: error })\n getTelemetryRuntime()?.reportError(error, { module: 'security', code: 'security.mfa_bypass_notification_failed' })\n } catch {}\n } finally {\n if (timeout) clearTimeout(timeout)\n }\n}\n\nexport function emitMfaEmergencyBypassStartupWarningIfNeeded(env: NodeJS.ProcessEnv = process.env): void {\n if (startupBypassWarningEmitted) return\n const bypass = parseBooleanWithDefault(\n env.OM_SECURITY_MFA_EMERGENCY_BYPASS,\n defaultSecurityModuleConfig.mfa.emergencyBypass,\n )\n if (!bypass) return\n startupBypassWarningEmitted = true\n emitMfaEmergencyBypassActiveWarning('startup', { startup: true })\n}\n\nexport function resetMfaEmergencyBypassStartupWarningForTests(): void {\n startupBypassWarningEmitted = false\n}\n\nemitMfaEmergencyBypassStartupWarningIfNeeded()\n\nexport function readSecuritySetupTokenSecret(env: NodeJS.ProcessEnv = process.env): string {\n const secret = readText(env.OM_SECURITY_MFA_SETUP_SECRET)\n ?? readText(env.AUTH_JWT_SECRET)\n ?? readText(env.AUTH_SECRET)\n ?? readText(env.JWT_SECRET)\n\n if (secret) {\n return secret\n }\n\n throw new Error(\n 'Security MFA setup tokens require OM_SECURITY_MFA_SETUP_SECRET, AUTH_JWT_SECRET, AUTH_SECRET, or JWT_SECRET.',\n )\n}\n\nfunction readWebAuthnOrigins(\n env: NodeJS.ProcessEnv,\n rpId: string,\n request?: Request,\n fallbackOrigins?: string[],\n): string[] {\n const fromList = (env.OM_SECURITY_WEBAUTHN_ORIGINS ?? '')\n .split(',')\n .map((value) => value.trim())\n .filter((value) => value.length > 0)\n const explicitOrigin = readText(env.OM_SECURITY_WEBAUTHN_ORIGIN)\n\n if (fromList.length > 0 || explicitOrigin) {\n return [...new Set([...fromList, ...(explicitOrigin ? [explicitOrigin] : [])])]\n }\n\n const requestOrigin = readRequestOrigin(request)\n const singletons = [\n requestOrigin,\n env.APP_URL,\n env.NEXT_PUBLIC_APP_URL,\n ]\n .map((value) => readText(value))\n .filter((value): value is string => typeof value === 'string')\n\n const origins = [...new Set([...fromList, ...singletons])]\n if (origins.length > 0) {\n return origins\n }\n\n if (fallbackOrigins && fallbackOrigins.length > 0) {\n return [...fallbackOrigins]\n }\n\n if (rpId === 'localhost') {\n return [...defaultSecurityModuleConfig.webauthn.expectedOrigins]\n }\n\n return [`https://${rpId}`]\n}\n\nfunction readText(value: string | undefined): string | undefined {\n const trimmed = value?.trim()\n return trimmed ? trimmed : undefined\n}\n\nfunction readHostname(value: string | undefined): string | undefined {\n const trimmed = readText(value)\n if (!trimmed) return undefined\n\n try {\n const url = new URL(trimmed)\n return readText(url.hostname)\n } catch {\n return undefined\n }\n}\n\nfunction readRequestHostname(request: Request | undefined): string | undefined {\n const url = readRequestUrl(request)\n return url ? readText(url.hostname) : undefined\n}\n\nfunction readRequestOrigin(request: Request | undefined): string | undefined {\n const url = readRequestUrl(request)\n return url ? readText(url.origin) : undefined\n}\n\nfunction readRequestUrl(request: Request | undefined): URL | null {\n if (!request) return null\n\n const forwardedHost = readForwardedHeader(request.headers.get('x-forwarded-host'))\n const forwardedProto = readForwardedHeader(request.headers.get('x-forwarded-proto'))\n if (forwardedHost) {\n const protocol = forwardedProto ?? 'https'\n try {\n return new URL(`${protocol}://${forwardedHost}`)\n } catch {\n return null\n }\n }\n\n try {\n return new URL(request.url)\n } catch {\n return null\n }\n}\n\nfunction readForwardedHeader(value: string | null): string | undefined {\n const trimmed = readText(value ?? undefined)\n if (!trimmed) return undefined\n const [first] = trimmed.split(',')\n return readText(first)\n}\n\nfunction areOriginsEqual(left: string[], right: string[]): boolean {\n if (left.length !== right.length) return false\n return left.every((value, index) => value === right[index])\n}\n\nfunction parsePositiveInteger(value: string | undefined): number | undefined {\n if (!value) return undefined\n const parsed = Number(value)\n if (!Number.isFinite(parsed) || parsed <= 0) return undefined\n return Math.trunc(parsed)\n}\n\nfunction parseNonNegativeInteger(value: string | undefined): number | undefined {\n if (!value) return undefined\n const parsed = Number(value)\n if (!Number.isFinite(parsed) || parsed < 0) return undefined\n return Math.trunc(parsed)\n}\n\nfunction clamp(value: number, min: number, max: number): number {\n return Math.min(Math.max(value, min), max)\n}\n"],
5
+ "mappings": "AAAA,SAAS,+BAA+B;AACxC,SAAS,oBAAoB;AAC7B,SAAS,2BAA2B;AACpC,SAAS,yBAAyB;AAElC,MAAM,sBAAsB;AAC5B,MAAM,cAAc;AACpB,MAAM,uBAAuB,KAAK,KAAK;AACvC,MAAM,oCAAoC,IAAI,KAAK;AACnD,MAAM,wCAAwC,KAAK,KAAK;AACxD,MAAM,uBAAuB;AAC7B,MAAM,6CAA6C;AAEnD,MAAM,uBAAuB,aAAa,UAAU,EAAE,MAAM,EAAE,WAAW,uBAAuB,CAAC;AACjG,IAAI,8BAA8B;AAyC3B,MAAM,8BAAoD;AAAA,EAC/D,MAAM;AAAA,IACJ,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,eAAe;AAAA,IACf,YAAY;AAAA,EACd;AAAA,EACA,UAAU;A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;AAC9E,MAAI,CAAC,MAAO,QAAO;AACnB,QAAM,SAAS,OAAO,KAAK;AAC3B,MAAI,CAAC,OAAO,SAAS,MAAM,KAAK,SAAS,EAAG,QAAO;AACnD,SAAO,KAAK,MAAM,MAAM;AAC1B;AAEA,SAAS,MAAM,OAAe,KAAa,KAAqB;AAC9D,SAAO,KAAK,IAAI,KAAK,IAAI,OAAO,GAAG,GAAG,GAAG;AAC3C;",
6
6
  "names": []
7
7
  }
@@ -1,4 +1,16 @@
1
1
  const notificationTypes = [
2
+ {
3
+ type: "security.mfa.emergency_bypass_detected",
4
+ module: "security",
5
+ category: "auth",
6
+ channels: ["in_app"],
7
+ titleKey: "security.notifications.mfaEmergencyBypass.title",
8
+ bodyKey: "security.notifications.mfaEmergencyBypass.body",
9
+ icon: "shield-alert",
10
+ severity: "warning",
11
+ actions: [],
12
+ expiresAfterHours: 168
13
+ },
2
14
  {
3
15
  type: "security.password.changed",
4
16
  module: "security",
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../src/modules/security/notifications.ts"],
4
- "sourcesContent": ["import type { NotificationTypeDefinition } from '@open-mercato/shared/modules/notifications/types'\n\nexport const notificationTypes: NotificationTypeDefinition[] = [\n {\n type: 'security.password.changed',\n module: 'security',\n category: 'auth',\n channels: ['in_app', 'email'],\n titleKey: 'security.notifications.passwordChanged.title',\n bodyKey: 'security.notifications.passwordChanged.body',\n icon: 'key-round',\n severity: 'success',\n actions: [\n {\n id: 'open_security_settings',\n labelKey: 'common.view',\n variant: 'outline',\n href: '/backend/profile/security',\n icon: 'external-link',\n },\n ],\n linkHref: '/backend/profile/security',\n expiresAfterHours: 168,\n },\n {\n type: 'security.mfa.enrolled',\n module: 'security',\n category: 'auth',\n channels: ['in_app', 'email'],\n titleKey: 'security.notifications.mfaEnrolled.title',\n bodyKey: 'security.notifications.mfaEnrolled.body',\n icon: 'shield-check',\n severity: 'success',\n actions: [\n {\n id: 'open_mfa_settings',\n labelKey: 'common.view',\n variant: 'outline',\n href: '/backend/profile/security/mfa',\n icon: 'external-link',\n },\n ],\n linkHref: '/backend/profile/security/mfa',\n expiresAfterHours: 168,\n },\n {\n type: 'security.mfa.reset',\n module: 'security',\n category: 'auth',\n channels: ['in_app', 'email'],\n titleKey: 'security.notifications.mfaReset.title',\n bodyKey: 'security.notifications.mfaReset.body',\n icon: 'shield-alert',\n severity: 'warning',\n actions: [\n {\n id: 'open_mfa_settings',\n labelKey: 'common.view',\n variant: 'outline',\n href: '/backend/profile/security/mfa',\n icon: 'external-link',\n },\n ],\n linkHref: '/backend/profile/security/mfa',\n expiresAfterHours: 168,\n },\n {\n type: 'security.mfa.enforcement_deadline',\n module: 'security',\n category: 'auth',\n channels: ['in_app', 'email'],\n titleKey: 'security.notifications.enforcementDeadline.title',\n bodyKey: 'security.notifications.enforcementDeadline.bodyImmediate',\n icon: 'clock-3',\n severity: 'warning',\n actions: [\n {\n id: 'open_mfa_settings',\n labelKey: 'common.view',\n variant: 'outline',\n href: '/backend/profile/security/mfa',\n icon: 'external-link',\n },\n ],\n linkHref: '/backend/profile/security/mfa',\n expiresAfterHours: 168,\n },\n]\n\nexport default notificationTypes\n"],
5
- "mappings": "AAEO,MAAM,oBAAkD;AAAA,EAC7D;AAAA,IACE,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,UAAU,CAAC,UAAU,OAAO;AAAA,IAC5B,UAAU;AAAA,IACV,SAAS;AAAA,IACT,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS;AAAA,MACP;AAAA,QACE,IAAI;AAAA,QACJ,UAAU;AAAA,QACV,SAAS;AAAA,QACT,MAAM;AAAA,QACN,MAAM;AAAA,MACR;AAAA,IACF;AAAA,IACA,UAAU;AAAA,IACV,mBAAmB;AAAA,EACrB;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,UAAU,CAAC,UAAU,OAAO;AAAA,IAC5B,UAAU;AAAA,IACV,SAAS;AAAA,IACT,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS;AAAA,MACP;AAAA,QACE,IAAI;AAAA,QACJ,UAAU;AAAA,QACV,SAAS;AAAA,QACT,MAAM;AAAA,QACN,MAAM;AAAA,MACR;AAAA,IACF;AAAA,IACA,UAAU;AAAA,IACV,mBAAmB;AAAA,EACrB;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,UAAU,CAAC,UAAU,OAAO;AAAA,IAC5B,UAAU;AAAA,IACV,SAAS;AAAA,IACT,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS;AAAA,MACP;AAAA,QACE,IAAI;AAAA,QACJ,UAAU;AAAA,QACV,SAAS;AAAA,QACT,MAAM;AAAA,QACN,MAAM;AAAA,MACR;AAAA,IACF;AAAA,IACA,UAAU;AAAA,IACV,mBAAmB;AAAA,EACrB;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,UAAU,CAAC,UAAU,OAAO;AAAA,IAC5B,UAAU;AAAA,IACV,SAAS;AAAA,IACT,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS;AAAA,MACP;AAAA,QACE,IAAI;AAAA,QACJ,UAAU;AAAA,QACV,SAAS;AAAA,QACT,MAAM;AAAA,QACN,MAAM;AAAA,MACR;AAAA,IACF;AAAA,IACA,UAAU;AAAA,IACV,mBAAmB;AAAA,EACrB;AACF;AAEA,IAAO,wBAAQ;",
4
+ "sourcesContent": ["import type { NotificationTypeDefinition } from '@open-mercato/shared/modules/notifications/types'\n\nexport const notificationTypes: NotificationTypeDefinition[] = [\n {\n type: 'security.mfa.emergency_bypass_detected',\n module: 'security',\n category: 'auth',\n channels: ['in_app'],\n titleKey: 'security.notifications.mfaEmergencyBypass.title',\n bodyKey: 'security.notifications.mfaEmergencyBypass.body',\n icon: 'shield-alert',\n severity: 'warning',\n actions: [],\n expiresAfterHours: 168,\n },\n {\n type: 'security.password.changed',\n module: 'security',\n category: 'auth',\n channels: ['in_app', 'email'],\n titleKey: 'security.notifications.passwordChanged.title',\n bodyKey: 'security.notifications.passwordChanged.body',\n icon: 'key-round',\n severity: 'success',\n actions: [\n {\n id: 'open_security_settings',\n labelKey: 'common.view',\n variant: 'outline',\n href: '/backend/profile/security',\n icon: 'external-link',\n },\n ],\n linkHref: '/backend/profile/security',\n expiresAfterHours: 168,\n },\n {\n type: 'security.mfa.enrolled',\n module: 'security',\n category: 'auth',\n channels: ['in_app', 'email'],\n titleKey: 'security.notifications.mfaEnrolled.title',\n bodyKey: 'security.notifications.mfaEnrolled.body',\n icon: 'shield-check',\n severity: 'success',\n actions: [\n {\n id: 'open_mfa_settings',\n labelKey: 'common.view',\n variant: 'outline',\n href: '/backend/profile/security/mfa',\n icon: 'external-link',\n },\n ],\n linkHref: '/backend/profile/security/mfa',\n expiresAfterHours: 168,\n },\n {\n type: 'security.mfa.reset',\n module: 'security',\n category: 'auth',\n channels: ['in_app', 'email'],\n titleKey: 'security.notifications.mfaReset.title',\n bodyKey: 'security.notifications.mfaReset.body',\n icon: 'shield-alert',\n severity: 'warning',\n actions: [\n {\n id: 'open_mfa_settings',\n labelKey: 'common.view',\n variant: 'outline',\n href: '/backend/profile/security/mfa',\n icon: 'external-link',\n },\n ],\n linkHref: '/backend/profile/security/mfa',\n expiresAfterHours: 168,\n },\n {\n type: 'security.mfa.enforcement_deadline',\n module: 'security',\n category: 'auth',\n channels: ['in_app', 'email'],\n titleKey: 'security.notifications.enforcementDeadline.title',\n bodyKey: 'security.notifications.enforcementDeadline.bodyImmediate',\n icon: 'clock-3',\n severity: 'warning',\n actions: [\n {\n id: 'open_mfa_settings',\n labelKey: 'common.view',\n variant: 'outline',\n href: '/backend/profile/security/mfa',\n icon: 'external-link',\n },\n ],\n linkHref: '/backend/profile/security/mfa',\n expiresAfterHours: 168,\n },\n]\n\nexport default notificationTypes\n"],
5
+ "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ACR;AAAA,IACF;AAAA,IACA,UAAU;AAAA,IACV,mBAAmB;AAAA,EACrB;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,UAAU,CAAC,UAAU,OAAO;AAAA,IAC5B,UAAU;AAAA,IACV,SAAS;AAAA,IACT,MAAM;AAAA,IACN,UAAU;AAAA,IACV,SAAS;AAAA,MACP;AAAA,QACE,IAAI;AAAA,QACJ,UAAU;AAAA,QACV,SAAS;AAAA,QACT,MAAM;AAAA,QACN,MAAM;AAAA,MACR;AAAA,IACF;AAAA,IACA,UAAU;AAAA,IACV,mBAAmB;AAAA,EACrB;AACF;AAEA,IAAO,wBAAQ;",
6
6
  "names": []
7
7
  }
@@ -14,7 +14,7 @@ import {
14
14
  } from "../data/entities.js";
15
15
  import { emitSecurityEvent } from "../events.js";
16
16
  import { sudoTargets as defaultSudoTargets } from "../security.sudo.js";
17
- import { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig } from "../lib/security-config.js";
17
+ import { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig, requestMfaEmergencyBypassNotification } from "../lib/security-config.js";
18
18
  const signedSudoTokenPayloadSchema = z.object({
19
19
  sid: z.string(),
20
20
  sub: z.string(),
@@ -92,7 +92,10 @@ class SudoChallengeService {
92
92
  return { required: false };
93
93
  }
94
94
  const userMethods = await this.mfaService.getUserMethods(userId);
95
- const method = this.resolveChallengeMethod(protection.config.challengeMethod, userMethods.length, userId, targetIdentifier);
95
+ const method = await this.resolveChallengeMethod(protection.config.challengeMethod, userMethods.length, userId, targetIdentifier, {
96
+ tenantId: scopeTenantId,
97
+ organizationId: scopeOrganizationId
98
+ });
96
99
  let sessionToken = randomBytes(16).toString("hex");
97
100
  let availableMfaMethods;
98
101
  if (method === "mfa") {
@@ -460,7 +463,7 @@ class SudoChallengeService {
460
463
  throw new SudoChallengeServiceError("Sudo configuration not found", 404);
461
464
  }
462
465
  }
463
- resolveChallengeMethod(configuredMethod, availableMfaMethodCount, userId, targetIdentifier) {
466
+ async resolveChallengeMethod(configuredMethod, availableMfaMethodCount, userId, targetIdentifier, scope) {
464
467
  if (this.securityConfig.mfa.emergencyBypass) {
465
468
  let wouldHaveBeenMfa = false;
466
469
  try {
@@ -475,6 +478,7 @@ class SudoChallengeService {
475
478
  userId,
476
479
  targetIdentifier
477
480
  });
481
+ if (scope) await requestMfaEmergencyBypassNotification(scope);
478
482
  }
479
483
  return "password";
480
484
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../../../src/modules/security/services/SudoChallengeService.ts"],
4
- "sourcesContent": ["import { createHmac, randomBytes, timingSafeEqual } from 'node:crypto'\nimport { z } from 'zod'\nimport type { EntityManager, FilterQuery } from '@mikro-orm/postgresql'\nimport { User } from '@open-mercato/core/modules/auth/data/entities'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport {\n dedupeSudoTargets,\n getSecuritySudoTargetEntries,\n type SecuritySudoTarget,\n} from '../lib/module-security-registry'\nimport {\n ChallengeMethod,\n SudoChallengeConfig,\n SudoChallengeMethodUsed,\n SudoSession,\n} from '../data/entities'\nimport { emitSecurityEvent } from '../events'\nimport type {\n SudoConfigInput,\n SudoConfigUpdateInput,\n} from '../data/validators'\nimport type { PasswordService } from './PasswordService'\nimport type { MfaService } from './MfaService'\nimport type { MfaVerificationService } from './MfaVerificationService'\nimport { sudoTargets as defaultSudoTargets } from '../security.sudo'\nimport type { SecurityModuleConfig } from '../lib/security-config'\nimport { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig } from '../lib/security-config'\n\ntype SudoMethod = 'password' | 'mfa'\n\nexport type SudoAvailableMethod = {\n type: string\n label: string\n icon: string\n}\n\nexport type SudoProtectionResolution = {\n protected: boolean\n config?: SudoChallengeConfig\n}\n\nconst signedSudoTokenPayloadSchema = z.object({\n sid: z.string(),\n sub: z.string(),\n tid: z.string().nullable(),\n oid: z.string().nullable(),\n tgt: z.string(),\n exp: z.number(),\n})\n\ntype SignedSudoTokenPayload = z.infer<typeof signedSudoTokenPayloadSchema>\n\ntype UserScope = {\n id: string\n tenantId: string | null\n organizationId: string | null\n}\n\ntype BoundPendingSudoSession = SudoSession & {\n targetIdentifier: string\n sudoConfigId: string\n sudoConfigUpdatedAt: Date\n}\n\nexport type SudoAuthScope = {\n tenantId: string | null\n organizationId?: string | null\n isSuperAdmin?: boolean\n}\n\ntype DeveloperDefaultPayload = {\n targetIdentifier: string\n label?: string | null\n ttlSeconds?: number\n challengeMethod?: ChallengeMethod\n}\n\nfunction readTrimmedEnv(value: string | undefined): string | undefined {\n const trimmed = value?.trim()\n return trimmed ? trimmed : undefined\n}\n\nexport class SudoChallengeServiceError extends Error {\n constructor(\n message: string,\n public readonly statusCode: number,\n ) {\n super(message)\n this.name = 'SudoChallengeServiceError'\n }\n}\n\nexport class SudoChallengeService {\n constructor(\n private readonly em: EntityManager,\n private readonly passwordService: PasswordService,\n private readonly mfaService: MfaService,\n private readonly mfaVerificationService: MfaVerificationService,\n private readonly securityConfig: SecurityModuleConfig = readSecurityModuleConfig(),\n ) {}\n\n async listConfigs(scope?: SudoAuthScope): Promise<SudoChallengeConfig[]> {\n await this.ensureDeveloperDefaultsRegistered()\n const filter: FilterQuery<SudoChallengeConfig> = { deletedAt: null }\n if (scope && !scope.isSuperAdmin) {\n if (!scope.tenantId) return []\n ;(filter as Record<string, unknown>).$or = [\n { tenantId: scope.tenantId },\n { tenantId: null },\n ]\n }\n return this.em.find(\n SudoChallengeConfig,\n filter,\n {\n orderBy: {\n targetIdentifier: 'asc',\n tenantId: 'asc',\n organizationId: 'asc',\n createdAt: 'asc',\n },\n },\n )\n }\n\n async getConfigById(id: string, scope?: SudoAuthScope): Promise<SudoChallengeConfig | null> {\n await this.ensureDeveloperDefaultsRegistered()\n const config = await this.em.findOne(SudoChallengeConfig, { id, deletedAt: null })\n if (!config) return null\n if (scope && !this.isConfigVisibleToScope(config, scope)) return null\n return config\n }\n\n async isProtected(\n targetIdentifier: string,\n tenantId?: string | null,\n organizationId?: string | null,\n ): Promise<SudoProtectionResolution> {\n await this.ensureDeveloperDefaultsRegistered()\n return this.resolveProtection(this.em, targetIdentifier, tenantId ?? null, organizationId ?? null)\n }\n\n async initiate(\n userId: string,\n targetIdentifier: string,\n options?: { tenantId?: string | null; organizationId?: string | null },\n ): Promise<{\n required: boolean\n sessionId?: string\n method?: SudoMethod\n availableMfaMethods?: SudoAvailableMethod[]\n expiresAt?: Date\n }> {\n const user = await this.findUserScope(userId)\n if (!user?.tenantId) {\n throw new SudoChallengeServiceError('User not found', 404)\n }\n const scopeTenantId = options?.tenantId !== undefined ? options.tenantId : user.tenantId\n const scopeOrganizationId = options?.organizationId !== undefined ? options.organizationId : user.organizationId\n const protection = await this.isProtected(\n targetIdentifier,\n scopeTenantId,\n scopeOrganizationId,\n )\n\n if (!protection.protected || !protection.config) {\n return { required: false }\n }\n\n const userMethods = await this.mfaService.getUserMethods(userId)\n const method = this.resolveChallengeMethod(protection.config.challengeMethod, userMethods.length, userId, targetIdentifier)\n\n let sessionToken = randomBytes(16).toString('hex')\n let availableMfaMethods: SudoAvailableMethod[] | undefined\n if (method === 'mfa') {\n const challenge = await this.mfaVerificationService.createChallenge(userId)\n sessionToken = challenge.challengeId\n availableMfaMethods = challenge.availableMethods\n }\n\n const expiresAt = new Date(Date.now() + this.securityConfig.sudo.pendingChallengeTtlMs)\n const session = this.em.create(SudoSession, {\n userId,\n tenantId: user.tenantId,\n scopeTenantId,\n scopeOrganizationId,\n targetIdentifier,\n sudoConfigId: protection.config.id,\n sudoConfigUpdatedAt: protection.config.updatedAt,\n sessionToken,\n challengeMethod: method,\n expiresAt,\n verifiedAt: null,\n createdAt: new Date(),\n })\n this.em.persist(session)\n await this.em.flush()\n\n await emitSecurityEvent('security.sudo.challenged', {\n userId,\n tenantId: user.tenantId,\n organizationId: user.organizationId,\n targetIdentifier,\n method,\n })\n\n return {\n required: true,\n sessionId: session.id,\n method,\n availableMfaMethods,\n expiresAt,\n }\n }\n\n async prepare(\n sessionId: string,\n methodType: string,\n options: { expectedUserId?: string } = {},\n request?: Request,\n ): Promise<{ clientData?: Record<string, unknown> }> {\n const session = await this.getPendingSession(sessionId)\n if (options.expectedUserId && session.userId !== options.expectedUserId) {\n throw new SudoChallengeServiceError('Sudo challenge user mismatch', 403)\n }\n if (session.challengeMethod !== 'mfa') {\n throw new SudoChallengeServiceError('This sudo session does not require MFA', 400)\n }\n return this.mfaVerificationService.prepareChallenge(\n session.sessionToken,\n methodType,\n { request },\n { userId: session.userId },\n )\n }\n\n async verify(\n sessionId: string,\n methodType: string,\n payload: unknown,\n options: {\n expectedUserId?: string\n tenantId?: string | null\n organizationId?: string | null\n targetIdentifier: string\n },\n request?: Request,\n ): Promise<{ sudoToken: string; expiresAt: Date }> {\n const session = await this.getPendingSession(sessionId)\n if (options.expectedUserId && session.userId !== options.expectedUserId) {\n throw new SudoChallengeServiceError('Sudo challenge user mismatch', 403)\n }\n const user = await this.findUserScope(session.userId)\n if (!user?.tenantId) {\n throw new SudoChallengeServiceError('User not found', 404)\n }\n if (user.tenantId !== session.tenantId) {\n throw new SudoChallengeServiceError('Unable to verify sudo challenge', 403)\n }\n\n const requestedTenantId = options.tenantId !== undefined ? options.tenantId : user.tenantId\n const requestedOrganizationId = options.organizationId !== undefined ? options.organizationId : user.organizationId\n if (\n options.targetIdentifier !== session.targetIdentifier\n || requestedTenantId !== session.scopeTenantId\n || requestedOrganizationId !== session.scopeOrganizationId\n ) {\n throw new SudoChallengeServiceError('Unable to verify sudo challenge', 403)\n }\n\n const scopeTenantId = session.scopeTenantId ?? null\n const scopeOrganizationId = session.scopeOrganizationId ?? null\n const protection = await this.isProtected(\n session.targetIdentifier,\n scopeTenantId,\n scopeOrganizationId,\n )\n if (!protection.protected || !protection.config) {\n throw new SudoChallengeServiceError('Sudo protection is not configured for this target', 404)\n }\n if (\n protection.config.id !== session.sudoConfigId\n || protection.config.updatedAt.getTime() !== session.sudoConfigUpdatedAt?.getTime()\n ) {\n throw new SudoChallengeServiceError('Unable to verify sudo challenge', 403)\n }\n\n let verified = false\n let methodUsed: string = methodType\n if (session.challengeMethod === 'password') {\n const password = this.readPassword(payload)\n verified = await this.passwordService.verifyPassword(session.userId, password)\n methodUsed = SudoChallengeMethodUsed.PASSWORD\n } else {\n verified = await this.mfaVerificationService.verifyChallenge(\n session.sessionToken,\n methodType,\n payload,\n { request },\n { userId: session.userId },\n )\n }\n\n if (!verified) {\n await emitSecurityEvent('security.sudo.failed', {\n userId: session.userId,\n tenantId: user.tenantId,\n organizationId: user.organizationId,\n targetIdentifier: session.targetIdentifier,\n method: methodUsed,\n })\n throw new SudoChallengeServiceError('Unable to verify sudo challenge', 401)\n }\n\n const pendingToken = session.sessionToken\n const { sudoToken, expiresAt, verifiedAt } = await this.em.transactional(async (transactionalEm) => {\n await transactionalEm.execute('lock table \"sudo_challenge_configs\" in share mode')\n const currentProtection = await this.resolveProtection(\n transactionalEm,\n session.targetIdentifier,\n scopeTenantId,\n scopeOrganizationId,\n )\n if (\n !currentProtection.protected\n || !currentProtection.config\n || currentProtection.config.id !== session.sudoConfigId\n || currentProtection.config.updatedAt.getTime() !== session.sudoConfigUpdatedAt.getTime()\n ) {\n throw new SudoChallengeServiceError('Unable to verify sudo challenge', 403)\n }\n\n const ttlSeconds = this.normalizeTtl(currentProtection.config.ttlSeconds)\n const currentVerifiedAt = new Date()\n const currentExpiresAt = new Date(Date.now() + ttlSeconds * 1000)\n const currentSudoToken = this.signToken({\n sid: session.id,\n sub: session.userId,\n tid: scopeTenantId,\n oid: scopeOrganizationId,\n tgt: session.targetIdentifier,\n exp: currentExpiresAt.getTime(),\n })\n const consumedRows = await transactionalEm.nativeUpdate(\n SudoSession,\n {\n id: session.id,\n sessionToken: pendingToken,\n verifiedAt: null,\n expiresAt: { $gt: currentVerifiedAt },\n },\n {\n sessionToken: currentSudoToken,\n expiresAt: currentExpiresAt,\n verifiedAt: currentVerifiedAt,\n },\n )\n if (consumedRows !== 1) {\n throw new SudoChallengeServiceError('Unable to verify sudo challenge', 400)\n }\n return {\n sudoToken: currentSudoToken,\n expiresAt: currentExpiresAt,\n verifiedAt: currentVerifiedAt,\n }\n })\n session.sessionToken = sudoToken\n session.expiresAt = expiresAt\n session.verifiedAt = verifiedAt\n\n await emitSecurityEvent('security.sudo.verified', {\n userId: session.userId,\n tenantId: scopeTenantId,\n organizationId: scopeOrganizationId,\n targetIdentifier: session.targetIdentifier,\n method: methodUsed,\n expiresAt: expiresAt.toISOString(),\n })\n\n return { sudoToken, expiresAt }\n }\n\n async validateToken(\n token: string,\n targetIdentifier: string,\n options?: {\n expectedUserId?: string\n tenantId?: string | null\n organizationId?: string | null\n },\n ): Promise<boolean> {\n if (!token) return false\n const payload = this.readSignedToken(token)\n if (!payload) return false\n if (payload.exp <= Date.now()) return false\n if (payload.tgt !== targetIdentifier) return false\n if (options?.expectedUserId && payload.sub !== options.expectedUserId) return false\n if (options?.tenantId !== undefined && payload.tid !== (options.tenantId ?? null)) return false\n if (options?.organizationId !== undefined && payload.oid !== (options.organizationId ?? null)) return false\n\n const session = await this.em.findOne(SudoSession, {\n id: payload.sid,\n userId: payload.sub,\n sessionToken: token,\n targetIdentifier,\n scopeTenantId: payload.tid,\n scopeOrganizationId: payload.oid,\n verifiedAt: { $ne: null },\n } as FilterQuery<SudoSession>)\n\n return Boolean(session && session.expiresAt.getTime() > Date.now())\n }\n\n async createConfig(\n input: SudoConfigInput,\n configuredBy: string,\n scope?: SudoAuthScope,\n ): Promise<SudoChallengeConfig> {\n await this.ensureDeveloperDefaultsRegistered()\n const inputTenantId = input.tenantId ?? null\n const inputOrganizationId = input.organizationId ?? null\n this.validateScope(inputTenantId, inputOrganizationId)\n if (scope) this.assertWriteScope(inputTenantId, inputOrganizationId, scope)\n await this.ensureUniqueConfig(input.targetIdentifier, inputTenantId, inputOrganizationId)\n\n const config = this.em.create(SudoChallengeConfig, {\n tenantId: inputTenantId,\n organizationId: inputOrganizationId,\n label: input.label ?? null,\n targetIdentifier: input.targetIdentifier,\n isEnabled: input.isEnabled,\n ttlSeconds: this.normalizeTtl(input.ttlSeconds),\n challengeMethod: input.challengeMethod,\n configuredBy,\n isDeveloperDefault: false,\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n this.em.persist(config)\n await this.em.flush()\n\n await emitSecurityEvent('security.sudo.config.created', {\n id: config.id,\n targetIdentifier: config.targetIdentifier,\n configuredBy,\n })\n\n return config\n }\n\n async updateConfig(\n id: string,\n input: SudoConfigUpdateInput,\n configuredBy: string,\n scope?: SudoAuthScope,\n ): Promise<SudoChallengeConfig> {\n await this.ensureDeveloperDefaultsRegistered()\n const config = await this.em.findOne(SudoChallengeConfig, { id, deletedAt: null })\n if (!config) {\n throw new SudoChallengeServiceError('Sudo configuration not found', 404)\n }\n if (scope) {\n this.assertWriteScope(config.tenantId ?? null, config.organizationId ?? null, scope)\n if (config.isDeveloperDefault && !scope.isSuperAdmin) {\n throw new SudoChallengeServiceError('Sudo configuration not found', 404)\n }\n }\n\n const nextTenantId = input.tenantId !== undefined ? input.tenantId ?? null : config.tenantId ?? null\n const nextOrganizationId = input.organizationId !== undefined ? input.organizationId ?? null : config.organizationId ?? null\n const nextTargetIdentifier = input.targetIdentifier ?? config.targetIdentifier\n this.validateScope(nextTenantId, nextOrganizationId)\n if (scope) this.assertWriteScope(nextTenantId, nextOrganizationId, scope)\n await this.ensureUniqueConfig(nextTargetIdentifier, nextTenantId, nextOrganizationId, config.id)\n\n if (input.tenantId !== undefined) config.tenantId = input.tenantId ?? null\n if (input.organizationId !== undefined) config.organizationId = input.organizationId ?? null\n if (input.label !== undefined) config.label = input.label ?? null\n if (input.targetIdentifier !== undefined) config.targetIdentifier = input.targetIdentifier\n if (input.isEnabled !== undefined) config.isEnabled = input.isEnabled\n if (input.ttlSeconds !== undefined) config.ttlSeconds = this.normalizeTtl(input.ttlSeconds)\n if (input.challengeMethod !== undefined) config.challengeMethod = input.challengeMethod\n config.configuredBy = configuredBy\n config.updatedAt = new Date()\n await this.em.flush()\n\n await emitSecurityEvent('security.sudo.config.updated', {\n id: config.id,\n targetIdentifier: config.targetIdentifier,\n configuredBy,\n })\n\n return config\n }\n\n async deleteConfig(id: string, scope?: SudoAuthScope): Promise<void> {\n const config = await this.em.findOne(SudoChallengeConfig, { id, deletedAt: null })\n if (!config) {\n throw new SudoChallengeServiceError('Sudo configuration not found', 404)\n }\n if (scope) {\n this.assertWriteScope(config.tenantId ?? null, config.organizationId ?? null, scope)\n if (config.isDeveloperDefault && !scope.isSuperAdmin) {\n throw new SudoChallengeServiceError('Sudo configuration not found', 404)\n }\n }\n config.deletedAt = new Date()\n config.updatedAt = new Date()\n await this.em.flush()\n\n await emitSecurityEvent('security.sudo.config.deleted', {\n id: config.id,\n targetIdentifier: config.targetIdentifier,\n })\n }\n\n async registerDeveloperDefault(\n input: DeveloperDefaultPayload,\n ): Promise<void> {\n const existing = await this.em.findOne(SudoChallengeConfig, {\n targetIdentifier: input.targetIdentifier,\n tenantId: null,\n organizationId: null,\n isDeveloperDefault: true,\n })\n\n if (existing) {\n const ttlSeconds = this.normalizeTtl(input.ttlSeconds)\n const challengeMethod = input.challengeMethod ?? ChallengeMethod.AUTO\n const changed = !existing.isEnabled\n || Boolean(existing.deletedAt)\n || existing.ttlSeconds !== ttlSeconds\n || existing.challengeMethod !== challengeMethod\n if (changed) {\n existing.isEnabled = true\n existing.deletedAt = null\n existing.ttlSeconds = ttlSeconds\n existing.challengeMethod = challengeMethod\n existing.updatedAt = new Date()\n await this.em.flush()\n }\n return\n }\n\n const config = this.em.create(SudoChallengeConfig, {\n tenantId: null,\n organizationId: null,\n label: input.label ?? null,\n targetIdentifier: input.targetIdentifier,\n isEnabled: true,\n isDeveloperDefault: true,\n ttlSeconds: this.normalizeTtl(input.ttlSeconds),\n challengeMethod: input.challengeMethod ?? ChallengeMethod.AUTO,\n configuredBy: null,\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n this.em.persist(config)\n await this.em.flush()\n }\n\n async cleanupExpired(): Promise<number> {\n return this.em.nativeDelete(SudoSession, {\n expiresAt: { $lte: new Date() },\n })\n }\n\n private async ensureDeveloperDefaultsRegistered(): Promise<void> {\n const registryEntries = getSecuritySudoTargetEntries()\n const registryTargets = registryEntries.flatMap((entry) => entry.targets ?? [])\n const fallbackTargets = registryEntries.length === 0 ? defaultSudoTargets : []\n\n for (const target of dedupeSudoTargets([\n ...registryTargets,\n ...fallbackTargets,\n ])) {\n await this.registerDeveloperDefault(this.readDeveloperDefault(target))\n }\n }\n\n private readDeveloperDefault(target: SecuritySudoTarget): DeveloperDefaultPayload {\n return {\n targetIdentifier: target.identifier,\n label: target.label ?? null,\n ttlSeconds: target.ttlSeconds,\n challengeMethod: this.toChallengeMethod(target.challengeMethod),\n }\n }\n\n private async ensureUniqueConfig(\n targetIdentifier: string,\n tenantId: string | null,\n organizationId: string | null,\n ignoreId?: string,\n ): Promise<void> {\n const existing = await this.em.findOne(SudoChallengeConfig, {\n targetIdentifier,\n tenantId,\n organizationId,\n deletedAt: null,\n })\n if (existing && existing.id !== ignoreId) {\n throw new SudoChallengeServiceError('A sudo configuration for this target and scope already exists', 409)\n }\n }\n\n private validateScope(tenantId: string | null, organizationId: string | null): void {\n if (organizationId && !tenantId) {\n throw new SudoChallengeServiceError('Organization-scoped sudo config requires a tenant', 400)\n }\n }\n\n private isConfigVisibleToScope(config: SudoChallengeConfig, scope: SudoAuthScope): boolean {\n if (scope.isSuperAdmin) return true\n if (!scope.tenantId) return false\n const configTenantId = config.tenantId ?? null\n if (configTenantId === null) return true\n return configTenantId === scope.tenantId\n }\n\n private assertWriteScope(\n targetTenantId: string | null,\n targetOrganizationId: string | null,\n scope: SudoAuthScope,\n ): void {\n if (scope.isSuperAdmin) return\n if (!scope.tenantId) {\n throw new SudoChallengeServiceError('Sudo configuration not found', 404)\n }\n if ((targetTenantId ?? null) !== scope.tenantId) {\n throw new SudoChallengeServiceError('Sudo configuration not found', 404)\n }\n if (\n scope.organizationId !== undefined\n && scope.organizationId !== null\n && targetOrganizationId !== null\n && targetOrganizationId !== scope.organizationId\n ) {\n throw new SudoChallengeServiceError('Sudo configuration not found', 404)\n }\n }\n\n private resolveChallengeMethod(\n configuredMethod: ChallengeMethod,\n availableMfaMethodCount: number,\n userId?: string,\n targetIdentifier?: string,\n ): SudoMethod {\n if (this.securityConfig.mfa.emergencyBypass) {\n let wouldHaveBeenMfa = false\n try {\n wouldHaveBeenMfa = this.resolveChallengeMethodWithoutBypass(configuredMethod, availableMfaMethodCount) === 'mfa'\n } catch {\n wouldHaveBeenMfa = configuredMethod === ChallengeMethod.MFA\n }\n if (wouldHaveBeenMfa) {\n emitMfaEmergencyBypassActiveWarning('sudo challenge downgraded to password', {\n configuredMethod,\n availableMfaMethodCount,\n userId,\n targetIdentifier,\n })\n }\n return 'password'\n }\n return this.resolveChallengeMethodWithoutBypass(configuredMethod, availableMfaMethodCount)\n }\n\n private resolveChallengeMethodWithoutBypass(\n configuredMethod: ChallengeMethod,\n availableMfaMethodCount: number,\n ): SudoMethod {\n if (configuredMethod === ChallengeMethod.PASSWORD) return 'password'\n if (configuredMethod === ChallengeMethod.MFA) {\n if (availableMfaMethodCount === 0) {\n throw new SudoChallengeServiceError('This sudo target requires MFA, but no MFA methods are configured', 400)\n }\n return 'mfa'\n }\n return availableMfaMethodCount > 0 ? 'mfa' : 'password'\n }\n\n private async resolveProtection(\n em: EntityManager,\n targetIdentifier: string,\n tenantId: string | null,\n organizationId: string | null,\n ): Promise<SudoProtectionResolution> {\n const candidates = await em.find(SudoChallengeConfig, {\n targetIdentifier,\n deletedAt: null,\n })\n\n const resolved = candidates\n .filter((config) => this.matchesScope(config, tenantId, organizationId))\n .sort((left, right) => this.compareConfigPriority(left, right, tenantId, organizationId))[0]\n\n if (!resolved || !resolved.isEnabled) {\n return { protected: false }\n }\n\n return { protected: true, config: resolved }\n }\n\n private matchesScope(config: SudoChallengeConfig, tenantId: string | null, organizationId: string | null): boolean {\n if (config.organizationId) {\n return config.organizationId === organizationId && config.tenantId === tenantId\n }\n if (config.tenantId) {\n return config.tenantId === tenantId\n }\n return true\n }\n\n private compareConfigPriority(\n left: SudoChallengeConfig,\n right: SudoChallengeConfig,\n tenantId: string | null,\n organizationId: string | null,\n ): number {\n const leftScore = this.getScopePriority(left, tenantId, organizationId)\n const rightScore = this.getScopePriority(right, tenantId, organizationId)\n if (leftScore !== rightScore) return leftScore - rightScore\n if (left.isDeveloperDefault !== right.isDeveloperDefault) {\n return left.isDeveloperDefault ? 1 : -1\n }\n return right.updatedAt.getTime() - left.updatedAt.getTime()\n }\n\n private getScopePriority(\n config: SudoChallengeConfig,\n tenantId: string | null,\n organizationId: string | null,\n ): number {\n if (config.organizationId === organizationId && config.tenantId === tenantId) return 0\n if (!config.organizationId && config.tenantId === tenantId) return 1\n if (!config.organizationId && !config.tenantId && !config.isDeveloperDefault) return 2\n if (!config.organizationId && !config.tenantId && config.isDeveloperDefault) return 3\n return 4\n }\n\n private async getPendingSession(sessionId: string): Promise<BoundPendingSudoSession> {\n const session = await this.em.findOne(SudoSession, { id: sessionId })\n if (!session) {\n throw new SudoChallengeServiceError('Sudo challenge session not found', 404)\n }\n if (!session.targetIdentifier || !session.sudoConfigId || !session.sudoConfigUpdatedAt) {\n throw new SudoChallengeServiceError('Sudo challenge session not found', 404)\n }\n if (session.verifiedAt) {\n throw new SudoChallengeServiceError('Unable to verify sudo challenge', 400)\n }\n if (session.expiresAt.getTime() <= Date.now()) {\n throw new SudoChallengeServiceError('Sudo challenge session expired', 400)\n }\n return session as BoundPendingSudoSession\n }\n\n private async findUserScope(userId: string): Promise<UserScope | null> {\n const user = await findOneWithDecryption(\n this.em,\n User,\n { id: userId, deletedAt: null },\n undefined,\n {},\n )\n\n if (!user) return null\n return {\n id: String(user.id),\n tenantId: user.tenantId ? String(user.tenantId) : null,\n organizationId: user.organizationId ? String(user.organizationId) : null,\n }\n }\n\n private normalizeTtl(value?: number | null): number {\n const rawValue = value ?? this.securityConfig.sudo.defaultTtlSeconds\n return Math.min(\n Math.max(rawValue, this.securityConfig.sudo.minTtlSeconds),\n this.securityConfig.sudo.maxTtlSeconds,\n )\n }\n\n private readPassword(payload: unknown): string {\n if (!payload || typeof payload !== 'object') {\n throw new SudoChallengeServiceError('Password is required', 400)\n }\n const maybePassword = (payload as Record<string, unknown>).password\n if (typeof maybePassword !== 'string' || maybePassword.trim().length === 0) {\n throw new SudoChallengeServiceError('Password is required', 400)\n }\n return maybePassword\n }\n\n private signToken(payload: SignedSudoTokenPayload): string {\n const encodedPayload = Buffer.from(JSON.stringify(payload)).toString('base64url')\n const signature = createHmac('sha256', this.getSudoSecret()).update(encodedPayload).digest('base64url')\n return `${encodedPayload}.${signature}`\n }\n\n private readSignedToken(token: string): SignedSudoTokenPayload | null {\n const [encodedPayload, signature] = token.split('.')\n if (!encodedPayload || !signature) return null\n\n const expected = createHmac('sha256', this.getSudoSecret()).update(encodedPayload).digest('base64url')\n const signatureBuffer = Buffer.from(signature)\n const expectedBuffer = Buffer.from(expected)\n if (signatureBuffer.length !== expectedBuffer.length) return null\n if (!timingSafeEqual(signatureBuffer, expectedBuffer)) return null\n\n try {\n const parsed = signedSudoTokenPayloadSchema.safeParse(\n JSON.parse(Buffer.from(encodedPayload, 'base64url').toString('utf8')),\n )\n if (!parsed.success) return null\n return parsed.data\n } catch {\n return null\n }\n }\n\n private getSudoSecret(): string {\n const secret =\n readTrimmedEnv(process.env.OM_SECURITY_SUDO_SECRET)\n ?? readTrimmedEnv(process.env.AUTH_JWT_SECRET)\n ?? readTrimmedEnv(process.env.AUTH_SECRET)\n ?? readTrimmedEnv(process.env.JWT_SECRET)\n if (secret) return secret\n throw new Error(\n 'Sudo step-up tokens require OM_SECURITY_SUDO_SECRET, AUTH_JWT_SECRET, AUTH_SECRET, or JWT_SECRET to be set.',\n )\n }\n\n private toChallengeMethod(\n method: SecuritySudoTarget['challengeMethod'],\n ): ChallengeMethod | undefined {\n switch (method) {\n case 'password':\n return ChallengeMethod.PASSWORD\n case 'mfa':\n return ChallengeMethod.MFA\n case 'auto':\n default:\n return ChallengeMethod.AUTO\n }\n }\n}\n\nexport default SudoChallengeService\n"],
5
- "mappings": "AAAA,SAAS,YAAY,aAAa,uBAAuB;AACzD,SAAS,SAAS;AAElB,SAAS,YAAY;AACrB,SAAS,6BAA6B;AACtC;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,yBAAyB;AAQlC,SAAS,eAAe,0BAA0B;AAElD,SAAS,qCAAqC,gCAAgC;AAe9E,MAAM,+BAA+B,EAAE,OAAO;AAAA,EAC5C,KAAK,EAAE,OAAO;AAAA,EACd,KAAK,EAAE,OAAO;AAAA,EACd,KAAK,EAAE,OAAO,EAAE,SAAS;AAAA,EACzB,KAAK,EAAE,OAAO,EAAE,SAAS;AAAA,EACzB,KAAK,EAAE,OAAO;AAAA,EACd,KAAK,EAAE,OAAO;AAChB,CAAC;AA6BD,SAAS,eAAe,OAA+C;AACrE,QAAM,UAAU,OAAO,KAAK;AAC5B,SAAO,UAAU,UAAU;AAC7B;AAEO,MAAM,kCAAkC,MAAM;AAAA,EACnD,YACE,SACgB,YAChB;AACA,UAAM,OAAO;AAFG;AAGhB,SAAK,OAAO;AAAA,EACd;AACF;AAEO,MAAM,qBAAqB;AAAA,EAChC,YACmB,IACA,iBACA,YACA,wBACA,iBAAuC,yBAAyB,GACjF;AALiB;AACA;AACA;AACA;AACA;AAAA,EAChB;AAAA,EAEH,MAAM,YAAY,OAAuD;AACvE,UAAM,KAAK,kCAAkC;AAC7C,UAAM,SAA2C,EAAE,WAAW,KAAK;AACnE,QAAI,SAAS,CAAC,MAAM,cAAc;AAChC,UAAI,CAAC,MAAM,SAAU,QAAO,CAAC;AAC5B,MAAC,OAAmC,MAAM;AAAA,QACzC,EAAE,UAAU,MAAM,SAAS;AAAA,QAC3B,EAAE,UAAU,K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AAM,CAAC;AAAA,MACtE;AACA,UAAI,CAAC,OAAO,QAAS,QAAO;AAC5B,aAAO,OAAO;AAAA,IAChB,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;AAAA,EAEQ,gBAAwB;AAC9B,UAAM,SACJ,eAAe,QAAQ,IAAI,uBAAuB,KAC/C,eAAe,QAAQ,IAAI,eAAe,KAC1C,eAAe,QAAQ,IAAI,WAAW,KACtC,eAAe,QAAQ,IAAI,UAAU;AAC1C,QAAI,OAAQ,QAAO;AACnB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAAA,EAEQ,kBACN,QAC6B;AAC7B,YAAQ,QAAQ;AAAA,MACd,KAAK;AACH,eAAO,gBAAgB;AAAA,MACzB,KAAK;AACH,eAAO,gBAAgB;AAAA,MACzB,KAAK;AAAA,MACL;AACE,eAAO,gBAAgB;AAAA,IAC3B;AAAA,EACF;AACF;AAEA,IAAO,+BAAQ;",
4
+ "sourcesContent": ["import { createHmac, randomBytes, timingSafeEqual } from 'node:crypto'\nimport { z } from 'zod'\nimport type { EntityManager, FilterQuery } from '@mikro-orm/postgresql'\nimport { User } from '@open-mercato/core/modules/auth/data/entities'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport {\n dedupeSudoTargets,\n getSecuritySudoTargetEntries,\n type SecuritySudoTarget,\n} from '../lib/module-security-registry'\nimport {\n ChallengeMethod,\n SudoChallengeConfig,\n SudoChallengeMethodUsed,\n SudoSession,\n} from '../data/entities'\nimport { emitSecurityEvent } from '../events'\nimport type {\n SudoConfigInput,\n SudoConfigUpdateInput,\n} from '../data/validators'\nimport type { PasswordService } from './PasswordService'\nimport type { MfaService } from './MfaService'\nimport type { MfaVerificationService } from './MfaVerificationService'\nimport { sudoTargets as defaultSudoTargets } from '../security.sudo'\nimport type { SecurityModuleConfig } from '../lib/security-config'\nimport { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig, requestMfaEmergencyBypassNotification } from '../lib/security-config'\n\ntype SudoMethod = 'password' | 'mfa'\n\nexport type SudoAvailableMethod = {\n type: string\n label: string\n icon: string\n}\n\nexport type SudoProtectionResolution = {\n protected: boolean\n config?: SudoChallengeConfig\n}\n\nconst signedSudoTokenPayloadSchema = z.object({\n sid: z.string(),\n sub: z.string(),\n tid: z.string().nullable(),\n oid: z.string().nullable(),\n tgt: z.string(),\n exp: z.number(),\n})\n\ntype SignedSudoTokenPayload = z.infer<typeof signedSudoTokenPayloadSchema>\n\ntype UserScope = {\n id: string\n tenantId: string | null\n organizationId: string | null\n}\n\ntype BoundPendingSudoSession = SudoSession & {\n targetIdentifier: string\n sudoConfigId: string\n sudoConfigUpdatedAt: Date\n}\n\nexport type SudoAuthScope = {\n tenantId: string | null\n organizationId?: string | null\n isSuperAdmin?: boolean\n}\n\ntype DeveloperDefaultPayload = {\n targetIdentifier: string\n label?: string | null\n ttlSeconds?: number\n challengeMethod?: ChallengeMethod\n}\n\nfunction readTrimmedEnv(value: string | undefined): string | undefined {\n const trimmed = value?.trim()\n return trimmed ? trimmed : undefined\n}\n\nexport class SudoChallengeServiceError extends Error {\n constructor(\n message: string,\n public readonly statusCode: number,\n ) {\n super(message)\n this.name = 'SudoChallengeServiceError'\n }\n}\n\nexport class SudoChallengeService {\n constructor(\n private readonly em: EntityManager,\n private readonly passwordService: PasswordService,\n private readonly mfaService: MfaService,\n private readonly mfaVerificationService: MfaVerificationService,\n private readonly securityConfig: SecurityModuleConfig = readSecurityModuleConfig(),\n ) {}\n\n async listConfigs(scope?: SudoAuthScope): Promise<SudoChallengeConfig[]> {\n await this.ensureDeveloperDefaultsRegistered()\n const filter: FilterQuery<SudoChallengeConfig> = { deletedAt: null }\n if (scope && !scope.isSuperAdmin) {\n if (!scope.tenantId) return []\n ;(filter as Record<string, unknown>).$or = [\n { tenantId: scope.tenantId },\n { tenantId: null },\n ]\n }\n return this.em.find(\n SudoChallengeConfig,\n filter,\n {\n orderBy: {\n targetIdentifier: 'asc',\n tenantId: 'asc',\n organizationId: 'asc',\n createdAt: 'asc',\n },\n },\n )\n }\n\n async getConfigById(id: string, scope?: SudoAuthScope): Promise<SudoChallengeConfig | null> {\n await this.ensureDeveloperDefaultsRegistered()\n const config = await this.em.findOne(SudoChallengeConfig, { id, deletedAt: null })\n if (!config) return null\n if (scope && !this.isConfigVisibleToScope(config, scope)) return null\n return config\n }\n\n async isProtected(\n targetIdentifier: string,\n tenantId?: string | null,\n organizationId?: string | null,\n ): Promise<SudoProtectionResolution> {\n await this.ensureDeveloperDefaultsRegistered()\n return this.resolveProtection(this.em, targetIdentifier, tenantId ?? null, organizationId ?? null)\n }\n\n async initiate(\n userId: string,\n targetIdentifier: string,\n options?: { tenantId?: string | null; organizationId?: string | null },\n ): Promise<{\n required: boolean\n sessionId?: string\n method?: SudoMethod\n availableMfaMethods?: SudoAvailableMethod[]\n expiresAt?: Date\n }> {\n const user = await this.findUserScope(userId)\n if (!user?.tenantId) {\n throw new SudoChallengeServiceError('User not found', 404)\n }\n const scopeTenantId = options?.tenantId !== undefined ? options.tenantId : user.tenantId\n const scopeOrganizationId = options?.organizationId !== undefined ? options.organizationId : user.organizationId\n const protection = await this.isProtected(\n targetIdentifier,\n scopeTenantId,\n scopeOrganizationId,\n )\n\n if (!protection.protected || !protection.config) {\n return { required: false }\n }\n\n const userMethods = await this.mfaService.getUserMethods(userId)\n const method = await this.resolveChallengeMethod(protection.config.challengeMethod, userMethods.length, userId, targetIdentifier, {\n tenantId: scopeTenantId,\n organizationId: scopeOrganizationId,\n })\n\n let sessionToken = randomBytes(16).toString('hex')\n let availableMfaMethods: SudoAvailableMethod[] | undefined\n if (method === 'mfa') {\n const challenge = await this.mfaVerificationService.createChallenge(userId)\n sessionToken = challenge.challengeId\n availableMfaMethods = challenge.availableMethods\n }\n\n const expiresAt = new Date(Date.now() + this.securityConfig.sudo.pendingChallengeTtlMs)\n const session = this.em.create(SudoSession, {\n userId,\n tenantId: user.tenantId,\n scopeTenantId,\n scopeOrganizationId,\n targetIdentifier,\n sudoConfigId: protection.config.id,\n sudoConfigUpdatedAt: protection.config.updatedAt,\n sessionToken,\n challengeMethod: method,\n expiresAt,\n verifiedAt: null,\n createdAt: new Date(),\n })\n this.em.persist(session)\n await this.em.flush()\n\n await emitSecurityEvent('security.sudo.challenged', {\n userId,\n tenantId: user.tenantId,\n organizationId: user.organizationId,\n targetIdentifier,\n method,\n })\n\n return {\n required: true,\n sessionId: session.id,\n method,\n availableMfaMethods,\n expiresAt,\n }\n }\n\n async prepare(\n sessionId: string,\n methodType: string,\n options: { expectedUserId?: string } = {},\n request?: Request,\n ): Promise<{ clientData?: Record<string, unknown> }> {\n const session = await this.getPendingSession(sessionId)\n if (options.expectedUserId && session.userId !== options.expectedUserId) {\n throw new SudoChallengeServiceError('Sudo challenge user mismatch', 403)\n }\n if (session.challengeMethod !== 'mfa') {\n throw new SudoChallengeServiceError('This sudo session does not require MFA', 400)\n }\n return this.mfaVerificationService.prepareChallenge(\n session.sessionToken,\n methodType,\n { request },\n { userId: session.userId },\n )\n }\n\n async verify(\n sessionId: string,\n methodType: string,\n payload: unknown,\n options: {\n expectedUserId?: string\n tenantId?: string | null\n organizationId?: string | null\n targetIdentifier: string\n },\n request?: Request,\n ): Promise<{ sudoToken: string; expiresAt: Date }> {\n const session = await this.getPendingSession(sessionId)\n if (options.expectedUserId && session.userId !== options.expectedUserId) {\n throw new SudoChallengeServiceError('Sudo challenge user mismatch', 403)\n }\n const user = await this.findUserScope(session.userId)\n if (!user?.tenantId) {\n throw new SudoChallengeServiceError('User not found', 404)\n }\n if (user.tenantId !== session.tenantId) {\n throw new SudoChallengeServiceError('Unable to verify sudo challenge', 403)\n }\n\n const requestedTenantId = options.tenantId !== undefined ? options.tenantId : user.tenantId\n const requestedOrganizationId = options.organizationId !== undefined ? options.organizationId : user.organizationId\n if (\n options.targetIdentifier !== session.targetIdentifier\n || requestedTenantId !== session.scopeTenantId\n || requestedOrganizationId !== session.scopeOrganizationId\n ) {\n throw new SudoChallengeServiceError('Unable to verify sudo challenge', 403)\n }\n\n const scopeTenantId = session.scopeTenantId ?? null\n const scopeOrganizationId = session.scopeOrganizationId ?? null\n const protection = await this.isProtected(\n session.targetIdentifier,\n scopeTenantId,\n scopeOrganizationId,\n )\n if (!protection.protected || !protection.config) {\n throw new SudoChallengeServiceError('Sudo protection is not configured for this target', 404)\n }\n if (\n protection.config.id !== session.sudoConfigId\n || protection.config.updatedAt.getTime() !== session.sudoConfigUpdatedAt?.getTime()\n ) {\n throw new SudoChallengeServiceError('Unable to verify sudo challenge', 403)\n }\n\n let verified = false\n let methodUsed: string = methodType\n if (session.challengeMethod === 'password') {\n const password = this.readPassword(payload)\n verified = await this.passwordService.verifyPassword(session.userId, password)\n methodUsed = SudoChallengeMethodUsed.PASSWORD\n } else {\n verified = await this.mfaVerificationService.verifyChallenge(\n session.sessionToken,\n methodType,\n payload,\n { request },\n { userId: session.userId },\n )\n }\n\n if (!verified) {\n await emitSecurityEvent('security.sudo.failed', {\n userId: session.userId,\n tenantId: user.tenantId,\n organizationId: user.organizationId,\n targetIdentifier: session.targetIdentifier,\n method: methodUsed,\n })\n throw new SudoChallengeServiceError('Unable to verify sudo challenge', 401)\n }\n\n const pendingToken = session.sessionToken\n const { sudoToken, expiresAt, verifiedAt } = await this.em.transactional(async (transactionalEm) => {\n await transactionalEm.execute('lock table \"sudo_challenge_configs\" in share mode')\n const currentProtection = await this.resolveProtection(\n transactionalEm,\n session.targetIdentifier,\n scopeTenantId,\n scopeOrganizationId,\n )\n if (\n !currentProtection.protected\n || !currentProtection.config\n || currentProtection.config.id !== session.sudoConfigId\n || currentProtection.config.updatedAt.getTime() !== session.sudoConfigUpdatedAt.getTime()\n ) {\n throw new SudoChallengeServiceError('Unable to verify sudo challenge', 403)\n }\n\n const ttlSeconds = this.normalizeTtl(currentProtection.config.ttlSeconds)\n const currentVerifiedAt = new Date()\n const currentExpiresAt = new Date(Date.now() + ttlSeconds * 1000)\n const currentSudoToken = this.signToken({\n sid: session.id,\n sub: session.userId,\n tid: scopeTenantId,\n oid: scopeOrganizationId,\n tgt: session.targetIdentifier,\n exp: currentExpiresAt.getTime(),\n })\n const consumedRows = await transactionalEm.nativeUpdate(\n SudoSession,\n {\n id: session.id,\n sessionToken: pendingToken,\n verifiedAt: null,\n expiresAt: { $gt: currentVerifiedAt },\n },\n {\n sessionToken: currentSudoToken,\n expiresAt: currentExpiresAt,\n verifiedAt: currentVerifiedAt,\n },\n )\n if (consumedRows !== 1) {\n throw new SudoChallengeServiceError('Unable to verify sudo challenge', 400)\n }\n return {\n sudoToken: currentSudoToken,\n expiresAt: currentExpiresAt,\n verifiedAt: currentVerifiedAt,\n }\n })\n session.sessionToken = sudoToken\n session.expiresAt = expiresAt\n session.verifiedAt = verifiedAt\n\n await emitSecurityEvent('security.sudo.verified', {\n userId: session.userId,\n tenantId: scopeTenantId,\n organizationId: scopeOrganizationId,\n targetIdentifier: session.targetIdentifier,\n method: methodUsed,\n expiresAt: expiresAt.toISOString(),\n })\n\n return { sudoToken, expiresAt }\n }\n\n async validateToken(\n token: string,\n targetIdentifier: string,\n options?: {\n expectedUserId?: string\n tenantId?: string | null\n organizationId?: string | null\n },\n ): Promise<boolean> {\n if (!token) return false\n const payload = this.readSignedToken(token)\n if (!payload) return false\n if (payload.exp <= Date.now()) return false\n if (payload.tgt !== targetIdentifier) return false\n if (options?.expectedUserId && payload.sub !== options.expectedUserId) return false\n if (options?.tenantId !== undefined && payload.tid !== (options.tenantId ?? null)) return false\n if (options?.organizationId !== undefined && payload.oid !== (options.organizationId ?? null)) return false\n\n const session = await this.em.findOne(SudoSession, {\n id: payload.sid,\n userId: payload.sub,\n sessionToken: token,\n targetIdentifier,\n scopeTenantId: payload.tid,\n scopeOrganizationId: payload.oid,\n verifiedAt: { $ne: null },\n } as FilterQuery<SudoSession>)\n\n return Boolean(session && session.expiresAt.getTime() > Date.now())\n }\n\n async createConfig(\n input: SudoConfigInput,\n configuredBy: string,\n scope?: SudoAuthScope,\n ): Promise<SudoChallengeConfig> {\n await this.ensureDeveloperDefaultsRegistered()\n const inputTenantId = input.tenantId ?? null\n const inputOrganizationId = input.organizationId ?? null\n this.validateScope(inputTenantId, inputOrganizationId)\n if (scope) this.assertWriteScope(inputTenantId, inputOrganizationId, scope)\n await this.ensureUniqueConfig(input.targetIdentifier, inputTenantId, inputOrganizationId)\n\n const config = this.em.create(SudoChallengeConfig, {\n tenantId: inputTenantId,\n organizationId: inputOrganizationId,\n label: input.label ?? null,\n targetIdentifier: input.targetIdentifier,\n isEnabled: input.isEnabled,\n ttlSeconds: this.normalizeTtl(input.ttlSeconds),\n challengeMethod: input.challengeMethod,\n configuredBy,\n isDeveloperDefault: false,\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n this.em.persist(config)\n await this.em.flush()\n\n await emitSecurityEvent('security.sudo.config.created', {\n id: config.id,\n targetIdentifier: config.targetIdentifier,\n configuredBy,\n })\n\n return config\n }\n\n async updateConfig(\n id: string,\n input: SudoConfigUpdateInput,\n configuredBy: string,\n scope?: SudoAuthScope,\n ): Promise<SudoChallengeConfig> {\n await this.ensureDeveloperDefaultsRegistered()\n const config = await this.em.findOne(SudoChallengeConfig, { id, deletedAt: null })\n if (!config) {\n throw new SudoChallengeServiceError('Sudo configuration not found', 404)\n }\n if (scope) {\n this.assertWriteScope(config.tenantId ?? null, config.organizationId ?? null, scope)\n if (config.isDeveloperDefault && !scope.isSuperAdmin) {\n throw new SudoChallengeServiceError('Sudo configuration not found', 404)\n }\n }\n\n const nextTenantId = input.tenantId !== undefined ? input.tenantId ?? null : config.tenantId ?? null\n const nextOrganizationId = input.organizationId !== undefined ? input.organizationId ?? null : config.organizationId ?? null\n const nextTargetIdentifier = input.targetIdentifier ?? config.targetIdentifier\n this.validateScope(nextTenantId, nextOrganizationId)\n if (scope) this.assertWriteScope(nextTenantId, nextOrganizationId, scope)\n await this.ensureUniqueConfig(nextTargetIdentifier, nextTenantId, nextOrganizationId, config.id)\n\n if (input.tenantId !== undefined) config.tenantId = input.tenantId ?? null\n if (input.organizationId !== undefined) config.organizationId = input.organizationId ?? null\n if (input.label !== undefined) config.label = input.label ?? null\n if (input.targetIdentifier !== undefined) config.targetIdentifier = input.targetIdentifier\n if (input.isEnabled !== undefined) config.isEnabled = input.isEnabled\n if (input.ttlSeconds !== undefined) config.ttlSeconds = this.normalizeTtl(input.ttlSeconds)\n if (input.challengeMethod !== undefined) config.challengeMethod = input.challengeMethod\n config.configuredBy = configuredBy\n config.updatedAt = new Date()\n await this.em.flush()\n\n await emitSecurityEvent('security.sudo.config.updated', {\n id: config.id,\n targetIdentifier: config.targetIdentifier,\n configuredBy,\n })\n\n return config\n }\n\n async deleteConfig(id: string, scope?: SudoAuthScope): Promise<void> {\n const config = await this.em.findOne(SudoChallengeConfig, { id, deletedAt: null })\n if (!config) {\n throw new SudoChallengeServiceError('Sudo configuration not found', 404)\n }\n if (scope) {\n this.assertWriteScope(config.tenantId ?? null, config.organizationId ?? null, scope)\n if (config.isDeveloperDefault && !scope.isSuperAdmin) {\n throw new SudoChallengeServiceError('Sudo configuration not found', 404)\n }\n }\n config.deletedAt = new Date()\n config.updatedAt = new Date()\n await this.em.flush()\n\n await emitSecurityEvent('security.sudo.config.deleted', {\n id: config.id,\n targetIdentifier: config.targetIdentifier,\n })\n }\n\n async registerDeveloperDefault(\n input: DeveloperDefaultPayload,\n ): Promise<void> {\n const existing = await this.em.findOne(SudoChallengeConfig, {\n targetIdentifier: input.targetIdentifier,\n tenantId: null,\n organizationId: null,\n isDeveloperDefault: true,\n })\n\n if (existing) {\n const ttlSeconds = this.normalizeTtl(input.ttlSeconds)\n const challengeMethod = input.challengeMethod ?? ChallengeMethod.AUTO\n const changed = !existing.isEnabled\n || Boolean(existing.deletedAt)\n || existing.ttlSeconds !== ttlSeconds\n || existing.challengeMethod !== challengeMethod\n if (changed) {\n existing.isEnabled = true\n existing.deletedAt = null\n existing.ttlSeconds = ttlSeconds\n existing.challengeMethod = challengeMethod\n existing.updatedAt = new Date()\n await this.em.flush()\n }\n return\n }\n\n const config = this.em.create(SudoChallengeConfig, {\n tenantId: null,\n organizationId: null,\n label: input.label ?? null,\n targetIdentifier: input.targetIdentifier,\n isEnabled: true,\n isDeveloperDefault: true,\n ttlSeconds: this.normalizeTtl(input.ttlSeconds),\n challengeMethod: input.challengeMethod ?? ChallengeMethod.AUTO,\n configuredBy: null,\n createdAt: new Date(),\n updatedAt: new Date(),\n })\n this.em.persist(config)\n await this.em.flush()\n }\n\n async cleanupExpired(): Promise<number> {\n return this.em.nativeDelete(SudoSession, {\n expiresAt: { $lte: new Date() },\n })\n }\n\n private async ensureDeveloperDefaultsRegistered(): Promise<void> {\n const registryEntries = getSecuritySudoTargetEntries()\n const registryTargets = registryEntries.flatMap((entry) => entry.targets ?? [])\n const fallbackTargets = registryEntries.length === 0 ? defaultSudoTargets : []\n\n for (const target of dedupeSudoTargets([\n ...registryTargets,\n ...fallbackTargets,\n ])) {\n await this.registerDeveloperDefault(this.readDeveloperDefault(target))\n }\n }\n\n private readDeveloperDefault(target: SecuritySudoTarget): DeveloperDefaultPayload {\n return {\n targetIdentifier: target.identifier,\n label: target.label ?? null,\n ttlSeconds: target.ttlSeconds,\n challengeMethod: this.toChallengeMethod(target.challengeMethod),\n }\n }\n\n private async ensureUniqueConfig(\n targetIdentifier: string,\n tenantId: string | null,\n organizationId: string | null,\n ignoreId?: string,\n ): Promise<void> {\n const existing = await this.em.findOne(SudoChallengeConfig, {\n targetIdentifier,\n tenantId,\n organizationId,\n deletedAt: null,\n })\n if (existing && existing.id !== ignoreId) {\n throw new SudoChallengeServiceError('A sudo configuration for this target and scope already exists', 409)\n }\n }\n\n private validateScope(tenantId: string | null, organizationId: string | null): void {\n if (organizationId && !tenantId) {\n throw new SudoChallengeServiceError('Organization-scoped sudo config requires a tenant', 400)\n }\n }\n\n private isConfigVisibleToScope(config: SudoChallengeConfig, scope: SudoAuthScope): boolean {\n if (scope.isSuperAdmin) return true\n if (!scope.tenantId) return false\n const configTenantId = config.tenantId ?? null\n if (configTenantId === null) return true\n return configTenantId === scope.tenantId\n }\n\n private assertWriteScope(\n targetTenantId: string | null,\n targetOrganizationId: string | null,\n scope: SudoAuthScope,\n ): void {\n if (scope.isSuperAdmin) return\n if (!scope.tenantId) {\n throw new SudoChallengeServiceError('Sudo configuration not found', 404)\n }\n if ((targetTenantId ?? null) !== scope.tenantId) {\n throw new SudoChallengeServiceError('Sudo configuration not found', 404)\n }\n if (\n scope.organizationId !== undefined\n && scope.organizationId !== null\n && targetOrganizationId !== null\n && targetOrganizationId !== scope.organizationId\n ) {\n throw new SudoChallengeServiceError('Sudo configuration not found', 404)\n }\n }\n\n private async resolveChallengeMethod(\n configuredMethod: ChallengeMethod,\n availableMfaMethodCount: number,\n userId?: string,\n targetIdentifier?: string,\n scope?: { tenantId: string | null; organizationId: string | null },\n ): Promise<SudoMethod> {\n if (this.securityConfig.mfa.emergencyBypass) {\n let wouldHaveBeenMfa = false\n try {\n wouldHaveBeenMfa = this.resolveChallengeMethodWithoutBypass(configuredMethod, availableMfaMethodCount) === 'mfa'\n } catch {\n wouldHaveBeenMfa = configuredMethod === ChallengeMethod.MFA\n }\n if (wouldHaveBeenMfa) {\n emitMfaEmergencyBypassActiveWarning('sudo challenge downgraded to password', {\n configuredMethod,\n availableMfaMethodCount,\n userId,\n targetIdentifier,\n })\n if (scope) await requestMfaEmergencyBypassNotification(scope)\n }\n return 'password'\n }\n return this.resolveChallengeMethodWithoutBypass(configuredMethod, availableMfaMethodCount)\n }\n\n private resolveChallengeMethodWithoutBypass(\n configuredMethod: ChallengeMethod,\n availableMfaMethodCount: number,\n ): SudoMethod {\n if (configuredMethod === ChallengeMethod.PASSWORD) return 'password'\n if (configuredMethod === ChallengeMethod.MFA) {\n if (availableMfaMethodCount === 0) {\n throw new SudoChallengeServiceError('This sudo target requires MFA, but no MFA methods are configured', 400)\n }\n return 'mfa'\n }\n return availableMfaMethodCount > 0 ? 'mfa' : 'password'\n }\n\n private async resolveProtection(\n em: EntityManager,\n targetIdentifier: string,\n tenantId: string | null,\n organizationId: string | null,\n ): Promise<SudoProtectionResolution> {\n const candidates = await em.find(SudoChallengeConfig, {\n targetIdentifier,\n deletedAt: null,\n })\n\n const resolved = candidates\n .filter((config) => this.matchesScope(config, tenantId, organizationId))\n .sort((left, right) => this.compareConfigPriority(left, right, tenantId, organizationId))[0]\n\n if (!resolved || !resolved.isEnabled) {\n return { protected: false }\n }\n\n return { protected: true, config: resolved }\n }\n\n private matchesScope(config: SudoChallengeConfig, tenantId: string | null, organizationId: string | null): boolean {\n if (config.organizationId) {\n return config.organizationId === organizationId && config.tenantId === tenantId\n }\n if (config.tenantId) {\n return config.tenantId === tenantId\n }\n return true\n }\n\n private compareConfigPriority(\n left: SudoChallengeConfig,\n right: SudoChallengeConfig,\n tenantId: string | null,\n organizationId: string | null,\n ): number {\n const leftScore = this.getScopePriority(left, tenantId, organizationId)\n const rightScore = this.getScopePriority(right, tenantId, organizationId)\n if (leftScore !== rightScore) return leftScore - rightScore\n if (left.isDeveloperDefault !== right.isDeveloperDefault) {\n return left.isDeveloperDefault ? 1 : -1\n }\n return right.updatedAt.getTime() - left.updatedAt.getTime()\n }\n\n private getScopePriority(\n config: SudoChallengeConfig,\n tenantId: string | null,\n organizationId: string | null,\n ): number {\n if (config.organizationId === organizationId && config.tenantId === tenantId) return 0\n if (!config.organizationId && config.tenantId === tenantId) return 1\n if (!config.organizationId && !config.tenantId && !config.isDeveloperDefault) return 2\n if (!config.organizationId && !config.tenantId && config.isDeveloperDefault) return 3\n return 4\n }\n\n private async getPendingSession(sessionId: string): Promise<BoundPendingSudoSession> {\n const session = await this.em.findOne(SudoSession, { id: sessionId })\n if (!session) {\n throw new SudoChallengeServiceError('Sudo challenge session not found', 404)\n }\n if (!session.targetIdentifier || !session.sudoConfigId || !session.sudoConfigUpdatedAt) {\n throw new SudoChallengeServiceError('Sudo challenge session not found', 404)\n }\n if (session.verifiedAt) {\n throw new SudoChallengeServiceError('Unable to verify sudo challenge', 400)\n }\n if (session.expiresAt.getTime() <= Date.now()) {\n throw new SudoChallengeServiceError('Sudo challenge session expired', 400)\n }\n return session as BoundPendingSudoSession\n }\n\n private async findUserScope(userId: string): Promise<UserScope | null> {\n const user = await findOneWithDecryption(\n this.em,\n User,\n { id: userId, deletedAt: null },\n undefined,\n {},\n )\n\n if (!user) return null\n return {\n id: String(user.id),\n tenantId: user.tenantId ? String(user.tenantId) : null,\n organizationId: user.organizationId ? String(user.organizationId) : null,\n }\n }\n\n private normalizeTtl(value?: number | null): number {\n const rawValue = value ?? this.securityConfig.sudo.defaultTtlSeconds\n return Math.min(\n Math.max(rawValue, this.securityConfig.sudo.minTtlSeconds),\n this.securityConfig.sudo.maxTtlSeconds,\n )\n }\n\n private readPassword(payload: unknown): string {\n if (!payload || typeof payload !== 'object') {\n throw new SudoChallengeServiceError('Password is required', 400)\n }\n const maybePassword = (payload as Record<string, unknown>).password\n if (typeof maybePassword !== 'string' || maybePassword.trim().length === 0) {\n throw new SudoChallengeServiceError('Password is required', 400)\n }\n return maybePassword\n }\n\n private signToken(payload: SignedSudoTokenPayload): string {\n const encodedPayload = Buffer.from(JSON.stringify(payload)).toString('base64url')\n const signature = createHmac('sha256', this.getSudoSecret()).update(encodedPayload).digest('base64url')\n return `${encodedPayload}.${signature}`\n }\n\n private readSignedToken(token: string): SignedSudoTokenPayload | null {\n const [encodedPayload, signature] = token.split('.')\n if (!encodedPayload || !signature) return null\n\n const expected = createHmac('sha256', this.getSudoSecret()).update(encodedPayload).digest('base64url')\n const signatureBuffer = Buffer.from(signature)\n const expectedBuffer = Buffer.from(expected)\n if (signatureBuffer.length !== expectedBuffer.length) return null\n if (!timingSafeEqual(signatureBuffer, expectedBuffer)) return null\n\n try {\n const parsed = signedSudoTokenPayloadSchema.safeParse(\n JSON.parse(Buffer.from(encodedPayload, 'base64url').toString('utf8')),\n )\n if (!parsed.success) return null\n return parsed.data\n } catch {\n return null\n }\n }\n\n private getSudoSecret(): string {\n const secret =\n readTrimmedEnv(process.env.OM_SECURITY_SUDO_SECRET)\n ?? readTrimmedEnv(process.env.AUTH_JWT_SECRET)\n ?? readTrimmedEnv(process.env.AUTH_SECRET)\n ?? readTrimmedEnv(process.env.JWT_SECRET)\n if (secret) return secret\n throw new Error(\n 'Sudo step-up tokens require OM_SECURITY_SUDO_SECRET, AUTH_JWT_SECRET, AUTH_SECRET, or JWT_SECRET to be set.',\n )\n }\n\n private toChallengeMethod(\n method: SecuritySudoTarget['challengeMethod'],\n ): ChallengeMethod | undefined {\n switch (method) {\n case 'password':\n return ChallengeMethod.PASSWORD\n case 'mfa':\n return ChallengeMethod.MFA\n case 'auto':\n default:\n return ChallengeMethod.AUTO\n }\n }\n}\n\nexport default SudoChallengeService\n"],
5
+ "mappings": "AAAA,SAAS,YAAY,aAAa,uBAAuB;AACzD,SAAS,SAAS;AAElB,SAAS,YAAY;AACrB,SAAS,6BAA6B;AACtC;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,yBAAyB;AAQlC,SAAS,eAAe,0BAA0B;AAElD,SAAS,qCAAqC,0BAA0B,6CAA6C;AAerH,MAAM,+BAA+B,EAAE,OAAO;AAAA,EAC5C,KAAK,EAAE,OAAO;AAAA,EACd,KAAK,EAAE,OAAO;AAAA,EACd,KAAK,EAAE,OAAO,EAAE,SAAS;AAAA,EACzB,KAAK,EAAE,OAAO,EAAE,SAAS;AAAA,EACzB,KAAK,EAAE,OAAO;AAAA,EACd,KAAK,EAAE,OAAO;AAChB,CAAC;AA6BD,SAAS,eAAe,OAA+C;AACrE,QAAM,UAAU,OAAO,KAAK;AAC5B,SAAO,UAAU,UAAU;AAC7B;AAEO,MAAM,kCAAkC,MAAM;AAAA,EACnD,YACE,SACgB,YAChB;AACA,UAAM,OAAO;AAFG;AAGhB,SAAK,OAAO;AAAA,EACd;AACF;AAEO,MAAM,qBAAqB;AAAA,EAChC,YAC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,CAAC,gBAAgB,SAAS,IAAI,MAAM,MAAM,GAAG;AACnD,QAAI,CAAC,kBAAkB,CAAC,UAAW,QAAO;AAE1C,UAAM,WAAW,WAAW,UAAU,KAAK,cAAc,CAAC,EAAE,OAAO,cAAc,EAAE,OAAO,WAAW;AACrG,UAAM,kBAAkB,OAAO,KAAK,SAAS;AAC7C,UAAM,iBAAiB,OAAO,KAAK,QAAQ;AAC3C,QAAI,gBAAgB,WAAW,eAAe,OAAQ,QAAO;AAC7D,QAAI,CAAC,gBAAgB,iBAAiB,cAAc,EAAG,QAAO;AAE9D,QAAI;AACF,YAAM,SAAS,6BAA6B;AAAA,QAC1C,KAAK,MAAM,OAAO,KAAK,gBAAgB,WAAW,EAAE,SAAS,MAAM,CAAC;AAAA,MACtE;AACA,UAAI,CAAC,OAAO,QAAS,QAAO;AAC5B,aAAO,OAAO;AAAA,IAChB,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;AAAA,EAEQ,gBAAwB;AAC9B,UAAM,SACJ,eAAe,QAAQ,IAAI,uBAAuB,KAC/C,eAAe,QAAQ,IAAI,eAAe,KAC1C,eAAe,QAAQ,IAAI,WAAW,KACtC,eAAe,QAAQ,IAAI,UAAU;AAC1C,QAAI,OAAQ,QAAO;AACnB,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAAA,EAEQ,kBACN,QAC6B;AAC7B,YAAQ,QAAQ;AAAA,MACd,KAAK;AACH,eAAO,gBAAgB;AAAA,MACzB,KAAK;AACH,eAAO,gBAAgB;AAAA,MACzB,KAAK;AAAA,MACL;AACE,eAAO,gBAAgB;AAAA,IAC3B;AAAA,EACF;AACF;AAEA,IAAO,+BAAQ;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1,52 @@
1
+ import { Notification } from "@open-mercato/core/modules/notifications/data/entities";
2
+ import { buildBatchNotificationFromType } from "@open-mercato/core/modules/notifications/lib/notificationBuilder";
3
+ import { getRecipientUserIdsForFeature, getScopedNotificationRecipientUserIds } from "@open-mercato/core/modules/notifications/lib/notificationRecipients";
4
+ import { resolveNotificationService } from "@open-mercato/core/modules/notifications/lib/notificationService";
5
+ import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
6
+ import { notificationTypes } from "../notifications.js";
7
+ import { mfaEmergencyBypassEventSchema } from "../data/validators.js";
8
+ const metadata = {
9
+ event: "security.mfa.emergency_bypass_detected",
10
+ persistent: true,
11
+ id: "security:mfa-emergency-bypass-notification"
12
+ };
13
+ async function mfaEmergencyBypassNotification(payload, ctx) {
14
+ const parsed = mfaEmergencyBypassEventSchema.safeParse(payload);
15
+ if (!parsed.success) return;
16
+ const { tenantId, organizationId, observedAt } = parsed.data;
17
+ if (ctx.tenantId !== tenantId || ctx.organizationId !== organizationId) return;
18
+ const typeDef = notificationTypes.find((type) => type.type === metadata.event);
19
+ if (!typeDef) return;
20
+ const scope = { tenantId, organizationId };
21
+ const groupKey = `${metadata.event}:${observedAt.slice(0, 10)}`;
22
+ const em = ctx.resolve("em").fork();
23
+ const existing = await findOneWithDecryption(em, Notification, {
24
+ ...scope,
25
+ type: metadata.event,
26
+ groupKey
27
+ }, void 0, scope);
28
+ if (existing) return;
29
+ const candidates = await getRecipientUserIdsForFeature(em.getKysely(), tenantId, "security.mfa.manage");
30
+ if (candidates.length === 0) return;
31
+ const scopedCandidates = await getScopedNotificationRecipientUserIds(em.getKysely(), tenantId, organizationId, candidates);
32
+ const rbac = ctx.resolve("rbacService");
33
+ const recipientUserIds = [];
34
+ for (let offset = 0; offset < scopedCandidates.length; offset += 10) {
35
+ const allowed = await Promise.all(scopedCandidates.slice(offset, offset + 10).map(async (userId) => ({
36
+ userId,
37
+ allowed: await rbac.userHasAllFeatures(userId, ["security.mfa.manage"], scope)
38
+ })));
39
+ recipientUserIds.push(...allowed.filter((candidate) => candidate.allowed).map((candidate) => candidate.userId));
40
+ }
41
+ if (recipientUserIds.length === 0) return;
42
+ await resolveNotificationService(ctx).createBatch(
43
+ { ...buildBatchNotificationFromType(typeDef, { recipientUserIds, groupKey }), channels: ["in_app"] },
44
+ scope,
45
+ { skipExistingGroup: true }
46
+ );
47
+ }
48
+ export {
49
+ mfaEmergencyBypassNotification as default,
50
+ metadata
51
+ };
52
+ //# sourceMappingURL=mfa-emergency-bypass-notification.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/modules/security/subscribers/mfa-emergency-bypass-notification.ts"],
4
+ "sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport { Notification } from '@open-mercato/core/modules/notifications/data/entities'\nimport { buildBatchNotificationFromType } from '@open-mercato/core/modules/notifications/lib/notificationBuilder'\nimport { getRecipientUserIdsForFeature, getScopedNotificationRecipientUserIds } from '@open-mercato/core/modules/notifications/lib/notificationRecipients'\nimport { resolveNotificationService } from '@open-mercato/core/modules/notifications/lib/notificationService'\nimport { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { notificationTypes } from '../notifications'\nimport { mfaEmergencyBypassEventSchema } from '../data/validators'\n\nexport const metadata = {\n event: 'security.mfa.emergency_bypass_detected',\n persistent: true,\n id: 'security:mfa-emergency-bypass-notification',\n}\n\ntype ResolverContext = {\n resolve: <T = unknown>(name: string) => T\n tenantId?: string | null\n organizationId?: string | null\n}\n\ntype RbacServiceLike = {\n userHasAllFeatures: (\n userId: string,\n features: string[],\n scope: { tenantId: string; organizationId: string },\n ) => Promise<boolean>\n}\n\nexport default async function mfaEmergencyBypassNotification(payload: unknown, ctx: ResolverContext) {\n const parsed = mfaEmergencyBypassEventSchema.safeParse(payload)\n if (!parsed.success) return\n const { tenantId, organizationId, observedAt } = parsed.data\n if (ctx.tenantId !== tenantId || ctx.organizationId !== organizationId) return\n\n const typeDef = notificationTypes.find((type) => type.type === metadata.event)\n if (!typeDef) return\n const scope = { tenantId, organizationId }\n const groupKey = `${metadata.event}:${observedAt.slice(0, 10)}`\n const em = ctx.resolve<EntityManager>('em').fork()\n\n const existing = await findOneWithDecryption(em, Notification, {\n ...scope,\n type: metadata.event,\n groupKey,\n }, undefined, scope)\n if (existing) return\n\n const candidates = await getRecipientUserIdsForFeature(em.getKysely(), tenantId, 'security.mfa.manage')\n if (candidates.length === 0) return\n const scopedCandidates = await getScopedNotificationRecipientUserIds(em.getKysely(), tenantId, organizationId, candidates)\n const rbac = ctx.resolve<RbacServiceLike>('rbacService')\n const recipientUserIds: string[] = []\n for (let offset = 0; offset < scopedCandidates.length; offset += 10) {\n const allowed = await Promise.all(scopedCandidates.slice(offset, offset + 10).map(async (userId) => ({\n userId,\n allowed: await rbac.userHasAllFeatures(userId, ['security.mfa.manage'], scope),\n })))\n recipientUserIds.push(...allowed.filter((candidate) => candidate.allowed).map((candidate) => candidate.userId))\n }\n if (recipientUserIds.length === 0) return\n\n await resolveNotificationService(ctx).createBatch(\n { ...buildBatchNotificationFromType(typeDef, { recipientUserIds, groupKey }), channels: ['in_app'] },\n scope,\n { skipExistingGroup: true },\n )\n}\n"],
5
+ "mappings": "AACA,SAAS,oBAAoB;AAC7B,SAAS,sCAAsC;AAC/C,SAAS,+BAA+B,6CAA6C;AACrF,SAAS,kCAAkC;AAC3C,SAAS,6BAA6B;AACtC,SAAS,yBAAyB;AAClC,SAAS,qCAAqC;AAEvC,MAAM,WAAW;AAAA,EACtB,OAAO;AAAA,EACP,YAAY;AAAA,EACZ,IAAI;AACN;AAgBA,eAAO,+BAAsD,SAAkB,KAAsB;AACnG,QAAM,SAAS,8BAA8B,UAAU,OAAO;AAC9D,MAAI,CAAC,OAAO,QAAS;AACrB,QAAM,EAAE,UAAU,gBAAgB,WAAW,IAAI,OAAO;AACxD,MAAI,IAAI,aAAa,YAAY,IAAI,mBAAmB,eAAgB;AAExE,QAAM,UAAU,kBAAkB,KAAK,CAAC,SAAS,KAAK,SAAS,SAAS,KAAK;AAC7E,MAAI,CAAC,QAAS;AACd,QAAM,QAAQ,EAAE,UAAU,eAAe;AACzC,QAAM,WAAW,GAAG,SAAS,KAAK,IAAI,WAAW,MAAM,GAAG,EAAE,CAAC;AAC7D,QAAM,KAAK,IAAI,QAAuB,IAAI,EAAE,KAAK;AAEjD,QAAM,WAAW,MAAM,sBAAsB,IAAI,cAAc;AAAA,IAC7D,GAAG;AAAA,IACH,MAAM,SAAS;AAAA,IACf;AAAA,EACF,GAAG,QAAW,KAAK;AACnB,MAAI,SAAU;AAEd,QAAM,aAAa,MAAM,8BAA8B,GAAG,UAAU,GAAG,UAAU,qBAAqB;AACtG,MAAI,WAAW,WAAW,EAAG;AAC7B,QAAM,mBAAmB,MAAM,sCAAsC,GAAG,UAAU,GAAG,UAAU,gBAAgB,UAAU;AACzH,QAAM,OAAO,IAAI,QAAyB,aAAa;AACvD,QAAM,mBAA6B,CAAC;AACpC,WAAS,SAAS,GAAG,SAAS,iBAAiB,QAAQ,UAAU,IAAI;AACnE,UAAM,UAAU,MAAM,QAAQ,IAAI,iBAAiB,MAAM,QAAQ,SAAS,EAAE,EAAE,IAAI,OAAO,YAAY;AAAA,MACnG;AAAA,MACA,SAAS,MAAM,KAAK,mBAAmB,QAAQ,CAAC,qBAAqB,GAAG,KAAK;AAAA,IAC/E,EAAE,CAAC;AACH,qBAAiB,KAAK,GAAG,QAAQ,OAAO,CAAC,cAAc,UAAU,OAAO,EAAE,IAAI,CAAC,cAAc,UAAU,MAAM,CAAC;AAAA,EAChH;AACA,MAAI,iBAAiB,WAAW,EAAG;AAEnC,QAAM,2BAA2B,GAAG,EAAE;AAAA,IACpC,EAAE,GAAG,+BAA+B,SAAS,EAAE,kBAAkB,SAAS,CAAC,GAAG,UAAU,CAAC,QAAQ,EAAE;AAAA,IACnG;AAAA,IACA,EAAE,mBAAmB,KAAK;AAAA,EAC5B;AACF;",
6
+ "names": []
7
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-mercato/enterprise",
3
- "version": "0.10.1-develop.7768.1.90fa052c11",
3
+ "version": "0.10.1-develop.7769.1.0e6458bb99",
4
4
  "license": "SEE LICENSE IN LICENSE.md",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -70,12 +70,12 @@
70
70
  "@dnd-kit/core": "^6.3.1",
71
71
  "@dnd-kit/sortable": "^10.0.0",
72
72
  "@dnd-kit/utilities": "^3.2.2",
73
- "@open-mercato/ai-assistant": "0.10.1-develop.7768.1.90fa052c11",
74
- "@open-mercato/core": "0.10.1-develop.7768.1.90fa052c11",
75
- "@open-mercato/queue": "0.10.1-develop.7768.1.90fa052c11",
76
- "@open-mercato/scheduler": "0.10.1-develop.7768.1.90fa052c11",
77
- "@open-mercato/ui": "0.10.1-develop.7768.1.90fa052c11",
78
- "@open-mercato/web-research": "0.10.1-develop.7768.1.90fa052c11",
73
+ "@open-mercato/ai-assistant": "0.10.1-develop.7769.1.0e6458bb99",
74
+ "@open-mercato/core": "0.10.1-develop.7769.1.0e6458bb99",
75
+ "@open-mercato/queue": "0.10.1-develop.7769.1.0e6458bb99",
76
+ "@open-mercato/scheduler": "0.10.1-develop.7769.1.0e6458bb99",
77
+ "@open-mercato/ui": "0.10.1-develop.7769.1.0e6458bb99",
78
+ "@open-mercato/web-research": "0.10.1-develop.7769.1.0e6458bb99",
79
79
  "@simplewebauthn/browser": "^13.3.0",
80
80
  "@simplewebauthn/server": "^13.3.2",
81
81
  "@simplewebauthn/types": "^12.0.0",
@@ -87,13 +87,13 @@
87
87
  },
88
88
  "peerDependencies": {
89
89
  "@mikro-orm/core": "^7.0.14",
90
- "@open-mercato/shared": "0.10.1-develop.7768.1.90fa052c11",
90
+ "@open-mercato/shared": "0.10.1-develop.7769.1.0e6458bb99",
91
91
  "react": "^19.0.0",
92
92
  "react-dom": "^19.0.0",
93
93
  "zod": ">=3.23.0"
94
94
  },
95
95
  "devDependencies": {
96
- "@open-mercato/shared": "0.10.1-develop.7768.1.90fa052c11",
96
+ "@open-mercato/shared": "0.10.1-develop.7769.1.0e6458bb99",
97
97
  "@types/jest": "^30.0.0",
98
98
  "@types/react": "^19.2.18",
99
99
  "@types/react-dom": "^19.2.5",
@@ -1,6 +1,6 @@
1
1
  import type { ApiInterceptor } from '@open-mercato/shared/lib/crud/api-interceptor'
2
2
  import { signJwt, verifyJwt } from '@open-mercato/shared/lib/auth/jwt'
3
- import { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig } from '../lib/security-config'
3
+ import { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig, requestMfaEmergencyBypassNotification } from '../lib/security-config'
4
4
 
5
5
  type JwtClaims = {
6
6
  sub: string
@@ -68,6 +68,7 @@ export const interceptors: ApiInterceptor[] = [
68
68
 
69
69
  if (readSecurityModuleConfig().mfa.emergencyBypass) {
70
70
  emitMfaEmergencyBypassActiveWarning('login MFA challenge bypassed', { userId: claims.sub })
71
+ await requestMfaEmergencyBypassNotification({ tenantId: claims.tenantId, organizationId: claims.orgId })
71
72
  return {}
72
73
  }
73
74
 
@@ -6,6 +6,12 @@ import { readSecurityModuleConfig } from '../lib/security-config'
6
6
  const passwordSchema = buildPasswordSchema()
7
7
  const securityConfig = readSecurityModuleConfig()
8
8
 
9
+ export const mfaEmergencyBypassEventSchema = z.object({
10
+ tenantId: z.string().uuid(),
11
+ organizationId: z.string().uuid(),
12
+ observedAt: z.string().datetime(),
13
+ })
14
+
9
15
  export const changePasswordSchema = z.object({
10
16
  currentPassword: z.string().min(1),
11
17
  newPassword: passwordSchema,
@@ -23,6 +23,7 @@ const events = [
23
23
  { id: 'security.mfa.verified', label: 'MFA Verified', category: 'lifecycle' },
24
24
  { id: 'security.mfa.otp.sent', label: 'MFA OTP Sent', category: 'lifecycle' },
25
25
  { id: 'security.mfa.reset', label: 'MFA Reset', category: 'lifecycle' },
26
+ { id: 'security.mfa.emergency_bypass_detected', label: 'MFA Emergency Bypass Active', category: 'system', excludeFromTriggers: true },
26
27
  { id: 'security.recovery.regenerated', label: 'Recovery Codes Regenerated', category: 'lifecycle' },
27
28
  { id: 'security.recovery.used', label: 'Recovery Code Used', category: 'lifecycle' },
28
29
  { id: 'security.enforcement.created', label: 'MFA Enforcement Created', category: 'lifecycle' },
@@ -329,6 +329,8 @@
329
329
  "security.notifications.enforcementDeadline.bodyOverdue": "Deine MFA-Registrierungsfrist ist am {deadline} abgelaufen. Richte MFA sofort ein, um den Kontozugriff zu erhalten.",
330
330
  "security.notifications.enforcementDeadline.bodyWithDeadline": "Deine Organisation erfordert jetzt MFA. Richte es innerhalb von {days} Tag(en) vor {deadline} ein, um den Kontozugriff zu erhalten.",
331
331
  "security.notifications.enforcementDeadline.title": "MFA-Registrierung erforderlich",
332
+ "security.notifications.mfaEmergencyBypass.body": "In Ihrer Organisation wurde eine MFA-Notfallumgehung erkannt. Der MFA-Schutz ist möglicherweise deaktiviert. Bitten Sie Ihren Systembetreiber, OM_SECURITY_MFA_EMERGENCY_BYPASS nach dem Notfall zu deaktivieren.",
333
+ "security.notifications.mfaEmergencyBypass.title": "MFA-Notfallumgehung erkannt",
332
334
  "security.notifications.mfaEnrolled.body": "{method} wurde für dein Konto aktiviert.",
333
335
  "security.notifications.mfaEnrolled.title": "MFA-Methode aktiviert",
334
336
  "security.notifications.mfaReset.body": "Ein Administrator hat deine MFA-Methoden zurückgesetzt. Grund: {reason}",
@@ -329,6 +329,8 @@
329
329
  "security.notifications.enforcementDeadline.bodyOverdue": "Your MFA enrollment deadline passed on {deadline}. Set up MFA immediately to keep account access.",
330
330
  "security.notifications.enforcementDeadline.bodyWithDeadline": "Your organization now requires MFA. Set it up within {days} day(s) before {deadline} to keep account access.",
331
331
  "security.notifications.enforcementDeadline.title": "MFA enrollment required",
332
+ "security.notifications.mfaEmergencyBypass.body": "MFA emergency bypass was detected in your organization. MFA protections may be disabled. Ask your system operator to disable OM_SECURITY_MFA_EMERGENCY_BYPASS after the emergency.",
333
+ "security.notifications.mfaEmergencyBypass.title": "MFA emergency bypass detected",
332
334
  "security.notifications.mfaEnrolled.body": "{method} was enabled for your account.",
333
335
  "security.notifications.mfaEnrolled.title": "MFA method enabled",
334
336
  "security.notifications.mfaReset.body": "An administrator reset your MFA methods. Reason: {reason}",
@@ -329,6 +329,8 @@
329
329
  "security.notifications.enforcementDeadline.bodyOverdue": "Tu plazo de inscripción en MFA venció el {deadline}. Configura MFA de inmediato para mantener el acceso a tu cuenta.",
330
330
  "security.notifications.enforcementDeadline.bodyWithDeadline": "Tu organización ahora requiere MFA. Configúralo dentro de {days} día(s) antes del {deadline} para mantener el acceso a tu cuenta.",
331
331
  "security.notifications.enforcementDeadline.title": "Se requiere inscripción en MFA",
332
+ "security.notifications.mfaEmergencyBypass.body": "Se detectó una omisión de emergencia de MFA en tu organización. La protección MFA puede estar desactivada. Pide al operador del sistema que desactive OM_SECURITY_MFA_EMERGENCY_BYPASS después de la emergencia.",
333
+ "security.notifications.mfaEmergencyBypass.title": "Se detectó una omisión de emergencia de MFA",
332
334
  "security.notifications.mfaEnrolled.body": "{method} fue habilitado para tu cuenta.",
333
335
  "security.notifications.mfaEnrolled.title": "Método MFA habilitado",
334
336
  "security.notifications.mfaReset.body": "Un administrador restableció tus métodos MFA. Motivo: {reason}",
@@ -329,6 +329,8 @@
329
329
  "security.notifications.enforcementDeadline.bodyOverdue": "MFA 등록 마감일인 {deadline}이(가) 지났습니다. 계정 접근을 유지하려면 즉시 MFA를 설정하세요.",
330
330
  "security.notifications.enforcementDeadline.bodyWithDeadline": "이제 조직에서 MFA를 요구합니다. 계정 접근을 유지하려면 {deadline} 전까지 {days}일 이내에 설정하세요.",
331
331
  "security.notifications.enforcementDeadline.title": "MFA 등록이 필요합니다",
332
+ "security.notifications.mfaEmergencyBypass.body": "조직에서 MFA 긴급 우회가 감지되었습니다. MFA 보호가 비활성화되어 있을 수 있습니다. 긴급 상황이 끝나면 시스템 운영자에게 OM_SECURITY_MFA_EMERGENCY_BYPASS를 비활성화하도록 요청하세요.",
333
+ "security.notifications.mfaEmergencyBypass.title": "MFA 긴급 우회 감지",
332
334
  "security.notifications.mfaEnrolled.body": "계정에 {method}이(가) 활성화되었습니다.",
333
335
  "security.notifications.mfaEnrolled.title": "MFA 방법이 활성화됨",
334
336
  "security.notifications.mfaReset.body": "관리자가 회원님의 MFA 방법을 재설정했습니다. 사유: {reason}",
@@ -329,6 +329,8 @@
329
329
  "security.notifications.enforcementDeadline.bodyOverdue": "Twój termin rejestracji MFA minął {deadline}. Skonfiguruj MFA natychmiast, aby zachować dostęp do konta.",
330
330
  "security.notifications.enforcementDeadline.bodyWithDeadline": "Twoja organizacja wymaga teraz MFA. Skonfiguruj je w ciągu {days} dnia/dni przed {deadline}, aby zachować dostęp do konta.",
331
331
  "security.notifications.enforcementDeadline.title": "Wymagana rejestracja MFA",
332
+ "security.notifications.mfaEmergencyBypass.body": "W Twojej organizacji wykryto awaryjne pominięcie MFA. Ochrona MFA może być wyłączona. Poproś operatora systemu o wyłączenie OM_SECURITY_MFA_EMERGENCY_BYPASS po zakończeniu sytuacji awaryjnej.",
333
+ "security.notifications.mfaEmergencyBypass.title": "Wykryto awaryjne pominięcie MFA",
332
334
  "security.notifications.mfaEnrolled.body": "Metoda {method} została włączona dla Twojego konta.",
333
335
  "security.notifications.mfaEnrolled.title": "Metoda MFA włączona",
334
336
  "security.notifications.mfaReset.body": "Administrator zresetował Twoje metody MFA. Powód: {reason}",
@@ -1,7 +1,7 @@
1
1
  import type { AuthContext } from '@open-mercato/shared/lib/auth/server'
2
2
  import { createLogger } from '@open-mercato/shared/lib/logger'
3
3
  import { isEnforcementDeadlineOverdue } from '../services/MfaEnforcementService'
4
- import { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig } from './security-config'
4
+ import { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig, requestMfaEmergencyBypassNotification } from './security-config'
5
5
 
6
6
  const MFA_ENROLLMENT_PATH = '/backend/profile/security/mfa'
7
7
  const logger = createLogger('security').child({ component: 'mfa-enforcement-redirect' })
@@ -105,6 +105,7 @@ export async function resolveMfaEnrollmentRedirect(args: {
105
105
  pathname,
106
106
  reason: 'enforcement-service-unavailable',
107
107
  })
108
+ void requestMfaEmergencyBypassNotification({ tenantId: auth.tenantId, organizationId: auth.orgId })
108
109
  return null
109
110
  }
110
111
  logger.error('Unable to resolve MFA enforcement service; redirecting to enrollment', {
@@ -128,6 +129,7 @@ export async function resolveMfaEnrollmentRedirect(args: {
128
129
  compliant: compliance.compliant,
129
130
  overdue: deadlineState.overdue,
130
131
  })
132
+ void requestMfaEmergencyBypassNotification({ tenantId: auth.tenantId, organizationId: auth.orgId })
131
133
  return null
132
134
  }
133
135
 
@@ -140,6 +142,7 @@ export async function resolveMfaEnrollmentRedirect(args: {
140
142
  pathname,
141
143
  reason: 'compliance-check-failed',
142
144
  })
145
+ void requestMfaEmergencyBypassNotification({ tenantId: auth.tenantId, organizationId: auth.orgId })
143
146
  return null
144
147
  }
145
148
  logger.error('Unable to verify MFA enforcement compliance; redirecting to enrollment', { err: error })
@@ -1,5 +1,7 @@
1
1
  import { parseBooleanWithDefault } from '@open-mercato/shared/lib/boolean'
2
2
  import { createLogger } from '@open-mercato/shared/lib/logger'
3
+ import { getTelemetryRuntime } from '@open-mercato/shared/lib/telemetry/runtime'
4
+ import { emitSecurityEvent } from '../events'
3
5
 
4
6
  const TOTP_PERIOD_SECONDS = 30
5
7
  const TOTP_DIGITS = 6
@@ -7,6 +9,7 @@ const DEFAULT_SETUP_TTL_MS = 10 * 60 * 1000
7
9
  const DEFAULT_WEBAUTHN_CHALLENGE_TTL_MS = 5 * 60 * 1000
8
10
  const DEFAULT_SUDO_PENDING_CHALLENGE_TTL_MS = 10 * 60 * 1000
9
11
  const MIN_SUDO_TTL_SECONDS = 30
12
+ const MFA_BYPASS_NOTIFICATION_ENQUEUE_TIMEOUT_MS = 2_000
10
13
 
11
14
  const securityBypassLogger = createLogger('security').child({ component: 'mfa-emergency-bypass' })
12
15
  let startupBypassWarningEmitted = false
@@ -182,6 +185,34 @@ export function emitMfaEmergencyBypassActiveWarning(context: string, extra?: Rec
182
185
  })
183
186
  }
184
187
 
188
+ export async function requestMfaEmergencyBypassNotification(scope: {
189
+ tenantId?: string | null
190
+ organizationId?: string | null
191
+ }): Promise<void> {
192
+ if (!scope.tenantId || !scope.organizationId) return
193
+ const { tenantId, organizationId } = scope
194
+ let timeout: ReturnType<typeof setTimeout> | undefined
195
+ try {
196
+ await Promise.race([
197
+ emitSecurityEvent('security.mfa.emergency_bypass_detected', {
198
+ tenantId,
199
+ organizationId,
200
+ observedAt: new Date().toISOString(),
201
+ }, { persistent: true, tenantId, organizationId }),
202
+ new Promise<never>((_resolve, reject) => {
203
+ timeout = setTimeout(() => reject(new Error('[internal] MFA bypass notification enqueue timed out')), MFA_BYPASS_NOTIFICATION_ENQUEUE_TIMEOUT_MS)
204
+ }),
205
+ ])
206
+ } catch (error) {
207
+ try {
208
+ securityBypassLogger.error('Unable to enqueue MFA emergency bypass notification', { err: error })
209
+ getTelemetryRuntime()?.reportError(error, { module: 'security', code: 'security.mfa_bypass_notification_failed' })
210
+ } catch {}
211
+ } finally {
212
+ if (timeout) clearTimeout(timeout)
213
+ }
214
+ }
215
+
185
216
  export function emitMfaEmergencyBypassStartupWarningIfNeeded(env: NodeJS.ProcessEnv = process.env): void {
186
217
  if (startupBypassWarningEmitted) return
187
218
  const bypass = parseBooleanWithDefault(
@@ -1,6 +1,18 @@
1
1
  import type { NotificationTypeDefinition } from '@open-mercato/shared/modules/notifications/types'
2
2
 
3
3
  export const notificationTypes: NotificationTypeDefinition[] = [
4
+ {
5
+ type: 'security.mfa.emergency_bypass_detected',
6
+ module: 'security',
7
+ category: 'auth',
8
+ channels: ['in_app'],
9
+ titleKey: 'security.notifications.mfaEmergencyBypass.title',
10
+ bodyKey: 'security.notifications.mfaEmergencyBypass.body',
11
+ icon: 'shield-alert',
12
+ severity: 'warning',
13
+ actions: [],
14
+ expiresAfterHours: 168,
15
+ },
4
16
  {
5
17
  type: 'security.password.changed',
6
18
  module: 'security',
@@ -24,7 +24,7 @@ import type { MfaService } from './MfaService'
24
24
  import type { MfaVerificationService } from './MfaVerificationService'
25
25
  import { sudoTargets as defaultSudoTargets } from '../security.sudo'
26
26
  import type { SecurityModuleConfig } from '../lib/security-config'
27
- import { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig } from '../lib/security-config'
27
+ import { emitMfaEmergencyBypassActiveWarning, readSecurityModuleConfig, requestMfaEmergencyBypassNotification } from '../lib/security-config'
28
28
 
29
29
  type SudoMethod = 'password' | 'mfa'
30
30
 
@@ -168,7 +168,10 @@ export class SudoChallengeService {
168
168
  }
169
169
 
170
170
  const userMethods = await this.mfaService.getUserMethods(userId)
171
- const method = this.resolveChallengeMethod(protection.config.challengeMethod, userMethods.length, userId, targetIdentifier)
171
+ const method = await this.resolveChallengeMethod(protection.config.challengeMethod, userMethods.length, userId, targetIdentifier, {
172
+ tenantId: scopeTenantId,
173
+ organizationId: scopeOrganizationId,
174
+ })
172
175
 
173
176
  let sessionToken = randomBytes(16).toString('hex')
174
177
  let availableMfaMethods: SudoAvailableMethod[] | undefined
@@ -640,12 +643,13 @@ export class SudoChallengeService {
640
643
  }
641
644
  }
642
645
 
643
- private resolveChallengeMethod(
646
+ private async resolveChallengeMethod(
644
647
  configuredMethod: ChallengeMethod,
645
648
  availableMfaMethodCount: number,
646
649
  userId?: string,
647
650
  targetIdentifier?: string,
648
- ): SudoMethod {
651
+ scope?: { tenantId: string | null; organizationId: string | null },
652
+ ): Promise<SudoMethod> {
649
653
  if (this.securityConfig.mfa.emergencyBypass) {
650
654
  let wouldHaveBeenMfa = false
651
655
  try {
@@ -660,6 +664,7 @@ export class SudoChallengeService {
660
664
  userId,
661
665
  targetIdentifier,
662
666
  })
667
+ if (scope) await requestMfaEmergencyBypassNotification(scope)
663
668
  }
664
669
  return 'password'
665
670
  }
@@ -0,0 +1,68 @@
1
+ import type { EntityManager } from '@mikro-orm/postgresql'
2
+ import { Notification } from '@open-mercato/core/modules/notifications/data/entities'
3
+ import { buildBatchNotificationFromType } from '@open-mercato/core/modules/notifications/lib/notificationBuilder'
4
+ import { getRecipientUserIdsForFeature, getScopedNotificationRecipientUserIds } from '@open-mercato/core/modules/notifications/lib/notificationRecipients'
5
+ import { resolveNotificationService } from '@open-mercato/core/modules/notifications/lib/notificationService'
6
+ import { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'
7
+ import { notificationTypes } from '../notifications'
8
+ import { mfaEmergencyBypassEventSchema } from '../data/validators'
9
+
10
+ export const metadata = {
11
+ event: 'security.mfa.emergency_bypass_detected',
12
+ persistent: true,
13
+ id: 'security:mfa-emergency-bypass-notification',
14
+ }
15
+
16
+ type ResolverContext = {
17
+ resolve: <T = unknown>(name: string) => T
18
+ tenantId?: string | null
19
+ organizationId?: string | null
20
+ }
21
+
22
+ type RbacServiceLike = {
23
+ userHasAllFeatures: (
24
+ userId: string,
25
+ features: string[],
26
+ scope: { tenantId: string; organizationId: string },
27
+ ) => Promise<boolean>
28
+ }
29
+
30
+ export default async function mfaEmergencyBypassNotification(payload: unknown, ctx: ResolverContext) {
31
+ const parsed = mfaEmergencyBypassEventSchema.safeParse(payload)
32
+ if (!parsed.success) return
33
+ const { tenantId, organizationId, observedAt } = parsed.data
34
+ if (ctx.tenantId !== tenantId || ctx.organizationId !== organizationId) return
35
+
36
+ const typeDef = notificationTypes.find((type) => type.type === metadata.event)
37
+ if (!typeDef) return
38
+ const scope = { tenantId, organizationId }
39
+ const groupKey = `${metadata.event}:${observedAt.slice(0, 10)}`
40
+ const em = ctx.resolve<EntityManager>('em').fork()
41
+
42
+ const existing = await findOneWithDecryption(em, Notification, {
43
+ ...scope,
44
+ type: metadata.event,
45
+ groupKey,
46
+ }, undefined, scope)
47
+ if (existing) return
48
+
49
+ const candidates = await getRecipientUserIdsForFeature(em.getKysely(), tenantId, 'security.mfa.manage')
50
+ if (candidates.length === 0) return
51
+ const scopedCandidates = await getScopedNotificationRecipientUserIds(em.getKysely(), tenantId, organizationId, candidates)
52
+ const rbac = ctx.resolve<RbacServiceLike>('rbacService')
53
+ const recipientUserIds: string[] = []
54
+ for (let offset = 0; offset < scopedCandidates.length; offset += 10) {
55
+ const allowed = await Promise.all(scopedCandidates.slice(offset, offset + 10).map(async (userId) => ({
56
+ userId,
57
+ allowed: await rbac.userHasAllFeatures(userId, ['security.mfa.manage'], scope),
58
+ })))
59
+ recipientUserIds.push(...allowed.filter((candidate) => candidate.allowed).map((candidate) => candidate.userId))
60
+ }
61
+ if (recipientUserIds.length === 0) return
62
+
63
+ await resolveNotificationService(ctx).createBatch(
64
+ { ...buildBatchNotificationFromType(typeDef, { recipientUserIds, groupKey }), channels: ['in_app'] },
65
+ scope,
66
+ { skipExistingGroup: true },
67
+ )
68
+ }