diff --git a/.php_cs b/.php_cs index dac1727..e59fbd4 100644 --- a/.php_cs +++ b/.php_cs @@ -3,13 +3,15 @@ require_once './vendor/autoload.php'; $finder = \Symfony\CS\Finder\DefaultFinder::create() - ->in('src/'); + ->in('bin/') + ->in('src/') + ->in('tests/'); return \Symfony\CS\Config\Config::create() ->setUsingCache(true) ->fixers([ - '-concat_without_spaces', - 'concat_with_spaces', + '-concat_without_spaces', + 'concat_with_spaces', 'ordered_use', ]) ->finder($finder); diff --git a/composer.json b/composer.json index 3e2e126..cc147ad 100644 --- a/composer.json +++ b/composer.json @@ -29,16 +29,17 @@ ], "require": { "php": ">=5.3.7", - "jeremykendall/password-validator": "2.*", - "wp-cli/php-cli-tools": "~0.9", - "zendframework/zend-authentication": "~2", - "zendframework/zend-permissions-acl": "~2", - "zendframework/zend-session": "~2" + "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" + "slim/slim": "^2.4.2", + "jeremykendall/debug-die": "0.0.1.*" }, "autoload": { "psr-0": { diff --git a/src/JeremyKendall/Slim/Auth/Bootstrap.php b/src/JeremyKendall/Slim/Auth/Bootstrap.php index f0e5b7a..93ecbcc 100644 --- a/src/JeremyKendall/Slim/Auth/Bootstrap.php +++ b/src/JeremyKendall/Slim/Auth/Bootstrap.php @@ -15,6 +15,7 @@ use Slim\Slim; use Zend\Authentication\Adapter\AbstractAdapter; use Zend\Authentication\AuthenticationService; +use Zend\Authentication\Storage\Session as SessionStorage; use Zend\Authentication\Storage\StorageInterface; use Zend\Permissions\Acl\AclInterface; @@ -101,10 +102,16 @@ public function getAcl() /** * 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; } diff --git a/src/JeremyKendall/Slim/Auth/AuthException.php b/src/JeremyKendall/Slim/Auth/Exception/AuthException.php similarity index 88% rename from src/JeremyKendall/Slim/Auth/AuthException.php rename to src/JeremyKendall/Slim/Auth/Exception/AuthException.php index f21388c..902a87b 100644 --- a/src/JeremyKendall/Slim/Auth/AuthException.php +++ b/src/JeremyKendall/Slim/Auth/Exception/AuthException.php @@ -9,7 +9,7 @@ * @license http://github.com/jeremykendall/slim-auth/blob/master/LICENSE MIT */ -namespace JeremyKendall\Slim\Auth; +namespace JeremyKendall\Slim\Auth\Exception; /** * Slim Auth Exception. diff --git a/src/JeremyKendall/Slim/Auth/Exception/HttpForbiddenException.php b/src/JeremyKendall/Slim/Auth/Exception/HttpForbiddenException.php index a913d60..19f8a0e 100644 --- a/src/JeremyKendall/Slim/Auth/Exception/HttpForbiddenException.php +++ b/src/JeremyKendall/Slim/Auth/Exception/HttpForbiddenException.php @@ -11,8 +11,6 @@ namespace JeremyKendall\Slim\Auth\Exception; -use JeremyKendall\Slim\Auth\AuthException; - /** * HTTP 403 Exception. */ @@ -20,16 +18,12 @@ class HttpForbiddenException extends AuthException { /** * Public constructor. - * - * @param string $message Exception message - * @param int $code Exception code - * @param Exception $previous Previous exception */ - public function __construct( - $message = 'You are not authorized to access this resource', - $code = 403, - \Exception $previous = null - ) { - parent::__construct($message, $code, $previous); + public function __construct() + { + $message = 'You are not authorized to access this resource'; + $code = 403; + + parent::__construct($message, $code); } } diff --git a/src/JeremyKendall/Slim/Auth/Exception/HttpUnauthorizedException.php b/src/JeremyKendall/Slim/Auth/Exception/HttpUnauthorizedException.php new file mode 100644 index 0000000..d0d7ee3 --- /dev/null +++ b/src/JeremyKendall/Slim/Auth/Exception/HttpUnauthorizedException.php @@ -0,0 +1,29 @@ +redirect($app->urlFor('login')); + throw new HttpUnauthorizedException(); } }; diff --git a/tests/JeremyKendall/Slim/Auth/Tests/Adapter/Db/PdoAdapterTest.php b/tests/JeremyKendall/Slim/Auth/Tests/Adapter/Db/PdoAdapterTest.php index a48f6ab..1a4ec0a 100644 --- a/tests/JeremyKendall/Slim/Auth/Tests/Adapter/Db/PdoAdapterTest.php +++ b/tests/JeremyKendall/Slim/Auth/Tests/Adapter/Db/PdoAdapterTest.php @@ -63,8 +63,8 @@ public function testAuthenticationSuccess() $this->passwordValidator->expects($this->once()) ->method('isValid') ->with( - $this->plainTextPassword, - $this->identity['hashed_password'], + $this->plainTextPassword, + $this->identity['hashed_password'], $this->identity['id'] ) ->will($this->returnValue(new ValidationResult(ValidationResult::SUCCESS))); @@ -89,7 +89,7 @@ public function testAuthenticationFailsBadPassword() ->method('isValid') ->with( 'bad password', - $this->identity['hashed_password'], + $this->identity['hashed_password'], $this->identity['id'] ) ->will($this->returnValue( @@ -128,8 +128,8 @@ public function testIssue13() $this->passwordValidator->expects($this->once()) ->method('isValid') ->with( - $this->plainTextPassword, - $this->identity['hashed_password'], + $this->plainTextPassword, + $this->identity['hashed_password'], $this->identity['id'] ) ->will($this->returnValue(new ValidationResult(ValidationResult::SUCCESS))); @@ -178,11 +178,11 @@ private function setUpDb($fetchStyle = PDO::FETCH_ASSOC) private function setUpAdapter() { - $this->passwordValidator = + $this->passwordValidator = $this->getMock('JeremyKendall\Password\PasswordValidatorInterface'); $this->adapter = new PdoAdapter( - $this->db, + $this->db, $tableName = 'application_users', $identityColumn = 'email_address', $credentialColumn = 'hashed_password', diff --git a/tests/JeremyKendall/Slim/Auth/Tests/BootstrapFunctionalTest.php b/tests/JeremyKendall/Slim/Auth/Tests/BootstrapFunctionalTest.php index 1c856f4..125dedb 100644 --- a/tests/JeremyKendall/Slim/Auth/Tests/BootstrapFunctionalTest.php +++ b/tests/JeremyKendall/Slim/Auth/Tests/BootstrapFunctionalTest.php @@ -18,7 +18,7 @@ class BootstrapFunctionalTest extends \PHPUnit_Framework_TestCase /** * Confirms that $this->app->auth and $this->app->authenticator - * return the expected class instances + * return the expected class instances. */ public function testBootstrap() { @@ -33,7 +33,7 @@ public function testBootstrap() $this->assertInstanceOf( 'JeremyKendall\Slim\Auth\Authenticator', - $app->authenticator + $app->authenticator ); } } diff --git a/tests/JeremyKendall/Slim/Auth/Tests/BootstrapTest.php b/tests/JeremyKendall/Slim/Auth/Tests/BootstrapTest.php index 97e8726..7055737 100644 --- a/tests/JeremyKendall/Slim/Auth/Tests/BootstrapTest.php +++ b/tests/JeremyKendall/Slim/Auth/Tests/BootstrapTest.php @@ -4,7 +4,6 @@ use JeremyKendall\Slim\Auth\Bootstrap; use Slim\Slim; -use Zend\Authentication\AuthenticationService; use Zend\Authentication\Storage\StorageInterface; use Zend\Permissions\Acl\Acl; @@ -56,10 +55,17 @@ public function testBootstrap() public function testGetSetStorage() { - $storage = $this->getMock('Zend\Authentication\Storage\StorageInterface'); + $defaultStorage = $this->bootstrap->getStorage(); + + $this->assertInstanceOf( + 'Zend\Authentication\Storage\StorageInterface', + $defaultStorage + ); + $this->assertEquals('slim_auth', $defaultStorage->getNamespace()); - $this->assertNull($this->bootstrap->getStorage()); + $storage = $this->getMock('Zend\Authentication\Storage\StorageInterface'); $this->bootstrap->setStorage($storage); + $this->assertSame($storage, $this->bootstrap->getStorage()); } diff --git a/tests/JeremyKendall/Slim/Auth/Tests/Middleware/AuthorizationTest.php b/tests/JeremyKendall/Slim/Auth/Tests/Middleware/AuthorizationTest.php index 7fc8bad..547ee4b 100644 --- a/tests/JeremyKendall/Slim/Auth/Tests/Middleware/AuthorizationTest.php +++ b/tests/JeremyKendall/Slim/Auth/Tests/Middleware/AuthorizationTest.php @@ -2,6 +2,7 @@ namespace JeremyKendall\Slim\Auth\Tests\Middleware; +use JeremyKendall\Slim\Auth\Exception\AuthException; use JeremyKendall\Slim\Auth\Middleware\Authorization; use Zend\Permissions\Acl\Acl; use Zend\Permissions\Acl\Role\GenericRole as Role; @@ -43,13 +44,12 @@ protected function tearDown() */ public function testRouteAuthentication( $requestMethod, - $path, + $path, $location, $hasIdentity, $identity, $httpStatus - ) - { + ) { \Slim\Environment::mock(array( 'REQUEST_METHOD' => $requestMethod, 'PATH_INFO' => $path, @@ -65,9 +65,9 @@ public function testRouteAuthentication( $app = new \Slim\Slim(array('debug' => false)); - $app->error(function(\Exception $e) use ($app) { - // Example of handling 403 FORBIDDEN - if ($e instanceof \JeremyKendall\Slim\Auth\Exception\HttpForbiddenException) { + $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()); } @@ -90,9 +90,9 @@ public function testRouteAuthentication( public function authenticationDataProvider() { - /** + /* $requestMethod, - $path, + $path, $location, $hasIdentity, $identity, @@ -103,7 +103,7 @@ public function authenticationDataProvider() array('GET', '/', null, false, null, 200), array('GET', '/login', null, false, null, 200), array('POST', '/login', null, false, null, 200), - array('GET', '/member', '/login', false, null, 302), + array('GET', '/member', null, false, null, 401), // Member array('GET', '/admin', null, true, new Identity('member'), 403), array('DELETE', '/member/photo/992892', null, true, array('role' => 'member'), 200), diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 9a0870c..571a775 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -9,12 +9,3 @@ $loader->add('JeremyKendall\\Slim\\Auth\\Tests\\', __DIR__); define('SLIM_MODE', 'testing'); - -function d($var) { - var_dump($var); -} - -function dd($var) { - d($var); - die(); -}