X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=application%2Fapi%2FApiMiddleware.php;h=f5b53b01fcc5f5f16c5d477d054c9483d2e142d9;hb=df25b28dcd3cde54d42c18a55a810daa82bf5727;hp=ff2093930c69b5e5ad0c22f03eee816d2f27e8ce;hpb=3e395a6bc63cba16b0772a382f6cbac0ce4ab906;p=github%2Fshaarli%2FShaarli.git diff --git a/application/api/ApiMiddleware.php b/application/api/ApiMiddleware.php index ff209393..f5b53b01 100644 --- a/application/api/ApiMiddleware.php +++ b/application/api/ApiMiddleware.php @@ -1,9 +1,9 @@ checkRequest($request); $response = $next($request, $response); - } catch(ApiException $e) { + } catch (ApiException $e) { $e->setResponse($response); $e->setDebug($this->conf->get('dev.debug', false)); $response = $e->getApiResponse(); } - return $response; + return $response + ->withHeader('Access-Control-Allow-Origin', '*') + ->withHeader( + 'Access-Control-Allow-Headers', + 'X-Requested-With, Content-Type, Accept, Origin, Authorization' + ) + ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS') + ; } /** @@ -98,8 +105,11 @@ class ApiMiddleware * * @throws ApiAuthorizationException The token couldn't be validated. */ - protected function checkToken($request) { - if (! $request->hasHeader('Authorization')) { + protected function checkToken($request) + { + if (!$request->hasHeader('Authorization') + && !isset($this->container->environment['REDIRECT_HTTP_AUTHORIZATION']) + ) { throw new ApiAuthorizationException('JWT token not provided'); } @@ -107,7 +117,11 @@ class ApiMiddleware throw new ApiAuthorizationException('Token secret must be set in Shaarli\'s administration'); } - $authorization = $request->getHeaderLine('Authorization'); + if (isset($this->container->environment['REDIRECT_HTTP_AUTHORIZATION'])) { + $authorization = $this->container->environment['REDIRECT_HTTP_AUTHORIZATION']; + } else { + $authorization = $request->getHeaderLine('Authorization'); + } if (! preg_match('/^Bearer (.*)/i', $authorization, $matches)) { throw new ApiAuthorizationException('Invalid JWT header'); @@ -117,7 +131,7 @@ class ApiMiddleware } /** - * Instantiate a new LinkDB including private links, + * Instantiate a new LinkDB including private bookmarks, * and load in the Slim container. * * FIXME! LinkDB could use a refactoring to avoid this trick. @@ -126,12 +140,10 @@ class ApiMiddleware */ protected function setLinkDb($conf) { - $linkDb = new \LinkDB( - $conf->get('resource.datastore'), - true, - $conf->get('privacy.hide_public_links'), - $conf->get('redirector.url'), - $conf->get('redirector.encode_url') + $linkDb = new BookmarkFileService( + $conf, + $this->container->get('history'), + true ); $this->container['db'] = $linkDb; }