2019-12-22 17:03:44 -08:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2019-12-22 17:03:44 -08:00
namespace Pterodactyl\Http\Controllers\Api\Client ;
2026-10-05 18:10:18 -04:00
use Illuminate\Http\JsonResponse ;
2019-12-22 17:03:44 -08:00
use Illuminate\Http\Request ;
use Illuminate\Http\Response ;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Facades\Hash ;
use Knuckles\Scribe\Attributes\BodyParam ;
use Knuckles\Scribe\Attributes\Endpoint ;
use Knuckles\Scribe\Attributes\Group ;
use Knuckles\Scribe\Attributes\Response as ScribeResponse ;
use Knuckles\Scribe\Attributes\Subgroup ;
use Pterodactyl\Contracts\Users\DisablesTwoFactor ;
use Pterodactyl\Contracts\Users\SetsUpTwoFactor ;
use Pterodactyl\Contracts\Users\TogglesTwoFactor ;
use Pterodactyl\Exceptions\Http\Base\InvalidPasswordProvidedException ;
2022-05-29 18:48:35 -04:00
use Pterodactyl\Facades\Activity ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Requests\Api\Client\Account\DisableTwoFactorRequest ;
use Pterodactyl\Http\Requests\Api\Client\Account\EnableTwoFactorRequest ;
use Pterodactyl\Support\JsonValueGuard ;
2019-12-22 17:03:44 -08:00
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException ;
2026-10-05 18:10:18 -04:00
use Throwable ;
2019-12-22 17:03:44 -08:00
2026-10-05 18:10:18 -04:00
#[ Group ( 'Client API' , 'Endpoints authenticated as a panel user using a client API token.' )]
#[ Subgroup ( 'Two-Factor Authentication' , 'Configure two-factor authentication for the authenticated user.' )]
2019-12-22 17:03:44 -08:00
class TwoFactorController extends ClientApiController
{
2026-10-05 18:10:18 -04:00
private const array TWO_FACTOR_SETUP_EXAMPLE = [
'data' => [
'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.' ,
],
],
];
2019-12-22 17:03:44 -08:00
/**
* 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.
*/
2026-10-05 18:10:18 -04:00
#[ 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
2019-12-22 17:03:44 -08:00
{
2026-10-05 18:10:18 -04:00
$user = $this -> authenticatedUser ( $request );
throw_if ( $user -> use_totp , BadRequestHttpException :: class , 'Two-factor authentication is already enabled on this account.' );
2019-12-22 17:03:44 -08:00
2020-06-27 11:06:35 -07:00
return new JsonResponse ([
2026-10-05 18:10:18 -04:00
'data' => $twoFactor -> setup ( $user ),
2019-12-22 17:03:44 -08:00
]);
}
/**
* Updates a user's account to have two-factor enabled.
*
2026-10-05 18:10:18 -04:00
* @throws Throwable
2019-12-22 17:03:44 -08:00
*/
2026-10-05 18:10:18 -04:00
#[ 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
2019-12-22 17:03:44 -08:00
{
2026-10-05 18:10:18 -04:00
$user = $this -> authenticatedUser ( $request );
$password = JsonValueGuard :: string ( $request -> validated ( 'password' ));
throw_unless ( Hash :: check ( $password , $user -> password ), InvalidPasswordProvidedException :: class , trans ( 'validation.internal.invalid_password' ));
2019-12-22 17:03:44 -08:00
2026-10-05 18:10:18 -04:00
$code = JsonValueGuard :: string ( $request -> validated ( 'code' ));
2019-12-22 17:03:44 -08:00
2026-10-05 18:10:18 -04:00
$tokens = $twoFactor -> toggle ( $user , $code , true );
2019-12-22 17:03:44 -08:00
2022-05-29 18:48:35 -04:00
Activity :: event ( 'user:two-factor.create' ) -> log ();
2020-07-02 21:55:25 -07:00
return new JsonResponse ([
'object' => 'recovery_tokens' ,
'attributes' => [
'tokens' => $tokens ,
],
]);
2019-12-22 17:03:44 -08:00
}
2019-12-22 20:41:25 -08:00
/**
* Disables two-factor authentication on an account if the password provided
* is valid.
*
2026-10-05 18:10:18 -04:00
* @throws Throwable
2019-12-22 20:41:25 -08:00
*/
2026-10-05 18:10:18 -04:00
#[ 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
2019-12-22 17:03:44 -08:00
{
2026-10-05 18:10:18 -04:00
$user = $this -> authenticatedUser ( $request );
$password = JsonValueGuard :: string ( $request -> validated ( 'password' ));
throw_unless ( Hash :: check ( $password , $user -> password ), InvalidPasswordProvidedException :: class , trans ( 'validation.internal.invalid_password' ));
2019-12-22 20:41:25 -08:00
2026-10-05 18:10:18 -04:00
$twoFactor -> disable ( $user );
2019-12-22 20:41:25 -08:00
2022-05-29 18:48:35 -04:00
Activity :: event ( 'user:two-factor.delete' ) -> log ();
2020-06-27 11:06:35 -07:00
return new JsonResponse ([], Response :: HTTP_NO_CONTENT );
2019-12-22 17:03:44 -08:00
}
}