getUser($request); $apiUser = $this->getApiUser($request); $workspace = $this->getWorkspace($request); $path = $request->getUri()->getPath(); if ($apiUser) { if (str_starts_with($path, '/admin/api/')) { if ($this->isAdminApiEnabled) { $user = $apiUser; } } elseif (str_starts_with($path, '/api/')) { if ($this->isApiEnabled) { $user = $apiUser; } } } if ($user) { $request = $request->withAttribute(UserEntity::class, $user); } if ($workspace) { $request = $request->withAttribute( WorkspaceEntity::class, $workspace ); } return $handler->handle($request); } private function getApiUser(ServerRequestInterface $request): ?UserEntity { $apiKey = $this->getTokenFromApiHeader($request); if (!$apiKey) { return null; } try { $cmd = new ReadUserCommand(new ApiKey($apiKey)); /** @var UserEntity $user */ $user = $this->dispatcher->dispatch($cmd); } catch (\Throwable $th) { return null; } return $user; } /** * @param ServerRequestInterface $request * @return null|UserEntity * @throws InvalidArgumentException * @throws UnexpectedValueException * @throws NoHandlerFoundException */ private function getUser(ServerRequestInterface $request): ?UserEntity { $token = $this->getTokenFromAuthorizationHeader($request) ?? $this->getTokenFromQueryParam($request) ?? $this->getTokenFromCookie($request); if (!$token) { return null; } try { $jwt = UserJwt::createFromJwtString($token); } catch ( SignatureInvalidException | BeforeValidException | ExpiredException | DomainException $th ) { return null; } try { $cmd = new ReadUserCommand($jwt->getUuid()); /** @var UserEntity $user */ $user = $this->dispatcher->dispatch($cmd); } catch (UserNotFoundException $th) { return null; } return $user; } /** * @param ServerRequestInterface $request * @return null|string */ private function getTokenFromAuthorizationHeader( ServerRequestInterface $request ): ?string { $header = $request->getHeaderLine('Authorization'); if (!$header) { return null; } $token = null; // Check if header has Bearer token if (preg_match('/Bearer\s(\S+)/', $header, $matches)) { $token = $matches[1]; } return $token; } private function getTokenFromApiHeader( ServerRequestInterface $request ): ?string { $header = $request->getHeaderLine('X-Api-Key'); if (!$header) { return null; } return $header; } /** * @param ServerRequestInterface $request * @return null|string */ private function getTokenFromQueryParam( ServerRequestInterface $request ): ?string { $params = $request->getQueryParams(); return $params['jwt'] ?? null; } /** * @param ServerRequestInterface $request * @return null|string */ private function getTokenFromCookie( ServerRequestInterface $request ): ?string { $path = $request->getUri()->getPath(); if ( str_starts_with($path, '/admin/api/') === false && str_starts_with($path, '/api/') === false ) { $cookie = UserCookie::createFromRequest($request); return $cookie ? $cookie->getValue() : null; } return null; } private function getWorkspace(ServerRequestInterface $req): ?WorkspaceEntity { $header = $req->getHeaderLine('X-Workspace-Id'); if (!$header) { return null; } try { $cmd = new ReadWorkspaceCommand($header); return $this->dispatcher->dispatch($cmd); } catch (\Throwable $th) { // Do nothing here } return null; } }