Path: blob/1.0-develop/app/Http/Controllers/Auth/LoginCheckpointController.php
10284 views
<?php12namespace Pterodactyl\Http\Controllers\Auth;34use Carbon\Carbon;5use Carbon\CarbonImmutable;6use Carbon\CarbonInterface;7use Pterodactyl\Models\User;8use Illuminate\Http\JsonResponse;9use PragmaRX\Google2FA\Google2FA;10use Illuminate\Support\Facades\Event;11use Illuminate\Contracts\Encryption\Encrypter;12use Illuminate\Database\Eloquent\ModelNotFoundException;13use Pterodactyl\Events\Auth\ProvidedAuthenticationToken;14use Pterodactyl\Http\Requests\Auth\LoginCheckpointRequest;15use Illuminate\Contracts\Validation\Factory as ValidationFactory;1617class LoginCheckpointController extends AbstractLoginController18{19private const TOKEN_EXPIRED_MESSAGE = 'The authentication token provided has expired, please refresh the page and try again.';2021/**22* LoginCheckpointController constructor.23*/24public function __construct(25private Encrypter $encrypter,26private Google2FA $google2FA,27private ValidationFactory $validation,28) {29parent::__construct();30}3132/**33* Handle a login where the user is required to provide a TOTP authentication34* token. Once a user has reached this stage it is assumed that they have already35* provided a valid username and password.36*37* @throws \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException38* @throws \PragmaRX\Google2FA\Exceptions\InvalidCharactersException39* @throws \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException40* @throws \Exception41* @throws \Illuminate\Validation\ValidationException42*/43public function __invoke(LoginCheckpointRequest $request): JsonResponse44{45if ($this->hasTooManyLoginAttempts($request)) {46$this->sendLockoutResponse($request);47}4849$details = $request->session()->get('auth_confirmation_token');50if (!$this->hasValidSessionData($details)) {51$this->sendFailedLoginResponse($request, null, self::TOKEN_EXPIRED_MESSAGE);52}5354if (!hash_equals($request->input('confirmation_token') ?? '', $details['token_value'])) {55$this->sendFailedLoginResponse($request);56}5758try {59$user = User::query()->findOrFail($details['user_id']);60} catch (ModelNotFoundException) {61$this->sendFailedLoginResponse($request, null, self::TOKEN_EXPIRED_MESSAGE);62}6364// Recovery tokens go through a slightly different pathway for usage.65if (!is_null($recoveryToken = $request->input('recovery_token'))) {66if ($this->isValidRecoveryToken($user, $recoveryToken)) {67Event::dispatch(new ProvidedAuthenticationToken($user, true));6869return $this->sendLoginResponse($user, $request);70}71} else {72$decrypted = $this->encrypter->decrypt($user->totp_secret);73$oldTimestamp = $user->totp_authenticated_at74? (int) floor($user->totp_authenticated_at->unix() / $this->google2FA->getKeyRegeneration())75: null;7677$verified = $this->google2FA->verifyKeyNewer(78$decrypted,79$request->input('authentication_code') ?? '',80$oldTimestamp,81config('pterodactyl.auth.2fa.window') ?? 1,82);8384if ($verified !== false) {85$user->update(['totp_authenticated_at' => Carbon::now()]);8687Event::dispatch(new ProvidedAuthenticationToken($user));8889return $this->sendLoginResponse($user, $request);90}91}9293$this->sendFailedLoginResponse($request, $user, !empty($recoveryToken) ? 'The recovery token provided is not valid.' : null);94}9596/**97* Determines if a given recovery token is valid for the user account. If we find a matching token98* it will be deleted from the database.99*100* @throws \Exception101*/102protected function isValidRecoveryToken(User $user, string $value): bool103{104foreach ($user->recoveryTokens as $token) {105if (password_verify($value, $token->token)) {106$token->delete();107108return true;109}110}111112return false;113}114115/**116* Determines if the data provided from the session is valid or not. This117* will return false if the data is invalid, or if more time has passed than118* was configured when the session was written.119*/120protected function hasValidSessionData(?array $data): bool121{122$validator = $this->validation->make($data ?? [], [123'user_id' => 'required|integer|min:1',124'token_value' => 'required|string',125'expires_at' => 'required',126]);127128if ($validator->fails()) {129return false;130}131132if (!$data['expires_at'] instanceof CarbonInterface) {133return false;134}135136if ($data['expires_at']->isBefore(CarbonImmutable::now())) {137return false;138}139140return true;141}142}143144145