|
1 | 1 | <?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 | | - */ |
9 | 2 |
|
10 | 3 | namespace Pterodactyl\Services\Users; |
11 | 4 |
|
| 5 | +use Carbon\Carbon; |
12 | 6 | use Pterodactyl\Models\User; |
13 | | -use PragmaRX\Google2FA\Contracts\Google2FA; |
| 7 | +use PragmaRX\Google2FA\Google2FA; |
| 8 | +use Illuminate\Contracts\Config\Repository; |
| 9 | +use Illuminate\Contracts\Encryption\Encrypter; |
14 | 10 | use Pterodactyl\Contracts\Repository\UserRepositoryInterface; |
15 | 11 | use Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid; |
16 | 12 |
|
17 | 13 | class ToggleTwoFactorService |
18 | 14 | { |
19 | 15 | /** |
20 | | - * @var \PragmaRX\Google2FA\Contracts\Google2FA |
| 16 | + * @var \Illuminate\Contracts\Config\Repository |
21 | 17 | */ |
22 | | - protected $google2FA; |
| 18 | + private $config; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var \Illuminate\Contracts\Encryption\Encrypter |
| 22 | + */ |
| 23 | + private $encrypter; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var \PragmaRX\Google2FA\Google2FA |
| 27 | + */ |
| 28 | + private $google2FA; |
23 | 29 |
|
24 | 30 | /** |
25 | 31 | * @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface |
26 | 32 | */ |
27 | | - protected $repository; |
| 33 | + private $repository; |
28 | 34 |
|
29 | 35 | /** |
30 | 36 | * ToggleTwoFactorService constructor. |
31 | 37 | * |
32 | | - * @param \PragmaRX\Google2FA\Contracts\Google2FA $google2FA |
| 38 | + * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter |
| 39 | + * @param \PragmaRX\Google2FA\Google2FA $google2FA |
| 40 | + * @param \Illuminate\Contracts\Config\Repository $config |
33 | 41 | * @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $repository |
34 | 42 | */ |
35 | 43 | public function __construct( |
| 44 | + Encrypter $encrypter, |
36 | 45 | Google2FA $google2FA, |
| 46 | + Repository $config, |
37 | 47 | UserRepositoryInterface $repository |
38 | 48 | ) { |
| 49 | + $this->config = $config; |
| 50 | + $this->encrypter = $encrypter; |
39 | 51 | $this->google2FA = $google2FA; |
40 | 52 | $this->repository = $repository; |
41 | 53 | } |
42 | 54 |
|
43 | 55 | /** |
44 | | - * @param int|\Pterodactyl\Models\User $user |
45 | | - * @param string $token |
46 | | - * @param null|bool $toggleState |
| 56 | + * Toggle 2FA on an account only if the token provided is valid. |
| 57 | + * |
| 58 | + * @param \Pterodactyl\Models\User $user |
| 59 | + * @param string $token |
| 60 | + * @param bool|null $toggleState |
47 | 61 | * @return bool |
48 | 62 | * |
49 | 63 | * @throws \Pterodactyl\Exceptions\Model\DataValidationException |
50 | 64 | * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
51 | 65 | * @throws \Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid |
52 | 66 | */ |
53 | | - public function handle($user, $token, $toggleState = null) |
| 67 | + public function handle(User $user, string $token, bool $toggleState = null): bool |
54 | 68 | { |
55 | | - if (! $user instanceof User) { |
56 | | - $user = $this->repository->find($user); |
57 | | - } |
| 69 | + $window = $this->config->get('pterodactyl.auth.2fa.window'); |
| 70 | + $secret = $this->encrypter->decrypt($user->totp_secret); |
| 71 | + |
| 72 | + $isValidToken = $this->google2FA->verifyKey($secret, $token, $window); |
58 | 73 |
|
59 | | - if (! $this->google2FA->verifyKey($user->totp_secret, $token, 2)) { |
| 74 | + if (! $isValidToken) { |
60 | 75 | throw new TwoFactorAuthenticationTokenInvalid; |
61 | 76 | } |
62 | 77 |
|
63 | 78 | $this->repository->withoutFresh()->update($user->id, [ |
| 79 | + 'totp_authenticated_at' => Carbon::now(), |
64 | 80 | 'use_totp' => (is_null($toggleState) ? ! $user->use_totp : $toggleState), |
65 | 81 | ]); |
66 | 82 |
|
|
0 commit comments