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

Commit

Permalink
Merge pull request zendframework/zendframework#2087 from davidwindell…
Browse files Browse the repository at this point in the history
…/patch-7

[Auth] Allow basic resolver to return AuthResult
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Adapter/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,11 @@ protected function _basicAuth($header)

$result = $this->basicResolver->resolve($creds[0], $this->realm, $creds[1]);

if ($result
if ($result instanceof Authentication\Result && $result->isValid()) {
return $result;
}

if (!$result instanceof Authentication\Result
&& !is_array($result)
&& $this->_secureStringCompare($result, $creds[1])
) {
Expand Down
24 changes: 24 additions & 0 deletions test/Adapter/Http/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,4 +480,28 @@ protected function _checkBadRequest($data)
// Make sure it set the right HTTP code
$this->assertEquals(400, $status);
}

public function testBasicAuthValidCredsWithCustomIdentityObjectResolverReturnsAuthResult()
{
$this->_basicResolver = new TestAsset\BasicAuthObjectResolver();

$result = $this->_doAuth('Basic ' . base64_encode('Bryce:ThisIsNotMyPassword'), 'basic');
$result = $result['result'];

$this->assertInstanceOf('Zend\\Authentication\\Result', $result);
$this->assertTrue($result->isValid());
}

public function testBasicAuthInvalidCredsWithCustomIdentityObjectResolverReturnsUnauthorizedResponse()
{
$this->_basicResolver = new TestAsset\BasicAuthObjectResolver();
$data = $this->_doAuth('Basic ' . base64_encode('David:ThisIsNotMyPassword'), 'basic');

$expected = array(
'type' => 'Basic ',
'realm' => 'realm="' . $this->_bothConfig['realm'] . '"',
);

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

namespace ZendTest\Authentication\Adapter\Http\TestAsset;

use Zend\Authentication\Result as AuthenticationResult;
use Zend\Authentication\Adapter\Http\ResolverInterface;

class BasicAuthObjectResolver implements ResolverInterface
{
public function resolve($username, $realm, $password = null)
{
if ($username == 'Bryce' && $password == 'ThisIsNotMyPassword') {
$identity = new \stdClass();

return new AuthenticationResult(
AuthenticationResult::SUCCESS,
$identity,
array('Authentication successful.')
);
}

return new AuthenticationResult(
AuthenticationResult::FAILURE,
null,
array('Authentication failed.')
);
}
}

0 comments on commit a185b42

Please sign in to comment.