misc_pterodactyl-panel/app/Services/Users/TwoFactorSetupService.php

82 lines
2.4 KiB
PHP
Raw Normal View History

<?php
2017-09-26 02:43:01 +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
*/
namespace Pterodactyl\Services\Users;
2017-08-31 02:14:20 +00:00
use Pterodactyl\Models\User;
use PragmaRX\Google2FAQRCode\Google2FA;
2017-11-18 18:35:33 +00:00
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
2017-08-31 02:14:20 +00:00
use Illuminate\Contracts\Config\Repository as ConfigRepository;
class TwoFactorSetupService
{
/**
* @var \Illuminate\Contracts\Config\Repository
*/
2017-11-18 18:35:33 +00:00
private $config;
/**
2017-11-18 18:35:33 +00:00
* @var \Illuminate\Contracts\Encryption\Encrypter
*/
2017-11-18 18:35:33 +00:00
private $encrypter;
/**
* @var PragmaRX\Google2FAQRCode\Google2FA
2017-11-18 18:35:33 +00:00
*/
private $google2FA;
/**
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
*/
2017-11-18 18:35:33 +00:00
private $repository;
/**
* TwoFactorSetupService constructor.
*
* @param \Illuminate\Contracts\Config\Repository $config
2017-11-18 18:35:33 +00:00
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
* @param PragmaRX\Google2FAQRCode\Google2FA $google2FA
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
*/
public function __construct(
ConfigRepository $config,
2017-11-18 18:35:33 +00:00
Encrypter $encrypter,
Google2FA $google2FA,
UserRepositoryInterface $repository
) {
$this->config = $config;
2017-11-18 18:35:33 +00:00
$this->encrypter = $encrypter;
$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-11-18 18:35:33 +00:00
* @param \Pterodactyl\Models\User $user
* @return string
*
* @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-11-18 18:35:33 +00:00
$secret = $this->google2FA->generateSecretKey($this->config->get('pterodactyl.auth.2fa.bytes'));
$image = $this->google2FA->getQRCodeInline($this->config->get('app.name'), $user->email, $secret);
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-11-18 18:35:33 +00:00
return $image;
}
}