Skip to content

Commit b6dcdee

Browse files
author
Luca Degasperi
committed
Implementing the class filters for higher testability
1 parent a06df29 commit b6dcdee

File tree

4 files changed

+58
-39
lines changed

4 files changed

+58
-39
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"league/oauth2-server": "2.1.x"
2020
},
2121
"require-dev": {
22+
"phpunit/phpunit": "3.7.22",
2223
"mockery/mockery": ">=0.7.2",
2324
"league/phpunit-coverage-listener": "~1.0",
2425
"whatthejeff/nyancat-phpunit-resultprinter": "~1.1"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php namespace LucaDegasperi\OAuth2Server\Filters;
2+
3+
use ResourceServer;
4+
use Response;
5+
6+
class OAuthFilter {
7+
8+
public function filter($route, $request, $scope = null)
9+
{
10+
try {
11+
ResourceServer::isValid(Config::get('oauth2-server-laravel::oauth2.http_headers_only'));
12+
}
13+
catch (League\OAuth2\Server\Exception\InvalidAccessTokenException $e) {
14+
return Response::json(array(
15+
'status' => 403,
16+
'error' => 'forbidden',
17+
'error_message' => $e->getMessage(),
18+
), 403);
19+
}
20+
21+
if ( ! is_null($scope)) {
22+
$scopes = explode(',', $scope);
23+
24+
foreach ($scopes as $s) {
25+
if ( ! ResourceServer::hasScope($s)) {
26+
return Response::json(array(
27+
'status' => 403,
28+
'error' => 'forbidden',
29+
'error_message' => 'Only access token with scope '.$s.' can use this endpoint',
30+
), 403);
31+
}
32+
}
33+
}
34+
}
35+
36+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php namespace LucaDegasperi\OAuth2Server\Filters;
2+
3+
use Response;
4+
5+
class OAuthOwnerFilter {
6+
7+
public function filter($route, $request, $scope = null)
8+
{
9+
if ( ! is_null($scope) and ResourceServer::getOwnerType() !== $scope){
10+
return Response::json(array(
11+
'status' => 403,
12+
'error' => 'forbidden',
13+
'error_message' => 'Only access tokens representing '.$scope.' can use this endpoint',
14+
), 403);
15+
}
16+
}
17+
18+
}

src/filters.php

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Route::filter('check-authorization-params', function($route, $request, $scope = null)
44
{
55
try {
6-
6+
77
$params = AuthorizationServer::getGrantType('authorization_code')->checkAuthoriseParams();
88

99
Session::put('client_id', $params['client_id']);
@@ -34,43 +34,7 @@
3434
});
3535

3636
// make sure an endpoint is accessible only by authrized members eventually with specific scopes
37-
Route::filter('oauth', function($route, $request, $scope = null)
38-
{
39-
try {
40-
ResourceServer::isValid(Config::get('oauth2-server-laravel::oauth2.http_headers_only'));
41-
}
42-
catch (League\OAuth2\Server\Exception\InvalidAccessTokenException $e) {
43-
return Response::json(array(
44-
'status' => 403,
45-
'error' => 'forbidden',
46-
'error_message' => $e->getMessage(),
47-
), 403);
48-
}
49-
50-
if ( ! is_null($scope)) {
51-
$scopes = explode(',', $scope);
52-
53-
foreach ($scopes as $s) {
54-
if ( ! ResourceServer::hasScope($s)) {
55-
return Response::json(array(
56-
'status' => 403,
57-
'error' => 'forbidden',
58-
'error_message' => 'Only access token with scope '.$s.' can use this endpoint',
59-
), 403);
60-
}
61-
}
62-
}
63-
64-
});
37+
Route::filter('oauth', 'LucaDegasperi\OAuth2Server\Filters\OAuthFilter');
6538

6639
// make sure an endpoint is accessible only by a specific owner
67-
Route::filter('oauth-owner', function($route, $request, $scope = null)
68-
{
69-
if ( ! is_null($scope) and ResourceServer::getOwnerType() !== $scope){
70-
return Response::json(array(
71-
'status' => 403,
72-
'error' => 'forbidden',
73-
'error_message' => 'Only access tokens representing '.$scope.' can use this endpoint',
74-
), 403);
75-
}
76-
});
40+
Route::filter('oauth-owner', 'LucaDegasperi\OAuth2Server\Filters\OAuthOwnerFilter');

0 commit comments

Comments
 (0)