11<?php
2- /**
3- * Pterodactyl - Panel
4- * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5- *
6- * This software is licensed under the terms of the MIT license.
7- * https://opensource.org/licenses/MIT
8- */
92
103namespace Pterodactyl \Services \Users ;
114
5+ use Exception ;
6+ use RuntimeException ;
127use Pterodactyl \Models \User ;
13- use Illuminate \Support \Collection ;
14- use PragmaRX \Google2FAQRCode \Google2FA ;
158use Illuminate \Contracts \Encryption \Encrypter ;
169use Pterodactyl \Contracts \Repository \UserRepositoryInterface ;
1710use Illuminate \Contracts \Config \Repository as ConfigRepository ;
1811
1912class TwoFactorSetupService
2013{
14+ const VALID_BASE32_CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567 ' ;
15+
2116 /**
2217 * @var \Illuminate\Contracts\Config\Repository
2318 */
@@ -28,11 +23,6 @@ class TwoFactorSetupService
2823 */
2924 private $ encrypter ;
3025
31- /**
32- * @var PragmaRX\Google2FAQRCode\Google2FA
33- */
34- private $ google2FA ;
35-
3626 /**
3727 * @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
3828 */
@@ -43,43 +33,51 @@ class TwoFactorSetupService
4333 *
4434 * @param \Illuminate\Contracts\Config\Repository $config
4535 * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
46- * @param PragmaRX\Google2FAQRCode\Google2FA $google2FA
4736 * @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
4837 */
4938 public function __construct (
5039 ConfigRepository $ config ,
5140 Encrypter $ encrypter ,
52- Google2FA $ google2FA ,
5341 UserRepositoryInterface $ repository
5442 ) {
5543 $ this ->config = $ config ;
5644 $ this ->encrypter = $ encrypter ;
57- $ this ->google2FA = $ google2FA ;
5845 $ this ->repository = $ repository ;
5946 }
6047
6148 /**
6249 * Generate a 2FA token and store it in the database before returning the
63- * QR code image.
50+ * QR code URL. This URL will need to be attached to a QR generating service in
51+ * order to function.
6452 *
6553 * @param \Pterodactyl\Models\User $user
66- * @return \Illuminate\Support\Collection
54+ * @return string
6755 *
6856 * @throws \Pterodactyl\Exceptions\Model\DataValidationException
6957 * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
7058 */
71- public function handle (User $ user ): Collection
59+ public function handle (User $ user ): string
7260 {
73- $ secret = $ this ->google2FA ->generateSecretKey ($ this ->config ->get ('pterodactyl.auth.2fa.bytes ' ));
74- $ image = $ this ->google2FA ->getQRCodeInline ($ this ->config ->get ('app.name ' ), $ user ->email , $ secret );
61+ $ secret = '' ;
62+ try {
63+ for ($ i = 0 ; $ i < $ this ->config ->get ('pterodactyl.auth.2fa.bytes ' , 16 ); $ i ++) {
64+ $ secret .= substr (self ::VALID_BASE32_CHARACTERS , random_int (0 , 31 ), 1 );
65+ }
66+ } catch (Exception $ exception ) {
67+ throw new RuntimeException ($ exception ->getMessage (), 0 , $ exception );
68+ }
7569
7670 $ this ->repository ->withoutFreshModel ()->update ($ user ->id , [
7771 'totp_secret ' => $ this ->encrypter ->encrypt ($ secret ),
7872 ]);
7973
80- return new Collection ([
81- 'image ' => $ image ,
82- 'secret ' => $ secret ,
83- ]);
74+ $ company = $ this ->config ->get ('app.name ' );
75+
76+ return sprintf (
77+ 'otpauth://totp/%1$s:%2$s?secret=%3$s&issuer=%1$s ' ,
78+ rawurlencode ($ company ),
79+ rawurlencode ($ user ->email ),
80+ rawurlencode ($ secret )
81+ );
8482 }
8583}
0 commit comments