|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Http\Controllers\Api\Client; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use Illuminate\Http\Response; |
| 7 | +use Illuminate\Http\JsonResponse; |
| 8 | +use Illuminate\Contracts\Validation\Factory; |
| 9 | +use Illuminate\Validation\ValidationException; |
| 10 | +use Pterodactyl\Services\Users\TwoFactorSetupService; |
| 11 | +use Pterodactyl\Services\Users\ToggleTwoFactorService; |
| 12 | +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
| 13 | + |
| 14 | +class TwoFactorController extends ClientApiController |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var \Pterodactyl\Services\Users\TwoFactorSetupService |
| 18 | + */ |
| 19 | + private $setupService; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Illuminate\Contracts\Validation\Factory |
| 23 | + */ |
| 24 | + private $validation; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var \Pterodactyl\Services\Users\ToggleTwoFactorService |
| 28 | + */ |
| 29 | + private $toggleTwoFactorService; |
| 30 | + |
| 31 | + /** |
| 32 | + * TwoFactorController constructor. |
| 33 | + * |
| 34 | + * @param \Pterodactyl\Services\Users\ToggleTwoFactorService $toggleTwoFactorService |
| 35 | + * @param \Pterodactyl\Services\Users\TwoFactorSetupService $setupService |
| 36 | + * @param \Illuminate\Contracts\Validation\Factory $validation |
| 37 | + */ |
| 38 | + public function __construct( |
| 39 | + ToggleTwoFactorService $toggleTwoFactorService, |
| 40 | + TwoFactorSetupService $setupService, |
| 41 | + Factory $validation |
| 42 | + ) { |
| 43 | + parent::__construct(); |
| 44 | + |
| 45 | + $this->setupService = $setupService; |
| 46 | + $this->validation = $validation; |
| 47 | + $this->toggleTwoFactorService = $toggleTwoFactorService; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns two-factor token credentials that allow a user to configure |
| 52 | + * it on their account. If two-factor is already enabled this endpoint |
| 53 | + * will return a 400 error. |
| 54 | + * |
| 55 | + * @param \Illuminate\Http\Request $request |
| 56 | + * @return \Illuminate\Http\JsonResponse |
| 57 | + * |
| 58 | + * @throws \Pterodactyl\Exceptions\Model\DataValidationException |
| 59 | + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
| 60 | + */ |
| 61 | + public function index(Request $request) |
| 62 | + { |
| 63 | + if ($request->user()->totp_enabled) { |
| 64 | + throw new BadRequestHttpException('Two-factor authentication is already enabled on this account.'); |
| 65 | + } |
| 66 | + |
| 67 | + return JsonResponse::create([ |
| 68 | + 'data' => [ |
| 69 | + 'image_url_data' => $this->setupService->handle($request->user()), |
| 70 | + ], |
| 71 | + ]); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Updates a user's account to have two-factor enabled. |
| 76 | + * |
| 77 | + * @param \Illuminate\Http\Request $request |
| 78 | + * @return \Illuminate\Http\JsonResponse |
| 79 | + * |
| 80 | + * @throws \Illuminate\Validation\ValidationException |
| 81 | + * @throws \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException |
| 82 | + * @throws \PragmaRX\Google2FA\Exceptions\InvalidCharactersException |
| 83 | + * @throws \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException |
| 84 | + * @throws \Pterodactyl\Exceptions\Model\DataValidationException |
| 85 | + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException |
| 86 | + * @throws \Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid |
| 87 | + */ |
| 88 | + public function store(Request $request) |
| 89 | + { |
| 90 | + $validator = $this->validation->make($request->all(), [ |
| 91 | + 'code' => 'required|string', |
| 92 | + ]); |
| 93 | + |
| 94 | + if ($validator->fails()) { |
| 95 | + throw new ValidationException($validator); |
| 96 | + } |
| 97 | + |
| 98 | + $this->toggleTwoFactorService->handle($request->user(), $request->input('code'), true); |
| 99 | + |
| 100 | + return JsonResponse::create([], Response::HTTP_NO_CONTENT); |
| 101 | + } |
| 102 | + |
| 103 | + public function delete() |
| 104 | + { |
| 105 | + } |
| 106 | +} |
0 commit comments