Skip to content

Commit

Permalink
Merge pull request #15 from jeremykendall/feature/0.0.7-alpha
Browse files Browse the repository at this point in the history
0.0.7 alpha
  • Loading branch information
jeremykendall committed Mar 25, 2015
2 parents 1955926 + dffaa9d commit cede73e
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 54 deletions.
8 changes: 5 additions & 3 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
7 changes: 7 additions & 0 deletions src/JeremyKendall/Slim/Auth/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 6 additions & 12 deletions src/JeremyKendall/Slim/Auth/Exception/HttpForbiddenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,19 @@

namespace JeremyKendall\Slim\Auth\Exception;

use JeremyKendall\Slim\Auth\AuthException;

/**
* HTTP 403 Exception.
*/
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* Slim Auth.
*
* @link http://github.com/jeremykendall/slim-auth Canonical source repo
*
* @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;

/**
* HTTP 401 Exception.
*/
class HttpUnauthorizedException extends AuthException
{
/**
* Public constructor.
*/
public function __construct()
{
$message = 'You must authenticate to access this resource.';
$code = 401;

parent::__construct($message, $code);
}
}
6 changes: 4 additions & 2 deletions src/JeremyKendall/Slim/Auth/Middleware/Authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace JeremyKendall\Slim\Auth\Middleware;

use JeremyKendall\Slim\Auth\Exception\HttpForbiddenException;
use JeremyKendall\Slim\Auth\Exception\HttpUnauthorizedException;
use Zend\Authentication\AuthenticationServiceInterface;
use Zend\Permissions\Acl\AclInterface;

Expand Down Expand Up @@ -57,7 +58,8 @@ public function __construct(AuthenticationServiceInterface $auth, AclInterface $
* Uses hook to check for user authorization.
* Will redirect to named login route if user is unauthorized.
*
* @throws \RuntimeException if there isn't a named 'login' route
* @throws HttpForbiddenException If an authenticated user is not authorized for the resource
* @throws HttpUnauthorizedException If an unauthenticated user is not authorized for the resource
*/
public function call()
{
Expand All @@ -77,7 +79,7 @@ public function call()
}

if (!$hasIdentity && !$isAllowed) {
return $app->redirect($app->urlFor('login'));
throw new HttpUnauthorizedException();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand All @@ -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(
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -33,7 +33,7 @@ public function testBootstrap()

$this->assertInstanceOf(
'JeremyKendall\Slim\Auth\Authenticator',
$app->authenticator
$app->authenticator
);
}
}
12 changes: 9 additions & 3 deletions tests/JeremyKendall/Slim/Auth/Tests/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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());
}
Expand All @@ -90,9 +90,9 @@ public function testRouteAuthentication(

public function authenticationDataProvider()
{
/**
/*
$requestMethod,
$path,
$path,
$location,
$hasIdentity,
$identity,
Expand All @@ -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),
Expand Down
9 changes: 0 additions & 9 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit cede73e

Please sign in to comment.