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

Commit

Permalink
Merge branch 'feature/zend-authentication-validator' of git://github.…
Browse files Browse the repository at this point in the history
…com/mwillbanks/zf2 into feature/3420
  • Loading branch information
Show file tree
Hide file tree
Showing 8 changed files with 555 additions and 140 deletions.
78 changes: 78 additions & 0 deletions src/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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 Zend\Authentication\Adapter;

/**
* @category Zend
* @package Zend_Authentication
* @subpackage Adapter
*/
abstract class AbstractAdapter implements ValidatableAdapterInterface
{

/**
* @var mixed
*/
protected $credential;

/**
* @var mixed
*/
protected $identity;

/**
* Returns the credential of the account being authenticated, or
* NULL if none is set.
*
* @return mixed
*/
public function getCredential()
{
return $this->credential;
}

/**
* Sets the credential for binding
*
* @param mixed $credential
* @return AbstractAdapter
*/
public function setCredential($credential)
{
$this->credential = $credential;

return $this;
}

/**
* Returns the identity of the account being authenticated, or
* NULL if none is set.
*
* @return mixed
*/
public function getIdentity()
{
return $this->identity;
}

/**
* Sets the identity for binding
*
* @param mixed $identity
* @return AbstractAdpter
*/
public function setIdentity($identity)
{
$this->identity = $identity;

return $this;
}
}
41 changes: 1 addition & 40 deletions src/Adapter/DbTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @package Zend_Authentication
* @subpackage Adapter
*/
class DbTable implements AdapterInterface
class DbTable extends AbstractAdapter
{

/**
Expand Down Expand Up @@ -58,20 +58,6 @@ class DbTable implements AdapterInterface
*/
protected $credentialColumn = null;

/**
* $identity - Identity value
*
* @var string
*/
protected $identity = null;

/**
* $credential - Credential values
*
* @var string
*/
protected $credential = null;

/**
* $credentialTreatment - Treatment applied to the credential, such as MD5() or PASSWORD()
*
Expand Down Expand Up @@ -193,31 +179,6 @@ public function setCredentialTreatment($treatment)
return $this;
}

/**
* setIdentity() - set the value to be used as the identity
*
* @param string $value
* @return DbTable Provides a fluent interface
*/
public function setIdentity($value)
{
$this->identity = $value;
return $this;
}

/**
* setCredential() - set the credential value to be used, optionally can specify a treatment
* to be used, should be supplied in parametrized form, such as 'MD5(?)' or 'PASSWORD(?)'
*
* @param string $credential
* @return DbTable Provides a fluent interface
*/
public function setCredential($credential)
{
$this->credential = $credential;
return $this;
}

/**
* setAmbiguityIdentity() - sets a flag for usage of identical identities
* with unique credentials. It accepts integers (0, 1) or boolean (true,
Expand Down
59 changes: 24 additions & 35 deletions src/Adapter/Digest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @package Zend_Authentication
* @subpackage Adapter
*/
class Digest implements AdapterInterface
class Digest extends AbstractAdapter
{
/**
* Filename against which authentication queries are performed
Expand All @@ -34,36 +34,27 @@ class Digest implements AdapterInterface
*/
protected $realm;

/**
* Digest authentication user
*
* @var string
*/
protected $username;

/**
* Password for the user of the realm
*
* @var string
*/
protected $password;

/**
* Sets adapter options
*
* @param mixed $filename
* @param mixed $realm
* @param mixed $username
* @param mixed $password
* @param mixed $identity
* @param mixed $credential
*/
public function __construct($filename = null, $realm = null, $username = null, $password = null)
public function __construct($filename = null, $realm = null, $identity = null, $credential = null)
{
$options = array('filename', 'realm', 'username', 'password');
foreach ($options as $option) {
if (null !== $$option) {
$methodName = 'set' . ucfirst($option);
$this->$methodName($$option);
}
if ($filename !== null) {
$this->setFilename($filename);
}
if ($realm !== null) {
$this->setRealm($realm);
}
if ($identity !== null) {
$this->setIdentity($identity);
}
if ($credential !== null) {
$this->setCredential($credential);
}
}

Expand Down Expand Up @@ -118,7 +109,7 @@ public function setRealm($realm)
*/
public function getUsername()
{
return $this->username;
return $this->getIdentity();
}

/**
Expand All @@ -129,8 +120,7 @@ public function getUsername()
*/
public function setUsername($username)
{
$this->username = (string) $username;
return $this;
return $this->setIdentity($username);
}

/**
Expand All @@ -140,7 +130,7 @@ public function setUsername($username)
*/
public function getPassword()
{
return $this->password;
return $this->getCredential();
}

/**
Expand All @@ -151,8 +141,7 @@ public function getPassword()
*/
public function setPassword($password)
{
$this->password = (string) $password;
return $this;
return $this->setCredential($password);
}

/**
Expand All @@ -163,7 +152,7 @@ public function setPassword($password)
*/
public function authenticate()
{
$optionsRequired = array('filename', 'realm', 'username', 'password');
$optionsRequired = array('filename', 'realm', 'identity', 'credential');
foreach ($optionsRequired as $optionRequired) {
if (null === $this->$optionRequired) {
throw new Exception\RuntimeException("Option '$optionRequired' must be set before authentication");
Expand All @@ -177,14 +166,14 @@ public function authenticate()
throw new Exception\UnexpectedValueException("Cannot open '$this->filename' for reading", 0, $error);
}

$id = "$this->username:$this->realm";
$id = "$this->identity:$this->realm";
$idLength = strlen($id);

$result = array(
'code' => AuthenticationResult::FAILURE,
'identity' => array(
'realm' => $this->realm,
'username' => $this->username,
'username' => $this->identity,
),
'messages' => array()
);
Expand All @@ -195,7 +184,7 @@ public function authenticate()
break;
}
if (substr($line, 0, $idLength) === $id) {
if ($this->_secureStringCompare(substr($line, -32), md5("$this->username:$this->realm:$this->password"))) {
if ($this->_secureStringCompare(substr($line, -32), md5("$this->identity:$this->realm:$this->credential"))) {
$result['code'] = AuthenticationResult::SUCCESS;
} else {
$result['code'] = AuthenticationResult::FAILURE_CREDENTIAL_INVALID;
Expand All @@ -206,7 +195,7 @@ public function authenticate()
}

$result['code'] = AuthenticationResult::FAILURE_IDENTITY_NOT_FOUND;
$result['messages'][] = "Username '$this->username' and realm '$this->realm' combination not found";
$result['messages'][] = "Username '$this->identity' and realm '$this->realm' combination not found";
return new AuthenticationResult($result['code'], $result['identity'], $result['messages']);
}

Expand Down
Loading

0 comments on commit 1f3adc9

Please sign in to comment.