4 use Shaarli\Api\Exceptions\ApiAuthorizationException
;
5 use Shaarli\Api\Exceptions\ApiException
;
6 use Shaarli\Bookmark\BookmarkFileService
;
7 use Shaarli\Config\ConfigManager
;
10 use Slim\Http\Response
;
15 * This will be called before accessing any API Controller.
16 * Its role is to make sure that the API is enabled, configured, and to validate the JWT token.
18 * If the request is validated, the controller is called, otherwise a JSON error response is returned.
25 * @var int JWT token validity in seconds (9 min).
27 public static $TOKEN_DURATION = 540;
30 * @var Container: contains conf, plugins, etc.
35 * @var ConfigManager instance.
40 * ApiMiddleware constructor.
42 * @param Container $container instance.
44 public function __construct($container)
46 $this->container
= $container;
47 $this->conf
= $this->container
->get('conf');
48 $this->setLinkDb($this->conf
);
52 * Middleware execution:
53 * - check the API request
54 * - execute the controller
55 * - return the response
57 * @param Request $request Slim request
58 * @param Response $response Slim response
59 * @param callable $next Next action
61 * @return Response response.
63 public function __invoke($request, $response, $next)
66 $this->checkRequest($request);
67 $response = $next($request, $response);
68 } catch (ApiException
$e) {
69 $e->setResponse($response);
70 $e->setDebug($this->conf
->get('dev.debug', false));
71 $response = $e->getApiResponse();
75 ->withHeader('Access-Control-Allow-Origin', '*')
77 'Access-Control-Allow-Headers',
78 'X-Requested-With, Content-Type, Accept, Origin, Authorization'
80 ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
85 * Check the request validity (HTTP method, request value, etc.),
86 * that the API is enabled, and the JWT token validity.
88 * @param Request $request Slim request
90 * @throws ApiAuthorizationException The API is disabled or the token is invalid.
92 protected function checkRequest($request)
94 if (! $this->conf
->get('api.enabled', true)) {
95 throw new ApiAuthorizationException('API is disabled');
97 $this->checkToken($request);
101 * Check that the JWT token is set and valid.
102 * The API secret setting must be set.
104 * @param Request $request Slim request
106 * @throws ApiAuthorizationException The token couldn't be validated.
108 protected function checkToken($request)
110 if (! $request->hasHeader('Authorization')) {
111 throw new ApiAuthorizationException('JWT token not provided');
114 if (empty($this->conf
->get('api.secret'))) {
115 throw new ApiAuthorizationException('Token secret must be set in Shaarli\'s administration');
118 $authorization = $request->getHeaderLine('Authorization');
120 if (! preg_match('/^Bearer (.*)/i', $authorization, $matches)) {
121 throw new ApiAuthorizationException('Invalid JWT header');
124 ApiUtils
::validateJwtToken($matches[1], $this->conf
->get('api.secret'));
128 * Instantiate a new LinkDB including private bookmarks,
129 * and load in the Slim container.
131 * FIXME! LinkDB could use a refactoring to avoid this trick.
133 * @param ConfigManager $conf instance.
135 protected function setLinkDb($conf)
137 $linkDb = new BookmarkFileService(
139 $this->container
->get('history'),
142 $this->container
['db'] = $linkDb;