Add new API middleware
This commit is contained in:
parent
47e14ccaae
commit
45a153427e
5 changed files with 144 additions and 19 deletions
|
@ -11,15 +11,17 @@ use Pterodactyl\Http\Middleware\EncryptCookies;
|
||||||
use Pterodactyl\Http\Middleware\VerifyCsrfToken;
|
use Pterodactyl\Http\Middleware\VerifyCsrfToken;
|
||||||
use Pterodactyl\Http\Middleware\VerifyReCaptcha;
|
use Pterodactyl\Http\Middleware\VerifyReCaptcha;
|
||||||
use Pterodactyl\Http\Middleware\AdminAuthenticate;
|
use Pterodactyl\Http\Middleware\AdminAuthenticate;
|
||||||
use Pterodactyl\Http\Middleware\HMACAuthorization;
|
|
||||||
use Illuminate\Routing\Middleware\ThrottleRequests;
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
||||||
use Pterodactyl\Http\Middleware\LanguageMiddleware;
|
use Pterodactyl\Http\Middleware\LanguageMiddleware;
|
||||||
use Illuminate\Foundation\Http\Kernel as HttpKernel;
|
use Illuminate\Foundation\Http\Kernel as HttpKernel;
|
||||||
|
use Pterodactyl\Http\Middleware\API\AuthenticateKey;
|
||||||
use Illuminate\Routing\Middleware\SubstituteBindings;
|
use Illuminate\Routing\Middleware\SubstituteBindings;
|
||||||
use Pterodactyl\Http\Middleware\AccessingValidServer;
|
use Pterodactyl\Http\Middleware\AccessingValidServer;
|
||||||
|
use Pterodactyl\Http\Middleware\API\SetSessionDriver;
|
||||||
use Illuminate\View\Middleware\ShareErrorsFromSession;
|
use Illuminate\View\Middleware\ShareErrorsFromSession;
|
||||||
use Pterodactyl\Http\Middleware\RedirectIfAuthenticated;
|
use Pterodactyl\Http\Middleware\RedirectIfAuthenticated;
|
||||||
use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth;
|
use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth;
|
||||||
|
use Pterodactyl\Http\Middleware\API\AuthenticateIPAccess;
|
||||||
use Pterodactyl\Http\Middleware\Daemon\DaemonAuthenticate;
|
use Pterodactyl\Http\Middleware\Daemon\DaemonAuthenticate;
|
||||||
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
|
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
|
||||||
use Pterodactyl\Http\Middleware\Server\AuthenticateAsSubuser;
|
use Pterodactyl\Http\Middleware\Server\AuthenticateAsSubuser;
|
||||||
|
@ -42,10 +44,6 @@ class Kernel extends HttpKernel
|
||||||
EncryptCookies::class,
|
EncryptCookies::class,
|
||||||
AddQueuedCookiesToResponse::class,
|
AddQueuedCookiesToResponse::class,
|
||||||
TrimStrings::class,
|
TrimStrings::class,
|
||||||
|
|
||||||
/*
|
|
||||||
* Custom middleware applied to all routes.
|
|
||||||
*/
|
|
||||||
TrustProxies::class,
|
TrustProxies::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -66,9 +64,11 @@ class Kernel extends HttpKernel
|
||||||
RequireTwoFactorAuthentication::class,
|
RequireTwoFactorAuthentication::class,
|
||||||
],
|
],
|
||||||
'api' => [
|
'api' => [
|
||||||
HMACAuthorization::class,
|
|
||||||
'throttle:60,1',
|
'throttle:60,1',
|
||||||
'bindings',
|
SubstituteBindings::class,
|
||||||
|
SetSessionDriver::class,
|
||||||
|
AuthenticateKey::class,
|
||||||
|
AuthenticateIPAccess::class,
|
||||||
],
|
],
|
||||||
'daemon' => [
|
'daemon' => [
|
||||||
SubstituteBindings::class,
|
SubstituteBindings::class,
|
||||||
|
|
39
app/Http/Middleware/API/AuthenticateIPAccess.php
Normal file
39
app/Http/Middleware/API/AuthenticateIPAccess.php
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pterodactyl\Http\Middleware\API;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use IPTools\IP;
|
||||||
|
use IPTools\Range;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||||
|
|
||||||
|
class AuthenticateIPAccess
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if a request IP has permission to access the API.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @param \Closure $next
|
||||||
|
* @return mixed
|
||||||
|
*
|
||||||
|
* @throws \Exception
|
||||||
|
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
|
||||||
|
*/
|
||||||
|
public function handle(Request $request, Closure $next)
|
||||||
|
{
|
||||||
|
$model = $request->attributes->get('api_key');
|
||||||
|
|
||||||
|
if (is_null($model->allowed_ips) || empty($model->allowed_ips)) {
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($model->allowed_ips as $ip) {
|
||||||
|
if (Range::parse($ip)->contains(new IP($request->ip()))) {
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new AccessDeniedHttpException('This IP address does not have permission to access the API using these credentials.');
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,10 +4,25 @@ namespace Pterodactyl\Http\Middleware\API;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Auth\AuthManager;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||||
|
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||||
|
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
||||||
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
|
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||||
|
|
||||||
class AuthenticateKey
|
class AuthenticateKey
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var \Illuminate\Auth\AuthManager
|
||||||
|
*/
|
||||||
|
private $auth;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Illuminate\Contracts\Config\Repository
|
||||||
|
*/
|
||||||
|
private $config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
|
* @var \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface
|
||||||
*/
|
*/
|
||||||
|
@ -17,9 +32,16 @@ class AuthenticateKey
|
||||||
* AuthenticateKey constructor.
|
* AuthenticateKey constructor.
|
||||||
*
|
*
|
||||||
* @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
|
* @param \Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface $repository
|
||||||
|
* @param \Illuminate\Auth\AuthManager $auth
|
||||||
|
* @param \Illuminate\Contracts\Config\Repository $config
|
||||||
*/
|
*/
|
||||||
public function __construct(ApiKeyRepositoryInterface $repository)
|
public function __construct(
|
||||||
{
|
ApiKeyRepositoryInterface $repository,
|
||||||
|
AuthManager $auth,
|
||||||
|
ConfigRepository $config
|
||||||
|
) {
|
||||||
|
$this->auth = $auth;
|
||||||
|
$this->config = $config;
|
||||||
$this->repository = $repository;
|
$this->repository = $repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,11 +52,23 @@ class AuthenticateKey
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Http\Request $request
|
* @param \Illuminate\Http\Request $request
|
||||||
* @param \Closure $next
|
* @param \Closure $next
|
||||||
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function handle(Request $request, Closure $next)
|
public function handle(Request $request, Closure $next)
|
||||||
{
|
{
|
||||||
$this->repository->findFirstWhere([
|
if (is_null($request->bearerToken())) {
|
||||||
'',
|
throw new HttpException(401, null, null, ['WWW-Authenticate' => 'Bearer']);
|
||||||
]);
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$model = $this->repository->findFirstWhere([['token', '=', $request->bearerToken()]]);
|
||||||
|
} catch (RecordNotFoundException $exception) {
|
||||||
|
throw new AccessDeniedHttpException;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->auth->guard()->loginUsingId($model->user_id);
|
||||||
|
$request->attributes->set('api_key', $model);
|
||||||
|
|
||||||
|
return $next($request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
52
app/Http/Middleware/API/SetSessionDriver.php
Normal file
52
app/Http/Middleware/API/SetSessionDriver.php
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pterodactyl\Http\Middleware\API;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Barryvdh\Debugbar\LaravelDebugbar;
|
||||||
|
use Illuminate\Contracts\Foundation\Application;
|
||||||
|
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
||||||
|
|
||||||
|
class SetSessionDriver
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var \Illuminate\Contracts\Foundation\Application
|
||||||
|
*/
|
||||||
|
private $app;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Illuminate\Contracts\Config\Repository
|
||||||
|
*/
|
||||||
|
private $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SetSessionDriver constructor.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Contracts\Foundation\Application $app
|
||||||
|
* @param \Illuminate\Contracts\Config\Repository $config
|
||||||
|
*/
|
||||||
|
public function __construct(Application $app, ConfigRepository $config)
|
||||||
|
{
|
||||||
|
$this->app = $app;
|
||||||
|
$this->config = $config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the session for API calls to only last for the one request.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @param \Closure $next
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle(Request $request, Closure $next)
|
||||||
|
{
|
||||||
|
if ($this->app->environment() !== 'production') {
|
||||||
|
$this->app->make(LaravelDebugbar::class)->disable();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->config->set('session.driver', 'array');
|
||||||
|
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
}
|
|
@ -29,13 +29,9 @@ class RouteServiceProvider extends ServiceProvider
|
||||||
*/
|
*/
|
||||||
public function map()
|
public function map()
|
||||||
{
|
{
|
||||||
Route::middleware(['api'])->prefix('/api/user')
|
// Route::middleware(['api'])->prefix('/api/user')
|
||||||
->namespace($this->namespace . '\API\User')
|
// ->namespace($this->namespace . '\API\User')
|
||||||
->group(base_path('routes/api.php'));
|
// ->group(base_path('routes/api.php'));
|
||||||
|
|
||||||
Route::middleware(['api'])->prefix('/api/admin')
|
|
||||||
->namespace($this->namespace . '\API\Admin')
|
|
||||||
->group(base_path('routes/api-admin.php'));
|
|
||||||
|
|
||||||
Route::middleware(['web', 'auth', 'csrf'])
|
Route::middleware(['web', 'auth', 'csrf'])
|
||||||
->namespace($this->namespace . '\Base')
|
->namespace($this->namespace . '\Base')
|
||||||
|
@ -53,6 +49,10 @@ class RouteServiceProvider extends ServiceProvider
|
||||||
->namespace($this->namespace . '\Server')
|
->namespace($this->namespace . '\Server')
|
||||||
->group(base_path('routes/server.php'));
|
->group(base_path('routes/server.php'));
|
||||||
|
|
||||||
|
Route::middleware(['api'])->prefix('/api/admin')
|
||||||
|
->namespace($this->namespace . '\API\Admin')
|
||||||
|
->group(base_path('routes/api-admin.php'));
|
||||||
|
|
||||||
Route::middleware(['daemon'])->prefix('/api/remote')
|
Route::middleware(['daemon'])->prefix('/api/remote')
|
||||||
->namespace($this->namespace . '\API\Remote')
|
->namespace($this->namespace . '\API\Remote')
|
||||||
->group(base_path('routes/api-remote.php'));
|
->group(base_path('routes/api-remote.php'));
|
||||||
|
|
Loading…
Reference in a new issue