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;
|
2021-10-30 13:41:38 -07:00
|
|
|
use Carbon\CarbonInterface;
|
2020-10-17 14:46:10 -07:00
|
|
|
use Pterodactyl\Models\User;
|
2022-02-13 14:44:50 -05:00
|
|
|
use Illuminate\Http\Request;
|
2019-06-22 13:33:11 -07:00
|
|
|
use PragmaRX\Google2FA\Google2FA;
|
2022-02-13 14:44:50 -05:00
|
|
|
use Pterodactyl\Models\SecurityKey;
|
2019-06-22 13:33:11 -07:00
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2022-02-13 14:44:50 -05:00
|
|
|
use Webauthn\PublicKeyCredentialRequestOptions;
|
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;
|
2021-09-21 21:30:08 -07:00
|
|
|
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
|
2022-02-13 14:44:50 -05:00
|
|
|
use Pterodactyl\Repositories\SecurityKeys\WebauthnServerRepository;
|
|
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2018-04-07 12:35:15 -05:00
|
|
|
|
|
|
|
|
class LoginCheckpointController extends AbstractLoginController
|
|
|
|
|
{
|
2021-09-30 16:08:11 -06:00
|
|
|
public 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
|
|
|
|
2022-02-13 14:44:50 -05:00
|
|
|
protected Encrypter $encrypter;
|
2019-06-22 13:33:11 -07:00
|
|
|
|
2022-02-13 14:44:50 -05:00
|
|
|
protected Google2FA $google2FA;
|
2019-06-22 13:33:11 -07:00
|
|
|
|
2022-02-13 14:44:50 -05:00
|
|
|
protected WebauthnServerRepository $repository;
|
|
|
|
|
|
|
|
|
|
protected ValidationFactory $validation;
|
2020-07-02 23:01:02 -07:00
|
|
|
|
2019-06-22 13:33:11 -07:00
|
|
|
/**
|
|
|
|
|
* LoginCheckpointController constructor.
|
|
|
|
|
*/
|
2022-02-13 14:44:50 -05:00
|
|
|
public function __construct(
|
|
|
|
|
Encrypter $encrypter,
|
|
|
|
|
Google2FA $google2FA,
|
|
|
|
|
ValidationFactory $validation,
|
|
|
|
|
WebauthnServerRepository $repository
|
|
|
|
|
) {
|
2021-09-21 21:30:08 -07:00
|
|
|
parent::__construct();
|
2019-06-22 13:33:11 -07:00
|
|
|
|
|
|
|
|
$this->encrypter = $encrypter;
|
2021-07-17 11:47:07 -06:00
|
|
|
$this->google2FA = $google2FA;
|
2021-09-21 21:30:08 -07:00
|
|
|
$this->validation = $validation;
|
2022-02-13 14:44:50 -05:00
|
|
|
$this->repository = $repository;
|
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
|
2021-09-30 16:08:11 -06:00
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
2018-04-07 12:35:15 -05:00
|
|
|
*/
|
2022-02-13 14:44:50 -05:00
|
|
|
public function token(LoginCheckpointRequest $request)
|
|
|
|
|
{
|
|
|
|
|
$user = $this->extractUserFromRequest($request);
|
|
|
|
|
|
|
|
|
|
// Recovery tokens go through a slightly different pathway for usage.
|
|
|
|
|
if (!is_null($recoveryToken = $request->input('recovery_token'))) {
|
|
|
|
|
if ($this->isValidRecoveryToken($user, $recoveryToken)) {
|
|
|
|
|
return $this->sendLoginResponse($user, $request);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$decrypted = $this->encrypter->decrypt($user->totp_secret);
|
|
|
|
|
|
|
|
|
|
if ($this->google2FA->verifyKey($decrypted, (string) $request->input('authentication_code') ?? '', config('pterodactyl.auth.2fa.window'))) {
|
|
|
|
|
return $this->sendLoginResponse($user, $request);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->sendFailedLoginResponse($request, $user, !empty($recoveryToken) ? 'The recovery token provided is not valid.' : null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Authenticates a login request using a security key for a user.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
*
|
|
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
|
*/
|
|
|
|
|
public function key(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$key = $request->session()->get(SecurityKey::PK_SESSION_NAME);
|
|
|
|
|
if (!$key instanceof PublicKeyCredentialRequestOptions) {
|
|
|
|
|
throw new BadRequestHttpException('No security keys configured in session.');
|
|
|
|
|
}
|
2022-02-13 14:57:45 -05:00
|
|
|
|
2022-02-13 14:44:50 -05:00
|
|
|
$user = $this->extractUserFromRequest($request);
|
|
|
|
|
|
|
|
|
|
$source = $this->repository->getServer($user)->loadAndCheckAssertionResponse(
|
|
|
|
|
$request->input('data'),
|
|
|
|
|
$key,
|
|
|
|
|
$user->toPublicKeyCredentialEntity(),
|
|
|
|
|
SecurityKey::getPsrRequestFactory($request)
|
|
|
|
|
);
|
|
|
|
|
|
2022-02-13 14:57:45 -05:00
|
|
|
if (!hash_equals($user->uuid, $source->getUserHandle())) {
|
|
|
|
|
throw new BadRequestHttpException('An unexpected error was encountered while validating that security key.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->sendLoginResponse($user, $request);
|
2022-02-13 14:44:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return \Pterodactyl\Models\User
|
|
|
|
|
*
|
|
|
|
|
* @throws \Illuminate\Validation\ValidationException
|
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
|
*/
|
|
|
|
|
protected function extractUserFromRequest(Request $request): User
|
2018-04-07 12:35:15 -05:00
|
|
|
{
|
2020-10-17 14:46:10 -07:00
|
|
|
if ($this->hasTooManyLoginAttempts($request)) {
|
|
|
|
|
$this->sendLockoutResponse($request);
|
|
|
|
|
}
|
2021-08-07 16:10:24 -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);
|
2020-10-17 14:46:10 -07:00
|
|
|
}
|
2020-04-25 13:01:16 -07:00
|
|
|
|
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']);
|
2020-10-17 14:46:10 -07:00
|
|
|
} catch (ModelNotFoundException $exception) {
|
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
|
|
|
|
2022-02-13 14:44:50 -05:00
|
|
|
return $user;
|
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.
|
|
|
|
|
*/
|
2021-09-30 16:08:11 -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
|
|
|
|
2021-09-30 16:08:11 -06:00
|
|
|
protected function hasValidSessionData(array $data): bool
|
|
|
|
|
{
|
|
|
|
|
return static::isValidSessionData($this->validation, $data);
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2021-09-30 16:08:11 -06:00
|
|
|
public static function isValidSessionData(ValidationFactory $validation, array $data): bool
|
2021-09-21 21:30:08 -07:00
|
|
|
{
|
2021-09-30 16:08:11 -06:00
|
|
|
$validator = $validation->make($data, [
|
2021-09-21 21:30:08 -07:00
|
|
|
'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
|
|
|
}
|