vsavritsky / slim-oauth2-middleware
适用于Slim Framework API的OAuth2中间件
v4.0.0
2023-01-20 21:26 UTC
Requires
- php: ^7.4 || ^8.0
- bshaffer/oauth2-server-php: ^1.11
- psr/http-message: ^1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
- vsavritsky/slim-oauth2-http: ^4.0
Requires (Dev)
- laminas/laminas-diactoros: ^2.8
- php-coveralls/php-coveralls: ^2.5
- phpunit/phpunit: ^8.5
- squizlabs/php_codesniffer: ^3.2
Suggests
- chadicus/slim-oauth2-routes: Offers standard OAuth2 routes for slim applications
This package is not auto-updated.
Last update: 2024-09-29 05:29:22 UTC
README
在OAuth2 Server中使用的中间件,用于Slim 3 Framework API
需求
Chadicus\Slim\OAuth2\Middleware 需要 PHP 5.6(或更高版本)。
Composer
要作为本地、项目依赖项添加库,请使用Composer!只需将 chadicus/slim-oauth2-middleware
依赖项添加到您的项目 composer.json
文件中,如下所示:
composer require chadicus/slim-oauth2-middleware
联系方式
开发者可通过以下方式联系:
项目构建
通过检出代码,将 Composer 添加到您的 PATH 并运行
composer install ./vendor/bin/phpunit ./vendor/bin/phpcs
社区
使用示例
使用授权中间件的简单示例。
use Chadicus\Slim\OAuth2\Middleware; use OAuth2; use OAuth2\Storage; use OAuth2\GrantType; use Slim; //set up storage for oauth2 server $storage = new Storage\Memory( [ 'client_credentials' => [ 'administrator' => [ 'client_id' => 'administrator', 'client_secret' => 'password', 'scope' => 'superUser', ], 'foo-client' => [ 'client_id' => 'foo-client', 'client_secret' => 'p4ssw0rd', 'scope' => 'basicUser canViewFoos', ], 'bar-client' => [ 'client_id' => 'foo-client', 'client_secret' => '!password1', 'scope' => 'basicUser', ], ], ] ); // create the oauth2 server $server = new OAuth2\Server( $storage, [ 'access_lifetime' => 3600, ], [ new GrantType\ClientCredentials($storage), ] ); //create the basic app $app = new Slim\App(); // create the authorization middlware $authMiddleware = new Middleware\Authorization($server, $app->getContainer()); //Assumes token endpoints available for creating access tokens $app->get('foos', function ($request, $response, $args) { //return all foos, no scope required })->add($authMiddleware); $getRouteCallback = function ($request, $response, $id) { //return details for a foo, requires superUser scope OR basicUser with canViewFoos scope }; $app->get('foos/id', $getRouteCallback)->add($authMiddleware->withRequiredScope(['superUser', ['basicUser', 'canViewFoos']])); $postRouteCallback = function ($request, $response, $args) { //Create a new foo, requires superUser scope }; $app->post('foos', $postRouteCallback)->add($authMiddleware->withRequiredScope(['superUser'])); $app->run();