use Illuminate\Auth\GuardHelpers;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Guard;
+use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;
use Symfony\Component\HttpFoundation\Request;
{
$this->request = $request;
}
-
-
+
/**
* @inheritDoc
*/
protected function getAuthorisedUserFromRequest(): Authenticatable
{
$authToken = trim($this->request->headers->get('Authorization', ''));
+ $this->validateTokenHeaderValue($authToken);
+
+ [$id, $secret] = explode(':', str_replace('Token ', '', $authToken));
+ $token = ApiToken::query()
+ ->where('token_id', '=', $id)
+ ->with(['user'])->first();
+
+ $this->validateToken($token, $secret);
+
+ return $token->user;
+ }
+
+ /**
+ * Validate the format of the token header value string.
+ * @throws ApiAuthException
+ */
+ protected function validateTokenHeaderValue(string $authToken): void
+ {
if (empty($authToken)) {
throw new ApiAuthException(trans('errors.api_no_authorization_found'));
}
if (strpos($authToken, ':') === false || strpos($authToken, 'Token ') !== 0) {
throw new ApiAuthException(trans('errors.api_bad_authorization_format'));
}
+ }
- [$id, $secret] = explode(':', str_replace('Token ', '', $authToken));
- $token = ApiToken::query()
- ->where('token_id', '=', $id)
- ->with(['user'])->first();
-
+ /**
+ * Validate the given secret against the given token and ensure the token
+ * currently has access to the instance API.
+ * @throws ApiAuthException
+ */
+ protected function validateToken(?ApiToken $token, string $secret): void
+ {
if ($token === null) {
throw new ApiAuthException(trans('errors.api_user_token_not_found'));
}
throw new ApiAuthException(trans('errors.api_incorrect_token_secret'));
}
+ $now = Carbon::now();
+ if ($token->expires_at <= $now) {
+ throw new ApiAuthException(trans('errors.api_user_token_expired'), 403);
+ }
+
if (!$token->user->can('access-api')) {
throw new ApiAuthException(trans('errors.api_user_no_api_permission'), 403);
}
-
- return $token->user;
}
/**
return Hash::check($credentials['secret'], $token->secret);
}
+ /**
+ * "Log out" the currently authenticated user.
+ */
+ public function logout()
+ {
+ $this->user = null;
+ }
}
\ No newline at end of file