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 PragmaRX \Google2FA \Google2FA ;
148use Illuminate \Contracts \Encryption \Encrypter ;
159use Pterodactyl \Contracts \Repository \UserRepositoryInterface ;
1610use Illuminate \Contracts \Config \Repository as ConfigRepository ;
1711
1812class TwoFactorSetupService
1913{
14+ const VALID_BASE32_CHARACTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567 ' ;
15+
2016 /**
2117 * @var \Illuminate\Contracts\Config\Repository
2218 */
@@ -27,11 +23,6 @@ class TwoFactorSetupService
2723 */
2824 private $ encrypter ;
2925
30- /**
31- * @var \PragmaRX\Google2FA\Google2FA
32- */
33- private $ google2FA ;
34-
3526 /**
3627 * @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface
3728 */
@@ -42,24 +33,22 @@ class TwoFactorSetupService
4233 *
4334 * @param \Illuminate\Contracts\Config\Repository $config
4435 * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
45- * @param \PragmaRX\Google2FA\Google2FA $google2FA
4636 * @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository
4737 */
4838 public function __construct (
4939 ConfigRepository $ config ,
5040 Encrypter $ encrypter ,
51- Google2FA $ google2FA ,
5241 UserRepositoryInterface $ repository
5342 ) {
5443 $ this ->config = $ config ;
5544 $ this ->encrypter = $ encrypter ;
56- $ this ->google2FA = $ google2FA ;
5745 $ this ->repository = $ repository ;
5846 }
5947
6048 /**
6149 * Generate a 2FA token and store it in the database before returning the
62- * QR code image.
50+ * QR code URL. This URL will need to be attached to a QR generating service in
51+ * order to function.
6352 *
6453 * @param \Pterodactyl\Models\User $user
6554 * @return string
@@ -69,13 +58,26 @@ public function __construct(
6958 */
7059 public function handle (User $ user ): string
7160 {
72- $ secret = $ this ->google2FA ->generateSecretKey ($ this ->config ->get ('pterodactyl.auth.2fa.bytes ' ));
73- $ image = $ this ->google2FA ->getQRCodeGoogleUrl ($ 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+ }
7469
7570 $ this ->repository ->withoutFreshModel ()->update ($ user ->id , [
7671 'totp_secret ' => $ this ->encrypter ->encrypt ($ secret ),
7772 ]);
7873
79- return $ image ;
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+ );
8082 }
8183}
0 commit comments