{"version":3,"file":"tenants.mjs","names":[],"sources":["../../../src/shared/router/tenants.ts"],"sourcesContent":["import type { AgentContext, AgentContextProvider } from '@credo-ts/core'\nimport { getApiForModuleByName, InjectionSymbols } from '@credo-ts/core'\nimport type { TenantsModule } from '@credo-ts/tenants'\n\nconst OPENID4VC_ACTOR_IDS_METADATA_KEY = '_openid4vc/openId4VcActorIds'\n\nexport async function getAgentContextForActorId(rootAgentContext: AgentContext, actorId: string) {\n  // Check if multi-tenancy is enabled, and if so find the associated multi-tenant record\n  // This is a bit hacky as it uses the tenants module to store the openid4vc actor id\n  // but this way we don't have to expose the contextCorrelationId in the openid metadata\n  const tenantsApi = getApiForModuleByName<TenantsModule>(rootAgentContext, 'TenantsModule')\n  if (tenantsApi) {\n    const [tenant] = await tenantsApi.findTenantsByQuery({\n      [OPENID4VC_ACTOR_IDS_METADATA_KEY]: [actorId],\n    })\n\n    if (tenant) {\n      const agentContextProvider = rootAgentContext.dependencyManager.resolve<AgentContextProvider>(\n        InjectionSymbols.AgentContextProvider\n      )\n      return agentContextProvider.getAgentContextForContextCorrelationId(`tenant-${tenant.id}`)\n    }\n  }\n\n  return rootAgentContext\n}\n\n/**\n * Store the actor id associated with a context correlation id. If multi-tenancy is not used\n * this method won't do anything as we can just use the actor from the default context. However\n * if multi-tenancy is used, we will store the actor id in the tenant record metadata so it can\n * be queried when a request comes in for the specific actor id.\n *\n * The reason for doing this is that we don't want to expose the context correlation id in the\n * actor metadata url, as it is then possible to see exactly which actors are registered under\n * the same agent.\n */\nexport async function storeActorIdForContextCorrelationId(agentContext: AgentContext, actorId: string) {\n  // It's kind of hacky, but we add support for the tenants module specifically here to map an actorId to\n  // a specific tenant. Otherwise we have to expose /:contextCorrelationId/:actorId in all the public URLs\n  // which is of course not so nice.\n  const tenantsApi = getApiForModuleByName<TenantsModule>(agentContext, 'TenantsModule')\n\n  // We don't want to query the tenant record if the current context is the root context\n  if (tenantsApi && tenantsApi.rootAgentContext.contextCorrelationId !== agentContext.contextCorrelationId) {\n    const tenantRecord = await tenantsApi.getTenantById(agentContext.contextCorrelationId.replace('tenant-', ''))\n\n    const currentOpenId4VcActorIds = tenantRecord.metadata.get<string[]>(OPENID4VC_ACTOR_IDS_METADATA_KEY) ?? []\n    const openId4VcActorIds = [...currentOpenId4VcActorIds, actorId]\n\n    tenantRecord.metadata.set(OPENID4VC_ACTOR_IDS_METADATA_KEY, openId4VcActorIds)\n    tenantRecord.setTag(OPENID4VC_ACTOR_IDS_METADATA_KEY, openId4VcActorIds)\n    await tenantsApi.updateTenant(tenantRecord)\n  }\n}\n"],"mappings":";;;AAIA,MAAM,mCAAmC;AAEzC,eAAsB,0BAA0B,kBAAgC,SAAiB;CAI/F,MAAM,aAAa,sBAAqC,kBAAkB,gBAAgB;AAC1F,KAAI,YAAY;EACd,MAAM,CAAC,UAAU,MAAM,WAAW,mBAAmB,GAClD,mCAAmC,CAAC,QAAQ,EAC9C,CAAC;AAEF,MAAI,OAIF,QAH6B,iBAAiB,kBAAkB,QAC9D,iBAAiB,qBAClB,CAC2B,uCAAuC,UAAU,OAAO,KAAK;;AAI7F,QAAO;;;;;;;;;;;;AAaT,eAAsB,oCAAoC,cAA4B,SAAiB;CAIrG,MAAM,aAAa,sBAAqC,cAAc,gBAAgB;AAGtF,KAAI,cAAc,WAAW,iBAAiB,yBAAyB,aAAa,sBAAsB;EACxG,MAAM,eAAe,MAAM,WAAW,cAAc,aAAa,qBAAqB,QAAQ,WAAW,GAAG,CAAC;EAG7G,MAAM,oBAAoB,CAAC,GADM,aAAa,SAAS,IAAc,iCAAiC,IAAI,EAAE,EACpD,QAAQ;AAEhE,eAAa,SAAS,IAAI,kCAAkC,kBAAkB;AAC9E,eAAa,OAAO,kCAAkC,kBAAkB;AACxE,QAAM,WAAW,aAAa,aAAa"}