{"version":3,"file":"authorizationRequestEndpoint.mjs","names":[],"sources":["../../../src/openid4vc-verifier/router/authorizationRequestEndpoint.ts"],"sourcesContent":["import { joinUriParts } from '@credo-ts/core'\nimport type { Response, Router } from 'express'\nimport {\n  getRequestContext,\n  sendErrorResponse,\n  sendNotFoundResponse,\n  sendUnknownServerErrorResponse,\n} from '../../shared/router'\nimport { OpenId4VcVerificationSessionState } from '../OpenId4VcVerificationSessionState'\nimport { OpenId4VcVerifierModuleConfig } from '../OpenId4VcVerifierModuleConfig'\nimport { OpenId4VpVerifierService } from '../OpenId4VpVerifierService'\nimport type { OpenId4VcVerificationRequest } from './requestContext'\n\nexport function configureAuthorizationRequestEndpoint(router: Router, config: OpenId4VcVerifierModuleConfig) {\n  router.get(\n    joinUriParts(config.authorizationRequestEndpoint, [':authorizationRequestId']),\n    async (request: OpenId4VcVerificationRequest, response: Response, next) => {\n      const { agentContext, verifier } = getRequestContext(request)\n\n      if (!request.params.authorizationRequestId || typeof request.params.authorizationRequestId !== 'string') {\n        return sendErrorResponse(\n          response,\n          next,\n          agentContext.config.logger,\n          400,\n          'invalid_request',\n          'Invalid authorization request url'\n        )\n      }\n\n      try {\n        const verifierService = agentContext.dependencyManager.resolve(OpenId4VpVerifierService)\n        const verifierConfig = agentContext.dependencyManager.resolve(OpenId4VcVerifierModuleConfig)\n\n        // We always use shortened URIs currently\n        const fullAuthorizationRequestUri = joinUriParts(verifierConfig.baseUrl, [\n          verifier.verifierId,\n          verifierConfig.authorizationRequestEndpoint,\n          request.params.authorizationRequestId,\n        ])\n\n        const [verificationSession] = await verifierService.findVerificationSessionsByQuery(agentContext, {\n          verifierId: verifier.verifierId,\n          $or: [\n            {\n              authorizationRequestId: request.params.authorizationRequestId,\n            },\n            // NOTE: this can soon be removed, authorization request id is cleaner,\n            // but only introduced since 0.6\n            {\n              authorizationRequestUri: fullAuthorizationRequestUri,\n            },\n          ],\n        })\n\n        // Not all requets are signed, and those are not fetcheable\n        if (!verificationSession || !verificationSession.authorizationRequestJwt) {\n          return sendErrorResponse(\n            response,\n            next,\n            agentContext.config.logger,\n            404,\n            'not_found',\n            'Authorization request not found'\n          )\n        }\n\n        if (\n          ![\n            OpenId4VcVerificationSessionState.RequestCreated,\n            OpenId4VcVerificationSessionState.RequestUriRetrieved,\n          ].includes(verificationSession.state)\n        ) {\n          return sendErrorResponse(\n            response,\n            next,\n            agentContext.config.logger,\n            400,\n            'invalid_request',\n            'Invalid state for authorization request'\n          )\n        }\n\n        if (verificationSession.expiresAt && Date.now() > verificationSession.expiresAt.getTime()) {\n          return sendNotFoundResponse(response, next, agentContext.config.logger, 'Session expired')\n        }\n\n        // It's okay to retrieve the offer multiple times. So we only update the state if it's not already retrieved\n        if (verificationSession.state !== OpenId4VcVerificationSessionState.RequestUriRetrieved) {\n          await verifierService.updateState(\n            agentContext,\n            verificationSession,\n            OpenId4VcVerificationSessionState.RequestUriRetrieved\n          )\n        }\n\n        response.type('application/oauth-authz-req+jwt').status(200).send(verificationSession.authorizationRequestJwt)\n        next()\n      } catch (error) {\n        return sendUnknownServerErrorResponse(response, next, agentContext.config.logger, error)\n      }\n    }\n  )\n}\n"],"mappings":";;;;;;;;AAaA,SAAgB,sCAAsC,QAAgB,QAAuC;AAC3G,QAAO,IACL,aAAa,OAAO,8BAA8B,CAAC,0BAA0B,CAAC,EAC9E,OAAO,SAAuC,UAAoB,SAAS;EACzE,MAAM,EAAE,cAAc,aAAa,kBAAkB,QAAQ;AAE7D,MAAI,CAAC,QAAQ,OAAO,0BAA0B,OAAO,QAAQ,OAAO,2BAA2B,SAC7F,QAAO,kBACL,UACA,MACA,aAAa,OAAO,QACpB,KACA,mBACA,oCACD;AAGH,MAAI;GACF,MAAM,kBAAkB,aAAa,kBAAkB,QAAQ,yBAAyB;GACxF,MAAM,iBAAiB,aAAa,kBAAkB,QAAQ,8BAA8B;GAG5F,MAAM,8BAA8B,aAAa,eAAe,SAAS;IACvE,SAAS;IACT,eAAe;IACf,QAAQ,OAAO;IACh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