vsavritsky / slim-oauth2-routes
Slim Framework API 内使用的 OAuth2 路由
v4.0.0
2023-01-20 21:15 UTC
Requires
- php: ^7.4 || ^8.0
- bshaffer/oauth2-server-php: ^1.9
- vsavritsky/slim-oauth2-http: ^4.0
Requires (Dev)
- laminas/laminas-diactoros: ^2.8
- php-coveralls/php-coveralls: ^2.5
- phpunit/phpunit: ^8.5
- slim/php-view: ^2.0.5
- squizlabs/php_codesniffer: ^3.2
Suggests
- chadicus/slim-oauth2-middleware: Adds OAuth2 middleware for API requests.
- slim/php-view: Simple template rendering
This package is not auto-updated.
Last update: 2024-09-29 05:02:46 UTC
README
用于在 Slim 3 框架 API 中使用的 OAuth2 服务器 路由回调
要求
Chadicus\Slim\OAuth2\Routes 需要 PHP 5.6(或更高版本)。
Composer
要添加库作为本地、项目级依赖项,请使用 Composer!只需将 chadicus/slim-oauth2-routes
添加到项目的 composer.json
文件中的依赖项即可,例如
composer require chadicus/slim-oauth2-routes
联系
开发者可通过以下方式联系
项目构建
检出代码后,将 Composer 添加到您的 PATH 并运行
./composer install ./vendor/bin/phpunit
关于使用视图的说明
authorize
和 receive-code
路由需要 view
对象。给定的视图对象必须实现一个渲染方法,例如在 slim/twig-view 和 slim/php-view 中找到的方法。最好有一个共同的 ViewInterface
,但截至现在还没有这样一个接口。
社区
示例用法
use Chadicus\Slim\OAuth2\Routes; use OAuth2; use OAuth2\GrantType; use OAuth2\Storage; use Slim; use Slim\Views; //Set-up the OAuth2 Server $storage = new Storage\Pdo(array('dsn' => $dsn, 'username' => $username, 'password' => $password)); $server = new OAuth2\Server($storage); $server->addGrantType(new GrantType\AuthorizationCode($storage)); $server->addGrantType(new GrantType\ClientCredentials($storage)); //Set-up the Slim Application $app = new Slim\App( [ 'view' => new Views\PhpRenderer('/path/to/chadicus/slim-oauth2-routes/templates'), ] ); $container = $app->getContainer(); $app->map(['GET', 'POST'], Routes\Authorize::ROUTE, new Routes\Authorize($server, $container['view']))->setName('authorize'); $app->post(Routes\Token::ROUTE, new Routes\Token($server))->setName('token'); $app->map(['GET', 'POST'], Routes\ReceiveCode::ROUTE, new Routes\ReceiveCode($container['view']))->setName('receive-code'); $app->post(Routes\Revoke::ROUTE, new Routes\Revoke($server))->setName('revoke'); //Add custom routes $slim->get('/foo', function($request, $response, $args) { $authorization = $request->getHeaderLine('Authorization'); //validate access token against your storage return $response->withStatus(200); }); //run the app $app->run();
授权和 UserIdProvider
在授权路由中,您可以定义一个 UserIdProviderInterface
来从传入的请求中提取用户_id。默认情况下,路由将在 GET
查询参数中查找。
class ArgumentUserIdProvider implements UserIdProviderInterface { public function getUserId(ServerRequestInterface $request, array $arguments) { return isset($arguments['user_id']) ? $arguments['user_id'] : null; } } //middleware to add user_id to route parameters $loginMiddelware = function ($request, $response, $next) { // Validate the user credentials $userId = MyUserService::getUserIdIfValidCredentials($request); if ($userId === false) { return $response->withStatus(303); } //Put user_id into the route parameters $route = $request->getAttribute('route'); $route->setArgument('user_id', $userId); //Credentials are valid, continue so the authorization code can be sent to the clients callback_uri return $next($request, $response); }; $authorizeRoute = new Routes\Authorize($server, $view, 'authorize.phtml', new ArgumentUserIdProvider()); $app->map( ['GET', 'POST'], Routes\Authorize::ROUTE, $authorizeRoute )->add($loginMiddleware)->setName('authorize');