2017-08-31 02:11:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Users;
|
|
|
|
|
2019-06-22 04:55:09 +00:00
|
|
|
use Exception;
|
|
|
|
use RuntimeException;
|
2017-08-31 02:14:20 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2017-11-18 18:35:33 +00:00
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2017-08-31 02:11:14 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
2017-08-31 02:14:20 +00:00
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
2017-08-31 02:11:14 +00:00
|
|
|
|
|
|
|
class TwoFactorSetupService
|
|
|
|
{
|
2019-06-22 04:55:09 +00:00
|
|
|
const VALID_BASE32_CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
|
|
|
|
|
2017-08-31 02:11:14 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Config\Repository
|
|
|
|
*/
|
2017-11-18 18:35:33 +00:00
|
|
|
private $config;
|
2017-08-31 02:11:14 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-18 18:35:33 +00:00
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
2017-08-31 02:11:14 +00:00
|
|
|
*/
|
2017-11-18 18:35:33 +00:00
|
|
|
private $encrypter;
|
|
|
|
|
2017-08-31 02:11:14 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
|
|
|
|
*/
|
2017-11-18 18:35:33 +00:00
|
|
|
private $repository;
|
2017-08-31 02:11:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TwoFactorSetupService constructor.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config
|
2017-11-18 18:35:33 +00:00
|
|
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
2017-08-31 02:11:14 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ConfigRepository $config,
|
2017-11-18 18:35:33 +00:00
|
|
|
Encrypter $encrypter,
|
2017-08-31 02:11:14 +00:00
|
|
|
UserRepositoryInterface $repository
|
|
|
|
) {
|
|
|
|
$this->config = $config;
|
2017-11-18 18:35:33 +00:00
|
|
|
$this->encrypter = $encrypter;
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-18 18:35:33 +00:00
|
|
|
* Generate a 2FA token and store it in the database before returning the
|
2019-06-22 04:55:09 +00:00
|
|
|
* QR code URL. This URL will need to be attached to a QR generating service in
|
|
|
|
* order to function.
|
2017-08-31 02:11:14 +00:00
|
|
|
*
|
2017-11-18 18:35:33 +00:00
|
|
|
* @param \Pterodactyl\Models\User $user
|
|
|
|
* @return string
|
2017-08-31 02:11:14 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2017-11-18 18:35:33 +00:00
|
|
|
public function handle(User $user): string
|
2017-08-31 02:11:14 +00:00
|
|
|
{
|
2019-06-22 04:55:09 +00:00
|
|
|
$secret = '';
|
|
|
|
try {
|
|
|
|
for ($i = 0; $i < $this->config->get('pterodactyl.auth.2fa.bytes', 16); $i++) {
|
|
|
|
$secret .= substr(self::VALID_BASE32_CHARACTERS, random_int(0, 31), 1);
|
|
|
|
}
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
throw new RuntimeException($exception->getMessage(), 0, $exception);
|
|
|
|
}
|
2017-08-31 02:11:14 +00:00
|
|
|
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->repository->withoutFreshModel()->update($user->id, [
|
2017-11-18 18:35:33 +00:00
|
|
|
'totp_secret' => $this->encrypter->encrypt($secret),
|
|
|
|
]);
|
2017-08-31 02:11:14 +00:00
|
|
|
|
2019-06-22 04:55:09 +00:00
|
|
|
$company = $this->config->get('app.name');
|
|
|
|
|
|
|
|
return sprintf(
|
|
|
|
'otpauth://totp/%1$s:%2$s?secret=%3$s&issuer=%1$s',
|
|
|
|
rawurlencode($company),
|
|
|
|
rawurlencode($user->email),
|
|
|
|
rawurlencode($secret)
|
|
|
|
);
|
2017-08-31 02:11:14 +00:00
|
|
|
}
|
|
|
|
}
|