-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Добавлен ThrottleRequests middleware адаптированный модуль. Т.к. моду…
…ль отдает ответы через JsonResponse, то ThrottleRequests начинает ругаться на не верный класс Response
- Loading branch information
1 parent
7aeaa91
commit d3b679b
Showing
2 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace KodiCMS\API\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Http\JsonResponse; | ||
use KodiCMS\API\Http\Response; | ||
|
||
class ThrottleRequests extends \Illuminate\Routing\Middleware\ThrottleRequests | ||
{ | ||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @param int $maxAttempts | ||
* @param int $decayMinutes | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1) | ||
{ | ||
$key = $this->resolveRequestSignature($request); | ||
|
||
if ($this->limiter->tooManyAttempts($key, $maxAttempts, $decayMinutes)) { | ||
return $this->buildResponse($key, $maxAttempts); | ||
} | ||
|
||
$this->limiter->hit($key, $decayMinutes); | ||
|
||
$response = $next($request); | ||
|
||
return $this->addJsonHeaders( | ||
$response, $maxAttempts, | ||
$this->calculateRemainingAttempts($key, $maxAttempts) | ||
); | ||
} | ||
|
||
/** | ||
* Create a 'too many attempts' response. | ||
* | ||
* @param string $key | ||
* @param int $maxAttempts | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
protected function buildResponse($key, $maxAttempts) | ||
{ | ||
$response = (new Response())->createResponse([ | ||
'type' => Response::TYPE_ERROR, | ||
'code' => 429, | ||
'message' => 'Too Many Attempts' | ||
]); | ||
|
||
return $this->addJsonHeaders( | ||
$response, $maxAttempts, | ||
$this->calculateRemainingAttempts($key, $maxAttempts), | ||
$this->limiter->availableIn($key) | ||
); | ||
} | ||
|
||
/** | ||
* Add the limit header information to the given response. | ||
* | ||
* @param \Illuminate\Http\JsonResponse $response | ||
* @param int $maxAttempts | ||
* @param int $remainingAttempts | ||
* @param int|null $retryAfter | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
protected function addJsonHeaders(JsonResponse $response, $maxAttempts, $remainingAttempts, $retryAfter = null) | ||
{ | ||
$headers = [ | ||
'X-RateLimit-Limit' => $maxAttempts, | ||
'X-RateLimit-Remaining' => $remainingAttempts, | ||
]; | ||
|
||
if (! is_null($retryAfter)) { | ||
$headers['Retry-After'] = $retryAfter; | ||
} | ||
|
||
$response->headers->add($headers); | ||
|
||
return $response; | ||
} | ||
} |