Laravel Passport As an OAuth Server



If you want users to log in to your WordPress site using their Laravel Application credentials, you can simply do it using our WP OAuth Client plugin. Once you configure the Laravel Passport with WordPress plugin, you can allow users to SSO to your WordPress site using Laravel Application. Similarly, To know more about other features we provide in the WP OAuth Client plugin, you can click here.

Follow the Step-by-Step Guide to configure Laravel Passport as an OAuth Server:

  • Create a laravel project on your local machine using command and set it up:
  • composer create-project --prefer-dist laravel/laravel blog

  • Install laravel passport
  • composer require laravel/passport

  • Go to config/app.php and add below provider
  • Laravel\Passport\PassportServiceProvider::class,

  • Run php artisan migrate command
  • Run php artisan passport:install command
  • Go to app/User.php model class, add HasApiTokens trait to the code:
  • namespace App;
    use Laravel\Passport\HasApiTokens;
    use Illuminate\Contracts\Auth\MustVerifyEmail;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Illuminate\Notifications\Notifiable;
    class User extends Authenticatable
    {
    use HasApiTokens, Notifiable;
    /**
    * The attributes that are mass assignable.
    *
    * @var array
    */

    protected $fillable = [ 'name', 'email', 'password', ];
    /**
    * The attributes that should be hidden for arrays.
    *
    * @var array
    */

    protected $hidden = [ 'password', 'remember_token', ];
    /**
    * The attributes that should be cast to native types.
    *
    * @var array
    */

    protected $casts = [ 'email_verified_at' => 'datetime', ]; }
  • Go to app/Providers/AuthServiceProvider.php, add Passport routes to the service code is given below:
  • namespace App\Providers;
    use Laravel\Passport\Passport;
    use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
    use Illuminate\Support\Facades\Gate;
    class AuthServiceProvider extends ServiceProvider
    {
    /**
    * The policy mappings for the application.
    *
    * @var array
    */

    protected $policies = [ // 'App\Model' => 'App\Policies\ModelPolicy', ];
    /**
    * Register any authentication / authorization services.
    *
    * @return void
    */

    public function boot()
    {
    $this->registerPolicies();
    Passport::routes();
    //
    }
    }
  • Go to config/auth.php change the api driver token to passport as we are going to use Passport library.
  • 'guards' => [
    'web' => [
    'driver' => 'session',
    'provider' => 'users',
    ],
    'api' => [
    'driver' => 'passport',
    'provider' => 'users',
    'hash' => false,
    ],
    ],
  • Now Set up a demo resource you can make an API route in routes/api.php file.
  • To get Client ID and Client Secret run following commands: php artisan passport:client It will ask you the following questions:
  • Which user ID should the client be assigned to?:
    > 1
    What should we name the client?:
    > Demo OAuth2 Client Account
    Where should we redirect the request after authorization?
    [http://localhost/auth/callback]:
    > http://localhost/oauth2_client/callback.php
    New client created successfully.
    Client ID: 1
    Client secret: zMm0tQ9Cp7LbjK3QTgPy1pssoT1X0u7sg0YWUW01
  • You can download OAuth Client plugin using the following link.
  • OAuth Single Sign On – SSO (OAuth client)
  • Now you can connect OAuth client with Laravel Passport (OAuth Provider) and the following endpoints are:
  • Authorization endpoint: http://your-laravel-site-url/oauth/authorize
    Token endpoint: http://your-laravel-site-url/oauth/token
    User info endpoint: http://your-laravel-site-url/api/user/get

Business Trial For Free

If you don't find what you are looking for, please contact us at info@xecurify.com or call us at +1 978 658 9387.