Skip to content

Commit 2908420

Browse files
committed
Remove the automatically added route middlewares and require them to be explicitly set when using either authentication or rate limiting.
Signed-off-by: Jason Lewis <[email protected]>
1 parent 8160dfb commit 2908420

File tree

9 files changed

+8
-119
lines changed

9 files changed

+8
-119
lines changed

src/Http/Middleware/Request.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public function __construct(Application $app, Router $router, RequestValidator $
6868
public function handle($request, Closure $next)
6969
{
7070
if ($this->validator->validateRequest($request)) {
71+
unset($next);
72+
7173
$request = $this->app->make('Dingo\Api\Contract\Http\Request')->createFromIlluminate($request);
7274

7375
return $this->sendRequestThroughRouter($request);

src/Provider/LaravelServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function boot()
2222
]);
2323

2424
$this->app['router']->middleware('api.auth', 'Dingo\Api\Http\Middleware\Auth');
25-
$this->app['router']->middleware('api.limiting', 'Dingo\Api\Http\Middleware\RateLimit');
25+
$this->app['router']->middleware('api.throttle', 'Dingo\Api\Http\Middleware\RateLimit');
2626
}
2727

2828
/**

src/Provider/LumenServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function boot()
2121

2222
$this->app->routeMiddleware([
2323
'api.auth' => 'Dingo\Api\Http\Middleware\Auth',
24-
'api.limiting' => 'Dingo\Api\Http\Middleware\RateLimit',
24+
'api.throttle' => 'Dingo\Api\Http\Middleware\RateLimit',
2525
]);
2626
}
2727

src/Routing/Helpers.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ trait Helpers
1313
*/
1414
protected $methodProperties = [
1515
'scopes' => [],
16-
'protected' => [],
17-
'unprotected' => [],
1816
'providers' => [],
1917
'rateLimit' => [],
2018
'throttles' => []

src/Routing/Route.php

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,6 @@ class Route
6565
*/
6666
protected $scopes = [];
6767

68-
/**
69-
* Indicates if the route is protected.
70-
*
71-
* @var bool
72-
*/
73-
protected $protected = false;
74-
7568
/**
7669
* Array of authentication providers.
7770
*
@@ -154,7 +147,6 @@ protected function setupRoute($route, Request $request)
154147
$this->makeController();
155148

156149
$this->setupScopes();
157-
$this->setupProtection();
158150
$this->setupAuthProviders();
159151
$this->setupRateLimiting();
160152
$this->setupThrottle();
@@ -207,24 +199,6 @@ protected function setupAuthProviders()
207199
});
208200
}
209201

210-
/**
211-
* Setup the route protection by merging the controller protection.
212-
*
213-
* @return void
214-
*/
215-
protected function setupProtection()
216-
{
217-
$this->protected = array_pull($this->action, 'protected', false);
218-
219-
$this->findControllerOptions('protected', function () {
220-
$this->protected = true;
221-
});
222-
223-
$this->findControllerOptions('unprotected', function () {
224-
$this->protected = false;
225-
});
226-
}
227-
228202
/**
229203
* Setup the route scopes by merging any controller scopes.
230204
*
@@ -272,7 +246,7 @@ protected function getControllerProperties()
272246
$method = $this->getControllerPropertiesMethodName();
273247

274248
return array_merge(
275-
['scope' => [], 'protected' => [], 'unprotected' => [], 'providers' => [], 'rateLimit' => [], 'throttles' => []],
249+
['scope' => [], 'providers' => [], 'rateLimit' => [], 'throttles' => []],
276250
$this->controller->$method()
277251
);
278252
}
@@ -383,16 +357,6 @@ public function getThrottle()
383357
return $this->throttle;
384358
}
385359

386-
/**
387-
* Determine if the route is protected.
388-
*
389-
* @return bool
390-
*/
391-
public function isProtected()
392-
{
393-
return $this->protected === true;
394-
}
395-
396360
/**
397361
* Get the name of the route.
398362
*

src/Routing/Router.php

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,6 @@
2121

2222
class Router
2323
{
24-
/**
25-
* Auth middleware identifier.
26-
*
27-
* @var string
28-
*/
29-
const API_AUTH_MIDDLEWARE = 'api.auth';
30-
31-
/**
32-
* Rate limit middleware identifier.
33-
*
34-
* @var string
35-
*/
36-
const API_RATE_LIMIT_MIDDLEWARE = 'api.limiting';
37-
3824
/**
3925
* Routing adapter instance.
4026
*
@@ -399,31 +385,9 @@ public function addRoute($methods, $uri, $action)
399385

400386
$action['uri'] = $uri;
401387

402-
$action = $this->addRouteMiddlewares($action);
403-
404388
return $this->adapter->addRoute((array) $methods, $action['version'], $uri, $action);
405389
}
406390

407-
/**
408-
* Add the route middlewares to the action array.
409-
*
410-
* @param array $action
411-
*
412-
* @return array
413-
*/
414-
protected function addRouteMiddlewares(array $action)
415-
{
416-
foreach ([static::API_RATE_LIMIT_MIDDLEWARE, static::API_AUTH_MIDDLEWARE] as $middleware) {
417-
if (($key = array_search($middleware, $action['middleware'])) !== false) {
418-
unset($action['middleware'][$key]);
419-
}
420-
421-
array_unshift($action['middleware'], $middleware);
422-
}
423-
424-
return $action;
425-
}
426-
427391
/**
428392
* Merge the last groups attributes.
429393
*

tests/Routing/RouteTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public function testCreatingNewRoute()
3131
'methods' => ['GET', 'HEAD'],
3232
'action' => [
3333
'scopes' => ['foo', 'bar'],
34-
'protected' => false,
3534
'providers' => ['foo'],
3635
'limit' => 5,
3736
'expires' => 10,
@@ -42,7 +41,6 @@ public function testCreatingNewRoute()
4241
]);
4342

4443
$this->assertEquals(['foo', 'bar'], $route->scopes(), 'Route did not setup scopes correctly.');
45-
$this->assertFalse($route->isProtected(), 'Route did not setup protection correctly.');
4644
$this->assertEquals(['foo'], $route->getAuthProviders(), 'Route did not setup authentication providers correctly.');
4745
$this->assertEquals(5, $route->getRateLimit(), 'Route did not setup rate limit correctly.');
4846
$this->assertEquals(10, $route->getRateExpiration(), 'Route did not setup rate limit expiration correctly.');
@@ -60,7 +58,6 @@ public function testControllerOptionsMergeAndOverrideRouteOptions()
6058
'methods' => ['GET', 'HEAD'],
6159
'action' => [
6260
'scopes' => ['foo', 'bar'],
63-
'protected' => false,
6461
'providers' => ['foo'],
6562
'limit' => 5,
6663
'expires' => 10,
@@ -72,7 +69,6 @@ public function testControllerOptionsMergeAndOverrideRouteOptions()
7269
]);
7370

7471
$this->assertEquals(['foo', 'bar', 'baz', 'bing'], $route->scopes(), 'Route did not setup scopes correctly.');
75-
$this->assertTrue($route->isProtected(), 'Route did not setup protection correctly.');
7672
$this->assertEquals(['foo', 'red', 'black'], $route->getAuthProviders(), 'Route did not setup authentication providers correctly.');
7773
$this->assertEquals(10, $route->getRateLimit(), 'Route did not setup rate limit correctly.');
7874
$this->assertEquals(20, $route->getRateExpiration(), 'Route did not setup rate limit expiration correctly.');

tests/Routing/RouterTest.php

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,27 @@ public function getAdapterInstance()
2020

2121
public function testRouteOptionsMergeCorrectly()
2222
{
23-
$this->router->version('v1', ['protected' => true, 'scopes' => 'foo|bar'], function () {
23+
$this->router->version('v1', ['scopes' => 'foo|bar'], function () {
2424
$this->router->get('foo', ['scopes' => ['baz'], function () {
25-
$this->assertTrue(
26-
$this->router->getCurrentRoute()->isProtected(),
27-
'Route was not protected but should be.'
28-
);
2925
$this->assertEquals(
3026
['foo', 'bar', 'baz'],
3127
$this->router->getCurrentRoute()->getScopes(),
3228
'Router did not merge string based group scopes with route based array scopes.'
3329
);
3430
}]);
3531

36-
$this->router->get('bar', ['protected' => false, function () {
37-
$this->assertFalse(
38-
$this->router->getCurrentRoute()->isProtected(),
39-
'Route was protected but should not be.'
40-
);
41-
}]);
42-
43-
$this->router->get('baz', ['protected' => false, function () {
32+
$this->router->get('baz', function () {
4433
$this->assertEquals(
4534
['foo', 'bar'],
4635
$this->router->getCurrentRoute()->getScopes(),
4736
'Router did not merge string based group scopes with route.'
4837
);
49-
}]);
38+
});
5039
});
5140

5241
$request = $this->createRequest('foo', 'GET', ['accept' => 'application/vnd.api.v1+json']);
5342
$this->router->dispatch($request);
5443

55-
$request = $this->createRequest('bar', 'GET', ['accept' => 'application/vnd.api.v1+json']);
56-
$this->router->dispatch($request);
57-
5844
$request = $this->createRequest('baz', 'GET', ['accept' => 'application/vnd.api.v1+json']);
5945
$this->router->dispatch($request);
6046

@@ -69,7 +55,6 @@ public function testRouteOptionsMergeCorrectly()
6955

7056
$this->assertEquals(['baz', 'bing'], $route->scopes());
7157
$this->assertEquals(['foo', 'red', 'black'], $route->getAuthProviders());
72-
$this->assertTrue($route->isProtected());
7358
$this->assertEquals(10, $route->getRateLimit());
7459
$this->assertEquals(20, $route->getRateExpiration());
7560
$this->assertEquals('Zippy', $route->getThrottle());
@@ -239,23 +224,6 @@ public function testUnsuccessfulResponseThrowsHttpException()
239224
$this->assertEquals('Failed!', $this->router->dispatch($request)->getContent(), 'Router did not throw and handle a HttpException.');
240225
}
241226

242-
public function testRouteMiddlewaresAreUnsetAndMovedIfManuallySetOnRoutes()
243-
{
244-
$this->router->version('v1', function () {
245-
$this->router->get('foo', ['middleware' => 'foo|api.auth', function () use (&$middleware) {
246-
$route = $this->router->getCurrentRoute();
247-
248-
$this->assertEquals(['api.auth', 'api.limiting', 'foo'], $route->getAction()['middleware']);
249-
250-
return 'foo';
251-
}]);
252-
});
253-
254-
$request = $this->createRequest('foo', 'GET');
255-
256-
$this->router->dispatch($request);
257-
}
258-
259227
public function testGroupNamespacesAreConcatenated()
260228
{
261229
$this->router->version('v1', ['namespace' => 'Dingo\Api'], function () {

tests/Stubs/RoutingControllerStub.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ public function __construct()
1313
$this->scopes('baz|bing');
1414
$this->scopes('bob', ['except' => ['index']]);
1515

16-
$this->protect();
17-
$this->unprotect(['except' => ['index']]);
18-
1916
$this->authenticateWith('red|black', ['only' => 'index']);
2017

2118
$this->rateLimit(10, 20);

0 commit comments

Comments
 (0)