15 namespace Fossology\UI\Api\Middlewares;
22 use Psr\Http\Message\ResponseInterface;
23 use Psr\Http\Message\ServerRequestInterface as Request;
24 use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
42 public function __invoke(Request $request, RequestHandler $handler) : ResponseInterface
44 $requestUri = $request->getUri();
45 $requestPath = strtolower($requestUri->getPath());
46 $authFreePaths = [
"/info",
"/openapi",
"/health"];
48 $isPassThroughPath =
false;
50 $parts = explode(
"/", $requestPath, 5);
51 $endpoint =
"/".end($parts);
52 foreach ($authFreePaths as $authFreePath) {
53 if ( $endpoint === $authFreePath ) {
54 $isPassThroughPath =
true;
59 if (stristr($request->getMethod(),
"options") !==
false) {
60 $response = $handler->handle($request);
61 } elseif ($isPassThroughPath) {
62 $response = $handler->handle($request);
63 } elseif (stristr($requestUri->getPath(),
"/tokens") !==
false &&
64 stristr($requestUri->getPath(),
"/users/tokens") ===
false &&
65 stristr($request->getMethod(),
"post") !==
false) {
66 $response = $handler->handle($request);
69 $authHelper = $GLOBALS[
'container']->get(
'helper.authHelper');
70 $authHeaders = $request->getHeader(
'Authorization');
71 if (!empty($authHeaders)) {
72 $jwtToken = $authHeaders[0];
78 $authHelper->verifyAuthToken($jwtToken, $userId, $tokenScope);
79 if (stristr($request->getMethod(),
"get") ===
false &&
80 stristr($tokenScope,
"write") ===
false) {
88 $queryParameters = $request->getQueryParams();
89 $groupName = $queryParameters[
'groupName'] ??
"";
91 $groupName = $request->getHeaderLine(
'groupName');
93 if (!empty($groupName)) {
94 $authHelper->userHasGroupAccess($userId, $groupName);
95 $authHelper->updateUserSession($userId, $tokenScope, $groupName);
97 $authHelper->updateUserSession($userId, $tokenScope);
99 $response = $handler->handle($request);
Provides helper methods for REST api.
static addCorsHeaders(ResponseInterface $response)
Authentication middleware for Slim framework.
static getVersion(ServerRequestInterface $request)