2010-10-19 12:06:43 +01:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-15 13:29:43 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-26 20:34:11 +00:00
|
|
|
namespace Symfony\Component\Security\Http\EntryPoint;
|
2010-10-19 12:06:43 +01:00
|
|
|
|
2011-01-26 20:34:11 +00:00
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
2011-02-01 20:59:24 +00:00
|
|
|
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
|
2011-02-04 18:38:42 +00:00
|
|
|
use Symfony\Component\Security\Core\Exception\NonceExpiredException;
|
2010-10-19 12:06:43 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpKernel\Log\LoggerInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DigestAuthenticationEntryPoint starts an HTTP Digest authentication.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2010-10-19 12:06:43 +01:00
|
|
|
*/
|
|
|
|
class DigestAuthenticationEntryPoint implements AuthenticationEntryPointInterface
|
|
|
|
{
|
2011-03-07 17:17:46 +00:00
|
|
|
private $key;
|
|
|
|
private $realmName;
|
|
|
|
private $nonceValiditySeconds;
|
|
|
|
private $logger;
|
2010-10-19 12:06:43 +01:00
|
|
|
|
|
|
|
public function __construct($realmName, $key, $nonceValiditySeconds = 300, LoggerInterface $logger = null)
|
|
|
|
{
|
|
|
|
$this->realmName = $realmName;
|
|
|
|
$this->key = $key;
|
|
|
|
$this->nonceValiditySeconds = $nonceValiditySeconds;
|
|
|
|
$this->logger = $logger;
|
|
|
|
}
|
|
|
|
|
2011-03-18 17:26:55 +00:00
|
|
|
public function start(Request $request, AuthenticationException $authException = null)
|
2010-10-19 12:06:43 +01:00
|
|
|
{
|
|
|
|
$expiryTime = microtime(true) + $this->nonceValiditySeconds * 1000;
|
|
|
|
$signatureValue = md5($expiryTime.':'.$this->key);
|
|
|
|
$nonceValue = $expiryTime.':'.$signatureValue;
|
|
|
|
$nonceValueBase64 = base64_encode($nonceValue);
|
|
|
|
|
|
|
|
$authenticateHeader = sprintf('Digest realm="%s", qop="auth", nonce="%s"', $this->realmName, $nonceValueBase64);
|
|
|
|
|
|
|
|
if ($authException instanceof NonceExpiredException) {
|
|
|
|
$authenticateHeader = $authenticateHeader.', stale="true"';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $this->logger) {
|
|
|
|
$this->logger->debug(sprintf('WWW-Authenticate header sent to user agent: "%s"', $authenticateHeader));
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = new Response();
|
|
|
|
$response->headers->set('WWW-Authenticate', $authenticateHeader);
|
2011-06-25 16:43:23 +01:00
|
|
|
$response->setStatusCode(401, $authException ? $authException->getMessage() : null);
|
2010-10-19 12:06:43 +01:00
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|