Path: blob/1.0-develop/app/Services/Users/TwoFactorSetupService.php
10277 views
<?php12namespace Pterodactyl\Services\Users;34use Pterodactyl\Models\User;5use Illuminate\Contracts\Encryption\Encrypter;6use Pterodactyl\Contracts\Repository\UserRepositoryInterface;7use Illuminate\Contracts\Config\Repository as ConfigRepository;89class TwoFactorSetupService10{11public const VALID_BASE32_CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';1213/**14* TwoFactorSetupService constructor.15*/16public function __construct(17private ConfigRepository $config,18private Encrypter $encrypter,19private UserRepositoryInterface $repository,20) {21}2223/**24* Generate a 2FA token and store it in the database before returning the25* QR code URL. This URL will need to be attached to a QR generating service in26* order to function.27*28* @throws \Pterodactyl\Exceptions\Model\DataValidationException29* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException30*/31public function handle(User $user): array32{33$secret = '';34try {35for ($i = 0; $i < $this->config->get('pterodactyl.auth.2fa.bytes', 16); ++$i) {36$secret .= substr(self::VALID_BASE32_CHARACTERS, random_int(0, 31), 1);37}38} catch (\Exception $exception) {39throw new \RuntimeException($exception->getMessage(), 0, $exception);40}4142$this->repository->withoutFreshModel()->update($user->id, [43'totp_secret' => $this->encrypter->encrypt($secret),44]);4546$company = urlencode(preg_replace('/\s/', '', $this->config->get('app.name')));4748return [49'image_url_data' => sprintf(50'otpauth://totp/%1$s:%2$s?secret=%3$s&issuer=%1$s',51rawurlencode($company),52rawurlencode($user->email),53rawurlencode($secret),54),55'secret' => $secret,56];57}58}596061