checkAuthorization($request); $resolve($next($request)); } ); } /** * Check authorization and return a new request with added attributes: * * 'authorization' => JWTToken * * @param ServerRequestInterface $request * @return ServerRequestInterface */ private function checkAuthorization(ServerRequestInterface $request): ServerRequestInterface { $authorization = $request->getHeaderLine('authorization'); if (str_starts_with(strtolower($authorization), "bearer ")) { $jwt = substr($authorization, strpos($authorization, " ")+1); $key = new JWTPlaintextKey($this->config->getJwtSecret()); $tok = new JWTToken($key, $jwt); if (!$tok->isValid()) { throw new SecurityException( message: "Invalid token", code: SecurityException::ERR_ACCESS_DENIED ); } return $request ->withAttribute('authorization', $tok); } else { return $request ->withAttribute('authorization', null); } } }