Skip to content

Commit

Permalink
Use hmac for digest nonces
Browse files Browse the repository at this point in the history
  • Loading branch information
ceeram committed Aug 26, 2017
1 parent e5808ba commit 82ac898
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/Auth/DigestAuthenticate.php
Expand Up @@ -17,6 +17,7 @@
use Cake\Controller\ComponentRegistry;
use Cake\Core\Configure;
use Cake\Http\ServerRequest;
use Cake\Utility\Security;

/**
* Digest Authentication adapter for AuthComponent.
Expand Down Expand Up @@ -250,7 +251,7 @@ public function loginHeaders(ServerRequest $request)
protected function generateNonce()
{
$expiryTime = microtime(true) + $this->getConfig('nonceLifetime');
$signatureValue = md5($expiryTime . ':' . $this->getConfig('secret'));
$signatureValue = hash_hmac('sha256', $expiryTime . ':' . $this->getConfig('secret'), Security::getSalt());
$nonceValue = $expiryTime . ':' . $signatureValue;

return base64_encode($nonceValue);
Expand All @@ -276,7 +277,8 @@ protected function validNonce($nonce)
if ($expires < microtime(true)) {
return false;
}
$check = hash_hmac('sha1', $expires . ':' . $this->getConfig('secret'), $this->getConfig('secret'));

return md5($expires . ':' . $this->getConfig('secret')) === $checksum;
return hash_equals($check, $checksum);
}
}
2 changes: 1 addition & 1 deletion tests/TestCase/Auth/DigestAuthenticateTest.php
Expand Up @@ -515,7 +515,7 @@ protected function generateNonce($secret = null, $expires = 300, $time = null)
$secret = $secret ?: Configure::read('Security.salt');
$time = $time ?: microtime(true);
$expiryTime = $time + $expires;
$signatureValue = md5($expiryTime . ':' . $secret);
$signatureValue = hash_hmac('sha1', $expiryTime . ':' . $secret, $secret);
$nonceValue = $expiryTime . ':' . $signatureValue;

return base64_encode($nonceValue);
Expand Down

0 comments on commit 82ac898

Please sign in to comment.