-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDigest.php
More file actions
50 lines (41 loc) · 1.26 KB
/
Digest.php
File metadata and controls
50 lines (41 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace PhpMiddleware\HttpAuthentication\RequestBuilder;
use PhpMiddleware\HttpAuthentication\Util;
use Psr\Http\Message\RequestInterface;
/**
* @link https://tools.ietf.org/html/rfc2069
*/
final class Digest implements RequestBuilderInterface
{
private $username;
private $password;
private $realm;
private $nonce;
public function __construct($username, $password, $realm, $nonce)
{
$this->username = $username;
$this->password = $password;
$this->realm = $realm;
$this->nonce = $nonce;
}
/**
* @param RequestInterface $request
*
* @return RequestInterface
*/
public function authenticate(RequestInterface $request)
{
$uri = (string) $request->getUri();
$a1 = Util::md5Implode([$this->username, $this->realm, $this->password]);
$a2 = Util::md5Implode([$request->getMethod(), $uri]);
$response = Util::md5Implode([$a1, $this->nonce, $a2]);
$value = Util::buildHeader('Digest', [
'username' => $this->username,
'realm' => $this->realm,
'nonce' => $this->nonce,
'uri' => $uri,
'response' => $response,
]);
return $request->withHeader('Authorization', $value);
}
}