PHPackages                             aaranda/lumen-passport-multiauth - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. aaranda/lumen-passport-multiauth

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

aaranda/lumen-passport-multiauth
================================

Lumen adaptation of Laravel Passport and multi-auth

0.1.x-dev(5y ago)024MITPHPPHP &gt;=7.3

Since Apr 2Pushed 5y ago1 watchersCompare

[ Source](https://github.com/aaranda93/lumen-passport-multiauth)[ Packagist](https://packagist.org/packages/aaranda/lumen-passport-multiauth)[ Docs](https://www.linkedin.com/in/aaranda93)[ RSS](/packages/aaranda-lumen-passport-multiauth/feed)WikiDiscussions main Synced yesterday

READMEChangelogDependencies (1)Versions (2)Used By (0)

Lumen Passport Multi-Auth
=========================

[](#lumen-passport-multi-auth)

Add passport and multi-authentication support to [Lumen](https://lumen.laravel.com/)

Compatibility
-------------

[](#compatibility)

Lumen Framework&gt;= 8.0Installing
----------

[](#installing)

Install using composer:

```
$ composer require aaranda/lumen-passport-multiauth:^0.1
```

Configuration
-------------

[](#configuration)

First of all, you will need to publish your Lumen configuration folder

```
cp -a vendor/laravel/lumen-framework/config config
```

so you can add the new providers and guards you want to use.

You need to uncomment and add some lines in your bootstrap/app.php file

```
//uncomment
    $app->withFacades();
//uncomment
    $app->withEloquent();

//add in the config file section
    $app->configure('auth');

//Uncomment your route middleware and add the next lines
    $app->routeMiddleware([
        'auth'     => App\Http\Middleware\Authenticate::class,
        'client' => App\Http\Middleware\CheckClientCredentials::class,
        'throttle' => Aaranda\LumenPassportMultiauth\Http\Middleware\ThrottleRequests::class,
        'oauth.providers' => Aaranda\LumenPassportMultiauth\Http\Middleware\AddCustomProvider::class,
        'multiauth' => Aaranda\LumenPassportMultiauth\Http\Middleware\MultiAuthenticate::class,

        //if you are going to use scopes, you can add the next route middlewares
        'scopes' => Laravel\Passport\Http\Middleware\CheckScopes::class,
        'scope' => Laravel\Passport\Http\Middleware\CheckForAnyScope::class,

    ]);

//and register the next service providers in this same order
    $app->register(App\Providers\AuthServiceProvider::class);
    $app->register(Aaranda\LumenPassportMultiauth\Providers\MultiauthServiceProvider::class);
    $app->register(Laravel\Passport\PassportServiceProvider::class);
```

Encapsulate and register the passport routes for access token with the registered middleware in `AuthServiceProvider` in app/Providers/AuthServiceProvider.php. This middleware will add the capability to `Passport` route `oauth/token` use the value of `provider` param on request:

```
namespace App\Providers;

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Aaranda\LumenPassportMultiauth\Passport;
use Illuminate\Support\Facades\Route;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Boot the authentication services for the application.
     *
     * @return void
     */
    public function boot()
    {

        Route::group(['middleware' => 'oauth.providers'], function () {

            Passport::routes(function ($router) {
                return $router->forAccessTokens();
            });
        });

        // change the default token expiration
        Passport::tokensExpireIn(Carbon::now()->addDays(15));

        // change the default refresh token expiration
        Passport::refreshTokensExpireIn(Carbon::now()->addDays(30));

    }
}
```

create in your App\\Http\\Middleware folder a CheckClientCredentials.php file

```
namespace App\Http\Middleware;

use Illuminate\Auth\AuthenticationException;
use Laravel\Passport\Exceptions\MissingScopeException;

class CheckClientCredentials extends CheckCredentials
{
    /**
     * Validate token credentials.
     *
     * @param  \Laravel\Passport\Token  $token
     * @return void
     *
     * @throws \Illuminate\Auth\AuthenticationException
     */
    protected function validateCredentials($token)
    {
        if (! $token) {
            return response('Unauthorized.', 401);
        }
    }

    /**
     * Validate token credentials.
     *
     * @param  \Laravel\Passport\Token  $token
     * @param  array  $scopes
     * @return void
     *
     * @throws \Laravel\Passport\Exceptions\MissingScopeException
     */
    protected function validateScopes($token, $scopes)
    {
        if (in_array('*', $token->scopes)) {
            return;
        }

        foreach ($scopes as $scope) {
            if ($token->cant($scope)) {
                throw new MissingScopeException($scope);
            }
        }
    }
}
```

and a CheckCredentials.php file

```
