Skip to content

Commit

Permalink
Implement #6: remove Zend_Auth_Result class and usefproject\amf\auth\…
Browse files Browse the repository at this point in the history
… AuthResult instead
  • Loading branch information
Bui Sy Nguyen committed Jan 21, 2016
1 parent a8b02eb commit b72069d
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 122 deletions.
13 changes: 6 additions & 7 deletions fproject/Zend/Amf/Adobe/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
/** @see Zend_Acl */
require_once 'Zend/Acl.php';

/** @see Zend_Auth_Result */
require_once 'Zend/Auth/Result.php';

/** @see Zend_Xml_Security */
require_once 'Zend/Xml/Security.php';

use fproject\amf\auth\AuthResult;

/**
* This class implements authentication against XML file with roles for Flex Builder.
*
Expand Down Expand Up @@ -99,7 +98,7 @@ public function getAcl()
* Perform authentication
*
* @throws \fproject\amf\AmfException
* @return Zend_Auth_Result
* @return AuthResult
* @see Zend_Auth_Adapter_Interface#authenticate()
*/
public function authenticate()
Expand All @@ -110,15 +109,15 @@ public function authenticate()
}

if(!isset($this->_users[$this->_username])) {
return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND,
return new AuthResult(AuthResult::FAILURE_IDENTITY_NOT_FOUND,
null,
array('Username not found')
);
}

