Skip to content

Commit b18c30f

Browse files
committed
Allow restricting access based on scopes
1 parent 59f5d55 commit b18c30f

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

README.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,22 @@ composer require arietimmerman/laravel-oauth-introspect-middleware
1919

2020
and add the Service Provider in your `config/app.php`
2121

22-
~~~
23-
\ArieTimmerman\Laravel\OAuth2\ServiceProvider::class
22+
~~~.php
23+
'providers' => [
24+
// [..]
25+
\ArieTimmerman\Laravel\OAuth2\ServiceProvider::class
26+
// [..]
27+
];
2428
~~~
2529

2630
and add the MiddleWare in your `App/Http/Kernel.php`
2731

28-
~~~
29-
\ArieTimmerman\Laravel\OAuth2\VerifyAccessToken::class
32+
~~~.php
33+
protected $routeMiddleware = [
34+
// [..]
35+
'verifyaccesstoken' => \ArieTimmerman\Laravel\OAuth2\VerifyAccessToken::class,
36+
// [..]
37+
];
3038
~~~
3139

3240
publish the configuration
@@ -54,3 +62,12 @@ AUTHORIZATION_SERVER_AUTHORIZATION_URL="${AUTHORIZATION_SERVER_URL}/oauth/author
5462
AUTHORIZATION_SERVER_REDIRECT_URL=https://my.machine.dom
5563
~~~
5664

65+
Now, use the middleware.
66+
67+
~~~.php
68+
Route::group(['middleware'=>'verifyaccesstoken:required-scope1,required-scope2'], function () {
69+
Route::get('/endpoint1', 'UserController@index');
70+
Route::resource('/resource', 'OrderController');
71+
});
72+
~~~
73+

routes/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
'client_id' => config('authorizationserver.authorization_server_client_id'),
1313
'redirect_uri' => config('authorizationserver.authorization_server_redirect_url'),
1414
'response_type' => 'token',
15-
'scope' => '',
15+
'scope' => 'place-orders',
1616
]);
1717

1818
return redirect(config('authorizationserver.authorization_server_authorization_url') . '?' . $query);

src/VerifyAccessToken.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ public function setClient(\GuzzleHttp\Client $client) {
3232
/**
3333
*/
3434
protected function getIntrospect($accessToken) {
35-
$guzzle = $this->getClient ();
35+
$guzzle = $this->getClient ();
3636

3737
$response = $guzzle->post ( config ( 'authorizationserver.authorization_server_introspect_url' ), [
3838
'form_params' => [
3939
'token_type_hint' => 'access_token',
4040

4141
// This is the access token for verifying the user's access token
42-
'token' => $this->getAccessToken ()
42+
'token' => $accessToken
4343
],
4444
'headers' => [
45-
'Authorization' => 'Bearer ' . $accessToken
45+
'Authorization' => 'Bearer ' . $this->getAccessToken ()
4646
]
4747
] );
48-
48+
4949
return json_decode ( ( string ) $response->getBody (), true );
5050
}
5151

@@ -88,9 +88,9 @@ protected function getAccessToken() {
8888
* @param \Closure $next
8989
* @return mixed
9090
*/
91-
public function handle($request, Closure $next, $scopes = null) {
91+
public function handle($request, Closure $next, ...$scopes) {
9292
$authorization = $request->header ( 'Authorization' );
93-
93+
9494
if (strlen ( $authorization ) == 0) {
9595
throw new InvalidInputException ( "No Authorization header present" );
9696
}
@@ -105,7 +105,6 @@ public function handle($request, Closure $next, $scopes = null) {
105105
try {
106106

107107
$result = $this->getIntrospect ( $receivedAccessToken );
108-
109108
if (! $result ['active']) {
110109

111110
throw new InvalidAccessTokenException ( "Invalid token!" );
@@ -119,15 +118,17 @@ public function handle($request, Closure $next, $scopes = null) {
119118

120119
$scopesForToken = \explode ( " ", $result ['scope'] );
121120

122-
if (count ( $scopes ) != count ( array_intersect ( $scopes, $scopesForToken ) )) {
123-
throw new InvalidAccessTokenException ( "Missing required scopes!" );
121+
if ( count($misingScopes = array_diff ( $scopes, $scopesForToken ) ) > 0 ) {
122+
throw new InvalidAccessTokenException ( "Missing the following required scopes: " . implode(" ,",$misingScopes) );
124123
} else {
125124
}
126125
}
127126
} catch ( RequestException $e ) {
128127
if ($e->hasResponse ()) {
129128
$result = json_decode ( ( string ) $e->getResponse ()->getBody (), true );
130129

130+
var_dump($result);exit;
131+
131132
if (isset ( $result ['error'] )) {
132133
throw new InvalidAccessTokenException ( $result ['error'] ['title'] ?? "Invalid token!");
133134
} else {

0 commit comments

Comments
 (0)