2018-04-07 12:35:15 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Auth;
|
|
|
|
|
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;
|
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;
|
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
2018-04-07 12:35:15 -05:00
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
|
|
|
|
|
|
|
class LoginCheckpointController extends AbstractLoginController
|
|
|
|
{
|
2019-06-22 13:33:11 -07:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Cache\Repository
|
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \PragmaRX\Google2FA\Google2FA
|
|
|
|
*/
|
|
|
|
private $google2FA;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
|
|
|
*/
|
|
|
|
private $encrypter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LoginCheckpointController constructor.
|
|
|
|
*
|
2019-09-05 21:32:57 -07:00
|
|
|
* @param \Illuminate\Auth\AuthManager $auth
|
|
|
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
|
|
|
* @param \PragmaRX\Google2FA\Google2FA $google2FA
|
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config
|
|
|
|
* @param \Illuminate\Contracts\Cache\Repository $cache
|
2019-06-22 13:33:11 -07:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
AuthManager $auth,
|
|
|
|
Encrypter $encrypter,
|
|
|
|
Google2FA $google2FA,
|
|
|
|
Repository $config,
|
|
|
|
CacheRepository $cache,
|
|
|
|
UserRepositoryInterface $repository
|
|
|
|
) {
|
|
|
|
parent::__construct($auth, $config);
|
|
|
|
|
|
|
|
$this->google2FA = $google2FA;
|
|
|
|
$this->cache = $cache;
|
|
|
|
$this->repository = $repository;
|
|
|
|
$this->encrypter = $encrypter;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Auth\LoginCheckpointRequest $request
|
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
|
2018-04-07 12:35:15 -05:00
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
*/
|
2018-04-08 15:18:13 -05:00
|
|
|
public function __invoke(LoginCheckpointRequest $request): JsonResponse
|
2018-04-07 12:35:15 -05:00
|
|
|
{
|
2020-04-25 13:01:16 -07:00
|
|
|
$token = $request->input('confirmation_token');
|
|
|
|
|
2018-04-07 12:35:15 -05:00
|
|
|
try {
|
2020-04-25 13:01:16 -07:00
|
|
|
$user = $this->repository->find($this->cache->get($token, 0));
|
2018-04-07 12:35:15 -05:00
|
|
|
} catch (RecordNotFoundException $exception) {
|
|
|
|
return $this->sendFailedLoginResponse($request);
|
|
|
|
}
|
|
|
|
|
2019-06-22 13:33:11 -07:00
|
|
|
$decrypted = $this->encrypter->decrypt($user->totp_secret);
|
2018-04-07 12:35:15 -05:00
|
|
|
|
2020-04-25 13:01:16 -07:00
|
|
|
if ($this->google2FA->verifyKey($decrypted, (string) $request->input('authentication_code') ?? '', config('pterodactyl.auth.2fa.window'))) {
|
|
|
|
$this->cache->delete($token);
|
|
|
|
|
2019-06-22 13:33:11 -07:00
|
|
|
return $this->sendLoginResponse($user, $request);
|
2018-04-07 12:35:15 -05:00
|
|
|
}
|
|
|
|
|
2019-06-22 13:33:11 -07:00
|
|
|
return $this->sendFailedLoginResponse($request, $user);
|
2018-04-07 12:35:15 -05:00
|
|
|
}
|
|
|
|
}
|