Very simple package to use OAuth2 in your Laravel5 app. Package is wrapper around oauth2-php library.
Current features:
- Client credentials grant type
- Resource Owner Password Credentials grant type
- Refresh token
- Middleware for checking valid token & user
TODO:
- Authorization Code grant type
- Implicit grant type
- Test
- Integration with Travis CI
Require this package with composer (Packagist) using the following command:
$ composer require media24si/simple-oauth2
Register the SimpleOAuth2ServiceProvider to the providers array in config/app.php
'Media24si\SimpleOAuth2\SimpleOAuth2ServiceProvider',
Publish vendor files:
$ php artisan vendor:publish
Migrate your database
$ php artisan migrate
Create route for generating tokens:
$ Route::any('/token', '\Media24si\SimpleOAuth2\Http\Controllers\TokenController@token');
When you publish vendor files simpleoauth2.php config file will be copied to config folder. Currently only options to configure are:
- username field
- password field
- additional conditions
SimpleOAuth2 uses Auth::attempt to authenticate user.
Package comes with one middleware. To use it, you need to register it app\Http\Kernel.php
under $routeMiddleware
'oauth' => '\Media24si\SimpleOAuth2\Http\Middleware\SimpleOAuth2'
Then in your routes you can request valid oauth2 token:
Route::get('/protected', ['middleware' => ['oauth'], function() { return 'Protected resource'; }]);
Because SimpleOAuth2 is using laravel authentication package you can request authenticated user with oauth:
Route::get('/protected', ['middleware' => ['oauth', 'auth'], function() { return 'Protected resource with valid user'; }]);
oauth2:create-client
$ php artisan oauth2:create-client
Create new oauth2 client.
You will need to provide:
- client name
- client redirect uris
- allowed grant types
oauth2:list-clients
$ php artisan oauth2:list-clients
List all clients.
The MIT License (MIT). Please see License File for more information.