Drop dependency on alt-three throttle (#3469)

I've just pulled in the subset of the features we were actually using.
This commit is contained in:
Graham Campbell
2019-02-15 10:26:39 +00:00
committed by GitHub
parent 8b02bfc04e
commit 37389ef55f
15 changed files with 154 additions and 80 deletions

View File

@@ -11,7 +11,6 @@
namespace CachetHQ\Cachet\Http;
use AltThree\Throttle\ThrottlingMiddleware;
use Barryvdh\Cors\HandleCors;
use CachetHQ\Cachet\Http\Middleware\Admin;
use CachetHQ\Cachet\Http\Middleware\ApiAuthentication;
@@ -21,6 +20,7 @@ use CachetHQ\Cachet\Http\Middleware\ReadyForUse;
use CachetHQ\Cachet\Http\Middleware\RedirectIfAuthenticated;
use CachetHQ\Cachet\Http\Middleware\SetupAlreadyCompleted;
use CachetHQ\Cachet\Http\Middleware\SubscribersConfigured;
use CachetHQ\Cachet\Http\Middleware\Throttler;
use CachetHQ\Cachet\Http\Middleware\TrustProxies;
use Illuminate\Auth\Middleware\Authorize;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
@@ -54,6 +54,6 @@ class Kernel extends HttpKernel
'ready' => ReadyForUse::class,
'setup' => SetupAlreadyCompleted::class,
'subscribers' => SubscribersConfigured::class,
'throttle' => ThrottlingMiddleware::class,
'throttle' => Throttler::class,
];
}

View File

@@ -18,7 +18,7 @@ use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
/**
* This is the acceptable middleware class.
*
* @author Graham Campbell <james@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
* @author James Brooks <james@alt-three.com>
*/
class Acceptable

View File

@@ -20,7 +20,7 @@ use Symfony\Component\HttpKernel\Exception\HttpException;
* This is the admin middleware class.
*
* @author Joseph Cohen <joe@alt-three.com>
* @author Graham Campbell <james@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
* @author James Brooks <james@alt-three.com>
*/
class Admin

View File

@@ -22,7 +22,7 @@ use Symfony\Component\HttpKernel\Exception\HttpException;
* This is the api authentication middleware class.
*
* @author Joseph Cohen <joe@alt-three.com>
* @author Graham Campbell <james@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
* @author James Brooks <james@alt-three.com>
*/
class ApiAuthentication

View File

@@ -20,7 +20,7 @@ use Symfony\Component\HttpKernel\Exception\HttpException;
* This is the authenticate middleware class.
*
* @author Joseph Cohen <joe@alt-three.com>
* @author Graham Campbell <james@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
* @author James Brooks <james@alt-three.com>
*/
class Authenticate

View File

@@ -22,7 +22,7 @@ use Jenssegers\Date\Date;
*
* @author James Brooks <james@alt-three.com>
* @author Joseph Cohen <joe@alt-three.com>
* @author Graham Campbell <james@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
*/
class Localize
{

View File

@@ -18,7 +18,7 @@ use Illuminate\Http\Request;
/**
* This is the ready for use middleware class.
*
* @author Graham Campbell <james@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
* @author James Brooks <james@alt-three.com>
* @author Joseph Cohen <joe@alt-three.com>
*/

View File

@@ -18,7 +18,7 @@ use Illuminate\Http\Request;
/**
* This is the redirect if authenticated middleware class.
*
* @author Graham Campbell <james@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
* @author Joseph Cohen <joe@alt-three.com>
* @author James Brooks <james@alt-three.com>
*/

View File

@@ -19,7 +19,7 @@ use Illuminate\Http\Request;
/**
* This is the setup already completed middelware class.
*
* @author Graham Campbell <james@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
* @author James Brooks <james@alt-three.com>
* @author Joseph Cohen <joe@alt-three.com>
*/

View File

@@ -19,7 +19,7 @@ use Illuminate\Http\Request;
* This is the subscribers configured middleware class.
*
* @author James Brooks <james@alt-three.com>
* @author Graham Campbell <james@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
*/
class SubscribersConfigured
{

View File

@@ -0,0 +1,125 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Http\Middleware;
use Closure;
use Illuminate\Cache\RateLimiter;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
/**
* This is the throttler middleware class.
*
* @author Graham Campbell <graham@alt-three.com>
*/
class Throttler
{
/**
* The rate limiter instance.
*
* @var \Illuminate\Cache\RateLimiter
*/
protected $limiter;
/**
* Create a new throttler middleware instance.
*
* @param \Illuminate\Cache\RateLimiter $limiter
*
* @return void
*/
public function __construct(RateLimiter $limiter)
{
$this->limiter = $limiter;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param int|string $limit
* @param int|string $decay
*
* @throws \Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException
*
* @return mixed
*/
public function handle(Request $request, Closure $next, $limit = 60, $decay = 1)
{
return $this->safeHandle($request, $next, (int) $limit, (int) $decay);
}
/**
* Handle an incoming request, with correct types.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param int $limit
* @param int $decay
*
* @throws \Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException
*
* @return mixed
*/
protected function safeHandle(Request $request, Closure $next, int $limit, int $decay)
{
$key = $request->fingerprint();
if ($this->limiter->tooManyAttempts($key, $limit, $decay)) {
throw $this->buildException($key, $limit);
}
$this->limiter->hit($key, $decay);
$response = $next($request);
$response->headers->add($this->getHeaders($key, $limit));
return $response;
}
/**
* Create a too many requests http exception.
*
* @param string $key
* @param int $limit
*
* @return \Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException
*/
protected function buildException(string $key, int $limit)
{
$after = $this->limiter->availableIn($key);
$exception = new TooManyRequestsHttpException($after, 'Rate limit exceeded.');
$exception->setHeaders($this->getHeaders($key, $limit, $after, $exception->getHeaders()));
return $exception;
}
/**
* Get the limit header information.
*
* @param string $key
* @param int $limit
* @param int|null $after
* @param array $merge
*
* @return array
*/
protected function getHeaders(string $key, int $limit, int $after = null, array $merge = [])
{
$remaining = $after === null ? $this->limiter->retriesLeft($key, $limit) : 0;
$headers = ['X-RateLimit-Limit' => $limit, 'X-RateLimit-Remaining' => $remaining];
return array_merge($headers, $merge);
}
}

View File

@@ -19,7 +19,7 @@ use Illuminate\Http\Request;
* This is the timezone middleware class.
*
* @author James Brooks <james@alt-three.com>
* @author Graham Campbell <james@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
*/
class Timezone
{

View File

@@ -13,6 +13,11 @@ namespace CachetHQ\Cachet\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
/**
* This is the verify csrf token middleware class.
*
* @author James Brooks <james@alt-three.com>
*/
class VerifyCsrfToken extends Middleware
{
/**