Custom Laravel Passport Check

Viewed 7289

Is There anyway to add or pass 1 more variable to findForPassport ?

In default laravel passport login, i only can pass 2 variable (username, password),but I want to pass 1 more variable and check in findForPassport if that user is belong other table or not .

2 Answers

From the link to passport issue #81 pointed by @Arun in OP:

There's probably a better way of doing this now but I extended the PassportServiceProvider and copied the registerAuthorizationServer function so that I could register my own grant type.

Swap out the provider in config\app.php with your new one:

'providers' => [
 //Laravel\Passport\PassportServiceProvider::class,
    App\Providers\PassportClientCredentialsServiceProvider::class,

Updated registerAuthorizationServer function that includes new grant option:

protected function registerAuthorizationServer()
  {
    parent::registerAuthorizationServer();
    $this->app->singleton(AuthorizationServer::class, function () {
      return tap($this->makeAuthorizationServer(), function ($server) {
        /**
         * @var $server AuthorizationServer
         */
        $server->enableGrantType(
          new ClientCredentialsGrant(), Passport::tokensExpireIn()
        );
        /** custom grant type */
        $server->enableGrantType(
          new PasswordOverrideGrant(
            $this->app->make(UserRepository::class),
            $this->app->make(RefreshTokenRepository::class)
          ), Passport::tokensExpireIn()
        );

        $server->enableGrantType(
          $this->makeAuthCodeGrant(), Passport::tokensExpireIn()
        );

        $server->enableGrantType(
          $this->makeRefreshTokenGrant(), Passport::tokensExpireIn()
        );

        $server->enableGrantType(
          $this->makePasswordGrant(), Passport::tokensExpireIn()
        );

        $server->enableGrantType(
          new PersonalAccessGrant(), new \DateInterval('P1Y')
        );
      });
    });
  }

PasswordOverrideGrant looks like this:

<?php

namespace App\Auth;

use App\User;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Grant\PasswordGrant;
use League\OAuth2\Server\RequestEvent;
use Psr\Http\Message\ServerRequestInterface;

class PasswordOverrideGrant extends PasswordGrant
{
  protected function validateUser(ServerRequestInterface $request, ClientEntityInterface $client)
  {
    $username = $this->getRequestParameter('username', $request);
    if (is_null($username)) {
      throw OAuthServerException::invalidRequest('username');
    }

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

    $credentials = [
      'username' => $username,
      'hash_token' => $custom_hash_token,
    ];

    $user = User::where($credentials)->first();

    if ($user instanceof User === false) {
      $this->getEmitter()->emit(new RequestEvent(RequestEvent::USER_AUTHENTICATION_FAILED, $request));

      throw OAuthServerException::invalidCredentials();
    }

    return $user;
  }
  public function getIdentifier()
  {
    return 'password_override';
  }
}
Related