From 790c969fd1da8d66500a9d5a462d0d1264dc1883 Mon Sep 17 00:00:00 2001 From: Jeremy Kendall Date: Fri, 14 Mar 2014 13:41:34 -0500 Subject: [PATCH 01/15] Updates documentation. Still incomplete, but much, much better. Thanks to @Chris911 for pointing out that the docs weren't just incomplete, but wrong. This should correct the wrong docs, but still doesn't complete them. --- README.md | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index d2cd1c5..ab6902b 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,8 @@ component, and authorization by using the Zend Framework [Acl][3] component. ## DOCUMENTATION INCOMPLETE This lib is usable, but is beta software, and this documentation is incomplete. -If you're *extremely* familiar with Zend Auth and Zend ACL, you can probably work -it out just fine. Otherwise, you might want to wait for the docs to be completed. +If you're familiar with Zend Auth and Zend ACL, you can probably work it out +just fine. Otherwise, you might want to wait for the docs to be completed. Caveat emptor and all that. @@ -47,8 +47,6 @@ require 'vendor/autoload.php'; ## Preparing Your App For Slim Auth -# Configuring Password Validator - ### Database Your database should have a user table, and that table must have a `role` @@ -136,9 +134,11 @@ route simply because it references a public resource. Now that you have a user database table with a `role` column and an ACL, you're ready to configure Slim Auth and add it to your application. -First, add `use` statements for the PDO adapter and the Slim Auth Bootstrap. +First, add `use` statements for the PasswordValidator (from the +[Password Validator][9] library), the PDO adapter, and the Slim Auth Bootstrap. ``` +use JeremyKendall\Password\PasswordValidator; use JeremyKendall\Slim\Auth\Adapter\Db\PdoAdapter; use JeremyKendall\Slim\Auth\Bootstrap; ``` @@ -167,26 +167,29 @@ From the Zend Authentication documentation: > type of authentication service, such as LDAP, RDBMS, or file-based storage. Slim Auth provides an RDBMS authentication adapter for PDO. The constructor -accepts four required arguments: +accepts five required arguments: * A `\PDO` instance * The name of the user table * The name of the identity, or username, column * The name of the credential, or password, column +* An instance of `JeremyKendall\Password\PasswordValidator` ``` $db = new \PDO(); -$adapter = new PdoAdapter($db, , , ); +$adapter = new PdoAdapter( + $db, + , + , + , + new PasswordValidator() +); ``` -#### Credential Validation Callback - -There is an optional fifth parameter: `$credentialValidationCallback`. If you -do not provide a callback (and it's recommended that you don't), Slim Auth uses -PHP's new password hash functionality by default. If you're not able to use -PHP 5.5's new password hashing functions *and* your version of PHP doesn't -support the userland implementation [password_compat][8], then you'll need to -provide your own credential validation functionality via a callback. +> **NOTE**: Please refer to the [Password Validator documentation][9] for more +> information on the proper use of the library. If you choose not to use the +> Password Validator library, you will need to create your own authentication +> adapter. ### Putting it all Together @@ -270,3 +273,4 @@ $app->get('/logout', function () use ($app) { [6]: https://packagist.org/packages/jeremykendall/slim-auth [7]: http://docs.slimframework.com/#Cookie-Session-Store [8]: https://github.com/ircmaxell/password_compat +[9]: https://github.com/jeremykendall/password-validator From 813540c287bcd6949c1c0b86f8dc21096bab577b Mon Sep 17 00:00:00 2001 From: Jeremy Kendall Date: Fri, 14 Mar 2014 13:42:35 -0500 Subject: [PATCH 02/15] Adds typehinting to PasswordValidator arg in PdoAdapter --- src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php b/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php index 7c306e8..8325397 100644 --- a/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php +++ b/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php @@ -59,7 +59,7 @@ public function __construct( $tableName, $identityColumn, $credentialColumn, - $passwordValidator + PasswordValidatorInterface $passwordValidator ) { $this->db = $db; From fb9453a780e8cb21df7047ba93fccc186731df99 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Thu, 17 Jul 2014 08:06:40 -0500 Subject: [PATCH 03/15] Adds minimum Slim versions. Fixes #5 and #8. --- README.md | 5 +++++ composer.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ab6902b..3cc5457 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,11 @@ just fine. Otherwise, you might want to wait for the docs to be completed. Caveat emptor and all that. +## Requirements + +Slim Auth works with all versions of Slim 2 >= 2.4.2. Slim Auth has not been tested +against the upcoming Slim 3 release. + ## Installation Install composer in your project: diff --git a/composer.json b/composer.json index 96f6ab9..61fa8a0 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,7 @@ "require-dev": { "league/phpunit-coverage-listener": "~1.1", "phpunit/phpunit": "4.*", - "slim/slim": "2.*" + "slim/slim": ">=2.4.2" }, "autoload": { "psr-0": { From 4b648a1436c5b13535735a0ac0758162901f8246 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Fri, 18 Jul 2014 10:29:17 -0500 Subject: [PATCH 04/15] Adds link to example implementation --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 3cc5457..d9cf616 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,11 @@ Caveat emptor and all that. Slim Auth works with all versions of Slim 2 >= 2.4.2. Slim Auth has not been tested against the upcoming Slim 3 release. +## Example Implementation + +I've put together an example implementation to demonstrate the library in +action. The example implementation can be found [here][10]. + ## Installation Install composer in your project: @@ -279,3 +284,4 @@ $app->get('/logout', function () use ($app) { [7]: http://docs.slimframework.com/#Cookie-Session-Store [8]: https://github.com/ircmaxell/password_compat [9]: https://github.com/jeremykendall/password-validator +[10]: https://github.com/jeremykendall/slim-auth-impl From 1f2343dc2effaeae3265343173ced2dfdc722a94 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Fri, 18 Jul 2014 10:41:14 -0500 Subject: [PATCH 05/15] Updates Slim version constraint --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 61fa8a0..1235299 100644 --- a/composer.json +++ b/composer.json @@ -44,7 +44,7 @@ "require-dev": { "league/phpunit-coverage-listener": "~1.1", "phpunit/phpunit": "4.*", - "slim/slim": ">=2.4.2" + "slim/slim": ">=2.4.2, <3.0" }, "autoload": { "psr-0": { From ce54dacad8dc08f844d6c16c50af85472c21c3f5 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Fri, 18 Jul 2014 14:31:20 -0500 Subject: [PATCH 06/15] Adds PHP 5.6 to .travis.yml --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 25bcd16..7ad8e20 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ php: - 5.3 - 5.4 - 5.5 + - 5.6 before_script: - composer self-update From f19ee333dfd920eaf7f5e866fe9ebf1fb1c0b9ca Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Wed, 25 Mar 2015 08:49:53 -0500 Subject: [PATCH 07/15] Add php-cs-fixer config and phing build files --- .php_cs | 15 ++++++++ build.properties | 2 ++ build.xml | 90 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 .php_cs create mode 100644 build.properties create mode 100644 build.xml diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..dac1727 --- /dev/null +++ b/.php_cs @@ -0,0 +1,15 @@ +in('src/'); + +return \Symfony\CS\Config\Config::create() + ->setUsingCache(true) + ->fixers([ + '-concat_without_spaces', + 'concat_with_spaces', + 'ordered_use', + ]) + ->finder($finder); diff --git a/build.properties b/build.properties new file mode 100644 index 0000000..16caf0e --- /dev/null +++ b/build.properties @@ -0,0 +1,2 @@ +project.basedir = . +passthru = true diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..026ab38 --- /dev/null +++ b/build.xml @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From a1ad799bd4140a36ef2b08b666c7e62557b69fe9 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Wed, 25 Mar 2015 08:50:40 -0500 Subject: [PATCH 08/15] Update documentation --- README.md | 112 ++++++++++++++++++++++++------------------------------ 1 file changed, 49 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index d9cf616..d163702 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,35 @@ # Slim Auth [![Build Status](https://travis-ci.org/jeremykendall/slim-auth.png?branch=master)](https://travis-ci.org/jeremykendall/slim-auth) [![Coverage Status](https://coveralls.io/repos/jeremykendall/slim-auth/badge.png?branch=master)](https://coveralls.io/r/jeremykendall/slim-auth?branch=master) [![Dependencies Status](https://depending.in/jeremykendall/slim-auth.png)](http://depending.in/jeremykendall/slim-auth) Slim Auth is an authorization and authentication library for the [Slim Framework][1]. -Authentication is accomplished by using the Zend Framework [Authentication][2] -component, and authorization by using the Zend Framework [Acl][3] component. +Authentication is provided by the Zend Framework [Zend\Authentication][2] +component, and authorization by the Zend Framework [Zend\Permissions\Acl][3] component. -## DOCUMENTATION INCOMPLETE +## Fair Warning: Documentation Mostly Complete -This lib is usable, but is beta software, and this documentation is incomplete. -If you're familiar with Zend Auth and Zend ACL, you can probably work it out -just fine. Otherwise, you might want to wait for the docs to be completed. +Slim Auth is fully functional and production ready (I've used it in production +in multiple projects), but this documentation is incomplete. (Current status of +the documentation is ~90% complete.) + +If you're familiar with [Zend\Authentication][2] and [Zend\Permissions\Acl][3], you'll be able to implement the library without any trouble. Otherwise, you might want to wait for the docs to be completed (no ETA) or open a GitHub issue with any questions or problems you encounter. Caveat emptor and all that. +## Slim SessionCookie No Longer Recomended + +**TL;DR**: You *will* experience unexpected behavior if you use +`Zend\Authentication\Storage\Session` as your auth storage and +`Slim\Middleware\SessionCookie` to provide encrypted cookies when your Slim +version is >= 2.6. + +Earlier versions of this documentation (and the [sample implementation][10]) +demonstrated the use of Slim's [SessionCookie Middleware](http://docs.slimframework.com/#Cookie-Session-Store) as a way to handle session storage in concert with Zend Session. As of [Slim 2.6.0](https://github.com/slimphp/Slim/releases/tag/2.6.0), +Zend Session and Slim's SessionCookie middleware no longer play well together, +and I've opted for a Zend Session only approach. + ## Requirements -Slim Auth works with all versions of Slim 2 >= 2.4.2. Slim Auth has not been tested -against the upcoming Slim 3 release. +Slim Auth works with all versions of Slim 2 >= 2.4.2. Slim Auth has not been +tested against the upcoming Slim 3 release. ## Example Implementation @@ -24,34 +38,23 @@ action. The example implementation can be found [here][10]. ## Installation -Install composer in your project: +Installation is provided via [Composer][11]. -``` -curl -s https://getcomposer.org/installer | php -``` - -Create a composer.json file in your project root: +First, install Composer. ``` -{ - "require": { - "jeremykendall/slim-auth": "*" - } -} +curl -s https://getcomposer.org/installer | php ``` -(*Please check Packagist for the [most recent version of Slim Auth][6]*) - -Install via composer: +Then install Slim Auth with the following Composer command. ``` -php composer.phar install +composer require jeremykendall/slim-auth ``` -Add this line to your application’s index.php file: +Finally, add this line at the top of your application’s index.php file: ``` -allow('guest', '/', 'GET'); $this->allow('guest', '/login', array('GET', 'POST')); $this->allow('guest', '/logout', 'GET'); @@ -127,7 +127,7 @@ class Acl extends ZendAcl #### The Guest Role Please note the `guest` role. **You must use the name** `guest` **as the role -assigned to an unauthenticated user**. The other role names are yours to choose. +assigned to unauthenticated users**. The other role names are yours to choose. #### Acl "Privileges" @@ -137,7 +137,7 @@ adding the third argument, you are restricting route access by HTTP method. If you do not provide an HTTP verb or verbs, you are allowing access to the specified route via *all* HTTP methods. **Be extremely vigilant here.** You wouldn't want to accidentally allow a 'guest' role access to an admin `DELETE` -route simply because it references a public resource. +route simply because you forgot to explicitly deny the `DELETE` route. ## Configuring Slim Auth: Defaults @@ -153,20 +153,10 @@ use JeremyKendall\Slim\Auth\Adapter\Db\PdoAdapter; use JeremyKendall\Slim\Auth\Bootstrap; ``` -Next, create your Slim application with `cookies.encrypt` and -`cookies.secret_key` as a minimum configuration. - ->*Default Slim Auth identity storage is session storage. You MUST set the ->following cookie encryption settings if you use the SessionCookie middleware, ->which this example does. Details on configuring different storage are available ->later in the documentation.* +Next, create your Slim application. ``` -$app = new \Slim\Slim(array( - // Config requirements for default Slim Auth implementation - 'cookies.encrypt' => true, - 'cookies.secret_key' => 'CHANGE ME. SERIOUSLY, CHANGE ME RIGHT NOW.', -)); +$app = new \Slim\Slim(); ``` ### Authentication Adapter @@ -176,7 +166,7 @@ From the Zend Authentication documentation: > `Zend\Authentication` adapters are used to authenticate against a particular > type of authentication service, such as LDAP, RDBMS, or file-based storage. -Slim Auth provides an RDBMS authentication adapter for PDO. The constructor +Slim Auth provides an RDBMS authentication adapter for [PDO][12]. The constructor accepts five required arguments: * A `\PDO` instance @@ -211,19 +201,6 @@ $authBootstrap = new Bootstrap($app, $adapter, $acl); $authBootstrap->bootstrap(); ``` -Finally, and this is *crucial*, you *must* add Slim's [SessionCookie][7] -Middleware, and you must add it *after* the Slim Auth `Boostrap::bootstrap()` -method has been called. - -> **NOTE**: This is only a requirement if you're using the default Session -> Storage *and* you opt to use the `SessionCookie` middleware. It is possible to -> configure Slim Auth to use storage other than Slim's SessionCookie. - -``` -// Add the session cookie middleware *after* auth to ensure it's executed first -$app->add(new \Slim\Middleware\SessionCookie()); -``` - ### Login Route You'll need a login route, of course, and it's important that you name your @@ -275,13 +252,22 @@ $app->get('/logout', function () use ($app) { }); ``` +## And Done + +That should get you most of the way. I'll complete documentation as soon as I'm +able, but can't currently commit to an ETA. Again, please feel free to open and +issue with any questions you might have regarding implementation. + +Thanks for considering Slim Auth for your project. + [1]: http://slimframework.com/ -[2]: http://framework.zend.com/manual/2.2/en/modules/zend.authentication.intro.html -[3]: http://framework.zend.com/manual/2.2/en/modules/zend.permissions.acl.intro.html +[2]: http://framework.zend.com/manual/current/en/modules/zend.authentication.intro.html +[3]: http://framework.zend.com/manual/current/en/modules/zend.permissions.acl.intro.html [4]: http://docs.slimframework.com/#Route-Names [5]: http://docs.slimframework.com/#Route-Helpers [6]: https://packagist.org/packages/jeremykendall/slim-auth -[7]: http://docs.slimframework.com/#Cookie-Session-Store [8]: https://github.com/ircmaxell/password_compat [9]: https://github.com/jeremykendall/password-validator [10]: https://github.com/jeremykendall/slim-auth-impl +[11]: http://getcomposer.org +[12]: http://php.net/manual/en/book.pdo.php From 3f1872bc20acd5bc9bbf56e49cee495eebbf8b5a Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Wed, 25 Mar 2015 08:51:05 -0500 Subject: [PATCH 09/15] Update dependencies and clear out cruft --- composer.json | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 1235299..3e2e126 100644 --- a/composer.json +++ b/composer.json @@ -27,24 +27,18 @@ "bin": [ "bin/version-check" ], - "repositories": [ - { - "type": "composer", - "url": "http://packages.zendframework.com/" - } - ], "require": { "php": ">=5.3.7", "jeremykendall/password-validator": "2.*", - "wp-cli/php-cli-tools": "~0.9.4", - "zendframework/zend-authentication": "2.3.*", - "zendframework/zend-permissions-acl": "2.3.*", - "zendframework/zend-session": "2.3.*" + "wp-cli/php-cli-tools": "~0.9", + "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, <3.0" + "slim/slim": "^2.4.2" }, "autoload": { "psr-0": { From 971fb1aa94c1a8649a6ec722cd93842aac2334e9 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Wed, 25 Mar 2015 08:53:30 -0500 Subject: [PATCH 10/15] Explicitly set \PDO::FETCH_ASSOC in PdoAdapter::findUser() Fixes #13 --- src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php b/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php index 8325397..359d381 100644 --- a/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php +++ b/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php @@ -119,7 +119,9 @@ private function findUser() $stmt = $this->db->prepare($sql); $stmt->execute(array('identity' => $this->getIdentity())); - return $stmt->fetch(); + // Explicitly setting fetch mode fixes + // https://github.com/jeremykendall/slim-auth/issues/13 + return $stmt->fetch(PDO::FETCH_ASSOC); } /** From 200405e09ab0ca06cb98e69ec39ae7ba530433d3 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Wed, 25 Mar 2015 08:57:33 -0500 Subject: [PATCH 11/15] Swap type hint implementations for interfaces Also includes CS fixes --- src/JeremyKendall/Slim/Auth/Authenticator.php | 29 +++++++++-------- src/JeremyKendall/Slim/Auth/Bootstrap.php | 32 +++++++++---------- .../Slim/Auth/Middleware/Authorization.php | 31 +++++++++--------- 3 files changed, 47 insertions(+), 45 deletions(-) diff --git a/src/JeremyKendall/Slim/Auth/Authenticator.php b/src/JeremyKendall/Slim/Auth/Authenticator.php index f97879d..5804070 100644 --- a/src/JeremyKendall/Slim/Auth/Authenticator.php +++ b/src/JeremyKendall/Slim/Auth/Authenticator.php @@ -1,43 +1,46 @@ auth = $auth; } /** - * Authenticates user + * Authenticates user. + * + * @param string $identity User identifier (username, email, etc) + * @param string $credential User password * - * @param string $identity User identifier (username, email, etc) - * @param string $credential User password * @return Zend\Authentication\Result + * * @throws Zend\Authentication\Exception\RuntimeException */ public function authenticate($identity, $credential) @@ -50,9 +53,7 @@ public function authenticate($identity, $credential) } /** - * Clears the identity from persistent storage - * - * @return void + * Clears the identity from persistent storage. */ public function logout() { diff --git a/src/JeremyKendall/Slim/Auth/Bootstrap.php b/src/JeremyKendall/Slim/Auth/Bootstrap.php index 499b265..f0e5b7a 100644 --- a/src/JeremyKendall/Slim/Auth/Bootstrap.php +++ b/src/JeremyKendall/Slim/Auth/Bootstrap.php @@ -1,30 +1,30 @@ app = $app; $this->adapter = $adapter; @@ -63,7 +63,7 @@ public function __construct(Slim $app, AbstractAdapter $adapter, Acl $acl) } /** - * Wires up Slim Auth defaults + * Wires up Slim Auth defaults. * * Creates the Zend AuthenticationService, adds the AuthenticationService * and the Authenticator to the Slim resource locator, and adds the @@ -89,7 +89,7 @@ public function bootstrap() } /** - * Get acl + * Get acl. * * @return acl */ @@ -99,7 +99,7 @@ public function getAcl() } /** - * Gets storage + * Gets storage. * * @return StorageInterface AuthenticationService storage */ @@ -109,7 +109,7 @@ public function getStorage() } /** - * Set storage + * Set storage. * * @param StorageInterface $storage the value to set */ @@ -119,7 +119,7 @@ public function setStorage(StorageInterface $storage) } /** - * Gets auth adapter adapter + * Gets auth adapter adapter. * * @return AbstractAdapter Auth adapter */ @@ -129,7 +129,7 @@ public function getAdapter() } /** - * Get authMiddleware + * Get authMiddleware. * * @return AuthorizationMiddleware Authorization middleware */ @@ -146,7 +146,7 @@ public function getAuthMiddleware() } /** - * Set authMiddleware + * Set authMiddleware. * * @param $authMiddleware Authorization middleware */ diff --git a/src/JeremyKendall/Slim/Auth/Middleware/Authorization.php b/src/JeremyKendall/Slim/Auth/Middleware/Authorization.php index 92feeb8..a1bdf84 100644 --- a/src/JeremyKendall/Slim/Auth/Middleware/Authorization.php +++ b/src/JeremyKendall/Slim/Auth/Middleware/Authorization.php @@ -1,18 +1,19 @@ auth = $auth; $this->acl = $acl; @@ -54,7 +55,7 @@ public function __construct(AuthenticationService $auth, Acl $acl) /** * Uses hook to check for user authorization. - * Will redirect to named login route if user is unauthorized + * Will redirect to named login route if user is unauthorized. * * @throws \RuntimeException if there isn't a named 'login' route */ @@ -88,7 +89,8 @@ public function call() /** * Gets role from user's identity. * - * @param mixed $identity User's identity. If null, returns role 'guest' + * @param mixed $identity User's identity. If null, returns role 'guest' + * * @return string User's role */ private function getRole($identity = null) @@ -96,7 +98,6 @@ private function getRole($identity = null) $role = null; if (is_object($identity)) { - // TODO: check for IdentityInterface (?) $role = $identity->getRole(); } From 92945b8bde8f4a0082006e4fea41a38b38adb2c0 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Wed, 25 Mar 2015 08:58:13 -0500 Subject: [PATCH 12/15] CS Fixes --- .../Slim/Auth/Adapter/Db/PdoAdapter.php | 34 +++++++++---------- src/JeremyKendall/Slim/Auth/AuthException.php | 7 ++-- .../Auth/Exception/HttpForbiddenException.php | 12 +++---- .../Slim/Auth/IdentityInterface.php | 9 ++--- 4 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php b/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php index 359d381..ef18267 100644 --- a/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php +++ b/src/JeremyKendall/Slim/Auth/Adapter/Db/PdoAdapter.php @@ -1,10 +1,11 @@ db = $db; $this->tableName = $tableName; $this->identityColumn = $identityColumn; @@ -70,7 +70,7 @@ public function __construct( } /** - * Performs authentication + * Performs authentication. * * @return AuthenticationResult Authentication result */ @@ -95,17 +95,17 @@ public function authenticate() unset($user[$this->getCredentialColumn()]); return new AuthenticationResult(AuthenticationResult::SUCCESS, $user, array()); - } else { - return new AuthenticationResult( - AuthenticationResult::FAILURE_CREDENTIAL_INVALID, - array(), - array('Invalid username or password provided') - ); } + + return new AuthenticationResult( + AuthenticationResult::FAILURE_CREDENTIAL_INVALID, + array(), + array('Invalid username or password provided') + ); } /** - * Finds user to authenticate + * Finds user to authenticate. * * @return array|null Array of user data, null if no user found */ @@ -125,7 +125,7 @@ private function findUser() } /** - * Get tableName + * Get tableName. * * @return string tableName */ @@ -135,7 +135,7 @@ public function getTableName() } /** - * Get identityColumn + * Get identityColumn. * * @return string identityColumn */ @@ -145,7 +145,7 @@ public function getIdentityColumn() } /** - * Get credentialColumn + * Get credentialColumn. * * @return string credentialColumn */ diff --git a/src/JeremyKendall/Slim/Auth/AuthException.php b/src/JeremyKendall/Slim/Auth/AuthException.php index 943d73d..f21388c 100644 --- a/src/JeremyKendall/Slim/Auth/AuthException.php +++ b/src/JeremyKendall/Slim/Auth/AuthException.php @@ -1,17 +1,18 @@ Date: Wed, 25 Mar 2015 08:58:25 -0500 Subject: [PATCH 13/15] Add comment to clarify Slim error handling --- .../Slim/Auth/Tests/Middleware/AuthorizationTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/JeremyKendall/Slim/Auth/Tests/Middleware/AuthorizationTest.php b/tests/JeremyKendall/Slim/Auth/Tests/Middleware/AuthorizationTest.php index 866c84b..7fc8bad 100644 --- a/tests/JeremyKendall/Slim/Auth/Tests/Middleware/AuthorizationTest.php +++ b/tests/JeremyKendall/Slim/Auth/Tests/Middleware/AuthorizationTest.php @@ -64,7 +64,9 @@ public function testRouteAuthentication( ->will($this->returnValue($identity)); $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->response->setStatus($e->getCode()); $app->response->setBody($e->getMessage()); From 635fc9ce71c3c2e0c2952f51c70051de30a4c3b6 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Wed, 25 Mar 2015 08:58:46 -0500 Subject: [PATCH 14/15] Update test to resolve post dependency update failures --- .../Slim/Auth/Tests/BootstrapTest.php | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/tests/JeremyKendall/Slim/Auth/Tests/BootstrapTest.php b/tests/JeremyKendall/Slim/Auth/Tests/BootstrapTest.php index ddbf2db..9bd7b38 100644 --- a/tests/JeremyKendall/Slim/Auth/Tests/BootstrapTest.php +++ b/tests/JeremyKendall/Slim/Auth/Tests/BootstrapTest.php @@ -39,22 +39,19 @@ public function testBootstrap() ->disableOriginalConstructor() ->getMock(); + $this->app->expects($this->exactly(2)) + ->method('__set') + ->withConsecutive( + ['auth', $this->anything()], + ['authenticator', $this->anything()] + ); + $this->app->expects($this->once()) ->method('add') ->with($authMiddleware); $this->bootstrap->setAuthMiddleware($authMiddleware); $this->bootstrap->bootstrap(); - - $this->assertInstanceOf( - 'Closure', - $this->app->auth - ); - - $this->assertInstanceOf( - 'Closure', - $this->app->authenticator - ); } public function testGetSetStorage() @@ -68,14 +65,17 @@ public function testGetSetStorage() public function testGetDefaultMiddleware() { - $auth = $this->getMockBuilder('Zend\Authentication\AuthenticationService') + $auth = $this->getMockBuilder('Zend\Authentication\AuthenticationServiceInterface') ->disableOriginalConstructor() ->getMock(); - $this->app->auth = $auth; + $this->app->expects($this->once()) + ->method('__get') + ->with('auth') + ->will($this->returnValue($auth)); $this->assertInstanceOf( - 'JeremyKendall\Slim\Auth\Middleware\Authorization', + 'JeremyKendall\Slim\Auth\Middleware\Authorization', $this->bootstrap->getAuthMiddleware() ); } @@ -84,7 +84,6 @@ private function getBootstrap(StorageInterface $storage = null) { $this->app = $this->getMockBuilder('Slim\Slim') ->disableOriginalConstructor() - ->setMethods(array('add')) ->getMock(); $this->adapter = $this->getMockBuilder('Zend\Authentication\Adapter\AbstractAdapter') From 3389164e68a8c0cb93965c2564f98feb58bc8256 Mon Sep 17 00:00:00 2001 From: jeremykendall Date: Wed, 25 Mar 2015 08:59:07 -0500 Subject: [PATCH 15/15] Update PDO adapter test to confirm fix for #13 --- .../Auth/Tests/Adapter/Db/PdoAdapterTest.php | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/tests/JeremyKendall/Slim/Auth/Tests/Adapter/Db/PdoAdapterTest.php b/tests/JeremyKendall/Slim/Auth/Tests/Adapter/Db/PdoAdapterTest.php index 475df15..a48f6ab 100644 --- a/tests/JeremyKendall/Slim/Auth/Tests/Adapter/Db/PdoAdapterTest.php +++ b/tests/JeremyKendall/Slim/Auth/Tests/Adapter/Db/PdoAdapterTest.php @@ -117,12 +117,36 @@ public function testAuthenticationFailsUserNotFound() $this->assertEquals('User not found.', $messages[0]); } - private function setUpDb() + /** + * @link https://github.com/jeremykendall/slim-auth/issues/13 + */ + public function testIssue13() + { + $this->setUpDb(PDO::FETCH_OBJ); + $this->setUpAdapter(); + + $this->passwordValidator->expects($this->once()) + ->method('isValid') + ->with( + $this->plainTextPassword, + $this->identity['hashed_password'], + $this->identity['id'] + ) + ->will($this->returnValue(new ValidationResult(ValidationResult::SUCCESS))); + + $this->adapter->setIdentity($this->identity['email_address']); + $this->adapter->setCredential($this->plainTextPassword); + + $result = $this->adapter->authenticate(); + $this->assertTrue($result->isValid()); + } + + private function setUpDb($fetchStyle = PDO::FETCH_ASSOC) { $dsn = 'sqlite::memory:'; $options = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, - PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC + PDO::ATTR_DEFAULT_FETCH_MODE => $fetchStyle, ); try {