fuzz/laravel-oauth
Laravel OAuth 
An OAuth wrapper to bridge lucadegasperi/oauth2-server-laravel and Laravel's authentication system while providing optional support for fuzz/magic-box repositories
Setup
-
Require the composer package
-
Set up your project
AuthServiceProviderto extendFuzz\Auth\Providers\AuthServiceProvider -
Follow instructions in
lucadegasperi/oauth2-server-laravelto set it up. -
Configure the
grant_typesarray inconfig/oauth2.phpto use the Fuzz grants (or extend/create your own)'grant_types' => [ 'password' => [ 'class' => \Fuzz\Auth\OAuth\Grants\PasswordGrant::class, 'callback' => '\Fuzz\Auth\OAuth\Grants\PasswordGrantVerifier@verify', 'access_token_ttl' => 7600, ], 'refresh_token' => [ 'class' => \Fuzz\Auth\OAuth\Grants\RefreshTokenGrant::class, 'access_token_ttl' => 7600, 'refresh_token_ttl' => 14600, ], ], -
Set up
config/auth.phpSet the default guard to
api'defaults' => [ 'guard' => 'api', 'passwords' => 'users', ],Set the
apiguard to use\Fuzz\Auth\Guards\OAuthGuard::classas its driver'api' => [ 'driver' => \Fuzz\Auth\Guards\OAuthGuard::class, 'provider' => 'users', ],Set Laravel to use the
oauthuser provider and set your project's User class'providers' => [ 'users' => [ 'driver' => 'oauth', 'model' => \App\User::class, 'token_key' => 'access_token', ], ], -
Create
app/Http/Middleware/OAuthMiddleware.phpand extendFuzz\Auth\Middleware\OAuthenticateMiddleware. Add it to the$routeMiddlewarearray in `app/Http/Kernel.php -
Your User class should implement the
Fuzz\Auth\Models\AgentInterfaceandIlluminate\Contracts\Auth\Authenticatableand their required methods
Usage
Protecting routes
Routes that require authentication can now be protected with the auth middleware:
$router->group(
['middleware' => 'auth'], function (Router $router) {
$router->get('locations', 'LocationsController@index');
});
Within any authenticated route, you can use all the default Laravel Auth methods such as Auth::user() to resolve the currently authenticated user. lucadegasperi/oauth2-server-laravel provides a way to protect routes based on scope, but you can also use Fuzz\Auth\Policies\RepositoryModelPolicy@requireScopes to throw League\OAuth2\Server\Exception\AccessDeniedException exceptions when a user does not have the required scopes.
Protecting resources
Laravel OAuth comes with a base Fuzz\Auth\Policies\RepositoryModelPolicy but you may create your own (implementing the Fuzz\Auth\Policies\RepositoryModelPolicyInterface might be helpful). Extending Fuzz\Auth\Policies\RepositoryModelPolicy will provide some base methods to ease writing policies for repositories.
Once a policy is set up and mapped to its model class, you may use it to check user permissions according to your policy:
if (policy(ModelClass::class)->index($user, $postRepository)) {
// Index stuff
}
Resolving the current user
All of Laravel's Auth methods will work, so resolving the current user is as simple as $user = Auth::user(). https://laravel.com/docs/5.2/authentication.
Auth will use your default guard unless specified. A typical guard set up for an OAuth specced API would be having one for users accessing via a client and another for client-only requests. Currently there is only Fuzz\Auth\Guards\OAuthGuard which is responsible for resolving the user for a request.
TODOs
- Separate
fuzz/laravel-oauthfromfuzz/magic-box - Support client requests in their own guard and be compatible with the current user
OAuthGuard
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...
Cli error handling for console/command-line PHP applications.
Version History
| Version | Released | Laravel | License |
|---|---|---|---|
| 1.0.0-p1 | 5.2.* | MIT | |
| 1.0 | 5.2.* | MIT |