2018-04-07 12:35:15 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Auth;
|
|
|
|
|
2021-09-21 21:30:08 -07:00
|
|
|
use Carbon\CarbonImmutable;
|
2022-05-04 19:01:29 -04:00
|
|
|
use Carbon\CarbonInterface;
|
2020-10-17 14:46:10 -07:00
|
|
|
use Pterodactyl\Models\User;
|
2018-04-07 12:35:15 -05:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2019-06-22 13:33:11 -07:00
|
|
|
use PragmaRX\Google2FA\Google2FA;
|
2022-05-28 17:03:58 -04:00
|
|
|
use Illuminate\Support\Facades\Event;
|
2019-06-22 13:33:11 -07:00
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2020-10-17 14:46:10 -07:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2022-05-28 17:03:58 -04:00
|
|
|
use Pterodactyl\Events\Auth\ProvidedAuthenticationToken;
|
2018-04-07 12:35:15 -05:00
|
|
|
use Pterodactyl\Http\Requests\Auth\LoginCheckpointRequest;
|
2021-09-21 21:30:08 -07:00
|
|
|
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
|
2018-04-07 12:35:15 -05:00
|
|
|
|
|
|
|
class LoginCheckpointController extends AbstractLoginController
|
|
|
|
{
|
2021-09-21 21:30:08 -07:00
|
|
|
private const TOKEN_EXPIRED_MESSAGE = 'The authentication token provided has expired, please refresh the page and try again.';
|
2019-06-22 13:33:11 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* LoginCheckpointController constructor.
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function __construct(
|
|
|
|
private Encrypter $encrypter,
|
|
|
|
private Google2FA $google2FA,
|
|
|
|
private ValidationFactory $validation
|
|
|
|
) {
|
2021-09-21 21:30:08 -07:00
|
|
|
parent::__construct();
|
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
|
|
|
*
|
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 \Exception
|
|
|
|
* @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);
|
|
|
|
}
|
2020-04-25 13:01:16 -07:00
|
|
|
|
2021-09-21 21:30:08 -07:00
|
|
|
$details = $request->session()->get('auth_confirmation_token');
|
|
|
|
if (!$this->hasValidSessionData($details)) {
|
|
|
|
$this->sendFailedLoginResponse($request, null, self::TOKEN_EXPIRED_MESSAGE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hash_equals($request->input('confirmation_token') ?? '', $details['token_value'])) {
|
|
|
|
$this->sendFailedLoginResponse($request);
|
|
|
|
}
|
|
|
|
|
2018-04-07 12:35:15 -05:00
|
|
|
try {
|
2020-07-02 23:01:02 -07:00
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
2021-09-21 21:30:08 -07:00
|
|
|
$user = User::query()->findOrFail($details['user_id']);
|
2022-10-14 10:59:20 -06:00
|
|
|
} catch (ModelNotFoundException) {
|
2021-09-21 21:30:08 -07:00
|
|
|
$this->sendFailedLoginResponse($request, null, self::TOKEN_EXPIRED_MESSAGE);
|
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)) {
|
2022-05-28 17:03:58 -04:00
|
|
|
Event::dispatch(new ProvidedAuthenticationToken($user, true));
|
|
|
|
|
2020-10-17 14:46:10 -07:00
|
|
|
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
|
|
|
|
2022-11-28 11:56:03 -05:00
|
|
|
if ($this->google2FA->verifyKey($decrypted, $request->input('authentication_code') ?? '', config('pterodactyl.auth.2fa.window'))) {
|
2022-05-28 17:03:58 -04:00
|
|
|
Event::dispatch(new ProvidedAuthenticationToken($user));
|
|
|
|
|
2020-07-02 23:01:02 -07:00
|
|
|
return $this->sendLoginResponse($user, $request);
|
|
|
|
}
|
2018-04-07 12:35:15 -05:00
|
|
|
}
|
|
|
|
|
2021-09-21 21:30:08 -07: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.
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
protected function isValidRecoveryToken(User $user, string $value): bool
|
2020-10-17 14:46:10 -07:00
|
|
|
{
|
|
|
|
foreach ($user->recoveryTokens as $token) {
|
|
|
|
if (password_verify($value, $token->token)) {
|
|
|
|
$token->delete();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-21 21:30:08 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the data provided from the session is valid or not. This
|
|
|
|
* will return false if the data is invalid, or if more time has passed than
|
|
|
|
* was configured when the session was written.
|
|
|
|
*/
|
|
|
|
protected function hasValidSessionData(array $data): bool
|
|
|
|
{
|
|
|
|
$validator = $this->validation->make($data, [
|
|
|
|
'user_id' => 'required|integer|min:1',
|
|
|
|
'token_value' => 'required|string',
|
|
|
|
'expires_at' => 'required',
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$data['expires_at'] instanceof CarbonInterface) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($data['expires_at']->isBefore(CarbonImmutable::now())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-07 12:35:15 -05:00
|
|
|
}
|