Middleware for Using OAuth2 within a Slim Framework API
Chadicus\Slim\OAuth2\Middleware requires PHP 5.5 (or later).
##Composer
To add the library as a local, per-project dependency use Composer! Simply add a dependency on
chadicus/slim-oauth2-middleware
to your project's composer.json
file such as:
{
"require": {
"chadicus/slim-oauth2-middleware": "~1.0"
}
}
##Contact Developers may be contacted at:
##Project Build With a checkout of the code get Composer in your PATH and run:
./composer install
./vendor/bin/phpunit
##Example Usage
Simple example for using the authorization middleware.
use Chadicus\Slim\OAuth2\Middleware;
use OAuth2\Server;
use OAuth2\Storage;
use OAuth2\GrantType;
use Slim\Slim;
//set up storage for oauth2 server
$storage = new Storage\Memory(
[
'client_credentials' => [
'testClientId' => [
'client_id' => 'chadicus-app',
'client_secret' => 'password',
],
],
]
);
// create the oauth2 server
$server = new Server(
$storage,
[
'access_lifetime' => 3600,
],
[
new GrantType\ClientCredentials($storage),
]
);
// create the authorization middlware
$authorization = new Middleware\Authorization($server);
$app = new Slim();
//Assumes token endpoints available for creating access tokens
$app->get('foos', $authorization, function () {
//return all foos, no scope required
});
$app->get('foos/id', $authorization->withRequiredScope(['superUser', ['basicUser', 'canViewFoos']]), function ($id) {
//return details for a foo, requires superUser scope OR basicUser with canViewFoos scope
});
$app->post('foos', $authorization->withRequiredScope(['superUser']), function () {
//Create a new foo, requires superUser scope
});