+ {
+ // Validate the token and it's users API access
+ try {
+ $this->ensureAuthorizedBySessionOrToken();
+ } catch (UnauthorizedException $exception) {
+ return $this->unauthorisedResponse($exception->getMessage(), $exception->getCode());
+ }
+
+ return $next($request);
+ }
+
+ /**
+ * Ensure the current user can access authenticated API routes, either via existing session
+ * authentication or via API Token authentication.
+ * @throws UnauthorizedException
+ */
+ protected function ensureAuthorizedBySessionOrToken(): void