Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 2 changed files with 250 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/Adapter/Callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Authentication\Adapter;

use Exception;
use Zend\Authentication\Exception\InvalidArgumentException;
use Zend\Authentication\Exception\RuntimeException;
use Zend\Authentication\Result;

/**
* Authentication Adapter authenticates using callback function.
*
* The Callback function must return an identity on authentication success,
* and false on authentication failure.
*/
class Callback extends AbstractAdapter
{
/**
* @var callable
*/
protected $callback;

/**
* @param callable $callback The authentication callback
*/
public function __construct($callback = null)
{
if (null !== $callback) {
$this->setCallback($callback);
}
}

/**
* Authenticate using the provided callback
*
* @return Result The authentication result
* @throws RuntimeException
*/
public function authenticate()
{
$callback = $this->getCallback();
if (! $callback) {
throw new RuntimeException('No callback provided');
}

try {
$identity = call_user_func($callback, $this->getIdentity(), $this->getCredential());
} catch (Exception $e) {
return new Result(Result::FAILURE_UNCATEGORIZED, null, array($e->getMessage()));
}

if (! $identity) {
return new Result(Result::FAILURE, null, array('Authentication failure'));
}

return new Result(Result::SUCCESS, $identity, array('Authentication success'));
}

/**
* Gets the value of callback.
*
* @return null|callable
*/
public function getCallback()
{
return $this->callback;
}

/**
* Sets the value of callback.
*
* @param callable $callback the callback
* @throws InvalidArgumentException
*/
public function setCallback($callback)
{
if (! is_callable($callback)) {
throw new InvalidArgumentException('Invalid callback provided');
}

$this->callback = $callback;
}
}
160 changes: 160 additions & 0 deletions test/Adapter/CallbackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Authentication\Adapter;

use Exception;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\Authentication\Adapter\Callback;
use Zend\Authentication\Result;

class CallbackTest extends TestCase
{
/**
* Callback authentication adapter
*
* @var Callback
*/
protected $adapter = null;

/**
* Set up test configuration
*/
public function setUp()
{
$this->setupAuthAdapter();
}

public function tearDown()
{
$this->adapter = null;
}

protected function setupAuthAdapter()
{
$this->adapter = new Callback();
}

/**
* Ensures expected behavior for an invalid callback
*/
public function testSetCallbackThrowsException()
{
$this->setExpectedException(
'Zend\Authentication\Exception\InvalidArgumentException',
'Invalid callback provided'
);
$this->adapter->setCallback('This is not a valid callback');
}

/**
* Ensures setter/getter behaviour for callback
*/
public function testCallbackSetGetMethods()
{
$callback = function () {
};
$this->adapter->setCallback($callback);
$this->assertEquals($callback, $this->adapter->getCallback());
}

/**
* Ensures constructor sets callback if provided
*/
public function testClassConstructorSetCallback()
{
$callback = function () {
};
$adapter = new Callback($callback);
$this->assertEquals($callback, $adapter->getCallback());
}

/**
* Ensures authenticate throws Exception if no callback is defined
*/
public function testAuthenticateThrowsException()
{
$this->setExpectedException(
'Zend\Authentication\Exception\RuntimeException',
'No callback provided'
);
$this->adapter->authenticate();
}

/**
* Ensures identity and credential are provided as arguments to callback
*/
public function testAuthenticateProvidesCallbackWithIdentityAndCredentials()
{
$adapter = $this->adapter;
$adapter->setIdentity('testIdentity');
$adapter->setCredential('testCredential');
$that = $this;
$callback = function ($identity, $credential) use ($that, $adapter) {
$that->assertEquals($identity, $adapter->getIdentity());
$that->assertEquals($credential, $adapter->getCredential());
};
$this->adapter->setCallback($callback);
$this->adapter->authenticate();
}

/**
* Ensures authentication result is invalid when callback throws exception
*/
public function testAuthenticateResultIfCallbackThrows()
{
$adapter = $this->adapter;
$exception = new Exception('Callback Exception');
$callback = function () use ($exception) {
throw $exception;
};
$adapter->setCallback($callback);
$result = $adapter->authenticate();
$this->assertFalse($result->isValid());
$this->assertEquals(Result::FAILURE_UNCATEGORIZED, $result->getCode());
$this->assertEquals(array($exception->getMessage()), $result->getMessages());
}

/**
* Ensures authentication result is invalid when callback returns falsy value
*/
public function testAuthenticateResultIfCallbackReturnsFalsy()
{
$that = $this;
$adapter = $this->adapter;
$falsyValues = array(false, null, '', '0', array(), 0, 0.0);
array_map(function ($falsy) use ($that, $adapter) {
$callback = function () use ($falsy) {
return $falsy;
};
$adapter->setCallback($callback);
$result = $adapter->authenticate();
$that->assertFalse($result->isValid());
$that->assertEquals(Result::FAILURE, $result->getCode());
$that->assertEquals(array('Authentication failure'), $result->getMessages());
}, $falsyValues);
}

/**
* Ensures authentication result is valid when callback returns truthy value
*/
public function testAuthenticateResultIfCallbackReturnsIdentity()
{
$adapter = $this->adapter;
$callback = function () {
return 'identity';
};
$adapter->setCallback($callback);
$result = $adapter->authenticate();
$this->assertTrue($result->isValid());
$this->assertEquals(Result::SUCCESS, $result->getCode());
$this->assertEquals('identity', $result->getIdentity());
$this->assertEquals(array('Authentication success'), $result->getMessages());
}
}

0 comments on commit 73755f7

Please sign in to comment.