createServerRequestFromGlobals(), Route::detectBasePath() ); if ($requestWrapped->getMethod() !== 'GET') { throw new BadRequest("Only GET requests allowed for entry points."); } if ($entryPoint === null) { $entryPoint = $requestWrapped->getQueryParam('entryPoint'); } if (!$entryPoint) { throw new BadRequest("No 'entryPoint' param."); } $portalId = Url::getPortalIdFromEnv(); if ($portalId && !$final) { $this->runThroughPortal($portalId, $entryPoint); return; } $responseWrapped = new ResponseWrapper(new Response()); try { $authRequired = $this->entryPointManager->checkAuthRequired($entryPoint); } catch (NotFound $exception) { $this->errorOutput->processWithBodyPrinting($requestWrapped, $responseWrapped, $exception); (new ResponseEmitter())->emit($responseWrapped->toPsr7()); return; } if ($authRequired && !$final) { $portalId = $this->detectPortalId($requestWrapped); if ($portalId) { $this->runThroughPortal($portalId, $entryPoint); return; } } $this->processRequest( $entryPoint, $requestWrapped, $responseWrapped, $authRequired ); (new ResponseEmitter())->emit($responseWrapped->toPsr7()); } private function processRequest( string $entryPoint, RequestWrapper $requestWrapped, ResponseWrapper $responseWrapped, bool $authRequired ): void { try { $this->processRequestInternal( $entryPoint, $requestWrapped, $responseWrapped, $authRequired ); } catch (Exception $exception) { $this->errorOutput->processWithBodyPrinting($requestWrapped, $responseWrapped, $exception); } } /** * @throws NotFound * @throws BadRequest */ private function processRequestInternal( string $entryPoint, RequestWrapper $request, ResponseWrapper $response, bool $authRequired ): void { $authentication = $this->authenticationFactory->create(); $apiAuth = $this->authBuilderFactory ->create() ->setAuthentication($authentication) ->setAuthRequired($authRequired) ->forEntryPoint() ->build(); $authResult = $apiAuth->process($request, $response); if (!$authResult->isResolved()) { return; } if ($authResult->isResolvedUseNoAuth()) { $this->applicationUser->setupSystemUser(); } ob_start(); $this->entryPointManager->run($entryPoint, $request, $response); $contents = ob_get_clean(); if ($contents) { $response->writeBody($contents); } } private function detectPortalId(RequestWrapper $request): ?string { if ($request->hasQueryParam('portalId')) { return $request->getQueryParam('portalId'); } $token = $request->getCookieParam('auth-token'); if (!$token) { return null; } $authToken = $this->authTokenManager->get($token); if ($authToken) { return $authToken->getPortalId(); } return null; } private function runThroughPortal(string $portalId, string $entryPoint): void { $app = new PortalApplication($portalId); $clientManager = $app->getContainer() ->getByClass(ClientManager::class); $clientManager->setBasePath($this->clientManager->getBasePath()); $clientManager->setApiUrl('api/v1/portal-access/' . $portalId); $params = RunnerParams::fromArray([ 'entryPoint' => $entryPoint, 'final' => true, ]); $app->run(EntryPointRunner::class, $params); } }