setupService = $setupService; $this->validation = $validation; $this->toggleTwoFactorService = $toggleTwoFactorService; } /** * Returns two-factor token credentials that allow a user to configure * it on their account. If two-factor is already enabled this endpoint * will return a 400 error. * * @return \Illuminate\Http\JsonResponse * * @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function index(Request $request) { if ($request->user()->use_totp) { throw new BadRequestHttpException('Two-factor authentication is already enabled on this account.'); } return new JsonResponse([ 'data' => $this->setupService->handle($request->user()), ]); } /** * Updates a user's account to have two-factor enabled. * * @return \Illuminate\Http\JsonResponse * * @throws \Throwable * @throws \Illuminate\Validation\ValidationException */ public function store(Request $request) { $validator = $this->validation->make($request->all(), [ 'code' => ['required', 'string', 'size:6'], 'password' => ['required', 'string'], ]); $data = $validator->validate(); if (!password_verify($data['password'], $request->user()->password)) { throw new BadRequestHttpException('The password provided was not valid.'); } $tokens = $this->toggleTwoFactorService->handle($request->user(), $data['code'], true); Activity::event('user:two-factor.create')->log(); return new JsonResponse([ 'object' => 'recovery_tokens', 'attributes' => [ 'tokens' => $tokens, ], ]); } /** * Disables two-factor authentication on an account if the password provided * is valid. * * @return \Illuminate\Http\JsonResponse * @throws \Throwable */ public function delete(Request $request) { if (!password_verify($request->input('password') ?? '', $request->user()->password)) { throw new BadRequestHttpException('The password provided was not valid.'); } /** @var \Pterodactyl\Models\User $user */ $user = $request->user(); $user->update([ 'totp_authenticated_at' => Carbon::now(), 'use_totp' => false, ]); Activity::event('user:two-factor.delete')->log(); return new JsonResponse([], Response::HTTP_NO_CONTENT); } }