Skip to content

A free open source IT asset/license management system

Refactorered limiting headers

snipeb3813a7121ba
diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php
index cca3aa86da..0d4eee28ec 100644
--- a/app/Exceptions/Handler.php
+++ b/app/Exceptions/Handler.php
@@ -109,17 +109,12 @@ class Handler extends ExceptionHandler
$statusCode = $e->getStatusCode();
+ // API throttle requests are handled in the RouteServiceProvider configureRateLimiting() method, so we don't need to handle them here
switch ($e->getStatusCode()) {
case '404':
return response()->json(Helper::formatStandardApiResponse('error', null, $statusCode . ' endpoint not found'), 404);
case '405':
return response()->json(Helper::formatStandardApiResponse('error', null, 'Method not allowed'), 405);
- case '429':
- return response()->json(Helper::formatStandardApiResponse('error', ['requests_per_minute' => (int) config('app.api_throttle_per_minute'), 'retry-after' => $e->getHeaders()['Retry-After']], 'Too many requests. Try again in '.($e->getHeaders()['Retry-After'] ?? 60).' seconds'), 429)
- ->header('Retry-After', $e->getHeaders()['Retry-After'] ?? 60)
- ->header('X-RateLimit-Reset', $e->getHeaders()['Retry-After'] ?? 60)
- ->header('X-Ratelimit-Remaining', $e->getHeaders()['X-RateLimit-Remaining'])
- ->header('X-Ratelimit-Limit', config('app.api_throttle_per_minute'));
default:
return response()->json(Helper::formatStandardApiResponse('error', null, $statusCode), $statusCode);
diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php
index 051f53fc02..b69e22e4f9 100644
--- a/app/Http/Kernel.php
+++ b/app/Http/Kernel.php
@@ -52,7 +52,6 @@ class Kernel extends HttpKernel
'auth:api',
\App\Http\Middleware\CheckLocale::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
- \App\Http\Middleware\SetAPIResponseHeaders::class,
],
'health' => [
diff --git a/app/Http/Middleware/SetAPIResponseHeaders.php b/app/Http/Middleware/SetAPIResponseHeaders.php
deleted file mode 100644
index 3e5fd47c85..0000000000
--- a/app/Http/Middleware/SetAPIResponseHeaders.php
+++ /dev/null
@@ -1,26 +0,0 @@
-<?php
-
-namespace App\Http\Middleware;
-
-use Closure;
-use Illuminate\Http\Request;
-
-class SetAPIResponseHeaders
-{
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @return mixed
- */
- public function handle(Request $request, Closure $next)
- {
- $response = $next($request);
- $response->headers->set('Retry-After', ($request->header('Retry-After') ?? 60));
- $response->headers->set('X-RateLimit-Reset', ($request->header('Retry-After') ?? 60));
- $response->headers->set('X-Ratelimit-Remaining', $request->header('X-RateLimit-Remaining'));
- $response->headers->set('X-Ratelimit-Limit', config('app.api_throttle_per_minute'));
- return $response;
- }
-}
\ No newline at end of file
diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php
index 276aaf1e4f..f7c6a3ee6b 100644
--- a/app/Providers/RouteServiceProvider.php
+++ b/app/Providers/RouteServiceProvider.php
@@ -83,9 +83,18 @@ class RouteServiceProvider extends ServiceProvider
protected function configureRateLimiting()
{
- // Rate limiter for API calls
+ // Rate limiter for API calls - this sends the correct API headers to show the user the remaining time they have to wait and gives them the 429 status code if they are throttled
RateLimiter::for('api', function (Request $request) {
- return Limit::perMinute(config('app.api_throttle_per_minute'))->by(optional($request->user())->id ?: $request->ip());
+ return Limit::perMinute(config('app.api_throttle_per_minute'))->by(optional($request->user())->id ?: $request->ip())
+ ->response(function ($request, $headers) {
+
+ return response()->json([
+ 'status' => 'error',
+ 'messages' => 'Too many requests. Try again in '.$headers['Retry-After'].' seconds.',
+ 'status_code' => 429,
+ 'retryAfter' => $headers['Retry-After'] ?? 60,
+ ], 429, $headers);
+ });;
});
// Rate limiter for forgotten password requests