2018-04-07 12:35:15 -05:00
<? php
2026-10-05 18:10:18 -04:00
declare ( strict_types = 1 );
2018-04-07 12:35:15 -05:00
namespace Pterodactyl\Http\Controllers\Auth ;
2026-10-05 18:10:18 -04:00
use Exception ;
2020-10-17 14:46:10 -07:00
use Illuminate\Database\Eloquent\ModelNotFoundException ;
2026-10-05 18:10:18 -04:00
use Illuminate\Http\JsonResponse ;
2026-10-08 17:22:14 -04:00
use Illuminate\Support\Facades\Crypt ;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Facades\Event ;
use Illuminate\Support\Facades\Hash ;
2026-10-11 16:09:53 -04:00
use Illuminate\Support\Fluent ;
2026-10-05 18:10:18 -04:00
use Illuminate\Validation\ValidationException ;
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\Unauthenticated ;
use PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException ;
use PragmaRX\Google2FA\Exceptions\InvalidCharactersException ;
use PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException ;
use PragmaRX\Google2FA\Google2FA ;
use Pterodactyl\Contracts\Users\CompletesLogins ;
use Pterodactyl\Data\LoginCheckpoint ;
2022-05-28 17:03:58 -04:00
use Pterodactyl\Events\Auth\ProvidedAuthenticationToken ;
2018-04-07 12:35:15 -05:00
use Pterodactyl\Http\Requests\Auth\LoginCheckpointRequest ;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\User ;
use Pterodactyl\Support\JsonValueGuard ;
2018-04-07 12:35:15 -05:00
2026-10-05 18:10:18 -04:00
#[ Group ( 'Authentication' , 'Browser session authentication endpoints used by the panel frontend.' , authenticated : false )]
2018-04-07 12:35:15 -05:00
class LoginCheckpointController extends AbstractLoginController
{
2026-10-05 18:10:18 -04:00
private const string TOKEN_EXPIRED_MESSAGE = 'The authentication token provided has expired, please refresh the page and try again.' ;
private const array LOGIN_COMPLETE_EXAMPLE = [
'data' => [
'complete' => true ,
'intended' => '/' ,
'user' => [
'uuid' => '9a8b7c6d-5e4f-4321-9876-123456789abc' ,
'username' => 'admin' ,
'email' => 'admin@example.com' ,
'name_first' => 'Admin' ,
'name_last' => 'User' ,
'language' => 'en' ,
'root_admin' => true ,
'use_totp' => true ,
'created_at' => '2026-06-29T12:00:00+00:00' ,
'updated_at' => '2026-06-29T12:00:00+00:00' ,
'identifier' => 'usr_1a2b3c4d' ,
],
],
];
private const array CHECKPOINT_FAILED_ERROR = [
'errors' => [
[
'code' => 'DisplayException' ,
'status' => '400' ,
'detail' => 'The authentication token provided has expired, please refresh the page and try again.' ,
],
],
];
2019-06-22 13:33:11 -07:00
2018-04-07 12:35:15 -05:00
/**
* Handle a login where the user is required to provide a TOTP authentication
2018-04-07 16:17:51 -05:00
* token. Once a user has reached this stage it is assumed that they have already
* provided a valid username and password.
2018-04-07 12:35:15 -05:00
*
2026-10-05 18:10:18 -04:00
* @throws IncompatibleWithGoogleAuthenticatorException
* @throws InvalidCharactersException
* @throws SecretKeyTooShortException
* @throws Exception
* @throws ValidationException
2018-04-07 12:35:15 -05:00
*/
2026-10-05 18:10:18 -04:00
#[ Unauthenticated ]
#[ Endpoint ( 'Complete login checkpoint' , 'Completes a two-factor login checkpoint using either a TOTP code or a recovery token.' )]
#[ BodyParam ( 'confirmation_token' , 'string' , 'The confirmation token returned by the login endpoint.' , required : true , example : 'b6e22f85d63c4d9db9739f9ab0a27a48f51f7ad38ef8c12fd157c4c6f4b2e51d' )]
#[ BodyParam ( 'authentication_code' , 'string' , 'The six-digit TOTP code. Required when recovery_token is not provided.' , required : false , example : '123456' , nullable : true )]
#[ BodyParam ( 'recovery_token' , 'string' , 'A recovery token. Required when authentication_code is not provided.' , required : false , example : 'r1a2b3c4d5' , nullable : true )]
#[ ScribeResponse ( self :: LOGIN_COMPLETE_EXAMPLE , description : 'Login completed and a browser session was created.' )]
#[ ScribeResponse ( self :: CHECKPOINT_FAILED_ERROR , status : 400 , description : 'The checkpoint token, TOTP code, or recovery token is invalid.' )]
2026-10-08 17:22:14 -04:00
public function __invoke ( LoginCheckpointRequest $request , CompletesLogins $logins , Google2FA $google2FA ) : JsonResponse
2018-04-07 12:35:15 -05:00
{
2020-10-17 14:46:10 -07:00
if ( $this -> hasTooManyLoginAttempts ( $request )) {
$this -> sendLockoutResponse ( $request );
}
2020-04-25 13:01:16 -07:00
2026-10-05 18:10:18 -04:00
$checkpoint = $logins -> pendingCheckpoint ();
if ( ! $checkpoint instanceof LoginCheckpoint ) {
2021-09-21 21:30:08 -07:00
$this -> sendFailedLoginResponse ( $request , null , self :: TOKEN_EXPIRED_MESSAGE );
}
2026-10-11 16:17:55 -04:00
if ( ! $checkpoint -> matches ( $request -> string ( 'confirmation_token' ) -> toString ())) {
2021-09-21 21:30:08 -07:00
$this -> sendFailedLoginResponse ( $request );
}
2018-04-07 12:35:15 -05:00
try {
2026-10-05 18:10:18 -04:00
$user = User :: query () -> findOrFail ( $checkpoint -> userId );
2022-10-14 10:59:20 -06:00
} catch ( ModelNotFoundException ) {
2021-09-21 21:30:08 -07:00
$this -> sendFailedLoginResponse ( $request , null , self :: TOKEN_EXPIRED_MESSAGE );
2020-10-17 14:46:10 -07:00
}
2020-07-02 23:01:02 -07:00
2020-10-17 14:46:10 -07:00
// Recovery tokens go through a slightly different pathway for usage.
2026-10-05 18:10:18 -04:00
if (( $recoveryToken = $request -> validated ( 'recovery_token' )) !== null ) {
2026-10-11 16:17:55 -04:00
if ( $this -> isValidRecoveryToken ( $user , $request -> string ( 'recovery_token' ) -> toString ())) {
2022-05-28 17:03:58 -04:00
Event :: dispatch ( new ProvidedAuthenticationToken ( $user , true ));
2026-10-05 18:10:18 -04:00
return $this -> sendLoginResponse ( $logins -> establish ( $user ), $request );
2020-07-02 23:01:02 -07:00
}
} else {
2026-10-05 18:10:18 -04:00
$totpSecret = $user -> totp_secret ;
if ( $totpSecret === null ) {
$this -> sendFailedLoginResponse ( $request , $user , self :: TOKEN_EXPIRED_MESSAGE );
}
2026-10-08 17:22:14 -04:00
$decrypted = JsonValueGuard :: string ( Crypt :: decrypt ( $totpSecret ));
2026-10-05 18:10:18 -04:00
// SAFETY: TOTP counters are discrete intervals, so flooring and converting the quotient to an integer is required.
2025-12-30 15:27:11 -05:00
$oldTimestamp = $user -> totp_authenticated_at
2026-10-05 18:10:18 -04:00
? ( int ) floor ( $user -> totp_authenticated_at -> unix () / $google2FA -> getKeyRegeneration ())
2025-12-30 15:27:11 -05:00
: null ;
2026-10-05 18:10:18 -04:00
$verified = $google2FA -> verifyKeyNewer (
2025-12-30 15:27:11 -05:00
$decrypted ,
2026-10-11 16:17:55 -04:00
$request -> string ( 'authentication_code' ) -> toString (),
2025-12-30 15:27:11 -05:00
$oldTimestamp ,
2026-10-11 16:09:53 -04:00
Fluent :: make ( config () -> array ( 'pterodactyl.auth.2fa' )) -> integer ( 'window' , 1 )
2025-12-30 15:27:11 -05:00
);
if ( $verified !== false ) {
2026-10-05 18:10:18 -04:00
$user -> update ([ 'totp_authenticated_at' => now ()]);
2018-04-07 12:35:15 -05:00
2022-05-28 17:03:58 -04:00
Event :: dispatch ( new ProvidedAuthenticationToken ( $user ));
2026-10-05 18:10:18 -04:00
return $this -> sendLoginResponse ( $logins -> establish ( $user ), $request );
2020-07-02 23:01:02 -07:00
}
2018-04-07 12:35:15 -05:00
}
2026-10-05 18:10:18 -04:00
$this -> sendFailedLoginResponse ( $request , $user , ! empty ( $recoveryToken ) ? 'The recovery token provided is not valid.' : null );
2018-04-07 12:35:15 -05:00
}
2020-10-17 14:46:10 -07:00
/**
* Determines if a given recovery token is valid for the user account. If we find a matching token
* it will be deleted from the database.
*
2026-10-05 18:10:18 -04:00
* @throws Exception
2020-10-17 14:46:10 -07:00
*/
2022-10-14 10:59:20 -06:00
protected function isValidRecoveryToken ( User $user , string $value ) : bool
2020-10-17 14:46:10 -07:00
{
foreach ( $user -> recoveryTokens as $token ) {
2026-10-05 18:10:18 -04:00
if ( Hash :: check ( $value , $token -> token )) {
2020-10-17 14:46:10 -07:00
$token -> delete ();
return true ;
}
}
return false ;
}
2018-04-07 12:35:15 -05:00
}