maksimovic / slim-oauth2-middleware
OAuth2 middleware for use within a Slim Framework API
Package info
github.com/maksimovic/slim-oauth2-middleware
pkg:composer/maksimovic/slim-oauth2-middleware
v4.0.1
2026-03-13 08:10 UTC
Requires
- php: ^8.1
- bshaffer/oauth2-server-php: ^1.9
- container-interop/container-interop: ^1.1
- maksimovic/psr-middleware: ^2.0
- maksimovic/slim-oauth2-http: ^4.0
- psr/http-message: ^1.0
Requires (Dev)
- laminas/laminas-diactoros: ^2.0 || ^3.0
- php-di/php-di: ^5.4
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: ^3.7
Suggests
- maksimovic/slim-oauth2-routes: Offers standard OAuth2 routes for slim applications
README
Fork Notice: This is a maintained fork of the abandoned
chadicus/slim-oauth2-middlewarepackage. Updated for PHP 8.1+.
Middleware for using OAuth2 Server within a Slim Framework API.
Requirements
PHP 8.1 or later.
Installation
composer require maksimovic/slim-oauth2-middleware
Example Usage
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', ], ], ] ); // Create the OAuth2 server $server = new OAuth2\Server( $storage, ['access_lifetime' => 3600], [new GrantType\ClientCredentials($storage)] ); // Create the Slim app $app = new Slim\App(); // Create the authorization middleware $authMiddleware = new Middleware\Authorization($server, $app->getContainer()); // No scope required $app->get('foos', function ($request, $response, $args) { // return all foos })->add($authMiddleware); // Requires superUser scope OR (basicUser AND canViewFoos) $app->get('foos/id', function ($request, $response, $id) { // return foo details })->add($authMiddleware->withRequiredScope(['superUser', ['basicUser', 'canViewFoos']])); // Requires superUser scope $app->post('foos', function ($request, $response, $args) { // create a new foo })->add($authMiddleware->withRequiredScope(['superUser'])); $app->run();
Development
composer install
composer test
composer test:coverage
composer cs-check
License
MIT