{"version":3,"file":"OpenBadgesHttpModule.mjs","names":[],"sources":["../../src/http/OpenBadgesHttpModule.ts"],"sourcesContent":["import type { AgentContext, DependencyManager, Module } from '@credo-ts/core'\nimport type { Router, Request, Response, NextFunction } from 'express'\n\nimport { AgentConfig } from '@credo-ts/core'\nimport { importExpress, setRequestContext } from './router'\nimport { OpenBadgesHttpModuleConfig, type OpenBadgesHttpModuleConfigOptions } from './OpenBadgesHttpModuleConfig'\nimport { configureServiceDescriptionEndpoint } from './endpoints/serviceDescription'\nimport { configureJwksEndpoint } from './endpoints/jwks'\nimport { configureRegistrationEndpoint } from './endpoints/registration'\nimport { configureAuthorizeEndpoint } from './endpoints/authorize'\nimport { configureTokenEndpoint } from './endpoints/token'\nimport { configureRevokeEndpoint } from './endpoints/revoke'\nimport { configureIntrospectEndpoint } from './endpoints/introspect'\nimport { configureDidDocumentEndpoint } from './endpoints/did'\nimport { configureProfileEndpoints } from './endpoints/profile'\nimport { configureCredentialsEndpoints } from './endpoints/credentials'\nimport { configureStatusListEndpoint } from './endpoints/statusList'\nimport { configureRefreshEndpoint } from './endpoints/refresh'\n\nexport class OpenBadgesHttpModule implements Module {\n  public readonly config: OpenBadgesHttpModuleConfig\n\n  public constructor(options: OpenBadgesHttpModuleConfigOptions) {\n    this.config = new OpenBadgesHttpModuleConfig(options)\n  }\n\n  public register(dependencyManager: DependencyManager) {\n    dependencyManager.resolve(AgentConfig).logger.info('[OB][HTTP] Registering OpenBadgesHttpModule')\n    // Expose the module instance via DI if needed in the future\n    dependencyManager.registerInstance(OpenBadgesHttpModule, this)\n  }\n\n  public async initialize(agentContext: AgentContext): Promise<void> {\n    this.configureRouter(agentContext)\n  }\n\n  private configureRouter(rootAgentContext: AgentContext) {\n    const { json, urlencoded, Router } = importExpress()\n    const contextRouter = this.config.router\n    const endpointRouter = this.config.useTenantRouting ? Router() : contextRouter\n\n    // parse application/x-www-form-urlencoded & json\n    contextRouter.use(urlencoded({ extended: false }))\n    contextRouter.use(json())\n\n    if (this.config.useTenantRouting) {\n      contextRouter.param(this.config.actorParamName, async (req: Request, res, next, actorId: string) => {\n        if (!actorId) return res.status(404).send('Not found')\n        try {\n          const { getAgentContextForActorId } = await import('./tenants')\n          const agentContext = await getAgentContextForActorId(rootAgentContext, actorId)\n          ;(req as any).requestContext = { agentContext }\n          next()\n        } catch (e) {\n          await rootAgentContext.endSession()\n          return res.status(404).send('Not found')\n        }\n      })\n      contextRouter.use(`/:${this.config.actorParamName}`, endpointRouter)\n    } else {\n      // Attach request context for single-tenant\n      contextRouter.use(setRequestContext(rootAgentContext))\n    }\n\n    // Endpoints\n    configureServiceDescriptionEndpoint(endpointRouter, this.config)\n    configureJwksEndpoint(endpointRouter, this.config)\n    configureDidDocumentEndpoint(endpointRouter, this.config)\n    configureRegistrationEndpoint(endpointRouter, this.config)\n    configureAuthorizeEndpoint(endpointRouter as unknown as Router, this)\n    configureTokenEndpoint(endpointRouter as unknown as Router, this)\n    configureRevokeEndpoint(endpointRouter as unknown as Router, this)\n    configureIntrospectEndpoint(endpointRouter as unknown as Router, this)\n    // Consent endpoint for dev/simple flows\n    const { configureConsentEndpoint } = require('./endpoints/consent')\n    configureConsentEndpoint(endpointRouter as unknown as Router, this)\n    configureProfileEndpoints(endpointRouter as unknown as Router, this.config, this)\n    configureCredentialsEndpoints(endpointRouter as unknown as Router, this.config, this)\n    configureStatusListEndpoint(endpointRouter as unknown as Router, this.config, this)\n    configureRefreshEndpoint(endpointRouter as unknown as Router, this.config, this)\n\n    // End session middleware for tenant contexts\n    contextRouter.use(async (req: Request, _res: Response, next: NextFunction) => {\n      const agentContext = (req as any).requestContext?.agentContext\n      if (agentContext && agentContext !== rootAgentContext) {\n        await agentContext.endSession().catch(() => {})\n      }\n      next()\n    })\n\n    // Error fallthrough safeguard\n    contextRouter.use(async (err: unknown, req: Request, res: Response, next: NextFunction) => {\n      if (!res.headersSent) {\n        res.status(500).json({ error: 'server_error', error_description: 'Unexpected error' })\n      }\n      const agentContext = (req as any).requestContext?.agentContext\n      if (agentContext && agentContext !== rootAgentContext) {\n        await agentContext.endSession().catch(() => {})\n      }\n      next(err)\n    })\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;aAI2D;AAe3D,IAAa,uBAAb,MAAa,qBAAuC;CAGlD,AAAO,YAAY,SAA4C;AAC7D,OAAK,SAAS,IAAI,2BAA2B,QAAQ;;CAGvD,AAAO,SAAS,mBAAsC;AACpD,oBAAkB,QAAQ,YAAY,CAAC,OAAO,KAAK,8CAA8C;AAEjG,oBAAkB,iBAAiB,sBAAsB,KAAK;;CAGhE,MAAa,WAAW,cAA2C;AACjE,OAAK,gBAAgB,aAAa;;CAGpC,AAAQ,gBAAgB,kBAAgC;EACtD,MAAM,EAAE,MAAM,YAAY,WAAW,eAAe;EACpD,MAAM,gBAAgB,KAAK,OAAO;EAClC,MAAM,iBAAiB,KAAK,OAAO,mBAAmB,QAAQ,GAAG;AAGjE,gBAAc,IAAI,WAAW,EAAE,UAAU,OAAO,CAAC,CAAC;AAClD,gBAAc,IAAI,MAAM,CAAC;AAEzB,MAAI,KAAK,OAAO,kBAAkB;AAChC,iBAAc,MAAM,KAAK,OAAO,gBAAgB,OAAO,KAAc,KAAK,MAAM,YAAoB;AAClG,QAAI,CAAC,QAAS,QAAO,IAAI,OAAO,IAAI,CAAC,KAAK,YAAY;AACtD,QAAI;KACF,MAAM,EAAE,8BAA8B,MAAM,OAAO;AAElD,KAAC,IAAY,iBAAiB,EAAE,cADZ,MAAM,0BAA0B,kBAAkB,QAAQ,EAChC;AAC/C,WAAM;aACC,GAAG;AACV,WAAM,iBAAiB,YAAY;AACnC,YAAO,IAAI,OAAO,IAAI,CAAC,KAAK,YAAY;;KAE1C;AACF,iBAAc,IAAI,KAAK,KAAK,OAAO,kBAAkB,eAAe;QAGpE,eAAc,IAAI,kBAAkB,iBAAiB,CAAC;AAIxD,sCAAoC,gBAAgB,KAAK,OAAO;AAChE,wBAAsB,gBAAgB,KAAK,OAAO;AAClD,+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