定制Laravel护照检查

时间:2017-07-22 03:41:06

标签: laravel laravel-passport

无论如何要向findForPassport添加或传递另外一个变量吗?

在默认的laravel passport登录中,我只能传递2个变量(用户名,密码),但是我想再传递1个变量并检查findForPassport,如果该用户是否属于其他表。

2 个答案:

答案 0 :(得分:2)

我希望这个答案可以帮助其他人。
如果要添加变量并将此变量传递给User Authenticate模型中的findPassport函数,则需要更新护照中的3个类:
- vendor \ league \ oauth2-server \ src \ Repositories \ UserRepositoryInterface中的 UserRepositoryInterface - vendor \ league \ oauth2-server \ src \ Grant \ PasswordGrant中的 PasswordGrant - vendor \ laravel \ passport \ src \ Bridge \ UserRepository中的 UserRepository

在示例代码中,我将添加父变量,代码将如下所示

UserRepositoryInterface类中的

+

interface UserRepositoryInterface extends RepositoryInterface
{

/**
 * Get a user entity.
 *
 * @param string                $username
 * @param string                $password
 * @param string                $grantType    The grant type used
 * @param ClientEntityInterface $clientEntity
 *
 * @return UserEntityInterface
 */
public function getUserEntityByUserCredentials(
    $username,
    $password,
    $parent,                           <------variable example 
    $grantType,
    ClientEntityInterface $clientEntity
);
}
PasswordGrant类中的

+

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

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

  /**
  * Get a user parent.
  * varaible example 
  */
    $parent = $this->getRequestParameter('parent', $request);  
    if (is_null($parent)) {
        throw OAuthServerException::invalidRequest('password');
    }

    $user = $this->userRepository->getUserEntityByUserCredentials(
        $username,
        $password,
        $parent,                          <--- variable example get from request
        $this->getIdentifier(),
        $client
    );
    if ($user instanceof UserEntityInterface === false) {
        $this->getEmitter()->emit(new RequestEvent(RequestEvent::USER_AUTHENTICATION_FAILED, $request));

        throw OAuthServerException::invalidCredentials();
    }

    return $user;
}  }

+在UserRepository类

class UserRepository implements UserRepositoryInterface
{

public function getUserEntityByUserCredentials($username, $password, $parent, $grantType, ClientEntityInterface $clientEntity) 
/*add 1more parameter that implement from UserRepositoryInterface*/
{
    $provider = config('auth.guards.api.provider');

    if (is_null($model = config('auth.providers.'.$provider.'.model'))) {
        throw new RuntimeException('Unable to determine authentication model from configuration.');
    }

    if (method_exists($model, 'findForPassport')) {
        $user = (new $model)->findForPassport($username,$parent);    <--- finally we pass parent variable to findForPassport here
    } else {
        $user = (new $model)->where('email', $username)->first();
    }

    if (! $user) {
        return;
    } elseif (method_exists($user, 'validateForPassportPasswordGrant')) {
        if (! $user->validateForPassportPasswordGrant($password)) {
            return;
        }
    } elseif (! $this->hasher->check($password, $user->getAuthPassword())) {
        return;
    }

    return new User($user->getAuthIdentifier());
}
}

然后你可以从findForPassport中的参数中获取$ parent值。但是请确保你作为雄辩的用户返回值。如果你想加入表,你可以查看下面的示例代码

class User extends Authenticatable{

..........
public function findForPassport($identifier,$parent) {
    $a = $this
        ->Join('role as r', 'r.user_id', '=', 'users.id')
        ->get();
    return $a->where('name', $identifier)->where('role_id',$parent)->first();

  }
}

答案 1 :(得分:0)

来自the link to passport issue #81 pointed by @Arun in OP

  

现在可能有更好的方法,但是我扩展了PassportServiceProvider并复制了registerAuthorizationServer函数,以便我可以注册自己的授予类型。

     

用新的替换掉config \ app.php中的提供程序:

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

已更新的registerAuthorizationServer函数,其中包括新的授予选项:

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看起来像这样:

<?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';
  }
}