42 protected $qop = self::QOP_AUTH;
49 $this->nonce = uniqid();
50 $this->opaque = md5(
$realm);
51 parent::__construct(
$realm, $request, $response);
115 $this->A1 = md5($this->digestParts[
'username'] .
':' . $this->realm .
':' .
$password);
127 return $this->digestParts[
'username'];
138 $A2 = $this->request->getMethod() .
':' . $this->digestParts[
'uri'];
140 if ($this->digestParts[
'qop'] ==
'auth-int') {
142 if (!($this->qop & self::QOP_AUTHINT))
return false;
144 $body = $this->request->getBody($asString =
true);
145 $this->request->setBody($body);
146 $A2 .=
':' . md5($body);
150 if (!($this->qop & self::QOP_AUTH))
return false;
155 $validResponse = md5(
"{$this->A1}:{$this->digestParts['nonce']}:{$this->digestParts['nc']}:{$this->digestParts['cnonce']}:{$this->digestParts['qop']}:{$A2}");
157 return $this->digestParts[
'response'] == $validResponse;
172 switch ($this->qop) {
173 case self::QOP_AUTH :
176 case self::QOP_AUTHINT :
179 case self::QOP_AUTH | self::QOP_AUTHINT :
180 $qop =
'auth,auth-int';
184 $this->response->addHeader(
'WWW-Authenticate',
'Digest realm="' . $this->realm .
'",qop="' .
$qop .
'",nonce="' . $this->nonce .
'",opaque="' . $this->opaque .
'"');
185 $this->response->setStatus(401);
201 return $this->request->getHeader(
'Authorization');
217 $needed_parts = [
'nonce' => 1,
'nc' => 1,
'cnonce' => 1,
'qop' => 1,
'username' => 1,
'uri' => 1,
'response' => 1];
220 preg_match_all(
'@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $digest, $matches, PREG_SET_ORDER);
222 foreach ($matches as
$m) {
223 $data[$m[1]] = $m[2] ? $m[2] : $m[3];
224 unset($needed_parts[$m[1]]);
227 return $needed_parts ? false :
$data;
This interface represents a HTTP response.
The RequestInterface represents a HTTP request.
setQOP($qop)
Sets the quality of protection value.
__construct($realm='SabreTooth', RequestInterface $request, ResponseInterface $response)
Initializes the object.
validateA1($A1)
Validates the user.
const QOP_AUTH
These constants are used in setQOP();.
requireLogin()
Returns an HTTP 401 header, forcing login.
getDigest()
This method returns the full digest string.
HTTP Authentication base class.
HTTP Digest Authentication handler.
init()
Gathers all information from the headers.
parseDigest($digest)
Parses the different pieces of the digest string into an array.
validate()
Validates the digest challenge.
validatePassword($password)
Validates authentication through a password.
getUsername()
Returns the username for the request.