authenticationFactory = $authenticationFactory; $this->entryPointManager = $entryPointManager; $this->entityManager = $entityManager; $this->clientManager = $clientManager; $this->applicationUser = $applicationUser; $this->authTokenManager = $authTokenManager; $this->authBuilderFactory = $authBuilderFactory; $this->params = $params ?? RunnerParams::fromNothing(); } public function run() : void { $requestWrapped = new RequestWrapper( ServerRequestCreatorFactory::create()->createServerRequestFromGlobals(), Route::detectBasePath() ); if ($requestWrapped->getMethod() !== 'GET') { throw new Error("Only GET requests allowed for entry points."); } $entryPoint = $this->params->get('entryPoint') ?? $requestWrapped->getQueryParam('entryPoint'); $final = $this->params->get('final') ?? false; if (!$entryPoint) { throw new Error(); } $authRequired = $this->entryPointManager->checkAuthRequired($entryPoint); $authNotStrict = $this->entryPointManager->checkNotStrictAuth($entryPoint); if ($authRequired && !$authNotStrict && !$final) { $portalId = $this->detectPortalId($requestWrapped); if ($portalId) { $this->runThroughPortal($portalId, $entryPoint); return; } } $responseWrapped = new ResponseWrapper(new Response()); $this->processRequest($entryPoint, $requestWrapped, $responseWrapped, $authRequired, $authNotStrict); (new ResponseEmitter())->emit($responseWrapped->getResponse()); } private function processRequest( string $entryPoint, RequestWrapper $requestWrapped, ResponseWrapper $responseWrapped, bool $authRequired, bool $authNotStrict ) : void { try { $authentication = $authNotStrict ? $this->authenticationFactory->createWithAnyAccessAllowed() : $this->authenticationFactory->create(); $apiAuth = $this->authBuilderFactory ->create() ->setAuthentication($authentication) ->setAuthRequired($authRequired) ->forEntryPoint() ->build(); $authResult = $apiAuth->process($requestWrapped, $responseWrapped); if (!$authResult->isResolved()) { return; } if ($authResult->isResolvedUseNoAuth()) { $this->applicationUser->setupSystemUser(); } ob_start(); $this->entryPointManager->run($entryPoint, $requestWrapped, $responseWrapped/*, $data*/); $contents = ob_get_clean(); if ($contents) { $responseWrapped->writeBody($contents); } } catch (Exception $e) { (new ApiErrorOutput($requestWrapped))->process($responseWrapped, $e, true); } } private function detectPortalId(RequestWrapper $requestWrapped) : ?string { if ($requestWrapped->hasQueryParam('portalId')) { return $requestWrapped->getQueryParam('portalId'); } $token = $requestWrapped->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); $app->setClientBasePath($this->clientManager->getBasePath()); $params = RunnerParams::fromArray([ 'entryPoint' => $entryPoint, 'final' => true, ]); $app->run(EntryPoint::class, $params); } }