3 namespace BookStack\Api;
5 use BookStack\Exceptions\ApiAuthException;
6 use Illuminate\Auth\GuardHelpers;
7 use Illuminate\Contracts\Auth\Authenticatable;
8 use Illuminate\Contracts\Auth\Guard;
9 use Illuminate\Support\Facades\Hash;
10 use Symfony\Component\HttpFoundation\Request;
12 class ApiTokenGuard implements Guard
18 * The request instance.
24 * The last auth exception thrown in this request.
25 * @var ApiAuthException
27 protected $lastAuthException;
30 * ApiTokenGuard constructor.
32 public function __construct(Request $request)
34 $this->request = $request;
40 public function user()
42 // Return the user if we've already retrieved them.
43 // Effectively a request-instance cache for this method.
44 if (!is_null($this->user)) {
50 $user = $this->getAuthorisedUserFromRequest();
51 } catch (ApiAuthException $exception) {
52 $this->lastAuthException = $exception;
60 * Determine if current user is authenticated. If not, throw an exception.
62 * @return \Illuminate\Contracts\Auth\Authenticatable
64 * @throws ApiAuthException
66 public function authenticate()
68 if (! is_null($user = $this->user())) {
72 if ($this->lastAuthException) {
73 throw $this->lastAuthException;
76 throw new ApiAuthException('Unauthorized');
80 * Check the API token in the request and fetch a valid authorised user.
81 * @throws ApiAuthException
83 protected function getAuthorisedUserFromRequest(): Authenticatable
85 $authToken = trim($this->request->headers->get('Authorization', ''));
86 $this->validateTokenHeaderValue($authToken);
88 [$id, $secret] = explode(':', str_replace('Token ', '', $authToken));
89 $token = ApiToken::query()
90 ->where('token_id', '=', $id)
91 ->with(['user'])->first();
93 $this->validateToken($token, $secret);
99 * Validate the format of the token header value string.
100 * @throws ApiAuthException
102 protected function validateTokenHeaderValue(string $authToken): void
104 if (empty($authToken)) {
105 throw new ApiAuthException(trans('errors.api_no_authorization_found'));
108 if (strpos($authToken, ':') === false || strpos($authToken, 'Token ') !== 0) {
109 throw new ApiAuthException(trans('errors.api_bad_authorization_format'));
114 * Validate the given secret against the given token and ensure the token
115 * currently has access to the instance API.
116 * @throws ApiAuthException
118 protected function validateToken(?ApiToken $token, string $secret): void
120 if ($token === null) {
121 throw new ApiAuthException(trans('errors.api_user_token_not_found'));
124 if (!Hash::check($secret, $token->secret)) {
125 throw new ApiAuthException(trans('errors.api_incorrect_token_secret'));
128 if (!$token->user->can('access-api')) {
129 throw new ApiAuthException(trans('errors.api_user_no_api_permission'), 403);
136 public function validate(array $credentials = [])
138 if (empty($credentials['id']) || empty($credentials['secret'])) {
142 $token = ApiToken::query()
143 ->where('token_id', '=', $credentials['id'])
144 ->with(['user'])->first();
146 if ($token === null) {
150 return Hash::check($credentials['secret'], $token->secret);
154 * "Log out" the currently authenticated user.
156 public function logout()