2017-08-31 02:11:14 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-08-31 02:11:14 +00:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-08-31 02:11:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Users;
|
|
|
|
|
2019-05-02 03:57:49 +00:00
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
2017-08-31 02:14:20 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2019-04-27 18:18:12 +00:00
|
|
|
use PragmaRX\Google2FAQRCode\Google2FA;
|
2018-06-21 06:05:35 +00:00
|
|
|
use Illuminate\Support\Collection;
|
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;
|
|
|
|
|
|
|
|
class TwoFactorSetupService
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
/**
|
2019-04-27 18:18:12 +00:00
|
|
|
* @var PragmaRX\Google2FAQRCode\Google2FA
|
2017-11-18 18:35:33 +00:00
|
|
|
*/
|
|
|
|
private $google2FA;
|
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
|
2019-04-27 18:18:12 +00:00
|
|
|
* @param PragmaRX\Google2FAQRCode\Google2FA $google2FA
|
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
|
|
|
Google2FA $google2FA,
|
|
|
|
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->google2FA = $google2FA;
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-18 18:35:33 +00:00
|
|
|
* Generate a 2FA token and store it in the database before returning the
|
|
|
|
* QR code image.
|
2017-08-31 02:11:14 +00:00
|
|
|
*
|
2017-11-18 18:35:33 +00:00
|
|
|
* @param \Pterodactyl\Models\User $user
|
2018-06-21 06:05:35 +00:00
|
|
|
* @return \Illuminate\Support\Collection
|
2017-08-31 02:11:14 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2018-06-21 06:05:35 +00:00
|
|
|
public function handle(User $user): Collection
|
2017-08-31 02:11:14 +00:00
|
|
|
{
|
2017-11-18 18:35:33 +00:00
|
|
|
$secret = $this->google2FA->generateSecretKey($this->config->get('pterodactyl.auth.2fa.bytes'));
|
2019-04-27 18:18:12 +00:00
|
|
|
$image = $this->google2FA->getQRCodeInline($this->config->get('app.name'), $user->email, $secret);
|
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
|
|
|
|
2018-06-21 06:05:35 +00:00
|
|
|
return new Collection([
|
|
|
|
'image' => $image,
|
|
|
|
'secret' => $secret,
|
|
|
|
]);
|
2017-08-31 02:11:14 +00:00
|
|
|
}
|
|
|
|
}
|