sonrac / lumen-league-oauth2
Lumen league oauth2 包装器
1.3.1
2018-11-07 23:04 UTC
Requires
- php: >=7.0
- laravel/lumen-framework: 5.*
- league/oauth2-server: ^6.0
- symfony/psr-http-message-bridge: ^1.0
- zendframework/zend-diactoros: ^1.6
Requires (Dev)
- fzaninotto/faker: ^1.7
- phpunit/phpunit: ^6.3
- refinery29/php-cs-fixer-config: ^0.6.7
- squizlabs/php_codesniffer: ^3.2
README
安装
composer require sonrac/lumen-league-oauth2
用法
首先注册服务提供者
添加到您的 bootstrap/app.php
$app->register(\sonrac\lumenRest\Oauth2ServiceProvider::class);
描述
league/oauth2 -server
对 Lumen 的实现
合约
在 sonrac\lumenRest\Oauth2ServiceProvider
中实现的合约或 oauth2 服务器
事件
在官方文档中描述了事件用法
中间件
使用 League\OAuth2\Server\Middleware\ResourceMiddleware
验证身份验证请求
使用 League\OAuth2\Server\Middleware\AuthorizationServerMiddleware
进行用户认证
示例路由
获取访问令牌
$app->router->post('/access_token', function (\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response) use ($app) { /* @var \League\OAuth2\Server\AuthorizationServer $server */ $server = $app->make(\League\OAuth2\Server\AuthorizationServer::class); try { // Try to respond to the request return $server->respondToAccessTokenRequest($request, $response); } catch (\League\OAuth2\Server\Exception\OAuthServerException $exception) { // All instances of OAuthServerException can be formatted into a HTTP response return $exception->generateHttpResponse($response); } catch (\Exception $exception) { // Unknown exception $body = new \Zend\Diactoros\Stream('php://temp', 'r+'); $body->write($exception->getMessage()); return $response->withStatus(500)->withBody($body); } });
授权第三方客户端(隐式和授权代码授予)
$app->router->get('/authorize', function (\League\OAuth2\Server\AuthorizationServer $server, \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response) { try { // Validate the HTTP request and return an AuthorizationRequest object. $authRequest = $server->validateAuthorizationRequest($request); // The auth request object can be serialized and saved into a user's session. // You will probably want to redirect the user at this point to a login endpoint. // Once the user has logged in set the user on the AuthorizationRequest $authRequest->setUser(app()->make(\League\OAuth2\Server\Entities\UserEntityInterface::class)); // an instance of UserEntityInterface // At this point you should redirect the user to an authorization page. // This form will ask the user to approve the client and the scopes requested. // Once the user has approved or denied the client update the status // (true = approved, false = denied) $authRequest->setAuthorizationApproved(true); // Return the HTTP redirect response return $server->completeAuthorizationRequest($authRequest, $response); } catch (\Exception $exception) { // Unknown exception $body = new \Zend\Diactoros\Stream('php://temp', 'r+'); $body->write($exception->getMessage()); return $response->withStatus(500)->withBody($body); } });
JWT 守卫
要使用 JWT 令牌,您需要定义 JWT 守卫
示例配置
'defaults' => [ 'guard' => 'jwt' ], 'guards' => [ 'jwt' => [ 'driver' => 'jwt', 'provider' => 'clients', ], 'user' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'clients' => [ 'driver' => 'eloquent', 'model' => app(\League\OAuth2\Server\Entities\ClientEntityInterface::class), ], 'users' => [ 'driver' => 'eloquent', 'model' => app(\League\OAuth2\Server\Entities\UserEntityInterface::class), ], ],
生成密钥
要使用 SSL 加密,首先生成密钥
php artisan generate:keys