Laravel Passport Multiple Authentication using Guards

前端 未结 4 518
说谎
说谎 2020-12-09 06:54

Can we use laravel passport with different guards to authenticate APIs for two different types of users. For example we have driver app for driver user and vendor app for ve

4条回答
  •  粉色の甜心
    2020-12-09 07:15

    Here is an example of auth.php and api.php to start with

    config/auth.php

     [
            'guard' => 'web',
            'passwords' => 'users',
        ],
    
        /*
        |--------------------------------------------------------------------------
        | Authentication Guards
        |--------------------------------------------------------------------------
        */
    
        'guards' => [
    
            'web' => [
                'driver' => 'session',
                'provider' => 'users',
            ],
    
            'driver-api' => [
                'driver' => 'passport',
                'provider' => 'drivers',
            ],
    
            'vendor-api' => [
                'driver' => 'passport',
                'provider' => 'vendors',
            ],
        ],
    
        /*
        |--------------------------------------------------------------------------
        | User Providers
        |--------------------------------------------------------------------------
        */
    
        'providers' => [
    
            'users' => [
                'driver' => 'eloquent',
                'model' => App\User::class,
            ],
    
            'drivers' => [
                'driver' => 'eloquent',
                'model' => App\Driver::class,
            ],
    
            'vendors' => [
                'driver' => 'eloquent',
                'model' => App\Vendor::class,
            ],
        ],
    
        /*
        |--------------------------------------------------------------------------
        | Resetting Passwords
        |--------------------------------------------------------------------------
        */
    
        'passwords' => [
    
            'users' => [
                'provider' => 'users',
                'table' => 'password_resets',
                'expire' => 60,
            ],
    
            'drivers' => [
                'provider' => 'drivers',
                'table' => 'password_resets',
                'expire' => 60,
            ],
    
            'vendors' => [
                'provider' => 'vendors',
                'table' => 'password_resets',
                'expire' => 60,
            ],
        ],
    
    ];
    

    routes/api.php

     'Driver', 'prefix' => 'driver/v1', 'middleware' => 'auth:driver-api'], function() {
    
        // define your routes here for the "drivers"
    });
    
    Route::group(['namespace' => 'Vendor', 'prefix' => 'vendor/v1', 'middleware' => 'auth:vendor-api'], function() {
    
        // define your routes here for the "vendors"
    });
    

    You have to modify this files:

    File: vendor\laravel\passport\src\Bridge\UserRepository.php

    Copy/Paste getUserEntityByUserCredentials to make a duplicate of it and name it getEntityByUserCredentials

    Then, in the new duplicated function, find the below:

    $provider = config('auth.guards.api.provider');
    

    And Replace it with:

    $provider = config('auth.guards.'.$provider.'.provider');
    

    File: vendor\league\oauth2-server\src\Grant\PasswordGrant.php

    in : validateUser method add after $username and $password :

    $customProvider = $this->getRequestParameter('customProvider', $request);
    
    if (is_null($customProvider)) {
       throw OAuthServerException::invalidRequest('customProvider');
    }
    

    And this instead of the original line

    $user = $this->userRepository->getEntityByUserCredentials(
        $username,
        $password,
        $this->getIdentifier(),
        $client,
        $customProvider
    );
    

    After doing this you'll be able to pass an extra key/value pair to your access token request, like for example:

    grant_type => password,
    client_id => someclientid
    client_secret => somesecret,
    username => someuser,
    password => somepass,
    client_scope => *,
    provider => driver-api // Or vendor-api
    

    I hope this will be helpful for you

提交回复
热议问题