实施自定义 Laravel 护照授予的正确方法是什么?
What is the proper way to implement a custom Laravel passport grant?
我一直在修改 laravel 护照,我似乎无法实现自定义授权类型。我正在使用 laravel 5.6 和 passport 6.0 。
经过研究,我在这个 CustomGrant library 中同样创建了一个 CustomRequestGrantProvider 和一个 CustomRequestGrant 但我没有运气,每次我都会做一个POST 使用 grant_type、client_id 和 localhost:8000/oauth/token 请求=40=]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;
}}
注意:所有导入和命名空间都是正确的,我只是为此删除了它们 post。
我什至想过编辑护照库,但我不确定它在未来的可持续性如何。
非常感谢任何帮助。
一些参考资料:
Custom Grants?
所以我最终创建了实现原始 类 的自定义 类(请看下图)。
laravel/passport 特征所需的 类 是下图中的 类,开头没有 Custom。
对于CustomUserRepositoryInterface,你只需要修改UserRepositoryInterface,例如你需要发送一个额外的参数给laravel post 请求.
这使我能够大量自定义 laravel 通行证,例如传递具有不同访问应用程序方式的多种帐户类型(例如使用 phone 号码、电子邮件、facebook_token 和 id).
很抱歉我没有深入探讨答案,但我正在考虑创建一个库并在 github 上分享我的工作,当然更新答案并分享 link , 但同样如此,这些是唯一 类 你需要改变才能达到这样的结果。
祝你有美好的一天:)
我一直在修改 laravel 护照,我似乎无法实现自定义授权类型。我正在使用 laravel 5.6 和 passport 6.0 。 经过研究,我在这个 CustomGrant library 中同样创建了一个 CustomRequestGrantProvider 和一个 CustomRequestGrant 但我没有运气,每次我都会做一个POST 使用 grant_type、client_id 和 localhost:8000/oauth/token 请求=40=]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;
}}
注意:所有导入和命名空间都是正确的,我只是为此删除了它们 post。
我什至想过编辑护照库,但我不确定它在未来的可持续性如何。
非常感谢任何帮助。
一些参考资料:
Custom Grants?
所以我最终创建了实现原始 类 的自定义 类(请看下图)。
laravel/passport 特征所需的 类 是下图中的 类,开头没有 Custom。
对于CustomUserRepositoryInterface,你只需要修改UserRepositoryInterface,例如你需要发送一个额外的参数给laravel post 请求.
这使我能够大量自定义 laravel 通行证,例如传递具有不同访问应用程序方式的多种帐户类型(例如使用 phone 号码、电子邮件、facebook_token 和 id).
很抱歉我没有深入探讨答案,但我正在考虑创建一个库并在 github 上分享我的工作,当然更新答案并分享 link , 但同样如此,这些是唯一 类 你需要改变才能达到这样的结果。
祝你有美好的一天:)