49 $this->nonce = uniqid();
50 $this->opaque = md5(
$realm);
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) {
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) {
224 unset($needed_parts[
$m[1]]);
227 return $needed_parts ? false :
$data;
An exception for terminatinating execution or to throw for unit testing.
HTTP Authentication base class.
HTTP Digest Authentication handler.
parseDigest($digest)
Parses the different pieces of the digest string into an array.
setQOP($qop)
Sets the quality of protection value.
validate()
Validates the digest challenge.
validatePassword($password)
Validates authentication through a password.
validateA1($A1)
Validates the user.
getUsername()
Returns the username for the request.
init()
Gathers all information from the headers.
requireLogin()
Returns an HTTP 401 header, forcing login.
getDigest()
This method returns the full digest string.
__construct($realm='SabreTooth', RequestInterface $request, ResponseInterface $response)
Initializes the object.
const QOP_AUTH
These constants are used in setQOP();.
The RequestInterface represents a HTTP request.
This interface represents a HTTP response.