Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

Remove response injection & update interfaces #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ php:
- 5.3
- 5.4
- 5.5
- hhvm

matrix:
allow_failures:
- php: hhvm

before_script:
- composer install --dev --prefer-source
Expand Down
22 changes: 0 additions & 22 deletions src/BaconAuthentication/AuthenticationEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ class AuthenticationEvent extends Event
*/
protected $request;

/**
* @var ResponseInterface
*/
protected $response;

/**
* @var ResultInterface|null
Expand All @@ -57,24 +53,6 @@ public function setRequest(RequestInterface $request)
return $this;
}

/**
* @return ResponseInterface
*/
public function getResponse()
{
return $this->response;
}

/**
* @param ResponseInterface $response
* @return AuthenticationEvent
*/
public function setResponse(ResponseInterface $response)
{
$this->response = $response;
return $this;
}

/**
* @return ResultInterface|null
*/
Expand Down
5 changes: 2 additions & 3 deletions src/BaconAuthentication/AuthenticationServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ interface AuthenticationServiceInterface
* Authenticates a request.
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @return ResultInterface
* @return Result\ResultInterface
*/
public function authenticate(RequestInterface $request, ResponseInterface $response);
public function authenticate(RequestInterface $request);

/**
* Resets all credentials to anonymous state.
Expand Down
38 changes: 20 additions & 18 deletions src/BaconAuthentication/PluggableAuthenticationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\Stdlib\PriorityQueue;
use Zend\Stdlib\RequestInterface;
use Zend\Stdlib\ResponseInterface;
Expand Down Expand Up @@ -112,8 +113,8 @@ public function addPlugin($plugin, $priority = 1)
$isValid = true;
}

if ($plugin instanceof EventAwarePluginInterface) {
$plugin->attachToEvents($this->getEventManager());
if ($plugin instanceof ListenerAggregateInterface) {
$plugin->attach($this->getEventManager());
$isValid = true;
}

Expand All @@ -132,17 +133,15 @@ public function addPlugin($plugin, $priority = 1)
*
* @see AuthenticationServiceInterface::authenticate()
* @param RequestInterface $request
* @param ResponseInterface $response
* @return ResultInterface
* @throws Exception\RuntimeException
*/
public function authenticate(RequestInterface $request, ResponseInterface $response)
public function authenticate(RequestInterface $request)
{
$events = $this->getEventManager();
$event = new AuthenticationEvent();
$event->setTarget($this)
->setRequest($request)
->setResponse($response);
->setRequest($request);

$shortCircuit = function ($result) {
return ($result instanceof ResultInterface);
Expand All @@ -153,12 +152,15 @@ public function authenticate(RequestInterface $request, ResponseInterface $respo
if ($eventResult->stopped()) {
$result = $eventResult->last();
} else {
$result = $this->runAuthenticationPlugins($request, $response);
$result = $this->runAuthenticationPlugins($request);
}

if ($result === null) {
if ($this->challenge($request, $response)) {
$result = new Result(Result::STATE_CHALLENGE);

$result = $this->challenge($request);

if ($result) {
$result = new Result(Result::STATE_CHALLENGE, $result);
$event->setResult($result);
}
} else {
Expand Down Expand Up @@ -220,14 +222,13 @@ public function resetCredentials(RequestInterface $request)
* Runs all authentication plugins to get a result.
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @return ResultInterface|null
*/
protected function runAuthenticationPlugins(RequestInterface $request, ResponseInterface $response)
protected function runAuthenticationPlugins(RequestInterface $request)
{
foreach ($this->extractionPlugins as $extractionPlugin) {
/* @var $extractionPlugin ExtractionPluginInterface */
$credentials = $extractionPlugin->extractCredentials($request, $response);
$credentials = $extractionPlugin->extractCredentials($request);

if ($credentials === null) {
continue;
Expand All @@ -254,19 +255,20 @@ protected function runAuthenticationPlugins(RequestInterface $request, ResponseI
* Tries to initiate a challenge.
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @return bool
*/
protected function challenge(RequestInterface $request, ResponseInterface $response)
protected function challenge(RequestInterface $request)
{
/* @var $challengePlugin ChallengePluginInterface */
foreach ($this->challengePlugins as $challengePlugin) {
/* @var $challengePlugin ChallengePluginInterface */
if ($challengePlugin->challenge($request, $response)) {
return true;

$response = $challengePlugin->challenge($request);
if ($response) {
return $response;
}
}

return false;
return null;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/BaconAuthentication/Plugin/ChallengePluginInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ interface ChallengePluginInterface
* If the response can be modified to trigger a challenge, true should be
* returned, false otherwise.
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @return bool
* @param RequestInterface $request
*
* @return null|\Zend\Stdlib\ResponseInterface
*/
public function challenge(RequestInterface $request, ResponseInterface $response);
public function challenge(RequestInterface $request);
}
26 changes: 0 additions & 26 deletions src/BaconAuthentication/Plugin/EventAwarePluginInterface.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/BaconAuthentication/Plugin/ExtractionPluginInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ interface ExtractionPluginInterface
* process and issue a challenge, a Result may be returned as well.
*
* @param RequestInterface $request
* @param ResponseInterface $response
*
* @return null|\BaconAuthentication\Result\ResultInterface|\Zend\Stdlib\ParametersInterface
*/
public function extractCredentials(RequestInterface $request, ResponseInterface $response);
public function extractCredentials(RequestInterface $request);
}
21 changes: 8 additions & 13 deletions src/BaconAuthentication/Plugin/HttpBasicAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Zend\Http\PhpEnvironment\Request as HttpRequest;
use Zend\Http\Response as HttpResponse;
use Zend\Stdlib\RequestInterface;
use Zend\Stdlib\ResponseInterface;
use Zend\Stdlib\Parameters;

/**
Expand Down Expand Up @@ -43,10 +42,9 @@ public function setRealm($realm)
*
* @see ExtractionPluginInterface::extractCredentials()
* @param RequestInterface $request
* @param ResponseInterface $response
* @return null|Parameters
*/
public function extractCredentials(RequestInterface $request, ResponseInterface $response)
public function extractCredentials(RequestInterface $request)
{
if (!$request instanceof HttpRequest) {
return null;
Expand All @@ -70,21 +68,18 @@ public function extractCredentials(RequestInterface $request, ResponseInterface
*
* @see ChallengePluginInterface::challenge()
* @param RequestInterface $request
* @param ResponseInterface $response
* @return bool
* @return null|HttpResponse
*/
public function challenge(RequestInterface $request, ResponseInterface $response)
public function challenge(RequestInterface $request)
{
if (!$response instanceof HttpResponse) {
return false;
if (!$request instanceof HttpRequest) {
return null;
}

$response->getHeaders()->addHeaderLine(
'WWW-Authenticate',
'Basic realm="' . addslashes($this->realm) . '"'
);
$response = new HttpResponse();
$response->setStatusCode(401);
$response->getHeaders()->addHeaderLine('WWW-Authenticate', 'Basic realm="' . addslashes($this->realm) . '"');

return true;
return $response;
}
}
20 changes: 8 additions & 12 deletions src/BaconAuthentication/Plugin/HttpPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,9 @@ public function setInputFilter(InputFilterInterface $inputFilter)
*
* @see ExtractionPluginInterface::extractCredentials()
* @param RequestInterface $request
* @param ResponseInterface $response
* @return null|Parameters
*/
public function extractCredentials(RequestInterface $request, ResponseInterface $response)
public function extractCredentials(RequestInterface $request)
{
if (!$request instanceof HttpRequest) {
return null;
Expand Down Expand Up @@ -139,21 +138,18 @@ public function extractCredentials(RequestInterface $request, ResponseInterface
*
* @see ChallengePluginInterface::challenge()
* @param RequestInterface $request
* @param ResponseInterface $response
* @return bool
* @return null|HttpResponse
*/
public function challenge(RequestInterface $request, ResponseInterface $response)
public function challenge(RequestInterface $request)
{
if (!$response instanceof HttpResponse) {
return false;
if (!$request instanceof HttpRequest) {
return null;
}

$response->getHeaders()->addHeaderLine(
'Location',
$this->loginFormUrl
);
$response = new HttpResponse();
$response->setStatusCode(302);
$response->getHeaders()->addHeaderLine('Location', $this->loginFormUrl);

return true;
return $response;
}
}
31 changes: 25 additions & 6 deletions src/BaconAuthentication/Plugin/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
use BaconAuthentication\AuthenticationEvent;
use BaconAuthentication\Result\Result;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\Session\ManagerInterface;
use Zend\Session\Container as SessionContainer;
use Zend\Stdlib\RequestInterface;

/**
* Plugin responsible for tracking the identity between multiple HTTP requests.
*/
class Session implements
EventAwarePluginInterface,
ResetPluginInterface
class Session implements ListenerAggregateInterface, ResetPluginInterface
{
/**
* Default session namespace.
Expand All @@ -33,6 +32,11 @@ class Session implements
*/
protected $session;

/**
* @var array
*/
protected $listeners = array();

/**
* @param string|null $namespace
* @param ManagerInterface $manager
Expand All @@ -53,10 +57,25 @@ public function __construct($namespace = null, ManagerInterface $manager = null)
* @param EventManagerInterface $events
* @return void
*/
public function attachToEvents(EventManagerInterface $events)
public function attach(EventManagerInterface $events)
{
$this->listeners[] = $events->attach(
AuthenticationEvent::EVENT_AUTHENTICATE_PRE,
array($this, 'checkSession')
);

$this->listeners[] = $events->attach(
AuthenticationEvent::EVENT_AUTHENTICATE_POST,
array($this, 'storeIdentifier')
);
}

public function detach(EventManagerInterface $events)
{
$events->attach(AuthenticationEvent::EVENT_AUTHENTICATE_PRE, array($this, 'checkSession'));
$events->attach(AuthenticationEvent::EVENT_AUTHENTICATE_POST, array($this, 'storeIdentifier'));
foreach ($this->listeners as $index => $callback) {
$events->detach($callback);
unset($this->listeners[$index]);
}
}

/**
Expand Down
4 changes: 0 additions & 4 deletions tests/BaconAuthenticationTest/AuthenticationEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ public static function setterGetterProvider()
'Request',
'Zend\Stdlib\RequestInterface',
),
array(
'Response',
'Zend\Stdlib\ResponseInterface',
),
array(
'Result',
'BaconAuthentication\Result\ResultInterface',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public function testAddEventAwarePlugin()
{
$service = new PluggableAuthenticationService();

$plugin = $this->getMock('BaconAuthentication\Plugin\EventAwarePluginInterface');
$plugin = $this->getMock('Zend\EventManager\ListenerAggregateInterface');
$plugin->expects($this->once())
->method('attachToEvents')
->method('attach')
->with($this->equalTo($service->getEventManager()));

$service->addPlugin($plugin);
Expand All @@ -49,8 +49,7 @@ public function testAuthenticateWithoutResult()

$service = new PluggableAuthenticationService();
$service->authenticate(
$this->getMock('Zend\Stdlib\RequestInterface'),
$this->getMock('Zend\Stdlib\ResponseInterface')
$this->getMock('Zend\Stdlib\RequestInterface')
);
}

Expand Down
Loading