diff --git a/CONTRIBUTING.md b/.github/CONTRIBUTING.md similarity index 89% rename from CONTRIBUTING.md rename to .github/CONTRIBUTING.md index 813df62..62cc3c2 100644 --- a/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -15,8 +15,8 @@ Code changes should be sent through [GitHub Pull Requests](https://github.com/ch While the build does not enforce 100% [PHPUnit](http://www.phpunit.de) code coverage, it will not allow coverage to drop below its current percentage. -The build will also not allow any errors for the [coding standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md). +The build will also not allow any errors for the [coding standard](http://chadicus.github.io/coding-standard/) ```sh -./vendor/bin/phpcs --standard=PSR2 src tests +./vendor/bin/phpcs --standard=./vendor/chadicus/coding-standard/Chadicus src tests ``` diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..4eef22e --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,6 @@ +## Expected Behavior + +## Actual Behavior + +## Steps to reproduce the behavior + diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..5eb7f2a --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,8 @@ +Fixes # . + +#### What does this PR do? + +#### Checklist +- [ ] Pull request contains a clear definition of changes +- [ ] Tests (either unit, integration, or acceptance) written and passing +- [ ] Relevant documentation produced and/or updated diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 59f462b..5be0ff6 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -21,7 +21,7 @@ tools: php_pdepend: true php_sim: true build_failure_conditions: - - 'elements.rating(<= B).new.exists' + - 'elements.rating(< B).new.exists' - 'issues.label("coding-style").new.exists' - 'issues.severity(>= MAJOR).new.exists' - 'project.metric("scrutinizer.quality", < 6)' diff --git a/.travis.yml b/.travis.yml index bafb5bb..77a1a01 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,12 @@ language: php php: - 5.6 - - 5.5 - 7 - hhvm install: composer install script: ./vendor/bin/phpunit --coverage-clover clover.xml -after_success: sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" -a "$TRAVIS_PHP_VERSION" != "7" ]; then ./vendor/bin/coveralls -v; fi' +after_success: sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then ./vendor/bin/coveralls -v; fi' matrix: fast_finish: true allow_failures: - - php: 7 - php: hhvm diff --git a/README.md b/README.md index c8011cc..3b82691 100644 --- a/README.md +++ b/README.md @@ -15,11 +15,11 @@ [![Documentation](https://img.shields.io/badge/reference-phpdoc-blue.svg?style=flat)](http://pholiophp.org/chadicus/slim-oauth2-middleware) -Middleware for Using OAuth2 within a Slim Framework API +Middleware for using [OAuth2 Server](http://bshaffer.github.io/oauth2-server-php-docs/) within a [Slim 3 Framework](http://www.slimframework.com/) API ## Requirements -Chadicus\Slim\OAuth2\Middleware requires PHP 5.5 (or later). +Chadicus\Slim\OAuth2\Middleware requires PHP 5.6 (or later). ##Composer To add the library as a local, per-project dependency use [Composer](http://getcomposer.org)! Simply add a dependency on @@ -28,7 +28,7 @@ To add the library as a local, per-project dependency use [Composer](http://getc ```json { "require": { - "chadicus/slim-oauth2-middleware": "~1.0" + "chadicus/slim-oauth2-middleware": "~3.0" } } ``` @@ -53,10 +53,10 @@ Simple example for using the authorization middleware. ```php use Chadicus\Slim\OAuth2\Middleware; -use OAuth2\Server; +use OAuth2; use OAuth2\Storage; use OAuth2\GrantType; -use Slim\Slim; +use Slim; //set up storage for oauth2 server $storage = new Storage\Memory( @@ -71,7 +71,7 @@ $storage = new Storage\Memory( ); // create the oauth2 server -$server = new Server( +$server = new OAuth2\Server( $storage, [ 'access_lifetime' => 3600, @@ -81,22 +81,29 @@ $server = new Server( ] ); -// create the authorization middlware -$authorization = new Middleware\Authorization($server); +//create the basic app +$app = new Slim\App(); -$app = new Slim(); +// create the authorization middlware +$authMiddleware = new Middleware\Authorization($server, $app->getContainer()); //Assumes token endpoints available for creating access tokens -$app->get('foos', $authorization, function () { +$app->get('foos', function ($request, $response, $args) { //return all foos, no scope required -}); +})->add($authMiddleware); -$app->get('foos/id', $authorization->withRequiredScope(['superUser', ['basicUser', 'canViewFoos']]), function ($id) { +$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']])); -$app->post('foos', $authorization->withRequiredScope(['superUser']), function () { +$postRouteCallback = function ($request, $response, $args) { //Create a new foo, requires superUser scope -}); +}; + +$app->post('foos', $postRouteCallback)->add($authMiddleware->withRequiredScope(['superUser'])); + +$app->run(); ``` diff --git a/composer.json b/composer.json index 075626b..5ada027 100644 --- a/composer.json +++ b/composer.json @@ -11,15 +11,15 @@ ], "license": "MIT", "require": { - "php": "~5.5 || ~7.0", - "bshaffer/oauth2-server-php": "~1.7", - "slim/slim": "~2.0", - "chadicus/slim-oauth2-http": "~1.0" + "php": "~5.6 || ~7.0", + "bshaffer/oauth2-server-php": "~1.8", + "slim/slim": "~3.0", + "chadicus/slim-oauth2-http": "~3.0" }, "require-dev": { - "phpunit/phpunit": "~4.0", + "phpunit/phpunit": "~5.0", "chadicus/coding-standard": "~1.0", - "satooshi/php-coveralls": "~0.6.1" + "satooshi/php-coveralls": "~1.0" }, "autoload": { "psr-4": {"Chadicus\\Slim\\OAuth2\\Middleware\\" : "src"} diff --git a/composer.lock b/composer.lock index 5fa3a65..467c23c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "16d4862787a734f3de05ca98f8b03f9e", - "content-hash": "df6ff655398113fb0a1b044c1339b3a8", + "hash": "bff07534f7d06233bca0371fc52159a0", + "content-hash": "329b0d45af03c9655869abcb08965e2b", "packages": [ { "name": "bshaffer/oauth2-server-php", @@ -58,26 +58,26 @@ }, { "name": "chadicus/slim-oauth2-http", - "version": "v1.0.0", + "version": "v3.0.0", "source": { "type": "git", "url": "https://github.com/chadicus/slim-oauth2-http.git", - "reference": "ad082c5f18e3cbaf446bc97cf3d59e67c5df54c3" + "reference": "864599654bb7b287ef0a0c25cc19adace79f1823" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/chadicus/slim-oauth2-http/zipball/ad082c5f18e3cbaf446bc97cf3d59e67c5df54c3", - "reference": "ad082c5f18e3cbaf446bc97cf3d59e67c5df54c3", + "url": "https://api.github.com/repos/chadicus/slim-oauth2-http/zipball/864599654bb7b287ef0a0c25cc19adace79f1823", + "reference": "864599654bb7b287ef0a0c25cc19adace79f1823", "shasum": "" }, "require": { - "bshaffer/oauth2-server-php": "~1.7", - "php": "~5.5 || ~7.0", - "slim/slim": "~2.0" + "bshaffer/oauth2-server-php": "~1.8", + "php": "~5.6 || ~7.0", + "slim/slim": "~3.0" }, "require-dev": { "chadicus/coding-standard": "~1.0", - "phpunit/phpunit": "~4.0", + "phpunit/phpunit": "~5.3", "satooshi/php-coveralls": "~0.6.1" }, "suggest": { @@ -108,32 +108,205 @@ "oauth2", "slim" ], - "time": "2016-02-03 21:12:46" + "time": "2016-05-22 16:11:10" + }, + { + "name": "container-interop/container-interop", + "version": "1.1.0", + "source": { + "type": "git", + "url": "https://github.com/container-interop/container-interop.git", + "reference": "fc08354828f8fd3245f77a66b9e23a6bca48297e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/container-interop/container-interop/zipball/fc08354828f8fd3245f77a66b9e23a6bca48297e", + "reference": "fc08354828f8fd3245f77a66b9e23a6bca48297e", + "shasum": "" + }, + "type": "library", + "autoload": { + "psr-4": { + "Interop\\Container\\": "src/Interop/Container/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Promoting the interoperability of container objects (DIC, SL, etc.)", + "time": "2014-12-30 15:22:37" + }, + { + "name": "nikic/fast-route", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/nikic/FastRoute.git", + "reference": "79843dce62ac52e9b628e73d5f1264cad10c65a6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/nikic/FastRoute/zipball/79843dce62ac52e9b628e73d5f1264cad10c65a6", + "reference": "79843dce62ac52e9b628e73d5f1264cad10c65a6", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "FastRoute\\": "src/" + }, + "files": [ + "src/functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Nikita Popov", + "email": "nikic@php.net" + } + ], + "description": "Fast request router for PHP", + "keywords": [ + "router", + "routing" + ], + "time": "2016-04-18 11:33:20" + }, + { + "name": "pimple/pimple", + "version": "v3.0.2", + "source": { + "type": "git", + "url": "https://github.com/silexphp/Pimple.git", + "reference": "a30f7d6e57565a2e1a316e1baf2a483f788b258a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/silexphp/Pimple/zipball/a30f7d6e57565a2e1a316e1baf2a483f788b258a", + "reference": "a30f7d6e57565a2e1a316e1baf2a483f788b258a", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "Pimple": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + } + ], + "description": "Pimple, a simple Dependency Injection Container", + "homepage": "http://pimple.sensiolabs.org", + "keywords": [ + "container", + "dependency injection" + ], + "time": "2015-09-11 15:10:35" + }, + { + "name": "psr/http-message", + "version": "1.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "85d63699f0dbedb190bbd4b0d2b9dc707ea4c298" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/85d63699f0dbedb190bbd4b0d2b9dc707ea4c298", + "reference": "85d63699f0dbedb190bbd4b0d2b9dc707ea4c298", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "time": "2015-05-04 20:22:00" }, { "name": "slim/slim", - "version": "2.6.2", + "version": "3.4.1", "source": { "type": "git", "url": "https://github.com/slimphp/Slim.git", - "reference": "20a02782f76830b67ae56a5c08eb1f563c351a37" + "reference": "794073a27b2dd9ef492124263038291b7f8daeb3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/slimphp/Slim/zipball/20a02782f76830b67ae56a5c08eb1f563c351a37", - "reference": "20a02782f76830b67ae56a5c08eb1f563c351a37", + "url": "https://api.github.com/repos/slimphp/Slim/zipball/794073a27b2dd9ef492124263038291b7f8daeb3", + "reference": "794073a27b2dd9ef492124263038291b7f8daeb3", "shasum": "" }, "require": { - "php": ">=5.3.0" + "container-interop/container-interop": "^1.1", + "nikic/fast-route": "^1.0", + "php": ">=5.5.0", + "pimple/pimple": "^3.0", + "psr/http-message": "^1.0" }, - "suggest": { - "ext-mcrypt": "Required for HTTP cookie encryption" + "provide": { + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0", + "squizlabs/php_codesniffer": "^2.5" }, "type": "library", "autoload": { - "psr-0": { - "Slim": "." + "psr-4": { + "Slim\\": "Slim" } }, "notification-url": "https://packagist.org/downloads/", @@ -141,35 +314,51 @@ "MIT" ], "authors": [ + { + "name": "Rob Allen", + "email": "rob@akrabat.com", + "homepage": "http://akrabat.com" + }, { "name": "Josh Lockhart", - "email": "info@joshlockhart.com", - "homepage": "http://www.joshlockhart.com/" + "email": "hello@joshlockhart.com", + "homepage": "https://joshlockhart.com" + }, + { + "name": "Gabriel Manricks", + "email": "gmanricks@me.com", + "homepage": "http://gabrielmanricks.com" + }, + { + "name": "Andrew Smith", + "email": "a.smith@silentworks.co.uk", + "homepage": "http://silentworks.co.uk" } ], - "description": "Slim Framework, a PHP micro framework", - "homepage": "http://github.com/codeguy/Slim", + "description": "Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs", + "homepage": "http://slimframework.com", "keywords": [ - "microframework", - "rest", + "api", + "framework", + "micro", "router" ], - "time": "2015-03-08 18:41:17" + "time": "2016-05-16 18:49:10" } ], "packages-dev": [ { "name": "chadicus/coding-standard", - "version": "v1.0.1", + "version": "v1.1.0", "source": { "type": "git", "url": "https://github.com/chadicus/coding-standard.git", - "reference": "971fac1625463a72df0034fbfdd263561f2ccb4f" + "reference": "a9a4e1be55cefff8388da7644f3d36a009f16a32" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/chadicus/coding-standard/zipball/971fac1625463a72df0034fbfdd263561f2ccb4f", - "reference": "971fac1625463a72df0034fbfdd263561f2ccb4f", + "url": "https://api.github.com/repos/chadicus/coding-standard/zipball/a9a4e1be55cefff8388da7644f3d36a009f16a32", + "reference": "a9a4e1be55cefff8388da7644f3d36a009f16a32", "shasum": "" }, "require": { @@ -193,7 +382,7 @@ "phpcs", "standard" ], - "time": "2015-12-01 19:20:36" + "time": "2016-01-25 15:11:21" }, { "name": "doctrine/instantiator", @@ -344,6 +533,48 @@ ], "time": "2015-03-18 18:23:50" }, + { + "name": "myclabs/deep-copy", + "version": "1.5.1", + "source": { + "type": "git", + "url": "https://github.com/myclabs/DeepCopy.git", + "reference": "a8773992b362b58498eed24bf85005f363c34771" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/a8773992b362b58498eed24bf85005f363c34771", + "reference": "a8773992b362b58498eed24bf85005f363c34771", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "doctrine/collections": "1.*", + "phpunit/phpunit": "~4.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "DeepCopy\\": "src/DeepCopy/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Create deep copies (clones) of your objects", + "homepage": "https://github.com/myclabs/DeepCopy", + "keywords": [ + "clone", + "copy", + "duplicate", + "object", + "object graph" + ], + "time": "2015-11-20 12:04:31" + }, { "name": "phpdocumentor/reflection-docblock", "version": "2.0.4", @@ -395,22 +626,24 @@ }, { "name": "phpspec/prophecy", - "version": "v1.5.0", + "version": "v1.6.0", "source": { "type": "git", "url": "https://github.com/phpspec/prophecy.git", - "reference": "4745ded9307786b730d7a60df5cb5a6c43cf95f7" + "reference": "3c91bdf81797d725b14cb62906f9a4ce44235972" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/4745ded9307786b730d7a60df5cb5a6c43cf95f7", - "reference": "4745ded9307786b730d7a60df5cb5a6c43cf95f7", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/3c91bdf81797d725b14cb62906f9a4ce44235972", + "reference": "3c91bdf81797d725b14cb62906f9a4ce44235972", "shasum": "" }, "require": { "doctrine/instantiator": "^1.0.2", + "php": "^5.3|^7.0", "phpdocumentor/reflection-docblock": "~2.0", - "sebastian/comparator": "~1.1" + "sebastian/comparator": "~1.1", + "sebastian/recursion-context": "~1.0" }, "require-dev": { "phpspec/phpspec": "~2.0" @@ -418,7 +651,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.4.x-dev" + "dev-master": "1.5.x-dev" } }, "autoload": { @@ -451,43 +684,44 @@ "spy", "stub" ], - "time": "2015-08-13 10:07:40" + "time": "2016-02-15 07:46:21" }, { "name": "phpunit/php-code-coverage", - "version": "2.2.4", + "version": "3.3.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "eabf68b476ac7d0f73793aada060f1c1a9bf8979" + "reference": "2431befdd451fac43fbcde94d1a92fb3b8b68f86" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/eabf68b476ac7d0f73793aada060f1c1a9bf8979", - "reference": "eabf68b476ac7d0f73793aada060f1c1a9bf8979", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/2431befdd451fac43fbcde94d1a92fb3b8b68f86", + "reference": "2431befdd451fac43fbcde94d1a92fb3b8b68f86", "shasum": "" }, "require": { - "php": ">=5.3.3", + "php": "^5.6 || ^7.0", "phpunit/php-file-iterator": "~1.3", "phpunit/php-text-template": "~1.2", - "phpunit/php-token-stream": "~1.3", + "phpunit/php-token-stream": "^1.4.2", + "sebastian/code-unit-reverse-lookup": "~1.0", "sebastian/environment": "^1.3.2", - "sebastian/version": "~1.0" + "sebastian/version": "~1.0|~2.0" }, "require-dev": { "ext-xdebug": ">=2.1.4", - "phpunit/phpunit": "~4" + "phpunit/phpunit": "~5" }, "suggest": { "ext-dom": "*", - "ext-xdebug": ">=2.2.1", + "ext-xdebug": ">=2.4.0", "ext-xmlwriter": "*" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "2.2.x-dev" + "dev-master": "3.3.x-dev" } }, "autoload": { @@ -513,7 +747,7 @@ "testing", "xunit" ], - "time": "2015-10-06 15:47:00" + "time": "2016-04-08 08:14:53" }, { "name": "phpunit/php-file-iterator", @@ -605,21 +839,24 @@ }, { "name": "phpunit/php-timer", - "version": "1.0.7", + "version": "1.0.8", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-timer.git", - "reference": "3e82f4e9fc92665fafd9157568e4dcb01d014e5b" + "reference": "38e9124049cf1a164f1e4537caf19c99bf1eb260" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/3e82f4e9fc92665fafd9157568e4dcb01d014e5b", - "reference": "3e82f4e9fc92665fafd9157568e4dcb01d014e5b", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/38e9124049cf1a164f1e4537caf19c99bf1eb260", + "reference": "38e9124049cf1a164f1e4537caf19c99bf1eb260", "shasum": "" }, "require": { "php": ">=5.3.3" }, + "require-dev": { + "phpunit/phpunit": "~4|~5" + }, "type": "library", "autoload": { "classmap": [ @@ -642,7 +879,7 @@ "keywords": [ "timer" ], - "time": "2015-06-21 08:01:12" + "time": "2016-05-12 18:03:57" }, { "name": "phpunit/php-token-stream", @@ -695,16 +932,16 @@ }, { "name": "phpunit/phpunit", - "version": "4.8.22", + "version": "5.3.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "dfb11aa5236376b4fc63853cf746af39fe780e72" + "reference": "00dd95ffb48805503817ced06399017df315fe5c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/dfb11aa5236376b4fc63853cf746af39fe780e72", - "reference": "dfb11aa5236376b4fc63853cf746af39fe780e72", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/00dd95ffb48805503817ced06399017df315fe5c", + "reference": "00dd95ffb48805503817ced06399017df315fe5c", "shasum": "" }, "require": { @@ -713,19 +950,22 @@ "ext-pcre": "*", "ext-reflection": "*", "ext-spl": "*", - "php": ">=5.3.3", + "myclabs/deep-copy": "~1.3", + "php": "^5.6 || ^7.0", "phpspec/prophecy": "^1.3.1", - "phpunit/php-code-coverage": "~2.1", + "phpunit/php-code-coverage": "^3.3.0", "phpunit/php-file-iterator": "~1.4", "phpunit/php-text-template": "~1.2", - "phpunit/php-timer": ">=1.0.6", - "phpunit/phpunit-mock-objects": "~2.3", + "phpunit/php-timer": "^1.0.6", + "phpunit/phpunit-mock-objects": "^3.1", "sebastian/comparator": "~1.1", "sebastian/diff": "~1.2", "sebastian/environment": "~1.3", "sebastian/exporter": "~1.2", "sebastian/global-state": "~1.0", - "sebastian/version": "~1.0", + "sebastian/object-enumerator": "~1.0", + "sebastian/resource-operations": "~1.0", + "sebastian/version": "~1.0|~2.0", "symfony/yaml": "~2.1|~3.0" }, "suggest": { @@ -737,7 +977,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "4.8.x-dev" + "dev-master": "5.3.x-dev" } }, "autoload": { @@ -763,30 +1003,30 @@ "testing", "xunit" ], - "time": "2016-02-02 09:01:21" + "time": "2016-05-11 13:28:45" }, { "name": "phpunit/phpunit-mock-objects", - "version": "2.3.8", + "version": "3.1.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", - "reference": "ac8e7a3db35738d56ee9a76e78a4e03d97628983" + "reference": "151c96874bff6fe61a25039df60e776613a61489" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/ac8e7a3db35738d56ee9a76e78a4e03d97628983", - "reference": "ac8e7a3db35738d56ee9a76e78a4e03d97628983", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/151c96874bff6fe61a25039df60e776613a61489", + "reference": "151c96874bff6fe61a25039df60e776613a61489", "shasum": "" }, "require": { "doctrine/instantiator": "^1.0.2", - "php": ">=5.3.3", + "php": ">=5.6", "phpunit/php-text-template": "~1.2", "sebastian/exporter": "~1.2" }, "require-dev": { - "phpunit/phpunit": "~4.4" + "phpunit/phpunit": "~5" }, "suggest": { "ext-soap": "*" @@ -794,7 +1034,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.3.x-dev" + "dev-master": "3.1.x-dev" } }, "autoload": { @@ -819,7 +1059,7 @@ "mock", "xunit" ], - "time": "2015-10-02 06:51:40" + "time": "2016-04-20 14:39:26" }, { "name": "psr/log", @@ -861,49 +1101,39 @@ }, { "name": "satooshi/php-coveralls", - "version": "v0.6.1", + "version": "v1.0.1", "source": { "type": "git", "url": "https://github.com/satooshi/php-coveralls.git", - "reference": "dd0df95bd37a7cf5c5c50304dfe260ffe4b50760" + "reference": "da51d304fe8622bf9a6da39a8446e7afd432115c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/satooshi/php-coveralls/zipball/dd0df95bd37a7cf5c5c50304dfe260ffe4b50760", - "reference": "dd0df95bd37a7cf5c5c50304dfe260ffe4b50760", + "url": "https://api.github.com/repos/satooshi/php-coveralls/zipball/da51d304fe8622bf9a6da39a8446e7afd432115c", + "reference": "da51d304fe8622bf9a6da39a8446e7afd432115c", "shasum": "" }, "require": { - "ext-curl": "*", "ext-json": "*", "ext-simplexml": "*", - "guzzle/guzzle": ">=3.0", - "php": ">=5.3", - "psr/log": "1.0.0", - "symfony/config": ">=2.0", - "symfony/console": ">=2.0", - "symfony/stopwatch": ">=2.2", - "symfony/yaml": ">=2.0" + "guzzle/guzzle": "^2.8|^3.0", + "php": ">=5.3.3", + "psr/log": "^1.0", + "symfony/config": "^2.1|^3.0", + "symfony/console": "^2.1|^3.0", + "symfony/stopwatch": "^2.0|^3.0", + "symfony/yaml": "^2.0|^3.0" }, - "require-dev": { - "apigen/apigen": "2.8.*@stable", - "pdepend/pdepend": "dev-master", - "phpmd/phpmd": "dev-master", - "phpunit/php-invoker": ">=1.1.0,<1.2.0", - "phpunit/phpunit": "3.7.*@stable", - "sebastian/finder-facade": "dev-master", - "sebastian/phpcpd": "1.4.*@stable", - "squizlabs/php_codesniffer": "1.4.*@stable", - "theseer/fdomdocument": "dev-master" + "suggest": { + "symfony/http-kernel": "Allows Symfony integration" }, "bin": [ - "composer/bin/coveralls" + "bin/coveralls" ], "type": "library", "autoload": { - "psr-0": { - "Contrib\\Component": "src/", - "Contrib\\Bundle": "src/" + "psr-4": { + "Satooshi\\": "src/Satooshi/" } }, "notification-url": "https://packagist.org/downloads/", @@ -925,7 +1155,52 @@ "github", "test" ], - "time": "2013-05-04 08:07:33" + "time": "2016-01-20 17:35:46" + }, + { + "name": "sebastian/code-unit-reverse-lookup", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/code-unit-reverse-lookup.git", + "reference": "c36f5e7cfce482fde5bf8d10d41a53591e0198fe" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/code-unit-reverse-lookup/zipball/c36f5e7cfce482fde5bf8d10d41a53591e0198fe", + "reference": "c36f5e7cfce482fde5bf8d10d41a53591e0198fe", + "shasum": "" + }, + "require": { + "php": ">=5.6" + }, + "require-dev": { + "phpunit/phpunit": "~5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Looks up which function or method a line of code belongs to", + "homepage": "https://github.com/sebastianbergmann/code-unit-reverse-lookup/", + "time": "2016-02-13 06:45:14" }, { "name": "sebastian/comparator", @@ -1045,16 +1320,16 @@ }, { "name": "sebastian/environment", - "version": "1.3.3", + "version": "1.3.7", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "6e7133793a8e5a5714a551a8324337374be209df" + "reference": "4e8f0da10ac5802913afc151413bc8c53b6c2716" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/6e7133793a8e5a5714a551a8324337374be209df", - "reference": "6e7133793a8e5a5714a551a8324337374be209df", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/4e8f0da10ac5802913afc151413bc8c53b6c2716", + "reference": "4e8f0da10ac5802913afc151413bc8c53b6c2716", "shasum": "" }, "require": { @@ -1091,7 +1366,7 @@ "environment", "hhvm" ], - "time": "2015-12-02 08:37:27" + "time": "2016-05-17 03:18:57" }, { "name": "sebastian/exporter", @@ -1210,6 +1485,52 @@ ], "time": "2015-10-12 03:26:01" }, + { + "name": "sebastian/object-enumerator", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/object-enumerator.git", + "reference": "d4ca2fb70344987502567bc50081c03e6192fb26" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/object-enumerator/zipball/d4ca2fb70344987502567bc50081c03e6192fb26", + "reference": "d4ca2fb70344987502567bc50081c03e6192fb26", + "shasum": "" + }, + "require": { + "php": ">=5.6", + "sebastian/recursion-context": "~1.0" + }, + "require-dev": { + "phpunit/phpunit": "~5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Traverses array structures and object graphs to enumerate all referenced objects", + "homepage": "https://github.com/sebastianbergmann/object-enumerator/", + "time": "2016-01-28 13:25:10" + }, { "name": "sebastian/recursion-context", "version": "1.0.2", @@ -1263,21 +1584,71 @@ "homepage": "http://www.github.com/sebastianbergmann/recursion-context", "time": "2015-11-11 19:50:13" }, + { + "name": "sebastian/resource-operations", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/resource-operations.git", + "reference": "ce990bb21759f94aeafd30209e8cfcdfa8bc3f52" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/ce990bb21759f94aeafd30209e8cfcdfa8bc3f52", + "reference": "ce990bb21759f94aeafd30209e8cfcdfa8bc3f52", + "shasum": "" + }, + "require": { + "php": ">=5.6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides a list of PHP built-in functions that operate on resources", + "homepage": "https://www.github.com/sebastianbergmann/resource-operations", + "time": "2015-07-28 20:34:47" + }, { "name": "sebastian/version", - "version": "1.0.6", + "version": "2.0.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/version.git", - "reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6" + "reference": "c829badbd8fdf16a0bad8aa7fa7971c029f1b9c5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/58b3a85e7999757d6ad81c787a1fbf5ff6c628c6", - "reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/c829badbd8fdf16a0bad8aa7fa7971c029f1b9c5", + "reference": "c829badbd8fdf16a0bad8aa7fa7971c029f1b9c5", "shasum": "" }, + "require": { + "php": ">=5.6" + }, "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, "autoload": { "classmap": [ "src/" @@ -1296,23 +1667,24 @@ ], "description": "Library that helps with managing the version number of Git-hosted PHP projects", "homepage": "https://github.com/sebastianbergmann/version", - "time": "2015-06-21 13:59:46" + "time": "2016-02-04 12:56:52" }, { "name": "squizlabs/php_codesniffer", - "version": "2.5.1", + "version": "2.6.0", "source": { "type": "git", "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "6731851d6aaf1d0d6c58feff1065227b7fda3ba8" + "reference": "1bcdf03b068a530ac1962ce671dead356eeba43b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/6731851d6aaf1d0d6c58feff1065227b7fda3ba8", - "reference": "6731851d6aaf1d0d6c58feff1065227b7fda3ba8", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/1bcdf03b068a530ac1962ce671dead356eeba43b", + "reference": "1bcdf03b068a530ac1962ce671dead356eeba43b", "shasum": "" }, "require": { + "ext-simplexml": "*", "ext-tokenizer": "*", "ext-xmlwriter": "*", "php": ">=5.1.2" @@ -1373,26 +1745,29 @@ "phpcs", "standards" ], - "time": "2016-01-19 23:39:10" + "time": "2016-04-03 22:58:34" }, { "name": "symfony/config", - "version": "v3.0.2", + "version": "v3.0.6", "source": { "type": "git", "url": "https://github.com/symfony/config.git", - "reference": "8c83ff9a2ffbed1e606bc816db11ddc2385a16ee" + "reference": "24f155da1ff180df8e15e34a8f6e2f8a0eadefa8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/config/zipball/8c83ff9a2ffbed1e606bc816db11ddc2385a16ee", - "reference": "8c83ff9a2ffbed1e606bc816db11ddc2385a16ee", + "url": "https://api.github.com/repos/symfony/config/zipball/24f155da1ff180df8e15e34a8f6e2f8a0eadefa8", + "reference": "24f155da1ff180df8e15e34a8f6e2f8a0eadefa8", "shasum": "" }, "require": { "php": ">=5.5.9", "symfony/filesystem": "~2.8|~3.0" }, + "suggest": { + "symfony/yaml": "To use the yaml reference dumper" + }, "type": "library", "extra": { "branch-alias": { @@ -1423,20 +1798,20 @@ ], "description": "Symfony Config Component", "homepage": "https://symfony.com", - "time": "2016-01-21 09:38:31" + "time": "2016-04-20 18:53:54" }, { "name": "symfony/console", - "version": "v3.0.2", + "version": "v3.0.6", "source": { "type": "git", "url": "https://github.com/symfony/console.git", - "reference": "5a02eaadaa285e2bb727eb6bbdfb8201fcd971b0" + "reference": "34a214710e0714b6efcf40ba3cd1e31373a97820" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/console/zipball/5a02eaadaa285e2bb727eb6bbdfb8201fcd971b0", - "reference": "5a02eaadaa285e2bb727eb6bbdfb8201fcd971b0", + "url": "https://api.github.com/repos/symfony/console/zipball/34a214710e0714b6efcf40ba3cd1e31373a97820", + "reference": "34a214710e0714b6efcf40ba3cd1e31373a97820", "shasum": "" }, "require": { @@ -1483,20 +1858,20 @@ ], "description": "Symfony Console Component", "homepage": "https://symfony.com", - "time": "2016-02-02 13:44:19" + "time": "2016-04-28 09:48:42" }, { "name": "symfony/event-dispatcher", - "version": "v2.8.2", + "version": "v2.8.6", "source": { "type": "git", "url": "https://github.com/symfony/event-dispatcher.git", - "reference": "ee278f7c851533e58ca307f66305ccb9188aceda" + "reference": "a158f13992a3147d466af7a23b564ac719a4ddd8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/ee278f7c851533e58ca307f66305ccb9188aceda", - "reference": "ee278f7c851533e58ca307f66305ccb9188aceda", + "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/a158f13992a3147d466af7a23b564ac719a4ddd8", + "reference": "a158f13992a3147d466af7a23b564ac719a4ddd8", "shasum": "" }, "require": { @@ -1543,20 +1918,20 @@ ], "description": "Symfony EventDispatcher Component", "homepage": "https://symfony.com", - "time": "2016-01-13 10:28:07" + "time": "2016-05-03 18:59:18" }, { "name": "symfony/filesystem", - "version": "v3.0.2", + "version": "v3.0.6", "source": { "type": "git", "url": "https://github.com/symfony/filesystem.git", - "reference": "064ac12afd2ceb8a2c1bfb7bed8e931c6dd1997f" + "reference": "74fec3511b62cb934b64bce1d96f06fffa4beafd" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/filesystem/zipball/064ac12afd2ceb8a2c1bfb7bed8e931c6dd1997f", - "reference": "064ac12afd2ceb8a2c1bfb7bed8e931c6dd1997f", + "url": "https://api.github.com/repos/symfony/filesystem/zipball/74fec3511b62cb934b64bce1d96f06fffa4beafd", + "reference": "74fec3511b62cb934b64bce1d96f06fffa4beafd", "shasum": "" }, "require": { @@ -1592,20 +1967,20 @@ ], "description": "Symfony Filesystem Component", "homepage": "https://symfony.com", - "time": "2016-01-27 11:34:55" + "time": "2016-04-12 18:09:53" }, { "name": "symfony/polyfill-mbstring", - "version": "v1.1.0", + "version": "v1.2.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "1289d16209491b584839022f29257ad859b8532d" + "reference": "dff51f72b0706335131b00a7f49606168c582594" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/1289d16209491b584839022f29257ad859b8532d", - "reference": "1289d16209491b584839022f29257ad859b8532d", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/dff51f72b0706335131b00a7f49606168c582594", + "reference": "dff51f72b0706335131b00a7f49606168c582594", "shasum": "" }, "require": { @@ -1617,7 +1992,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.1-dev" + "dev-master": "1.2-dev" } }, "autoload": { @@ -1651,20 +2026,20 @@ "portable", "shim" ], - "time": "2016-01-20 09:13:37" + "time": "2016-05-18 14:26:46" }, { "name": "symfony/stopwatch", - "version": "v3.0.2", + "version": "v3.0.6", "source": { "type": "git", "url": "https://github.com/symfony/stopwatch.git", - "reference": "4a204804952ff267ace88cf499e0b4bb302a475e" + "reference": "6015187088421e9499d8f8316bdb396f8b806c06" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/stopwatch/zipball/4a204804952ff267ace88cf499e0b4bb302a475e", - "reference": "4a204804952ff267ace88cf499e0b4bb302a475e", + "url": "https://api.github.com/repos/symfony/stopwatch/zipball/6015187088421e9499d8f8316bdb396f8b806c06", + "reference": "6015187088421e9499d8f8316bdb396f8b806c06", "shasum": "" }, "require": { @@ -1700,20 +2075,20 @@ ], "description": "Symfony Stopwatch Component", "homepage": "https://symfony.com", - "time": "2016-01-03 15:35:16" + "time": "2016-03-04 07:55:57" }, { "name": "symfony/yaml", - "version": "v3.0.2", + "version": "v3.0.6", "source": { "type": "git", "url": "https://github.com/symfony/yaml.git", - "reference": "3cf0709d7fe936e97bee9e954382e449003f1d9a" + "reference": "0047c8366744a16de7516622c5b7355336afae96" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/yaml/zipball/3cf0709d7fe936e97bee9e954382e449003f1d9a", - "reference": "3cf0709d7fe936e97bee9e954382e449003f1d9a", + "url": "https://api.github.com/repos/symfony/yaml/zipball/0047c8366744a16de7516622c5b7355336afae96", + "reference": "0047c8366744a16de7516622c5b7355336afae96", "shasum": "" }, "require": { @@ -1749,7 +2124,7 @@ ], "description": "Symfony Yaml Component", "homepage": "https://symfony.com", - "time": "2016-02-02 13:44:19" + "time": "2016-03-04 07:55:57" } ], "aliases": [], @@ -1758,7 +2133,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": "~5.5 || ~7.0" + "php": "~5.6 || ~7.0" }, "platform-dev": [] } diff --git a/src/Authorization.php b/src/Authorization.php index e384fb1..b907667 100644 --- a/src/Authorization.php +++ b/src/Authorization.php @@ -1,13 +1,18 @@ server = $server; - } + private $scopes; /** - * Verify request contains valid access token. + * Container for token. * - * @param array $scopes Scopes required for authorization. $scopes can be given as an array of arrays. OR logic will - * use with each grouping. Example: Given ['superUser', ['basicUser', 'aPermission']], the - * request will be verified if the request token has 'superUser' scope OR 'basicUser' and - * 'aPermission' as its scope. - * @return void + * @var ArrayAccess */ - public function call(array $scopes = [null]) - { - if (!$this->verify($scopes)) { - MessageBridge::mapResponse($this->server->getResponse(), $this->app->response()); - $this->app->stop(); - } //@codeCoverageIgnore since stop() throws - - $this->app->token = $this->server->getResourceController()->getToken(); + private $container; - if ($this->next !== null) { - $this->next->call(); - } + /** + * Create a new instance of the Authroization middleware. + * + * @param OAuth2\Server $server The configured OAuth2 server. + * @param ArrayAccess $container A container object in which to store the token from the request. + * @param array $scopes Scopes required for authorization. $scopes can be given as an array of arrays. OR + * logic will use with each grouping. Example: + * Given ['superUser', ['basicUser', 'aPermission']], the request will be verified + * if the request token has 'superUser' scope OR 'basicUser' and 'aPermission' as + * its scope. + */ + public function __construct(OAuth2\Server $server, ArrayAccess $container, array $scopes = []) + { + $this->server = $server; + $this->container = $container; + $this->scopes = $scopes; } /** - * Helper method to verify a resource request, allowing return early on success cases + * Execute this middleware. * - * @param array $scopes Scopes required for authorization. + * @param ServerRequestInterface $request The PSR7 request. + * @param ResponseInterface $response The PSR7 response. + * @param callable $next The Next middleware. * - * @return boolean True if the request is verified, otherwise false + * @return Slim\Http\Response */ - private function verify(array $scopes = [null]) + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { + $oauth2Request = RequestBridge::toOAuth2($request); + + $scopes = $this->scopes; + if (empty($scopes)) { + $scopes = [null]; //use at least 1 null scope + } + foreach ($scopes as $scope) { if (is_array($scope)) { $scope = implode(' ', $scope); } - $oauth2Request = MessageBridge::newOauth2Request($this->app->request()); if ($this->server->verifyResourceRequest($oauth2Request, null, $scope)) { - return true; + $this->container['token'] = $this->server->getResourceController()->getToken(); + return $next($request, $response); } } - return false; - } - - /** - * Allows this middleware to be used as a callable. - * - * @return void - */ - public function __invoke() - { - $this->call(); + return ResponseBridge::fromOAuth2($this->server->getResponse()); } /** @@ -87,13 +90,12 @@ public function __invoke() * * @param array $scopes Scopes require for authorization. * - * @return callable + * @return Authorization */ public function withRequiredScope(array $scopes) { - $auth = $this; - return function () use ($auth, $scopes) { - return $auth->call($scopes); - }; + $clone = clone $this; + $clone->scopes = $scopes; + return $clone; } } diff --git a/src/MiddlewareInterface.php b/src/MiddlewareInterface.php new file mode 100644 index 0000000..c8f6a58 --- /dev/null +++ b/src/MiddlewareInterface.php @@ -0,0 +1,23 @@ + [ 'atokenvalue' => [ @@ -42,7 +41,7 @@ public function call() ] ); - $server = new \OAuth2\Server( + $server = new OAuth2\Server( $storage, [ 'enforce_state' => true, @@ -51,23 +50,22 @@ public function call() ] ); - \Slim\Environment::mock( - [ - 'CONTENT_TYPE' => 'application/json', - 'PATH_INFO' => '/foo', - ] - ); + $uri = Http\Uri::createFromString('localhost:888/foos'); + $headers = new Http\Headers(); + $headers->set('Authorization', 'Bearer atokenvalue'); + + $request = new Http\Request('PATCH', $uri, $headers, [], [], new Http\RequestBody(), []); + + $container = new ArrayObject(); - $slim = self::getSlimInstance(); - $slim->get('/foo', self::$emptyFunction); - $slim->add(new Authorization($server)); + $middleware = new Authorization($server, $container); - $env = \Slim\Environment::getInstance(); - $slim->request = new \Slim\Http\Request($env); - $slim->request->headers->set('Authorization', 'Bearer atokenvalue'); - $slim->response = new \Slim\Http\Response(); + $next = function ($request, $response) { + return $response; + }; + + $middleware($request, new Http\Response(), $next); - $slim->run(); $this->assertSame( [ 'access_token' => 'atokenvalue', @@ -76,23 +74,21 @@ public function call() 'expires' => 99999999900, 'scope' => null, ], - $slim->token + $container['token'] ); } /** - * Verify behavior of call with expired access token + * Verify behavior of __invoke() with expired access token. * * @test - * @covers ::call + * @covers ::__invoke * * @return void - * - * @throws \Exception Thrown only if /foo route is executed. */ - public function callExpiredToken() + public function invokeExpiredToken() { - $storage = new \OAuth2\Storage\Memory( + $storage = new Storage\Memory( [ 'access_tokens' => [ 'atokenvalue' => [ @@ -106,7 +102,7 @@ public function callExpiredToken() ] ); - $server = new \OAuth2\Server( + $server = new OAuth2\Server( $storage, [ 'enforce_state' => true, @@ -115,35 +111,24 @@ public function callExpiredToken() ] ); - \Slim\Environment::mock( - [ - 'CONTENT_TYPE' => 'application/json', - 'PATH_INFO' => '/foo', - ] - ); + $uri = Http\Uri::createFromString('localhost:888/foos'); + $headers = new Http\Headers(); + $headers->set('Authorization', 'Bearer atokenvalue'); + + $request = new Http\Request('PATCH', $uri, $headers, [], [], new Http\RequestBody(), []); - $slim = self::getSlimInstance(); - $slim->get('/foo', function () { + $middleware = new Authorization($server, new ArrayObject); + + $next = function () { throw new \Exception('This will not get executed'); - }); - $slim->add(new Authorization($server)); - - $env = \Slim\Environment::getInstance(); - $slim->request = new \Slim\Http\Request($env); - $slim->request->headers->set('Authorization', 'Bearer atokenvalue'); - $slim->response = new \Slim\Http\Response(); - - try { - $slim->run(); - } catch (\Exception $e) { - //ignore this error - $this->assertInstanceOf('\Slim\Exception\Stop', $e); - } - - $this->assertSame(401, $slim->response->status()); + }; + + $response = $middleware($request, new Http\Response(), $next); + + $this->assertSame(401, $response->getStatusCode()); $this->assertSame( '{"error":"expired_token","error_description":"The access token provided has expired"}', - $slim->response->body() + (string)$response->getBody() ); } @@ -151,14 +136,14 @@ public function callExpiredToken() * Verify basic behaviour of withRequiredScope(). * * @test - * @covers ::call + * @covers ::__invoke * @covers ::withRequiredScope * * @return void */ public function withRequiredScope() { - $storage = new \OAuth2\Storage\Memory( + $storage = new Storage\Memory( [ 'access_tokens' => [ 'atokenvalue' => [ @@ -172,7 +157,7 @@ public function withRequiredScope() ] ); - $server = new \OAuth2\Server( + $server = new OAuth2\Server( $storage, [ 'enforce_state' => true, @@ -181,26 +166,23 @@ public function withRequiredScope() ] ); - \Slim\Environment::mock( - [ - 'CONTENT_TYPE' => 'application/json', - 'PATH_INFO' => '/foo', - ] - ); + $uri = Http\Uri::createFromString('localhost:888/foos'); + $headers = new Http\Headers(); + $headers->set('Authorization', 'Bearer atokenvalue'); - $slim = self::getSlimInstance(); - $authorization = new Authorization($server); - $authorization->setApplication($slim); - $slim->get('/foo', $authorization->withRequiredScope(['allowFoo']), self::$emptyFunction); + $request = new Http\Request('PATCH', $uri, $headers, [], [], new Http\RequestBody(), []); - $env = \Slim\Environment::getInstance(); - $slim->request = new \Slim\Http\Request($env); - $slim->request->headers->set('Authorization', 'Bearer atokenvalue'); - $slim->response = new \Slim\Http\Response(); + $container = new ArrayObject(); + + $middleware = new Authorization($server, $container); + + $next = function ($request, $response) { + return $response; + }; - $slim->run(); + $response = $middleware->withRequiredScope(['allowFoo'])->__invoke($request, new Http\Response(), $next); - $this->assertSame(200, $slim->response->status()); + $this->assertSame(200, $response->getStatusCode()); $this->assertSame( [ 'access_token' => 'atokenvalue', @@ -209,7 +191,7 @@ public function withRequiredScope() 'expires' => 99999999900, 'scope' => 'allowFoo anotherScope', ], - $slim->token + $container['token'] ); } @@ -217,14 +199,14 @@ public function withRequiredScope() * Verify behaviour of withRequiredScope() with insufficient scope. * * @test - * @covers ::call + * @covers ::__invoke * @covers ::withRequiredScope * * @return void */ public function withRequiredScopeInsufficientScope() { - $storage = new \OAuth2\Storage\Memory( + $storage = new Storage\Memory( [ 'access_tokens' => [ 'atokenvalue' => [ @@ -238,7 +220,7 @@ public function withRequiredScopeInsufficientScope() ] ); - $server = new \OAuth2\Server( + $server = new OAuth2\Server( $storage, [ 'enforce_state' => true, @@ -247,58 +229,41 @@ public function withRequiredScopeInsufficientScope() ] ); - \Slim\Environment::mock( - [ - 'CONTENT_TYPE' => 'application/json', - 'PATH_INFO' => '/foo', - ] - ); + $uri = Http\Uri::createFromString('localhost:888/foos'); + $headers = new Http\Headers(); + $headers->set('Authorization', 'Bearer atokenvalue'); - $slim = self::getSlimInstance(); - $authorization = new Authorization($server); - $authorization->setApplication($slim); - $slim->get('/foo', $authorization->withRequiredScope(['allowFoo']), self::$emptyFunction); + $request = new Http\Request('PATCH', $uri, $headers, [], [], new Http\RequestBody(), []); - $env = \Slim\Environment::getInstance(); - $slim->request = new \Slim\Http\Request($env); - $slim->request->headers->set('Authorization', 'Bearer atokenvalue'); - $slim->response = new \Slim\Http\Response(); + $middleware = new Authorization($server, new ArrayObject(), ['allowFoo']); - $slim->run(); + $next = function ($request, $response) { + throw new \Exception('This will not get executed'); + }; - $this->assertSame(403, $slim->response->status()); + $response = $middleware($request, new Http\Response(), $next); + + $this->assertSame(403, $response->getStatusCode()); $this->assertSame( '{"error":"insufficient_scope","error_description":"The request requires higher privileges than provided ' . 'by the access token"}', - $slim->response->body() + (string)$response->getBody() ); } /** - * Verify Authorization is invokeable. + * Verify behavior of __invoke() without access token. * * @test * @covers ::__invoke * * @return void */ - public function invoke() + public function invokeNoTokenProvided() { - $storage = new \OAuth2\Storage\Memory( - [ - 'access_tokens' => [ - 'atokenvalue' => [ - 'access_token' => 'atokenvalue', - 'client_id' => 'a client id', - 'user_id' => 'a user id', - 'expires' => 99999999900, - 'scope' => null, - ], - ], - ] - ); + $storage = new Storage\Memory([]); - $server = new \OAuth2\Server( + $server = new OAuth2\Server( $storage, [ 'enforce_state' => true, @@ -307,41 +272,46 @@ public function invoke() ] ); - \Slim\Environment::mock( - [ - 'CONTENT_TYPE' => 'application/json', - 'PATH_INFO' => '/foo', - ] - ); + $uri = Http\Uri::createFromString('localhost:888/foos'); - $slim = self::getSlimInstance(); - $authorization = new Authorization($server); - $authorization->setApplication($slim); - $slim->get('/foo', $authorization, self::$emptyFunction); + $request = new Http\Request('PATCH', $uri, new Http\Headers(), [], [], new Http\RequestBody(), []); - $env = \Slim\Environment::getInstance(); - $slim->request = new \Slim\Http\Request($env); - $slim->request->headers->set('Authorization', 'Bearer atokenvalue'); - $slim->response = new \Slim\Http\Response(); + $middleware = new Authorization($server, new ArrayObject()); - $slim->run(); + $next = function ($request, $response) { + throw new \Exception('This will not get executed'); + }; + + $response = $middleware($request, new Http\Response(), $next); - $this->assertSame(200, $slim->response->status()); + $this->assertSame(401, $response->getStatusCode()); } /** - * Verify behavior of call without access token + * Verify __invoke() with scopes using OR logic * * @test - * @covers ::call + * @covers ::__invoke * * @return void */ - public function callNoTokenProvided() + public function invokeWithEitherScope() { - $storage = new \OAuth2\Storage\Memory([]); + $storage = new Storage\Memory( + [ + 'access_tokens' => [ + 'atokenvalue' => [ + 'access_token' => 'atokenvalue', + 'client_id' => 'a client id', + 'user_id' => 'a user id', + 'expires' => 99999999900, + 'scope' => 'basicUser withPermission anExtraScope', + ], + ], + ] + ); - $server = new \OAuth2\Server( + $server = new OAuth2\Server( $storage, [ 'enforce_state' => true, @@ -350,44 +320,46 @@ public function callNoTokenProvided() ] ); - \Slim\Environment::mock( - [ - 'CONTENT_TYPE' => 'application/json', - 'PATH_INFO' => '/foo', - ] - ); + $uri = Http\Uri::createFromString('localhost:888/foos'); + $headers = new Http\Headers(); + $headers->set('Authorization', 'Bearer atokenvalue'); + + $request = new Http\Request('PATCH', $uri, $headers, [], [], new Http\RequestBody(), []); - $slim = self::getSlimInstance(); - $authorization = new Authorization($server); - $authorization->setApplication($slim); - $slim->get('/foo', $authorization, function () { - echo json_encode(['success' => true]); - }); + $container = new ArrayObject(); - $env = \Slim\Environment::getInstance(); - $slim->request = new \Slim\Http\Request($env); - $slim->response = new \Slim\Http\Response(); + $middleware = new Authorization($server, $container, ['superUser', ['basicUser', 'withPermission']]); - try { - $slim->run(); - } catch (\Exception $e) { - $this->assertInstanceOf('\Slim\Exception\Stop', $e); - } + $next = function ($request, $response) { + return $response; + }; + + $response = $middleware($request, new Http\Response(), $next); - $this->assertSame(401, $slim->response->status()); + $this->assertSame(200, $response->getStatusCode()); + $this->assertSame( + [ + 'access_token' => 'atokenvalue', + 'client_id' => 'a client id', + 'user_id' => 'a user id', + 'expires' => 99999999900, + 'scope' => 'basicUser withPermission anExtraScope', + ], + $container['token'] + ); } /** - * Verify call with scopes using OR logic + * Verify behavior of the middleware with empty scope * * @test - * @covers ::call + * @covers ::__invoke * * @return void */ - public function callWithEitherScope() + public function invokeWithEmptyScope() { - $storage = new \OAuth2\Storage\Memory( + $storage = new Storage\Memory( [ 'access_tokens' => [ 'atokenvalue' => [ @@ -395,93 +367,47 @@ public function callWithEitherScope() 'client_id' => 'a client id', 'user_id' => 'a user id', 'expires' => 99999999900, - 'scope' => 'basicUser withPermission anExtraScope', + 'scope' => null, ], ], ] ); - $server = new \OAuth2\Server( + $server = new OAuth2\Server( $storage, [ - 'enforce_state' => true, - 'allow_implicit' => false, + 'enforce_state' => true, + 'allow_implicit' => false, 'access_lifetime' => 3600 ] ); - \Slim\Environment::mock( - [ - 'CONTENT_TYPE' => 'application/json', - 'PATH_INFO' => '/foo', - ] - ); + $uri = Http\Uri::createFromString('localhost:888/foos'); + $headers = new Http\Headers(); + $headers->set('Authorization', 'Bearer atokenvalue'); - $slim = self::getSlimInstance(); - $authorization = new Authorization($server); - $authorization->setApplication($slim); - $slim->get( - '/foo', - $authorization->withRequiredScope(['superUser', ['basicUser', 'withPermission']]), - self::$emptyFunction - ); + $request = new Http\Request('PATCH', $uri, $headers, [], [], new Http\RequestBody(), []); + + $container = new ArrayObject(); - $env = \Slim\Environment::getInstance(); - $slim->request = new \Slim\Http\Request($env); - $slim->request->headers->set('Authorization', 'Bearer atokenvalue'); - $slim->response = new \Slim\Http\Response(); + $middleware = new Authorization($server, $container, []); - $slim->run(); + $next = function ($request, $response) { + return $response; + }; + + $middleware($request, new Http\Response(), $next); - $this->assertSame(200, $slim->response->status()); $this->assertSame( [ 'access_token' => 'atokenvalue', 'client_id' => 'a client id', 'user_id' => 'a user id', 'expires' => 99999999900, - 'scope' => 'basicUser withPermission anExtraScope', + 'scope' => null, ], - $slim->token + $container['token'] ); } - /** - * Helper method to return a new instance of \Slim\Slim. - * - * @return \Slim\Slim - */ - private static function getSlimInstance() - { - return new \Slim\Slim( - [ - 'version' => '0.0.0', - 'debug' => false, - 'mode'=> 'testing' - ] - ); - } - - /** - * Prepare each test. - * - * @return void - */ - protected function setUp() - { - //empty function to use within tests - self::$emptyFunction = function () { - }; - ob_start(); - } - - /** - * Perform cleanup after each test. - * - * @return void - */ - protected function tearDown() - { - ob_end_clean(); - } }