2018-04-07 12:35:15 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Auth;
|
|
|
|
|
2020-10-17 14:46:10 -07:00
|
|
|
use Pterodactyl\Models\User;
|
2019-06-22 13:33:11 -07:00
|
|
|
use Illuminate\Auth\AuthManager;
|
2018-04-07 12:35:15 -05:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2019-06-22 13:33:11 -07:00
|
|
|
use PragmaRX\Google2FA\Google2FA;
|
|
|
|
use Illuminate\Contracts\Config\Repository;
|
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2020-10-17 14:46:10 -07:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2018-04-07 12:35:15 -05:00
|
|
|
use Pterodactyl\Http\Requests\Auth\LoginCheckpointRequest;
|
2019-06-22 13:33:11 -07:00
|
|
|
use Illuminate\Contracts\Cache\Repository as CacheRepository;
|
2018-04-07 12:35:15 -05:00
|
|
|
|
|
|
|
class LoginCheckpointController extends AbstractLoginController
|
|
|
|
{
|
2021-07-17 11:47:07 -06:00
|
|
|
private CacheRepository $cache;
|
|
|
|
private Encrypter $encrypter;
|
|
|
|
private Google2FA $google2FA;
|
2019-06-22 13:33:11 -07:00
|
|
|
|
2020-07-02 23:01:02 -07:00
|
|
|
|
2019-06-22 13:33:11 -07:00
|
|
|
/**
|
|
|
|
* LoginCheckpointController constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
AuthManager $auth,
|
|
|
|
Repository $config,
|
|
|
|
CacheRepository $cache,
|
2021-07-17 11:47:07 -06:00
|
|
|
Encrypter $encrypter,
|
|
|
|
Google2FA $google2FA
|
2019-06-22 13:33:11 -07:00
|
|
|
) {
|
|
|
|
parent::__construct($auth, $config);
|
|
|
|
|
|
|
|
$this->cache = $cache;
|
|
|
|
$this->encrypter = $encrypter;
|
2021-07-17 11:47:07 -06:00
|
|
|
$this->google2FA = $google2FA;
|
2019-06-22 13:33:11 -07:00
|
|
|
}
|
|
|
|
|
2018-04-07 12:35:15 -05:00
|
|
|
/**
|
|
|
|
* Handle a login where the user is required to provide a TOTP authentication
|
2018-04-07 16:17:51 -05:00
|
|
|
* token. Once a user has reached this stage it is assumed that they have already
|
|
|
|
* provided a valid username and password.
|
2018-04-07 12:35:15 -05:00
|
|
|
*
|
2020-04-25 13:01:16 -07:00
|
|
|
* @return \Illuminate\Http\JsonResponse|void
|
2018-04-07 12:35:15 -05:00
|
|
|
*
|
2019-06-22 13:33:11 -07:00
|
|
|
* @throws \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException
|
|
|
|
* @throws \PragmaRX\Google2FA\Exceptions\InvalidCharactersException
|
|
|
|
* @throws \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException
|
2020-10-17 14:46:10 -07:00
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
2018-04-07 12:35:15 -05:00
|
|
|
*/
|
2018-04-08 15:18:13 -05:00
|
|
|
public function __invoke(LoginCheckpointRequest $request): JsonResponse
|
2018-04-07 12:35:15 -05:00
|
|
|
{
|
2020-10-17 14:46:10 -07:00
|
|
|
if ($this->hasTooManyLoginAttempts($request)) {
|
|
|
|
$this->sendLockoutResponse($request);
|
2021-07-17 11:47:07 -06:00
|
|
|
return;
|
2020-10-17 14:46:10 -07:00
|
|
|
}
|
2020-04-25 13:01:16 -07:00
|
|
|
|
2020-10-17 14:46:10 -07:00
|
|
|
$token = $request->input('confirmation_token');
|
2018-04-07 12:35:15 -05:00
|
|
|
try {
|
2020-07-02 23:01:02 -07:00
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
2020-10-17 14:46:10 -07:00
|
|
|
$user = User::query()->findOrFail($this->cache->get($token, 0));
|
|
|
|
} catch (ModelNotFoundException $exception) {
|
|
|
|
$this->incrementLoginAttempts($request);
|
2018-04-07 12:35:15 -05:00
|
|
|
|
2021-07-17 11:47:07 -06:00
|
|
|
$this->sendFailedLoginResponse(
|
2021-01-23 12:33:34 -08:00
|
|
|
$request,
|
|
|
|
null,
|
|
|
|
'The authentication token provided has expired, please refresh the page and try again.'
|
2020-10-17 14:46:10 -07:00
|
|
|
);
|
2021-07-17 11:47:07 -06:00
|
|
|
return;
|
2020-10-17 14:46:10 -07:00
|
|
|
}
|
2020-07-02 23:01:02 -07:00
|
|
|
|
2020-10-17 14:46:10 -07:00
|
|
|
// Recovery tokens go through a slightly different pathway for usage.
|
2021-01-23 12:33:34 -08:00
|
|
|
if (!is_null($recoveryToken = $request->input('recovery_token'))) {
|
2020-10-17 14:46:10 -07:00
|
|
|
if ($this->isValidRecoveryToken($user, $recoveryToken)) {
|
|
|
|
return $this->sendLoginResponse($user, $request);
|
2020-07-02 23:01:02 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$decrypted = $this->encrypter->decrypt($user->totp_secret);
|
2018-04-07 12:35:15 -05:00
|
|
|
|
2020-07-02 23:01:02 -07:00
|
|
|
if ($this->google2FA->verifyKey($decrypted, (string) $request->input('authentication_code') ?? '', config('pterodactyl.auth.2fa.window'))) {
|
|
|
|
$this->cache->delete($token);
|
2020-04-25 13:01:16 -07:00
|
|
|
|
2020-07-02 23:01:02 -07:00
|
|
|
return $this->sendLoginResponse($user, $request);
|
|
|
|
}
|
2018-04-07 12:35:15 -05:00
|
|
|
}
|
|
|
|
|
2020-10-17 14:46:10 -07:00
|
|
|
$this->incrementLoginAttempts($request);
|
2021-07-17 11:47:07 -06:00
|
|
|
$this->sendFailedLoginResponse($request, $user, !empty($recoveryToken) ? 'The recovery token provided is not valid.' : null);
|
2018-04-07 12:35:15 -05:00
|
|
|
}
|
2020-10-17 14:46:10 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if a given recovery token is valid for the user account. If we find a matching token
|
|
|
|
* it will be deleted from the database.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
protected function isValidRecoveryToken(User $user, string $value)
|
|
|
|
{
|
|
|
|
foreach ($user->recoveryTokens as $token) {
|
|
|
|
if (password_verify($value, $token->token)) {
|
|
|
|
$token->delete();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-07 12:35:15 -05:00
|
|
|
}
|