dev-master
9999999-devLaravel Middleware for letting a resource owner verify a OAuth2 access tokens with a remote authorization server
MIT
The Requires
The Development Requires
by Arie Timmerman
Wallogit.com
2017 © Pedro Peláez
Laravel Middleware for letting a resource owner verify a OAuth2 access tokens with a remote authorization server
Especially for a microservices architecture, authentication and authorization functions should be delegated. Protecting resources is best done by implementing the web services as a pure OAuth2 resource server, relying on token verification on a remote authorization server., (*2)
Laravel Passport provides a full OAuth2 server implementation, yet misses optional OAuth2 functionalties as defined in OAuth 2.0 Token Introspection (RFC7662)., (*3)
The Introspection endpoint is provided by ipunkt/laravel-oauth-introspection. This package provides the middleware required for verifying an access token against a remote Introspection endpoint., (*4)
Note: To prevent token scanning attacks, the endpoint MUST also require some form of authorization to access this endpoint. The provided middleware assumes the introspection endpoint requires an OAuth2 Bearer token retrieved using a client credentials grant. Therefore, you MUST provide a valid client id and client secret., (*5)
Install the package on your resource server, (*6)
composer require arietimmerman/laravel-oauth-introspect-middleware
and add the Service Provider in your config/app.php, (*7)
~~~.php 'providers' => [ // [..] \ArieTimmerman\Laravel\OAuth2\ServiceProvider::class // [..] ];, (*8)
and add the MiddleWare in your `App/Http/Kernel.php` ~~~.php protected $routeMiddleware = [ // [..] 'verifyaccesstoken' => \ArieTimmerman\Laravel\OAuth2\VerifyAccessToken::class, // Or 'verifyaccesstoken_has_any' => \ArieTimmerman\Laravel\OAuth2\VerifyAccessTokenHasAnyScope::class, // [..] ];
publish the configuration, (*9)
php artisan vendor:publish
In your .env file, define the following properties, (*10)
~~~.properties, (*11)
AUTHORIZATION_SERVER_URL="https://authorization.server.dom", (*12)
AUTHORIZATION_SERVER_CLIENT_ID="123", (*13)
AUTHORIZATION_SERVER_CLIENT_SECRET="abcdefg", (*14)
AUTHORIZATION_SERVER_TOKEN_URL="${AUTHORIZATION_SERVER_URL}/oauth/token", (*15)
AUTHORIZATION_SERVER_INTROSPECT_URL="${AUTHORIZATION_SERVER_URL}/oauth/introspect", (*16)
AUTHORIZATION_SERVER_AUTHORIZATION_URL="${AUTHORIZATION_SERVER_URL}/oauth/authorize" AUTHORIZATION_SERVER_REDIRECT_URL=https://my.machine.dom, (*17)
Now, use the middleware. ~~~.php Route::group(['middleware'=>'verifyaccesstoken:required-scope1,required-scope2'], function () { Route::get('/endpoint1', 'UserController@index'); Route::resource('/resource', 'OrderController'); }); // or if only one of the scopes from the list is required Route::group(['middleware'=>'verifyaccesstoken_has_any:required-scope1,required-scope2'], function () { Route::get('/endpoint1', 'UserController@index'); Route::resource('/resource', 'OrderController'); });
Laravel Middleware for letting a resource owner verify a OAuth2 access tokens with a remote authorization server
MIT