Throttle Mode Problem

I added the following method in trait ThrottlesLoginsin Laravel 5.5

protected function TotalRegisterAttemptsLeft($request) {
    $this->incrementAttempts($request);
    return $this->limiter()->retriesLeft($this->resolveRequestSignature($request), 3);
}

Route

Route::post('apiregister', 
    array(
        'uses'          =>  'API\Register\RegisterAPIController@Registration', 
        'as'            =>  'apiRegister',
        'middleware'    =>  'throttle:3,1'
    )
);

This method worked fine in 5.4. Let me explain the problem.

I have a POST register that has a maximum of 3 attempts. After using all three attempts, the user will have to wait 60 seconds.

But the problem is that, say, I spend 12 seconds during three attempts. After 3 attempts, let's say try after 48 seconds. Instead, say: try again after 60 seconds.

Please let me know if you need more information.

+4
source share
1 answer

, ThrottleRequests-Middleware. :

public function test_lock_opens_immediately_after_decay()
{
    Carbon::setTestNow(null);

    Route::get('/', function () {
        return 'yes';
    })->middleware(ThrottleRequests::class.':2,1');

    $response = $this->withoutExceptionHandling()->get('/');
    $this->assertEquals('yes', $response->getContent());
    $this->assertEquals(2, $response->headers->get('X-RateLimit-Limit'));
    $this->assertEquals(1, $response->headers->get('X-RateLimit-Remaining'));

    Carbon::setTestNow(
        Carbon::now()->addSeconds(10)
    );

    $response = $this->withoutExceptionHandling()->get('/');
    $this->assertEquals('yes', $response->getContent());
    $this->assertEquals(2, $response->headers->get('X-RateLimit-Limit'));
    $this->assertEquals(0, $response->headers->get('X-RateLimit-Remaining'));

    Carbon::setTestNow(
        Carbon::now()->addSeconds(58)
    );

    try {
        $this->withoutExceptionHandling()->get('/');
    } catch (Throwable $e) {
        $this->assertEquals(429, $e->getStatusCode());
        $this->assertEquals(2, $e->getHeaders()['X-RateLimit-Limit']);
        $this->assertEquals(0, $e->getHeaders()['X-RateLimit-Remaining']);
        $this->assertEquals(2, $e->getHeaders()['Retry-After']);
        $this->assertEquals(Carbon::now()->addSeconds(2)->getTimestamp(), $e->getHeaders()['X-RateLimit-Reset']);
    }
}

Carbon::setTestNow(
    Carbon::now()->addSeconds(10)
);

. phpunit:

./vendor/bin/phpunit tests/Integration/Http/ThrottleRequestsTest.php
PHPUnit 6.5.5 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.2.1
Configuration: /Volumes/Workspace/Projects/laravel/phpunit.xml.dist

F                                                                   1 / 1 (100%)

Time: 172 ms, Memory: 10.00MB

There was 1 failure:

1) Illuminate\Tests\Integration\Http\ThrottleRequestsTest::test_lock_opens_immediately_after_decay
Failed asserting that -8 matches expected 2.

/Volumes/Workspace/Projects/laravel/tests/Integration/Http/ThrottleRequestsTest.php:54

PR ~, , , - : ~

https://github.com/laravel/framework/pull/22725/files

edit: PR, . API, , . .

, .

+4

Source: https://habr.com/ru/post/1691856/


All Articles