[ 'image_url_data' => 'otpauth://totp/Pterodactyl:user@example.com?secret=EXAMPLESECRET&issuer=Pterodactyl', 'secret' => 'EXAMPLESECRET', ], ]; private const array RECOVERY_TOKENS_EXAMPLE = [ 'object' => 'recovery_tokens', 'attributes' => [ 'tokens' => [ 'r1a2b3c4d5', 'r6e7f8g9h0', ], ], ]; private const array BAD_REQUEST_ERROR = [ 'errors' => [ [ 'code' => 'InvalidPasswordProvidedException', 'status' => '400', 'detail' => 'The password provided was invalid for this account.', ], ], ]; private const array ALREADY_ENABLED_ERROR = [ 'errors' => [ [ 'code' => 'BadRequestHttpException', 'status' => '400', 'detail' => 'Two-factor authentication is already enabled on this account.', ], ], ]; /** * 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. */ #[Endpoint('Get two-factor setup', 'Creates or returns the current two-factor setup payload for the authenticated user.')] #[ScribeResponse(self::TWO_FACTOR_SETUP_EXAMPLE, description: 'Two-factor setup data returned.')] #[ScribeResponse(self::ALREADY_ENABLED_ERROR, status: 400, description: 'Two-factor authentication is already enabled.')] public function index(Request $request, SetsUpTwoFactor $twoFactor): JsonResponse { $user = $this->authenticatedUser($request); throw_if($user->use_totp, BadRequestHttpException::class, 'Two-factor authentication is already enabled on this account.'); return new JsonResponse([ 'data' => $twoFactor->setup($user), ]); } /** * Updates a user's account to have two-factor enabled. * * @throws Throwable */ #[Endpoint('Enable two-factor authentication', 'Enables two-factor authentication for the authenticated user and returns one-time recovery tokens.')] #[BodyParam('code', 'string', 'The six-digit TOTP code generated by the authenticator app.', required: true, example: '123456')] #[BodyParam('password', 'string', 'The current account password.', required: true, example: 'current-password')] #[ScribeResponse(self::RECOVERY_TOKENS_EXAMPLE, description: 'Two-factor authentication enabled. Store these recovery tokens now; they are not shown again.')] #[ScribeResponse(self::BAD_REQUEST_ERROR, status: 400, description: 'The password or TOTP code is invalid.')] public function store(EnableTwoFactorRequest $request, TogglesTwoFactor $twoFactor): JsonResponse { $user = $this->authenticatedUser($request); $password = JsonValueGuard::string($request->validated('password')); throw_unless(Hash::check($password, $user->password), InvalidPasswordProvidedException::class, trans('validation.internal.invalid_password')); $code = JsonValueGuard::string($request->validated('code')); $tokens = $twoFactor->toggle($user, $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. * * @throws Throwable */ #[Endpoint('Disable two-factor authentication', 'Disables two-factor authentication for the authenticated user after verifying their password.')] #[BodyParam('password', 'string', 'The current account password.', required: true, example: 'current-password')] #[ScribeResponse(status: 204, description: 'Two-factor authentication disabled.')] #[ScribeResponse(self::BAD_REQUEST_ERROR, status: 400, description: 'The password is invalid.')] public function delete(DisableTwoFactorRequest $request, DisablesTwoFactor $twoFactor): JsonResponse { $user = $this->authenticatedUser($request); $password = JsonValueGuard::string($request->validated('password')); throw_unless(Hash::check($password, $user->password), InvalidPasswordProvidedException::class, trans('validation.internal.invalid_password')); $twoFactor->disable($user); Activity::event('user:two-factor.delete')->log(); return new JsonResponse([], Response::HTTP_NO_CONTENT); } }