Я возился с паспортом laravel, и я не могу реализовать пользовательский тип гранта. Я использую laravel 5.6 с паспортом 6.0. После исследования я создал CustomRequestGrantProvider и CustomRequestGrant аналогичным образом в этом библиотека CustomGrant, но мне не повезло, и каждый раз я делал POST-запрос к localhost:8000/oauth/token с grant_type, client_id< /strong> и client_secret
{"error": "unsupported_grant_type",
"message": "The authorization grant type is not supported by the authorization server.",
"hint": "Check that all required parameters have been provided"}
И похоже мой запрос даже не проходит. Я добавил провайдера в app.php.
Это мой CustomRequestGrantProvider
class CustomRequestGrantProvider extends PassportServiceProvider{
public function boot()
{
$this->loadViewsFrom(__DIR__.'/../resources/views', 'passport');
$this->deleteCookieOnLogout();
}
public function register()
{
$this->registerAuthorizationServer();
}
protected function registerAuthorizationServer()
{
$this->app->singleton(AuthorizationServer::class, function () {
return tap($this->makeAuthorizationServer(), function ($server) {
$server->enableGrantType(
$this->makeCustomRequestGrant(), Passport::tokensExpireIn()
);
});
});
}
protected function makeCustomRequestGrant()
{
$grant = new CustomRequestGrant(
$this->app->make(UserRepository::class),
$this->app->make(RefreshTokenRepository::class)
);
$grant->setRefreshTokenTTL(Passport::refreshTokensExpireIn());
return $grant;
}}
а это мой CustomRequestGrant
class CustomRequestGrant extends AbstractGrant{
public function __construct(
UserRepositoryInterface $userRepository,
RefreshTokenRepositoryInterface $refreshTokenRepository
)
{
$this->setUserRepository($userRepository);
$this->setRefreshTokenRepository($refreshTokenRepository);
$this->refreshTokenTTL = new \DateInterval('P1M');
}
public function respondToAccessTokenRequest(
ServerRequestInterface $request,
ResponseTypeInterface $responseType,
\DateInterval $accessTokenTTL
)
{
// Validate request
$client = $this->validateClient($request);
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request));
$user = $this->validateUser($request);
// Finalize the requested scopes
$scopes = $this->scopeRepository->finalizeScopes($scopes, $this->getIdentifier(), $client, $user->getIdentifier());
// Issue and persist new tokens
$accessToken = $this->issueAccessToken($accessTokenTTL, $client, $user->getIdentifier(), $scopes);
$refreshToken = $this->issueRefreshToken($accessToken);
// Inject tokens into response
$responseType->setAccessToken($accessToken);
$responseType->setRefreshToken($refreshToken);
return $responseType;
}
public function getIdentifier()
{
return 'custom_request';
}
protected function validateUser(ServerRequestInterface $request)
{
$laravelRequest = new Request($request->getParsedBody());
$user = $this->getUserEntityByRequest($laravelRequest);
if ($user instanceof UserEntityInterface === false) {
$this->getEmitter()->emit(new RequestEvent(RequestEvent::USER_AUTHENTICATION_FAILED, $request));
throw OAuthServerException::invalidCredentials();
}
return $user;
}
protected function getUserEntityByRequest(Request $request)
{
if (is_null($model = config('auth.providers.users.model'))) {
throw OAuthServerException::serverError('Unable to determine user model from configuration.');
}
if (method_exists($model, 'byPassportCustomRequest')) {
$user = (new $model)->byPassportCustomRequest($request);
} else {
throw OAuthServerException::serverError('Unable to find byPassportCustomRequest method on user model.');
}
return ($user) ? new User($user->id) : null;
}}
Примечание. Все импорты и пространства имен верны, я просто удалил их ради этого поста.
Я даже думал о редактировании библиотеки паспортов, но не уверен, насколько она будет устойчивой в будущем.
Любая помощь очень ценится.
Несколько ссылок: