项目作者: kslimani

项目描述 :
Laravel Passport custom grant types provider
高级语言: PHP
项目地址: git://github.com/kslimani/laravel-passport-grant.git
创建时间: 2019-05-14T13:45:11Z
项目社区:https://github.com/kslimani/laravel-passport-grant

开源协议:

下载


License: MIT

laravel-passport-grant

This library provide a very simple way to add custom grant types for Laravel Passport OAuth2 server.

Installation

Note: this documentation assumes Laravel Passport installation is completed.

To get started, install package via the Composer package manager :

  1. composer require kslimani/laravel-passport-grant

Publish the passportgrant.php configuration file using vendor:publish Artisan command :

  1. php artisan vendor:publish --provider="Sk\Passport\GrantTypesServiceProvider" --tag="config"

Configuration

In your config/passportgrant.php configuration file, enable any custom grant types providing user provider class.

  1. // "grants" is an array of user provider class indexed by grant type
  2. 'grants' => [
  3. // 'acme' => 'App\Passport\AcmeUserProvider',
  4. ],

User provider

User provider class roles are :

  • validate /oauth/token request custom parameters
  • provide user entity instance

User provider class must implements the Sk\Passport\UserProviderInterface :

  1. /**
  2. * Validate request parameters.
  3. *
  4. * @param \Psr\Http\Message\ServerRequestInterface $request
  5. * @return void
  6. * @throws \League\OAuth2\Server\Exception\OAuthServerException
  7. */
  8. public function validate(ServerRequestInterface $request);
  9. /**
  10. * Retrieve user instance from request.
  11. *
  12. * @param \Psr\Http\Message\ServerRequestInterface $request
  13. * @return mixed|null
  14. */
  15. public function retrieve(ServerRequestInterface $request);

If request validation fails, the validate() method must throw a League\OAuth2\Server\Exception\OAuthServerException invalid parameter exception.

On success, the retrieve() method must return a League\OAuth2\Server\Entities\UserEntityInterface or Illuminate\Contracts\Auth\Authenticatable instance. Otherwise null on failure.

User provider example

For convenience, the UserProvider class provide methods to validate and retrieve request custom parameters.

Therefore, creating a user provider becomes simple :

  1. <?php
  2. namespace App\Passport;
  3. use App\User;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use Sk\Passport\UserProvider;
  6. class AcmeUserProvider extends UserProvider
  7. {
  8. /**
  9. * {@inheritdoc}
  10. */
  11. public function validate(ServerRequestInterface $request)
  12. {
  13. // It is not necessary to validate the "grant_type", "client_id",
  14. // "client_secret" and "scope" expected parameters because it is
  15. // already validated internally.
  16. $this->validateRequest($request, [
  17. 'acme_id' => ['required', 'integer', 'min:1'],
  18. 'acme_name' => ['required', 'string', 'min:1'],
  19. ]);
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function retrieve(ServerRequestInterface $request)
  25. {
  26. $inputs = $this->only($request, [
  27. 'acme_id',
  28. 'acme_name',
  29. ]);
  30. // Here insert your logic to retrieve user entity instance
  31. // For example, let's assume that users table has "acme_id" column
  32. $user = User::where('acme_id', $inputs['acme_id'])->first();
  33. return $user;
  34. }
  35. }

Token request example

Request an access token for “acme” grant type :

  1. // Assuming $http is \GuzzleHttp\Client instance
  2. $response = $http->post('https://your-app.com/oauth/token', [
  3. 'form_params' => [
  4. 'grant_type' => 'acme',
  5. 'client_id' => 'client-id',
  6. 'client_secret' => 'client-secret',
  7. 'acme_id' => 1337,
  8. 'acme_name' => 'Joe',
  9. 'scope' => '',
  10. ],
  11. ]);

License

The MIT License (MIT). Please see License File for more information.