From bd13ac6fb88e5443b99be1fcd84647f75e2ac273 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Wed, 30 Dec 2015 22:21:14 -0600 Subject: [PATCH 01/13] Remove ./bin/version-check. Fixes #26. --- bin/version-check | 26 -------------------------- composer.json | 3 --- 2 files changed, 29 deletions(-) delete mode 100755 bin/version-check diff --git a/bin/version-check b/bin/version-check deleted file mode 100755 index 4a58a50..0000000 --- a/bin/version-check +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env php -=5.3.7", "jeremykendall/password-validator": "3.*", From 2c8f172a3bd6c459d090c94e6bd145527933332b Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Wed, 30 Dec 2015 22:33:00 -0600 Subject: [PATCH 02/13] Bump PHP minimum version requirement. Fixes #29. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1920e8b..dbffc51 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "zf2" ], "require": { - "php": ">=5.3.7", + "php": ">=5.5.0", "jeremykendall/password-validator": "3.*", "wp-cli/php-cli-tools": "~0.10", "zendframework/zend-authentication": "2.*", From ed4d6b94df7a9948b07da940d9395a939e9978c4 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Wed, 30 Dec 2015 22:46:56 -0600 Subject: [PATCH 03/13] Update PHP versions in .travis.yml. Related to #29. --- .travis.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7ad8e20..8be5dd9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,11 @@ language: php php: - - 5.3 - - 5.4 - - 5.5 - - 5.6 + - '5.5' + - '5.6' + - '7.0' + - hhvm + - nightly before_script: - composer self-update From 72bb153156fbd7df747f98f6b68824746e66f7da Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Sat, 2 Jan 2016 17:40:12 -0600 Subject: [PATCH 04/13] Remove composer self-update from .travis.yml --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8be5dd9..6f6986c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,6 @@ php: - nightly before_script: - - composer self-update - composer install --prefer-source script: phpunit -c travis.xml From ef66012ff3918b51e93dea7eea17a0f297a9f0e3 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Sat, 2 Jan 2016 17:45:14 -0600 Subject: [PATCH 05/13] Remove unnecessary deps, update travis.xml. --- composer.json | 2 -- travis.xml | 29 ----------------------------- 2 files changed, 31 deletions(-) diff --git a/composer.json b/composer.json index dbffc51..b5d1f74 100644 --- a/composer.json +++ b/composer.json @@ -27,13 +27,11 @@ "require": { "php": ">=5.5.0", "jeremykendall/password-validator": "3.*", - "wp-cli/php-cli-tools": "~0.10", "zendframework/zend-authentication": "2.*", "zendframework/zend-permissions-acl": "2.*", "zendframework/zend-session": "2.*" }, "require-dev": { - "league/phpunit-coverage-listener": "~1.1", "phpunit/phpunit": "4.*", "slim/slim": "^2.4.2", "jeremykendall/debug-die": "0.0.1.*" diff --git a/travis.xml b/travis.xml index 9932b15..f5347b1 100644 --- a/travis.xml +++ b/travis.xml @@ -8,33 +8,4 @@ ./src/JeremyKendall/Slim/Auth - - - - - - - - - - - - - - - JeremyKendall\Slim\Auth - - - kNi5xmfyfKFIidYMQXXSpY3NFMSoDJARO - - - https://coveralls.io/api/v1/jobs - - - /tmp/jeremykendall/slim/auth - - - - - From 9e8f52010ee618e667d3449fddbc2a22e647db73 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Sat, 2 Jan 2016 17:48:42 -0600 Subject: [PATCH 06/13] Update php-cs-fixer config, CS fixes. --- .php_cs | 4 +++- src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php | 1 - src/JeremyKendall/Slim/Auth/Authenticator.php | 1 - src/JeremyKendall/Slim/Auth/Bootstrap.php | 1 - src/JeremyKendall/Slim/Auth/Exception/AuthException.php | 1 - .../Slim/Auth/Exception/HttpForbiddenException.php | 1 - .../Slim/Auth/Exception/HttpUnauthorizedException.php | 1 - src/JeremyKendall/Slim/Auth/IdentityInterface.php | 1 - src/JeremyKendall/Slim/Auth/Middleware/Authorization.php | 1 - 9 files changed, 3 insertions(+), 9 deletions(-) diff --git a/.php_cs b/.php_cs index e59fbd4..f605ef9 100644 --- a/.php_cs +++ b/.php_cs @@ -3,14 +3,16 @@ require_once './vendor/autoload.php'; $finder = \Symfony\CS\Finder\DefaultFinder::create() - ->in('bin/') ->in('src/') ->in('tests/'); return \Symfony\CS\Config\Config::create() ->setUsingCache(true) ->fixers([ + '-single_blank_line_before_namespace', + '-pre_increment', '-concat_without_spaces', + '-phpdoc_inline_tag', 'concat_with_spaces', 'ordered_use', ]) diff --git a/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php b/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php index ef18267..2e4f1c7 100644 --- a/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php +++ b/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php @@ -8,7 +8,6 @@ * @copyright Copyright (c) 2015 Jeremy Kendall (http://about.me/jeremykendall) * @license http://github.com/jeremykendall/slim-auth/blob/master/LICENSE MIT */ - namespace JeremyKendall\Slim\Auth\Adapter\Db; use JeremyKendall\Password\PasswordValidatorInterface; diff --git a/src/JeremyKendall/Slim/Auth/Authenticator.php b/src/JeremyKendall/Slim/Auth/Authenticator.php index 5804070..8ddf060 100644 --- a/src/JeremyKendall/Slim/Auth/Authenticator.php +++ b/src/JeremyKendall/Slim/Auth/Authenticator.php @@ -8,7 +8,6 @@ * @copyright Copyright (c) 2015 Jeremy Kendall (http://about.me/jeremykendall) * @license http://github.com/jeremykendall/slim-auth/blob/master/LICENSE MIT */ - namespace JeremyKendall\Slim\Auth; use Zend\Authentication\AuthenticationServiceInterface; diff --git a/src/JeremyKendall/Slim/Auth/Bootstrap.php b/src/JeremyKendall/Slim/Auth/Bootstrap.php index 93ecbcc..fdcec73 100644 --- a/src/JeremyKendall/Slim/Auth/Bootstrap.php +++ b/src/JeremyKendall/Slim/Auth/Bootstrap.php @@ -8,7 +8,6 @@ * @copyright Copyright (c) 2015 Jeremy Kendall (http://about.me/jeremykendall) * @license http://github.com/jeremykendall/slim-auth/blob/master/LICENSE MIT */ - namespace JeremyKendall\Slim\Auth; use JeremyKendall\Slim\Auth\Middleware\Authorization as AuthorizationMiddleware; diff --git a/src/JeremyKendall/Slim/Auth/Exception/AuthException.php b/src/JeremyKendall/Slim/Auth/Exception/AuthException.php index 902a87b..7a89d86 100644 --- a/src/JeremyKendall/Slim/Auth/Exception/AuthException.php +++ b/src/JeremyKendall/Slim/Auth/Exception/AuthException.php @@ -8,7 +8,6 @@ * @copyright Copyright (c) 2015 Jeremy Kendall (http://about.me/jeremykendall) * @license http://github.com/jeremykendall/slim-auth/blob/master/LICENSE MIT */ - namespace JeremyKendall\Slim\Auth\Exception; /** diff --git a/src/JeremyKendall/Slim/Auth/Exception/HttpForbiddenException.php b/src/JeremyKendall/Slim/Auth/Exception/HttpForbiddenException.php index 19f8a0e..934d2e2 100644 --- a/src/JeremyKendall/Slim/Auth/Exception/HttpForbiddenException.php +++ b/src/JeremyKendall/Slim/Auth/Exception/HttpForbiddenException.php @@ -8,7 +8,6 @@ * @copyright Copyright (c) 2015 Jeremy Kendall (http://about.me/jeremykendall) * @license http://github.com/jeremykendall/slim-auth/blob/master/LICENSE MIT */ - namespace JeremyKendall\Slim\Auth\Exception; /** diff --git a/src/JeremyKendall/Slim/Auth/Exception/HttpUnauthorizedException.php b/src/JeremyKendall/Slim/Auth/Exception/HttpUnauthorizedException.php index d0d7ee3..ec21179 100644 --- a/src/JeremyKendall/Slim/Auth/Exception/HttpUnauthorizedException.php +++ b/src/JeremyKendall/Slim/Auth/Exception/HttpUnauthorizedException.php @@ -8,7 +8,6 @@ * @copyright Copyright (c) 2015 Jeremy Kendall (http://about.me/jeremykendall) * @license http://github.com/jeremykendall/slim-auth/blob/master/LICENSE MIT */ - namespace JeremyKendall\Slim\Auth\Exception; /** diff --git a/src/JeremyKendall/Slim/Auth/IdentityInterface.php b/src/JeremyKendall/Slim/Auth/IdentityInterface.php index fa1271f..06277f2 100644 --- a/src/JeremyKendall/Slim/Auth/IdentityInterface.php +++ b/src/JeremyKendall/Slim/Auth/IdentityInterface.php @@ -8,7 +8,6 @@ * @copyright Copyright (c) 2015 Jeremy Kendall (http://about.me/jeremykendall) * @license http://github.com/jeremykendall/slim-auth/blob/master/LICENSE MIT */ - namespace JeremyKendall\Slim\Auth; /** diff --git a/src/JeremyKendall/Slim/Auth/Middleware/Authorization.php b/src/JeremyKendall/Slim/Auth/Middleware/Authorization.php index 625368b..16bdf4b 100644 --- a/src/JeremyKendall/Slim/Auth/Middleware/Authorization.php +++ b/src/JeremyKendall/Slim/Auth/Middleware/Authorization.php @@ -8,7 +8,6 @@ * @copyright Copyright (c) 2015 Jeremy Kendall (http://about.me/jeremykendall) * @license http://github.com/jeremykendall/slim-auth/blob/master/LICENSE MIT */ - namespace JeremyKendall\Slim\Auth\Middleware; use JeremyKendall\Slim\Auth\Exception\HttpForbiddenException; From 540236613939656615b194c852a6e346c7fa34a1 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Sat, 2 Jan 2016 18:01:29 -0600 Subject: [PATCH 07/13] Convert library to PSR-4 --- composer.json | 7 ++++++- phpunit.xml.dist | 4 ++-- .../Slim/Auth => }/Adapter/Db/PdoAdapter.php | 0 src/{JeremyKendall/Slim/Auth => }/Authenticator.php | 0 src/{JeremyKendall/Slim/Auth => }/Bootstrap.php | 0 .../Slim/Auth => }/Exception/AuthException.php | 0 .../Slim/Auth => }/Exception/HttpForbiddenException.php | 0 .../Slim/Auth => }/Exception/HttpUnauthorizedException.php | 0 src/{JeremyKendall/Slim/Auth => }/IdentityInterface.php | 0 .../Slim/Auth => }/Middleware/Authorization.php | 0 .../Slim/Auth => }/Tests/Adapter/Db/PdoAdapterTest.php | 0 .../Slim/Auth => }/Tests/AuthenticatorTest.php | 0 .../Slim/Auth => }/Tests/BootstrapFunctionalTest.php | 0 .../{JeremyKendall/Slim/Auth => }/Tests/BootstrapTest.php | 0 .../Slim/Auth => }/Tests/Middleware/AuthorizationTest.php | 0 travis.xml | 4 ++-- 16 files changed, 10 insertions(+), 5 deletions(-) rename src/{JeremyKendall/Slim/Auth => }/Adapter/Db/PdoAdapter.php (100%) rename src/{JeremyKendall/Slim/Auth => }/Authenticator.php (100%) rename src/{JeremyKendall/Slim/Auth => }/Bootstrap.php (100%) rename src/{JeremyKendall/Slim/Auth => }/Exception/AuthException.php (100%) rename src/{JeremyKendall/Slim/Auth => }/Exception/HttpForbiddenException.php (100%) rename src/{JeremyKendall/Slim/Auth => }/Exception/HttpUnauthorizedException.php (100%) rename src/{JeremyKendall/Slim/Auth => }/IdentityInterface.php (100%) rename src/{JeremyKendall/Slim/Auth => }/Middleware/Authorization.php (100%) rename tests/{JeremyKendall/Slim/Auth => }/Tests/Adapter/Db/PdoAdapterTest.php (100%) rename tests/{JeremyKendall/Slim/Auth => }/Tests/AuthenticatorTest.php (100%) rename tests/{JeremyKendall/Slim/Auth => }/Tests/BootstrapFunctionalTest.php (100%) rename tests/{JeremyKendall/Slim/Auth => }/Tests/BootstrapTest.php (100%) rename tests/{JeremyKendall/Slim/Auth => }/Tests/Middleware/AuthorizationTest.php (100%) diff --git a/composer.json b/composer.json index b5d1f74..7ffc6d7 100644 --- a/composer.json +++ b/composer.json @@ -37,10 +37,15 @@ "jeremykendall/debug-die": "0.0.1.*" }, "autoload": { - "psr-0": { + "psr-4": { "JeremyKendall\\Slim\\Auth\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "JeremyKendall\\Slim\\Auth\\Tests\\": "tests/" + } + }, "suggest": { "zendframework/zend-db": "For the Zend\\Authentication\\Adapter\\DbTable adapters." } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 7d56383..42f1f23 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,11 +1,11 @@ - tests/JeremyKendall/Slim/Auth/Tests + tests - ./src/JeremyKendall/Slim/Auth + src diff --git a/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php b/src/Adapter/Db/PdoAdapter.php similarity index 100% rename from src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php rename to src/Adapter/Db/PdoAdapter.php diff --git a/src/JeremyKendall/Slim/Auth/Authenticator.php b/src/Authenticator.php similarity index 100% rename from src/JeremyKendall/Slim/Auth/Authenticator.php rename to src/Authenticator.php diff --git a/src/JeremyKendall/Slim/Auth/Bootstrap.php b/src/Bootstrap.php similarity index 100% rename from src/JeremyKendall/Slim/Auth/Bootstrap.php rename to src/Bootstrap.php diff --git a/src/JeremyKendall/Slim/Auth/Exception/AuthException.php b/src/Exception/AuthException.php similarity index 100% rename from src/JeremyKendall/Slim/Auth/Exception/AuthException.php rename to src/Exception/AuthException.php diff --git a/src/JeremyKendall/Slim/Auth/Exception/HttpForbiddenException.php b/src/Exception/HttpForbiddenException.php similarity index 100% rename from src/JeremyKendall/Slim/Auth/Exception/HttpForbiddenException.php rename to src/Exception/HttpForbiddenException.php diff --git a/src/JeremyKendall/Slim/Auth/Exception/HttpUnauthorizedException.php b/src/Exception/HttpUnauthorizedException.php similarity index 100% rename from src/JeremyKendall/Slim/Auth/Exception/HttpUnauthorizedException.php rename to src/Exception/HttpUnauthorizedException.php diff --git a/src/JeremyKendall/Slim/Auth/IdentityInterface.php b/src/IdentityInterface.php similarity index 100% rename from src/JeremyKendall/Slim/Auth/IdentityInterface.php rename to src/IdentityInterface.php diff --git a/src/JeremyKendall/Slim/Auth/Middleware/Authorization.php b/src/Middleware/Authorization.php similarity index 100% rename from src/JeremyKendall/Slim/Auth/Middleware/Authorization.php rename to src/Middleware/Authorization.php diff --git a/tests/JeremyKendall/Slim/Auth/Tests/Adapter/Db/PdoAdapterTest.php b/tests/Tests/Adapter/Db/PdoAdapterTest.php similarity index 100% rename from tests/JeremyKendall/Slim/Auth/Tests/Adapter/Db/PdoAdapterTest.php rename to tests/Tests/Adapter/Db/PdoAdapterTest.php diff --git a/tests/JeremyKendall/Slim/Auth/Tests/AuthenticatorTest.php b/tests/Tests/AuthenticatorTest.php similarity index 100% rename from tests/JeremyKendall/Slim/Auth/Tests/AuthenticatorTest.php rename to tests/Tests/AuthenticatorTest.php diff --git a/tests/JeremyKendall/Slim/Auth/Tests/BootstrapFunctionalTest.php b/tests/Tests/BootstrapFunctionalTest.php similarity index 100% rename from tests/JeremyKendall/Slim/Auth/Tests/BootstrapFunctionalTest.php rename to tests/Tests/BootstrapFunctionalTest.php diff --git a/tests/JeremyKendall/Slim/Auth/Tests/BootstrapTest.php b/tests/Tests/BootstrapTest.php similarity index 100% rename from tests/JeremyKendall/Slim/Auth/Tests/BootstrapTest.php rename to tests/Tests/BootstrapTest.php diff --git a/tests/JeremyKendall/Slim/Auth/Tests/Middleware/AuthorizationTest.php b/tests/Tests/Middleware/AuthorizationTest.php similarity index 100% rename from tests/JeremyKendall/Slim/Auth/Tests/Middleware/AuthorizationTest.php rename to tests/Tests/Middleware/AuthorizationTest.php diff --git a/travis.xml b/travis.xml index f5347b1..bbdd8b7 100644 --- a/travis.xml +++ b/travis.xml @@ -1,11 +1,11 @@ - tests/JeremyKendall/Slim/Auth/Tests + tests - ./src/JeremyKendall/Slim/Auth + src From 776580e6eee5b5d2bdccaae5f7897c4eba0c5719 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Sat, 2 Jan 2016 18:20:07 -0600 Subject: [PATCH 08/13] Fix PSR-4 directory issue with tests. --- .php_cs | 1 + tests/{Tests => }/Adapter/Db/PdoAdapterTest.php | 0 tests/{Tests => }/AuthenticatorTest.php | 0 tests/{Tests => }/BootstrapFunctionalTest.php | 0 tests/{Tests => }/BootstrapTest.php | 0 tests/{Tests => }/Middleware/AuthorizationTest.php | 0 6 files changed, 1 insertion(+) rename tests/{Tests => }/Adapter/Db/PdoAdapterTest.php (100%) rename tests/{Tests => }/AuthenticatorTest.php (100%) rename tests/{Tests => }/BootstrapFunctionalTest.php (100%) rename tests/{Tests => }/BootstrapTest.php (100%) rename tests/{Tests => }/Middleware/AuthorizationTest.php (100%) diff --git a/.php_cs b/.php_cs index f605ef9..15c9cc4 100644 --- a/.php_cs +++ b/.php_cs @@ -9,6 +9,7 @@ $finder = \Symfony\CS\Finder\DefaultFinder::create() return \Symfony\CS\Config\Config::create() ->setUsingCache(true) ->fixers([ + '-psr0', // Because it causes such grief with the Tests namespace in the tests folder '-single_blank_line_before_namespace', '-pre_increment', '-concat_without_spaces', diff --git a/tests/Tests/Adapter/Db/PdoAdapterTest.php b/tests/Adapter/Db/PdoAdapterTest.php similarity index 100% rename from tests/Tests/Adapter/Db/PdoAdapterTest.php rename to tests/Adapter/Db/PdoAdapterTest.php diff --git a/tests/Tests/AuthenticatorTest.php b/tests/AuthenticatorTest.php similarity index 100% rename from tests/Tests/AuthenticatorTest.php rename to tests/AuthenticatorTest.php diff --git a/tests/Tests/BootstrapFunctionalTest.php b/tests/BootstrapFunctionalTest.php similarity index 100% rename from tests/Tests/BootstrapFunctionalTest.php rename to tests/BootstrapFunctionalTest.php diff --git a/tests/Tests/BootstrapTest.php b/tests/BootstrapTest.php similarity index 100% rename from tests/Tests/BootstrapTest.php rename to tests/BootstrapTest.php diff --git a/tests/Tests/Middleware/AuthorizationTest.php b/tests/Middleware/AuthorizationTest.php similarity index 100% rename from tests/Tests/Middleware/AuthorizationTest.php rename to tests/Middleware/AuthorizationTest.php From 75d9b3aed124e7e72e03a20fb6fc07fe59b6f754 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Sat, 2 Jan 2016 22:07:57 -0600 Subject: [PATCH 09/13] Delete unused Exceptions and Slim 2.x Bootstrap. --- src/Bootstrap.php | 163 -------------------- src/Exception/AuthException.php | 18 --- src/Exception/HttpForbiddenException.php | 28 ---- src/Exception/HttpUnauthorizedException.php | 28 ---- tests/BootstrapFunctionalTest.php | 39 ----- tests/BootstrapTest.php | 107 ------------- 6 files changed, 383 deletions(-) delete mode 100644 src/Bootstrap.php delete mode 100644 src/Exception/AuthException.php delete mode 100644 src/Exception/HttpForbiddenException.php delete mode 100644 src/Exception/HttpUnauthorizedException.php delete mode 100644 tests/BootstrapFunctionalTest.php delete mode 100644 tests/BootstrapTest.php diff --git a/src/Bootstrap.php b/src/Bootstrap.php deleted file mode 100644 index fdcec73..0000000 --- a/src/Bootstrap.php +++ /dev/null @@ -1,163 +0,0 @@ -app = $app; - $this->adapter = $adapter; - $this->acl = $acl; - } - - /** - * Wires up Slim Auth defaults. - * - * Creates the Zend AuthenticationService, adds the AuthenticationService - * and the Authenticator to the Slim resource locator, and adds the - * AuthorizationMiddleware to the $app instance. - */ - public function bootstrap() - { - $storage = $this->getStorage(); - $adapter = $this->getAdapter(); - - $this->app->auth = function () use ($storage, $adapter) { - return new AuthenticationService($storage, $adapter); - }; - - $app = $this->app; - - $this->app->authenticator = function () use ($app) { - return new Authenticator($app->auth); - }; - - // Add the custom middleware - $this->app->add($this->getAuthMiddleware()); - } - - /** - * Get acl. - * - * @return acl - */ - public function getAcl() - { - return $this->acl; - } - - /** - * Gets storage. - * - * Returns instance of Zend\Authentication\Storage\Session if storage is null - * - * @return StorageInterface AuthenticationService storage - */ - public function getStorage() - { - if (is_null($this->storage)) { - $this->storage = new SessionStorage('slim_auth'); - } - - return $this->storage; - } - - /** - * Set storage. - * - * @param StorageInterface $storage the value to set - */ - public function setStorage(StorageInterface $storage) - { - $this->storage = $storage; - } - - /** - * Gets auth adapter adapter. - * - * @return AbstractAdapter Auth adapter - */ - public function getAdapter() - { - return $this->adapter; - } - - /** - * Get authMiddleware. - * - * @return AuthorizationMiddleware Authorization middleware - */ - public function getAuthMiddleware() - { - if ($this->authMiddleware === null) { - $this->authMiddleware = new AuthorizationMiddleware( - $this->app->auth, - $this->getAcl() - ); - } - - return $this->authMiddleware; - } - - /** - * Set authMiddleware. - * - * @param $authMiddleware Authorization middleware - */ - public function setAuthMiddleware(AuthorizationMiddleware $authMiddleware) - { - $this->authMiddleware = $authMiddleware; - } -} diff --git a/src/Exception/AuthException.php b/src/Exception/AuthException.php deleted file mode 100644 index 7a89d86..0000000 --- a/src/Exception/AuthException.php +++ /dev/null @@ -1,18 +0,0 @@ -app->auth and $this->app->authenticator - * return the expected class instances. - */ - public function testBootstrap() - { - $app = new Slim(); - $adapter = $this->getMockBuilder('Zend\Authentication\Adapter\AbstractAdapter') - ->disableOriginalConstructor() - ->getMock(); - $acl = new Acl(); - - $bootstrap = new Bootstrap($app, $adapter, $acl); - $bootstrap->bootstrap(); - - $this->assertInstanceOf( - 'JeremyKendall\Slim\Auth\Authenticator', - $app->authenticator - ); - } -} diff --git a/tests/BootstrapTest.php b/tests/BootstrapTest.php deleted file mode 100644 index 7055737..0000000 --- a/tests/BootstrapTest.php +++ /dev/null @@ -1,107 +0,0 @@ -getBootstrap(); - } - - protected function tearDown() - { - $this->bootstrap = null; - } - - public function testConstructorSetsDefaults() - { - $this->assertSame($this->adapter, $this->bootstrap->getAdapter()); - $this->assertSame($this->acl, $this->bootstrap->getAcl()); - } - - public function testBootstrap() - { - $authMiddleware = $this - ->getMockBuilder('JeremyKendall\Slim\Auth\Middleware\Authorization') - ->disableOriginalConstructor() - ->getMock(); - - $this->app->expects($this->exactly(2)) - ->method('__set') - ->withConsecutive( - array('auth', $this->anything()), - array('authenticator', $this->anything()) - ); - - $this->app->expects($this->once()) - ->method('add') - ->with($authMiddleware); - - $this->bootstrap->setAuthMiddleware($authMiddleware); - $this->bootstrap->bootstrap(); - } - - public function testGetSetStorage() - { - $defaultStorage = $this->bootstrap->getStorage(); - - $this->assertInstanceOf( - 'Zend\Authentication\Storage\StorageInterface', - $defaultStorage - ); - $this->assertEquals('slim_auth', $defaultStorage->getNamespace()); - - $storage = $this->getMock('Zend\Authentication\Storage\StorageInterface'); - $this->bootstrap->setStorage($storage); - - $this->assertSame($storage, $this->bootstrap->getStorage()); - } - - public function testGetDefaultMiddleware() - { - $auth = $this->getMockBuilder('Zend\Authentication\AuthenticationServiceInterface') - ->disableOriginalConstructor() - ->getMock(); - - $this->app->expects($this->once()) - ->method('__get') - ->with('auth') - ->will($this->returnValue($auth)); - - $this->assertInstanceOf( - 'JeremyKendall\Slim\Auth\Middleware\Authorization', - $this->bootstrap->getAuthMiddleware() - ); - } - - private function getBootstrap(StorageInterface $storage = null) - { - $this->app = $this->getMockBuilder('Slim\Slim') - ->disableOriginalConstructor() - ->getMock(); - - $this->adapter = $this->getMockBuilder('Zend\Authentication\Adapter\AbstractAdapter') - ->disableOriginalConstructor() - ->getMock(); - - $this->acl = new Acl(); - - $this->bootstrap = new Bootstrap($this->app, $this->adapter, $this->acl); - - if ($storage !== null) { - $this->bootstrap->setStorage($storage); - } - } -} From ef79ee18482f77f38125e5879b7c67dfcb5de748 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Sat, 2 Jan 2016 22:11:41 -0600 Subject: [PATCH 10/13] Update Slim to 3.x --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7ffc6d7..c91d2cf 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ }, "require-dev": { "phpunit/phpunit": "4.*", - "slim/slim": "^2.4.2", + "slim/slim": "^3.0", "jeremykendall/debug-die": "0.0.1.*" }, "autoload": { From 2c2c2c9c242c5de1eceef599dbf9a877f0502f7d Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Sat, 2 Jan 2016 22:11:50 -0600 Subject: [PATCH 11/13] Update Middleware for Slim 3.x --- src/Middleware/Authorization.php | 65 +++++++------- tests/Middleware/AuthorizationTest.php | 117 +++++++++++++++---------- 2 files changed, 101 insertions(+), 81 deletions(-) diff --git a/src/Middleware/Authorization.php b/src/Middleware/Authorization.php index 16bdf4b..1a4986d 100644 --- a/src/Middleware/Authorization.php +++ b/src/Middleware/Authorization.php @@ -5,27 +5,21 @@ * * @link http://github.com/jeremykendall/slim-auth Canonical source repo * - * @copyright Copyright (c) 2015 Jeremy Kendall (http://about.me/jeremykendall) + * @copyright Copyright (c) 2016 Jeremy Kendall (http://about.me/jeremykendall) * @license http://github.com/jeremykendall/slim-auth/blob/master/LICENSE MIT */ namespace JeremyKendall\Slim\Auth\Middleware; -use JeremyKendall\Slim\Auth\Exception\HttpForbiddenException; -use JeremyKendall\Slim\Auth\Exception\HttpUnauthorizedException; +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; use Zend\Authentication\AuthenticationServiceInterface; use Zend\Permissions\Acl\AclInterface; /** * Authorization middleware: Checks user's authorization to access the * requested URI. - * - * Will redirect a guest to name login route if they attempt to visit a - * secured URI. - * - * Returns HTTP 403 if authenticated user visits a URI they are not - * authorized for. */ -class Authorization extends \Slim\Middleware +final class Authorization { /** * Authentication service. @@ -54,37 +48,40 @@ public function __construct(AuthenticationServiceInterface $auth, AclInterface $ } /** - * Uses hook to check for user authorization. - * Will redirect to named login route if user is unauthorized. + * Determines whether or not user has access to requested resource. + * + * @param ServerRequestInterface $request + * @param ResponseInterface $response + * @param callable $next * - * @throws HttpForbiddenException If an authenticated user is not authorized for the resource - * @throws HttpUnauthorizedException If an unauthenticated user is not authorized for the resource + * @return ResponseInterface Status 401 if not authenticated, 403 if not authorized */ - public function call() + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { - $app = $this->app; - $auth = $this->auth; - $acl = $this->acl; - $role = $this->getRole($auth->getIdentity()); + $route = $request->getAttribute('route', null); - $isAuthorized = function () use ($app, $auth, $acl, $role) { - $resource = $app->router->getCurrentRoute()->getPattern(); - $privilege = $app->request->getMethod(); - $hasIdentity = $auth->hasIdentity(); - $isAllowed = $acl->isAllowed($role, $resource, $privilege); + if ($route === null) { + // User likely accessing a non-existant route. Calling next middleware. + return $next($request, $response); + } - if ($hasIdentity && !$isAllowed) { - throw new HttpForbiddenException(); - } + $role = $this->getRole($this->auth->getIdentity()); + $resource = $routePattern = $route->getPattern(); + $privilege = $request->getMethod(); + $hasIdentity = $this->auth->hasIdentity(); + $isAllowed = $this->acl->isAllowed($role, $resource, $privilege); - if (!$hasIdentity && !$isAllowed) { - throw new HttpUnauthorizedException(); - } - }; + if ($hasIdentity && !$isAllowed) { + // Authenticated but unauthorized for this resource + return $response->withStatus(403); + } - $app->hook('slim.before.dispatch', $isAuthorized); + if (!$hasIdentity && !$isAllowed) { + // Not authenticated and must be authenticated to access this resource + return $response->withStatus(401); + } - $this->next->call(); + return $next($request, $response); } /** @@ -106,7 +103,7 @@ private function getRole($identity = null) $role = $identity['role']; } - if (!$role) { + if ($role === null) { $role = 'guest'; } diff --git a/tests/Middleware/AuthorizationTest.php b/tests/Middleware/AuthorizationTest.php index 547ee4b..6ec8fae 100644 --- a/tests/Middleware/AuthorizationTest.php +++ b/tests/Middleware/AuthorizationTest.php @@ -2,20 +2,23 @@ namespace JeremyKendall\Slim\Auth\Tests\Middleware; -use JeremyKendall\Slim\Auth\Exception\AuthException; use JeremyKendall\Slim\Auth\Middleware\Authorization; +use Slim\Http\Environment; +use Slim\Http\Request; +use Slim\Http\Response; +use Slim\Route; use Zend\Permissions\Acl\Acl; use Zend\Permissions\Acl\Role\GenericRole as Role; class AuthorizationTest extends \PHPUnit_Framework_TestCase { /** - * @var Zend\Authentication\AuthenticationService + * @var Zend\Authentication\AuthenticationServiceInterface */ private $auth; /** - * @var Zend\Permissions\Acl\Acl + * @var Zend\Permissions\Acl\AclInterface */ private $acl; @@ -27,9 +30,9 @@ class AuthorizationTest extends \PHPUnit_Framework_TestCase protected function setUp() { parent::setUp(); - $this->auth = $this->getMock('Zend\Authentication\AuthenticationService'); + + $this->auth = $this->getMock('Zend\Authentication\AuthenticationServiceInterface'); $this->acl = $this->getConfiguredAcl(); - $this->middleware = new Authorization($this->auth, $this->acl); } protected function tearDown() @@ -48,12 +51,17 @@ public function testRouteAuthentication( $location, $hasIdentity, $identity, - $httpStatus + $httpStatus, + $pattern ) { - \Slim\Environment::mock(array( + $env = Environment::mock([ 'REQUEST_METHOD' => $requestMethod, - 'PATH_INFO' => $path, - )); + 'REQUEST_URI' => $path, + ]); + + $request = Request::createFromEnvironment($env); + $response = new Response(); + $middleware = new Authorization($this->auth, $this->acl); $this->auth->expects($this->once()) ->method('hasIdentity') @@ -63,29 +71,43 @@ public function testRouteAuthentication( ->method('getIdentity') ->will($this->returnValue($identity)); - $app = new \Slim\Slim(array('debug' => false)); - - $app->error(function (\Exception $e) use ($app) { - // Example of handling Auth Exceptions - if ($e instanceof AuthException) { - $app->response->setStatus($e->getCode()); - $app->response->setBody($e->getMessage()); - } - }); - $app->get('/', function () {}); - $app->get('/member', function () {}); - $app->delete('/member/photo/:id', function ($id) {}); - $app->get('/admin', function () {}); - $app->map('/login', function () {}) - ->via('GET', 'POST') - ->name('login'); - $app->add($this->middleware); - ob_start(); - $app->run(); - ob_end_clean(); - - $this->assertEquals($httpStatus, $app->response->status()); - $this->assertEquals($location, $app->response->header('location')); + // ROUTE + $route = new Route([$requestMethod], $pattern, function ($req, $res, $args) {}); + $request = $request->withAttribute('route', $route); + + $next = function ($req, $res) { + return $res; + }; + $response = $middleware($request, $response, $next); + + $this->assertEquals($httpStatus, $response->getStatusCode()); + $this->assertEquals($location, $response->getHeaderLine('Location')); + } + + public function testNullRouteDoesNotAttemptAuth() + { + $env = Environment::mock([ + 'REQUEST_METHOD' => 'GET', + 'REQUEST_URI' => '/does-not-exist-in-app', + ]); + + $request = Request::createFromEnvironment($env); + $response = new Response(); + $middleware = new Authorization($this->auth, $this->acl); + + $this->auth->expects($this->never()) + ->method('hasIdentity'); + + $this->auth->expects($this->never()) + ->method('getIdentity'); + + $next = function ($req, $res) { + return $res; + }; + $response = $middleware($request, $response, $next); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertEquals('', $response->getHeaderLine('Location')); } public function authenticationDataProvider() @@ -96,20 +118,21 @@ public function authenticationDataProvider() $location, $hasIdentity, $identity, - $httpStatus + $httpStatus, + $pattern */ - return array( + return [ // Guest - array('GET', '/', null, false, null, 200), - array('GET', '/login', null, false, null, 200), - array('POST', '/login', null, false, null, 200), - array('GET', '/member', null, false, null, 401), + ['GET', '/', null, false, null, 200, '/'], + ['GET', '/login', null, false, null, 200, '/login'], + ['POST', '/login', null, false, null, 200, '/login'], + ['GET', '/member', null, false, null, 401, '/member'], // Member - array('GET', '/admin', null, true, new Identity('member'), 403), - array('DELETE', '/member/photo/992892', null, true, array('role' => 'member'), 200), + ['GET', '/admin', null, true, new Identity('member'), 403, '/admin'], + ['DELETE', '/member/photo/992892', null, true, ['role' => 'member'], 200, '/member/photo/{id}'], // Admin - array('GET', '/admin', null, true, array('role' => 'admin'), 200), - ); + ['GET', '/admin', null, true, ['role' => 'admin'], 200, '/member/photo/{id}'], + ]; } private function getConfiguredAcl() @@ -123,15 +146,15 @@ private function getConfiguredAcl() $acl->addResource('/'); $acl->addResource('/login'); $acl->addResource('/member'); - $acl->addResource('/member/photo/:id'); + $acl->addResource('/member/photo/{id}'); $acl->addResource('/admin'); $acl->allow('guest', '/'); - $acl->allow('guest', '/login', array('GET', 'POST')); + $acl->allow('guest', '/login', ['GET', 'POST']); $acl->deny('guest', '/admin'); $acl->allow('member', '/member'); - $acl->allow('member', '/member/photo/:id', 'DELETE'); + $acl->allow('member', '/member/photo/{id}', 'DELETE'); // admin gets everything $acl->allow('admin'); @@ -140,9 +163,9 @@ private function getConfiguredAcl() } } -class Identity implements \JeremyKendall\Slim\Auth\IdentityInterface +final class Identity implements \JeremyKendall\Slim\Auth\IdentityInterface { - protected $identity; + private $identity; public function __construct($identity) { From 2a17facdfd74c2cbb5dab03594ef6613cefbea5b Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Sat, 2 Jan 2016 22:17:11 -0600 Subject: [PATCH 12/13] Update copyright years --- LICENSE | 2 +- src/Adapter/Db/PdoAdapter.php | 2 +- src/Authenticator.php | 2 +- src/IdentityInterface.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/LICENSE b/LICENSE index 1f56432..58d656e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (C) 2013 Jeremy Kendall +Copyright (C) 2013-2016 Jeremy Kendall Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/src/Adapter/Db/PdoAdapter.php b/src/Adapter/Db/PdoAdapter.php index 2e4f1c7..c58177a 100644 --- a/src/Adapter/Db/PdoAdapter.php +++ b/src/Adapter/Db/PdoAdapter.php @@ -5,7 +5,7 @@ * * @link http://github.com/jeremykendall/slim-auth Canonical source repo * - * @copyright Copyright (c) 2015 Jeremy Kendall (http://about.me/jeremykendall) + * @copyright Copyright (c) 2016 Jeremy Kendall (http://about.me/jeremykendall) * @license http://github.com/jeremykendall/slim-auth/blob/master/LICENSE MIT */ namespace JeremyKendall\Slim\Auth\Adapter\Db; diff --git a/src/Authenticator.php b/src/Authenticator.php index 8ddf060..c793ce0 100644 --- a/src/Authenticator.php +++ b/src/Authenticator.php @@ -5,7 +5,7 @@ * * @link http://github.com/jeremykendall/slim-auth Canonical source repo * - * @copyright Copyright (c) 2015 Jeremy Kendall (http://about.me/jeremykendall) + * @copyright Copyright (c) 2016 Jeremy Kendall (http://about.me/jeremykendall) * @license http://github.com/jeremykendall/slim-auth/blob/master/LICENSE MIT */ namespace JeremyKendall\Slim\Auth; diff --git a/src/IdentityInterface.php b/src/IdentityInterface.php index 06277f2..4d420e1 100644 --- a/src/IdentityInterface.php +++ b/src/IdentityInterface.php @@ -5,7 +5,7 @@ * * @link http://github.com/jeremykendall/slim-auth Canonical source repo * - * @copyright Copyright (c) 2015 Jeremy Kendall (http://about.me/jeremykendall) + * @copyright Copyright (c) 2016 Jeremy Kendall (http://about.me/jeremykendall) * @license http://github.com/jeremykendall/slim-auth/blob/master/LICENSE MIT */ namespace JeremyKendall\Slim\Auth; From eed945b55f5e69bf0ea94540e5d9ff7c9b517fe0 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Sat, 2 Jan 2016 22:29:36 -0600 Subject: [PATCH 13/13] De-cruft and de-CRAP --- src/Middleware/Authorization.php | 18 ++++++------------ tests/Middleware/AuthorizationTest.php | 3 +-- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/Middleware/Authorization.php b/src/Middleware/Authorization.php index 1a4986d..5017c4d 100644 --- a/src/Middleware/Authorization.php +++ b/src/Middleware/Authorization.php @@ -61,15 +61,15 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res $route = $request->getAttribute('route', null); if ($route === null) { - // User likely accessing a non-existant route. Calling next middleware. + // User likely accessing a nonexistent route. Calling next middleware. return $next($request, $response); } $role = $this->getRole($this->auth->getIdentity()); - $resource = $routePattern = $route->getPattern(); + $resource = $route->getPattern(); $privilege = $request->getMethod(); - $hasIdentity = $this->auth->hasIdentity(); $isAllowed = $this->acl->isAllowed($role, $resource, $privilege); + $hasIdentity = $this->auth->hasIdentity(); if ($hasIdentity && !$isAllowed) { // Authenticated but unauthorized for this resource @@ -93,20 +93,14 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res */ private function getRole($identity = null) { - $role = null; - if (is_object($identity)) { - $role = $identity->getRole(); + return $identity->getRole(); } if (is_array($identity) && isset($identity['role'])) { - $role = $identity['role']; - } - - if ($role === null) { - $role = 'guest'; + return $identity['role']; } - return $role; + return 'guest'; } } diff --git a/tests/Middleware/AuthorizationTest.php b/tests/Middleware/AuthorizationTest.php index 6ec8fae..c62b21d 100644 --- a/tests/Middleware/AuthorizationTest.php +++ b/tests/Middleware/AuthorizationTest.php @@ -131,7 +131,7 @@ public function authenticationDataProvider() ['GET', '/admin', null, true, new Identity('member'), 403, '/admin'], ['DELETE', '/member/photo/992892', null, true, ['role' => 'member'], 200, '/member/photo/{id}'], // Admin - ['GET', '/admin', null, true, ['role' => 'admin'], 200, '/member/photo/{id}'], + ['GET', '/admin', null, true, ['role' => 'admin'], 200, '/admin'], ]; } @@ -151,7 +151,6 @@ private function getConfiguredAcl() $acl->allow('guest', '/'); $acl->allow('guest', '/login', ['GET', 'POST']); - $acl->deny('guest', '/admin'); $acl->allow('member', '/member'); $acl->allow('member', '/member/photo/{id}', 'DELETE');