$user = $this->_users[$this->_username];
if($user["password"] != $this->_password) {
return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID,
return new AuthResult(AuthResult::FAILURE_CREDENTIAL_INVALID,
null,
array('Authentication failed')
);
Expand All @@ -127,6 +126,6 @@ public function authenticate()
$id = new stdClass();
$id->role = $user["role"];
$id->name = $this->_username;
return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $id);
return new AuthResult(AuthResult::SUCCESS, $id);
}
}
4 changes: 2 additions & 2 deletions fproject/Zend/Amf/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ protected function _errorMessage($objectEncoding, $message, $description, $detai
*
* @param string $userId
* @param string $password
* @return bool|Zend_Auth_Result
* @return bool|\fproject\amf\auth\AuthResult
* @throws \fproject\amf\AmfException
*
*/
Expand Down Expand Up @@ -512,7 +512,7 @@ protected function _handle(Zend_Amf_Request $request)
$headers[Zend_Amf_Constants::CREDENTIALS_HEADER]->userid,
$headers[Zend_Amf_Constants::CREDENTIALS_HEADER]->password
);
if ($authResult === true || $authResult->getCode() == Zend_Auth_Result::SUCCESS) {
if ($authResult === true || $authResult->getCode() == \fproject\amf\auth\AuthResult::SUCCESS) {
// use RequestPersistentHeader to clear credentials
$response->addAmfHeader(
new Zend_Amf_Value_MessageHeader(
Expand Down
2 changes: 1 addition & 1 deletion fproject/Zend/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function setStorage(AuthStorageInterface $storage)
* Authenticates against the supplied adapter
*
* @param Zend_Auth_Adapter_Interface $adapter
* @return Zend_Auth_Result
* @return \fproject\amf\auth\AuthResult
*/
public function authenticate(Zend_Auth_Adapter_Interface $adapter)
{
Expand Down
30 changes: 13 additions & 17 deletions fproject/Zend/Auth/Adapter/DbTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@
*/
require_once 'Zend/Db/Adapter/Abstract.php';

/**
* @see Zend_Auth_Result
*/
require_once 'Zend/Auth/Result.php';

use fproject\amf\auth\AuthResult;

/**
* @category Zend
Expand Down Expand Up @@ -359,15 +355,15 @@ public function getResultRowObject($returnColumns = null, $omitColumns = null)
* table and attempt to find a record matching the provided identity.
*
* @throws \fproject\amf\AmfException if answering the authentication query is impossible
* @return Zend_Auth_Result
* @return AuthResult
*/
public function authenticate()
{
$this->_authenticateSetup();
$dbSelect = $this->_authenticateCreateSelect();
$resultIdentities = $this->_authenticateQuerySelect($dbSelect);

if ( ($authResult = $this->_authenticateValidateResultSet($resultIdentities)) instanceof Zend_Auth_Result) {
if ( ($authResult = $this->_authenticateValidateResultSet($resultIdentities)) instanceof AuthResult) {
return $authResult;
}

Expand Down Expand Up @@ -415,7 +411,7 @@ protected function _authenticateSetup()
}

$this->_authenticateResultInfo = array(
'code' => Zend_Auth_Result::FAILURE,
'code' => AuthResult::FAILURE,
'identity' => $this->_identity,
'messages' => array()
);
Expand Down Expand Up @@ -490,17 +486,17 @@ protected function _authenticateQuerySelect(Zend_Db_Select $dbSelect)
* certain that only one record was returned in the resultset
*
* @param array $resultIdentities
* @return true|Zend_Auth_Result
* @return true|AuthResult
*/
protected function _authenticateValidateResultSet(array $resultIdentities)
{

if (count($resultIdentities) < 1) {
$this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND;
$this->_authenticateResultInfo['code'] = AuthResult::FAILURE_IDENTITY_NOT_FOUND;
$this->_authenticateResultInfo['messages'][] = 'A record with the supplied identity could not be found.';
return $this->_authenticateCreateAuthResult();
} elseif (count($resultIdentities) > 1 && false === $this->getAmbiguityIdentity()) {
$this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_IDENTITY_AMBIGUOUS;
$this->_authenticateResultInfo['code'] = AuthResult::FAILURE_IDENTITY_AMBIGUOUS;
$this->_authenticateResultInfo['messages'][] = 'More than one record matches the supplied identity.';
return $this->_authenticateCreateAuthResult();
}
Expand All @@ -514,35 +510,35 @@ protected function _authenticateValidateResultSet(array $resultIdentities)
* identity provided to this adapter.
*
* @param array $resultIdentity
* @return Zend_Auth_Result
* @return AuthResult
*/
protected function _authenticateValidateResult($resultIdentity)
{
$zendAuthCredentialMatchColumn = $this->_zendDb->foldCase('zend_auth_credential_match');

if ($resultIdentity[$zendAuthCredentialMatchColumn] != '1') {
$this->_authenticateResultInfo['code'] = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
$this->_authenticateResultInfo['code'] = AuthResult::FAILURE_CREDENTIAL_INVALID;
$this->_authenticateResultInfo['messages'][] = 'Supplied credential is invalid.';
return $this->_authenticateCreateAuthResult();
}

unset($resultIdentity[$zendAuthCredentialMatchColumn]);
$this->_resultRow = $resultIdentity;

$this->_authenticateResultInfo['code'] = Zend_Auth_Result::SUCCESS;
$this->_authenticateResultInfo['code'] = AuthResult::SUCCESS;
$this->_authenticateResultInfo['messages'][] = 'Authentication successful.';
return $this->_authenticateCreateAuthResult();
}

/**
* _authenticateCreateAuthResult() - Creates a Zend_Auth_Result object from
* _authenticateCreateAuthResult() - Creates a AuthResult object from
* the information that has been collected during the authenticate() attempt.
*
* @return Zend_Auth_Result
* @return AuthResult
*/
protected function _authenticateCreateAuthResult()
{
return new Zend_Auth_Result(
return new AuthResult(
$this->_authenticateResultInfo['code'],
$this->_authenticateResultInfo['identity'],
$this->_authenticateResultInfo['messages']
Expand Down
15 changes: 8 additions & 7 deletions fproject/Zend/Auth/Adapter/Digest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/
require_once 'Zend/Auth/Adapter/Interface.php';

use fproject\amf\auth\AuthResult;

/**
* @category Zend
Expand Down Expand Up @@ -176,7 +177,7 @@ public function setPassword($password)
* Defined by Zend_Auth_Adapter_Interface
*
* @throws \fproject\amf\AmfException
* @return Zend_Auth_Result
* @return AuthResult
*/
public function authenticate()
{
Expand All @@ -195,7 +196,7 @@ public function authenticate()
$idLength = strlen($id);

$result = array(
'code' => Zend_Auth_Result::FAILURE,
'code' => AuthResult::FAILURE,
'identity' => array(
'realm' => $this->_realm,
'username' => $this->_username,
Expand All @@ -206,18 +207,18 @@ public function authenticate()
while ($line = trim(fgets($fileHandle))) {
if (substr($line, 0, $idLength) === $id) {
if ($this->_secureStringCompare(substr($line, -32), md5("$this->_username:$this->_realm:$this->_password"))) {
$result['code'] = Zend_Auth_Result::SUCCESS;
$result['code'] = AuthResult::SUCCESS;
} else {
$result['code'] = Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID;
$result['code'] = AuthResult::FAILURE_CREDENTIAL_INVALID;
$result['messages'][] = 'Password incorrect';
}
return new Zend_Auth_Result($result['code'], $result['identity'], $result['messages']);
return new AuthResult($result['code'], $result['identity'], $result['messages']);
}
}

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

/**
Expand Down
25 changes: 13 additions & 12 deletions fproject/Zend/Auth/Adapter/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/
require_once 'Zend/Auth/Adapter/Interface.php';

use fproject\amf\auth\AuthResult;

/**
* HTTP Authentication Adapter
Expand Down Expand Up @@ -335,7 +336,7 @@ public function getResponse()
* Authenticate
*
* @throws \fproject\amf\AmfException
* @return Zend_Auth_Result
* @return AuthResult
*/
public function authenticate()
{
Expand Down Expand Up @@ -363,8 +364,8 @@ public function authenticate()
// answer with only the selected auth scheme.
if (!in_array($clientScheme, $this->_supportedSchemes)) {
$this->_response->setHttpResponseCode(400);
return new Zend_Auth_Result(
Zend_Auth_Result::FAILURE_UNCATEGORIZED,
return new AuthResult(
AuthResult::FAILURE_UNCATEGORIZED,
array(),
array('Client requested an incorrect or unsupported authentication scheme')
);
Expand Down Expand Up @@ -396,7 +397,7 @@ public function authenticate()
* Sets a 401 or 407 Unauthorized response code, and creates the
* appropriate Authenticate header(s) to prompt for credentials.
*
* @return Zend_Auth_Result Always returns a non-identity Auth result
* @return AuthResult Always returns a non-identity Auth result
*/
protected function _challengeClient()
{
Expand All @@ -417,8 +418,8 @@ protected function _challengeClient()
if (in_array('digest', $this->_acceptSchemes)) {
$this->_response->setHeader($headerName, $this->_digestHeader());
}
return new Zend_Auth_Result(
Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID,
return new AuthResult(
AuthResult::FAILURE_CREDENTIAL_INVALID,
array(),
array('Invalid or absent credentials; challenging client')
);
Expand Down Expand Up @@ -462,7 +463,7 @@ protected function _digestHeader()
*
* @param string $header Client's Authorization header
* @throws \fproject\amf\AmfException
* @return Zend_Auth_Result
* @return AuthResult
*/
protected function _basicAuth($header)
{
Expand Down Expand Up @@ -496,7 +497,7 @@ protected function _basicAuth($header)
$password = $this->_basicResolver->resolve($creds[0], $this->_realm);
if ($password && $this->_secureStringCompare($password, $creds[1])) {
$identity = array('username'=>$creds[0], 'realm'=>$this->_realm);
return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $identity);
return new AuthResult(AuthResult::SUCCESS, $identity);
} else {
return $this->_challengeClient();
}
Expand All @@ -507,7 +508,7 @@ protected function _basicAuth($header)
*
* @param string $header Client's Authorization header
* @throws \fproject\amf\AmfException
* @return Zend_Auth_Result Valid auth result only on successful auth
* @return AuthResult Valid auth result only on successful auth
*/
protected function _digestAuth($header)
{
Expand All @@ -521,8 +522,8 @@ protected function _digestAuth($header)
$data = $this->_parseDigestAuth($header);
if ($data === false) {
$this->_response->setHttpResponseCode(400);
return new Zend_Auth_Result(
Zend_Auth_Result::FAILURE_UNCATEGORIZED,
return new AuthResult(
AuthResult::FAILURE_UNCATEGORIZED,
array(),
array('Invalid Authorization header format')
);
Expand Down Expand Up @@ -586,7 +587,7 @@ protected function _digestAuth($header)
// a 401 code and exit to prevent access to the protected resource.
if ($this->_secureStringCompare($digest, $data['response'])) {
$identity = array('username'=>$data['username'], 'realm'=>$data['realm']);
return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $identity);
return new AuthResult(AuthResult::SUCCESS, $identity);
} else {
return $this->_challengeClient();
}
Expand Down
Loading

0 comments on commit b72069d

Please sign in to comment.