2017-08-31 04:11:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Users;
|
|
|
|
|
2017-11-18 19:35:33 +01:00
|
|
|
use Carbon\Carbon;
|
2020-07-03 06:55:25 +02:00
|
|
|
use Illuminate\Support\Str;
|
2017-08-31 04:14:20 +02:00
|
|
|
use Pterodactyl\Models\User;
|
2017-11-18 19:35:33 +01:00
|
|
|
use PragmaRX\Google2FA\Google2FA;
|
2020-07-03 07:11:07 +02:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2017-11-18 19:35:33 +01:00
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2017-08-31 04:11:14 +02:00
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
2020-07-03 06:55:25 +02:00
|
|
|
use Pterodactyl\Repositories\Eloquent\RecoveryTokenRepository;
|
2017-08-31 04:11:14 +02:00
|
|
|
use Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid;
|
|
|
|
|
|
|
|
class ToggleTwoFactorService
|
|
|
|
{
|
2017-11-18 19:35:33 +01:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
|
|
|
*/
|
|
|
|
private $encrypter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \PragmaRX\Google2FA\Google2FA
|
|
|
|
*/
|
|
|
|
private $google2FA;
|
2017-08-31 04:11:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
|
|
|
*/
|
2017-11-18 19:35:33 +01:00
|
|
|
private $repository;
|
2017-08-31 04:11:14 +02:00
|
|
|
|
2020-07-03 06:55:25 +02:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\RecoveryTokenRepository
|
|
|
|
*/
|
|
|
|
private $recoveryTokenRepository;
|
|
|
|
|
2020-07-03 07:11:07 +02:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
2017-08-31 04:11:14 +02:00
|
|
|
/**
|
|
|
|
* ToggleTwoFactorService constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2020-07-03 07:11:07 +02:00
|
|
|
ConnectionInterface $connection,
|
2017-11-18 19:35:33 +01:00
|
|
|
Encrypter $encrypter,
|
2017-08-31 04:11:14 +02:00
|
|
|
Google2FA $google2FA,
|
2020-07-03 06:55:25 +02:00
|
|
|
RecoveryTokenRepository $recoveryTokenRepository,
|
2017-08-31 04:11:14 +02:00
|
|
|
UserRepositoryInterface $repository
|
|
|
|
) {
|
2017-11-18 19:35:33 +01:00
|
|
|
$this->encrypter = $encrypter;
|
2017-08-31 04:11:14 +02:00
|
|
|
$this->google2FA = $google2FA;
|
|
|
|
$this->repository = $repository;
|
2020-07-03 06:55:25 +02:00
|
|
|
$this->recoveryTokenRepository = $recoveryTokenRepository;
|
2020-07-03 07:11:07 +02:00
|
|
|
$this->connection = $connection;
|
2017-08-31 04:11:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-18 19:35:33 +01:00
|
|
|
* Toggle 2FA on an account only if the token provided is valid.
|
|
|
|
*
|
2020-07-03 06:55:25 +02:00
|
|
|
* @return string[]
|
2017-08-31 04:11:14 +02:00
|
|
|
*
|
2020-07-03 07:11:07 +02:00
|
|
|
* @throws \Throwable
|
2019-12-08 20:02:59 +01:00
|
|
|
* @throws \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException
|
|
|
|
* @throws \PragmaRX\Google2FA\Exceptions\InvalidCharactersException
|
|
|
|
* @throws \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException
|
2017-08-31 04:11:14 +02:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid
|
|
|
|
*/
|
2020-07-03 06:55:25 +02:00
|
|
|
public function handle(User $user, string $token, bool $toggleState = null): array
|
2017-08-31 04:11:14 +02:00
|
|
|
{
|
2017-11-18 19:35:33 +01:00
|
|
|
$secret = $this->encrypter->decrypt($user->totp_secret);
|
|
|
|
|
2019-12-08 20:02:59 +01:00
|
|
|
$isValidToken = $this->google2FA->verifyKey($secret, $token, config()->get('pterodactyl.auth.2fa.window'));
|
2017-08-31 04:11:14 +02:00
|
|
|
|
2021-01-23 21:33:34 +01:00
|
|
|
if (!$isValidToken) {
|
2021-03-21 18:43:01 +01:00
|
|
|
throw new TwoFactorAuthenticationTokenInvalid();
|
2020-07-03 06:55:25 +02:00
|
|
|
}
|
|
|
|
|
2020-07-03 07:11:07 +02:00
|
|
|
return $this->connection->transaction(function () use ($user, $toggleState) {
|
|
|
|
// Now that we're enabling 2FA on the account, generate 10 recovery tokens for the account
|
|
|
|
// and store them hashed in the database. We'll return them to the caller so that the user
|
|
|
|
// can see and save them.
|
|
|
|
//
|
|
|
|
// If a user is unable to login with a 2FA token they can provide one of these backup codes
|
|
|
|
// which will then be marked as deleted from the database and will also bypass 2FA protections
|
|
|
|
// on their account.
|
|
|
|
$tokens = [];
|
2021-01-23 21:33:34 +01:00
|
|
|
if ((!$toggleState && !$user->use_totp) || $toggleState) {
|
2020-07-03 07:11:07 +02:00
|
|
|
$inserts = [];
|
2021-01-23 21:33:34 +01:00
|
|
|
for ($i = 0; $i < 10; ++$i) {
|
2020-07-03 07:11:07 +02:00
|
|
|
$token = Str::random(10);
|
|
|
|
|
|
|
|
$inserts[] = [
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'token' => password_hash($token, PASSWORD_DEFAULT),
|
2021-03-21 18:43:01 +01:00
|
|
|
// insert() won't actually set the time on the models, so make sure we do this
|
|
|
|
// manually here.
|
|
|
|
'created_at' => Carbon::now(),
|
2020-07-03 07:11:07 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$tokens[] = $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Before inserting any new records make sure all of the old ones are deleted to avoid
|
|
|
|
// any issues or storing an unnecessary number of tokens in the database.
|
|
|
|
$this->recoveryTokenRepository->deleteWhere(['user_id' => $user->id]);
|
|
|
|
|
|
|
|
// Bulk insert the hashed tokens.
|
|
|
|
$this->recoveryTokenRepository->insert($inserts);
|
2020-07-03 06:55:25 +02:00
|
|
|
}
|
|
|
|
|
2020-07-03 07:11:07 +02:00
|
|
|
$this->repository->withoutFreshModel()->update($user->id, [
|
|
|
|
'totp_authenticated_at' => Carbon::now(),
|
2021-01-23 21:33:34 +01:00
|
|
|
'use_totp' => (is_null($toggleState) ? !$user->use_totp : $toggleState),
|
2020-07-03 07:11:07 +02:00
|
|
|
]);
|
2017-08-31 04:11:14 +02:00
|
|
|
|
2020-07-03 07:11:07 +02:00
|
|
|
return $tokens;
|
|
|
|
});
|
2017-08-31 04:11:14 +02:00
|
|
|
}
|
|
|
|
}
|