namespace BookStack\Api;
+use BookStack\Access\LoginService;
use BookStack\Exceptions\ApiAuthException;
use Illuminate\Auth\GuardHelpers;
use Illuminate\Contracts\Auth\Authenticatable;
class ApiTokenGuard implements Guard
{
-
use GuardHelpers;
/**
*/
protected $request;
+ /**
+ * @var LoginService
+ */
+ protected $loginService;
/**
* The last auth exception thrown in this request.
+ *
* @var ApiAuthException
*/
protected $lastAuthException;
/**
* ApiTokenGuard constructor.
*/
- public function __construct(Request $request)
+ public function __construct(Request $request, LoginService $loginService)
{
$this->request = $request;
+ $this->loginService = $loginService;
}
-
+
/**
- * @inheritDoc
+ * {@inheritdoc}
*/
public function user()
{
}
$user = null;
+
try {
$user = $this->getAuthorisedUserFromRequest();
} catch (ApiAuthException $exception) {
}
$this->user = $user;
+
return $user;
}
/**
* Determine if current user is authenticated. If not, throw an exception.
*
- * @return \Illuminate\Contracts\Auth\Authenticatable
- *
* @throws ApiAuthException
+ *
+ * @return \Illuminate\Contracts\Auth\Authenticatable
*/
public function authenticate()
{
- if (! is_null($user = $this->user())) {
+ if (!is_null($user = $this->user())) {
return $user;
}
/**
* Check the API token in the request and fetch a valid authorised user.
+ *
* @throws ApiAuthException
*/
protected function getAuthorisedUserFromRequest(): Authenticatable
$this->validateToken($token, $secret);
+ if ($this->loginService->awaitingEmailConfirmation($token->user)) {
+ throw new ApiAuthException(trans('errors.email_confirmation_awaiting'));
+ }
+
return $token->user;
}
/**
* Validate the format of the token header value string.
+ *
* @throws ApiAuthException
*/
protected function validateTokenHeaderValue(string $authToken): void
/**
* 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
}
/**
- * @inheritDoc
+ * {@inheritdoc}
*/
public function validate(array $credentials = [])
{
{
$this->user = null;
}
-}
\ No newline at end of file
+}