2017-11-19 19:32:17 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-13 22:06:19 +00:00
|
|
|
namespace Pterodactyl\Http\Middleware\Api\Admin;
|
2017-11-19 19:32:17 +00:00
|
|
|
|
|
|
|
use Closure;
|
|
|
|
use Illuminate\Http\Request;
|
2018-01-14 18:06:15 +00:00
|
|
|
use Pterodactyl\Models\ApiKey;
|
2017-11-19 20:05:13 +00:00
|
|
|
use Illuminate\Auth\AuthManager;
|
2018-01-13 22:06:19 +00:00
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2017-11-19 20:05:13 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
|
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
2017-11-19 19:32:17 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
|
2017-11-19 20:05:13 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
2017-11-19 19:32:17 +00:00
|
|
|
|
|
|
|
class AuthenticateKey
|
|
|
|
{
|
2017-11-19 20:05:13 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Auth\AuthManager
|
|
|
|
*/
|
|
|
|
private $auth;
|
|
|
|
|
2018-01-13 22:06:19 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
|
|
|
*/
|
|
|
|
private $encrypter;
|
|
|
|
|
2017-11-19 19:32:17 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AuthenticateKey constructor.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
|
2017-11-19 20:05:13 +00:00
|
|
|
* @param \Illuminate\Auth\AuthManager $auth
|
2018-01-13 22:06:19 +00:00
|
|
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
2017-11-19 19:32:17 +00:00
|
|
|
*/
|
2018-01-13 22:06:19 +00:00
|
|
|
public function __construct(ApiKeyRepositoryInterface $repository, AuthManager $auth, Encrypter $encrypter)
|
|
|
|
{
|
2017-11-19 20:05:13 +00:00
|
|
|
$this->auth = $auth;
|
2018-01-13 22:06:19 +00:00
|
|
|
$this->encrypter = $encrypter;
|
2017-11-19 19:32:17 +00:00
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an API request by verifying that the provided API key
|
2018-01-13 22:06:19 +00:00
|
|
|
* is in a valid format and exists in the database.
|
2017-11-19 19:32:17 +00:00
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
2017-11-19 20:05:13 +00:00
|
|
|
* @return mixed
|
2017-11-19 20:34:55 +00:00
|
|
|
*
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
|
2017-11-19 19:32:17 +00:00
|
|
|
*/
|
|
|
|
public function handle(Request $request, Closure $next)
|
|
|
|
{
|
2017-11-19 20:05:13 +00:00
|
|
|
if (is_null($request->bearerToken())) {
|
|
|
|
throw new HttpException(401, null, null, ['WWW-Authenticate' => 'Bearer']);
|
|
|
|
}
|
|
|
|
|
2018-01-13 22:06:19 +00:00
|
|
|
$raw = $request->bearerToken();
|
2018-01-14 18:06:15 +00:00
|
|
|
$identifier = substr($raw, 0, ApiKey::IDENTIFIER_LENGTH);
|
|
|
|
$token = substr($raw, ApiKey::IDENTIFIER_LENGTH);
|
2018-01-13 22:06:19 +00:00
|
|
|
|
2017-11-19 20:05:13 +00:00
|
|
|
try {
|
2018-01-13 22:06:19 +00:00
|
|
|
$model = $this->repository->findFirstWhere([['identifier', '=', $identifier]]);
|
2017-11-19 20:05:13 +00:00
|
|
|
} catch (RecordNotFoundException $exception) {
|
|
|
|
throw new AccessDeniedHttpException;
|
|
|
|
}
|
|
|
|
|
2018-01-13 22:06:19 +00:00
|
|
|
if (! hash_equals($this->encrypter->decrypt($model->token), $token)) {
|
|
|
|
throw new AccessDeniedHttpException;
|
|
|
|
}
|
|
|
|
|
2017-11-19 20:05:13 +00:00
|
|
|
$this->auth->guard()->loginUsingId($model->user_id);
|
|
|
|
$request->attributes->set('api_key', $model);
|
|
|
|
|
|
|
|
return $next($request);
|
2017-11-19 19:32:17 +00:00
|
|
|
}
|
|
|
|
}
|