auth = $auth; $this->encrypter = $encrypter; $this->repository = $repository; } /** * Handle an API request by verifying that the provided API key * is in a valid format and exists in the database. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed * * @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function handle(Request $request, Closure $next) { if (is_null($request->bearerToken())) { throw new HttpException(401, null, null, ['WWW-Authenticate' => 'Bearer']); } $raw = $request->bearerToken(); $identifier = substr($raw, 0, ApiKey::IDENTIFIER_LENGTH); $token = substr($raw, ApiKey::IDENTIFIER_LENGTH); try { $model = $this->repository->findFirstWhere([ ['identifier', '=', $identifier], ['key_type', '=', ApiKey::TYPE_APPLICATION], ]); } catch (RecordNotFoundException $exception) { throw new AccessDeniedHttpException; } if (! hash_equals($this->encrypter->decrypt($model->token), $token)) { throw new AccessDeniedHttpException; } $this->auth->guard()->loginUsingId($model->user_id); $request->attributes->set('api_key', $model); $this->repository->withoutFreshModel()->update($model->id, ['last_used_at' => Chronos::now()]); return $next($request); } }