1 | <?php |
||
14 | class DigestAuthentication |
||
15 | { |
||
16 | use Utils\AuthenticationTrait; |
||
17 | use Utils\AttributeTrait; |
||
18 | |||
19 | const KEY = 'USERNAME'; |
||
20 | |||
21 | /** |
||
22 | * @var string|null The nonce value |
||
23 | */ |
||
24 | private $nonce; |
||
25 | |||
26 | /** |
||
27 | * Returns the username. |
||
28 | * |
||
29 | * @param ServerRequestInterface $request |
||
30 | * |
||
31 | * @return string|null |
||
32 | */ |
||
33 | public static function getUsername(ServerRequestInterface $request) |
||
34 | { |
||
35 | return self::getAttribute($request, self::KEY); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Set the nonce value. |
||
40 | * |
||
41 | * @param string $nonce |
||
42 | * |
||
43 | * @return self |
||
44 | */ |
||
45 | public function nonce($nonce) |
||
46 | { |
||
47 | $this->nonce = $nonce; |
||
48 | |||
49 | return $this; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Execute the middleware. |
||
54 | * |
||
55 | * @param ServerRequestInterface $request |
||
56 | * @param ResponseInterface $response |
||
57 | * @param callable $next |
||
58 | * |
||
59 | * @return ResponseInterface |
||
60 | */ |
||
61 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
62 | { |
||
63 | if ($this->login($request, $username)) { |
||
64 | return $next( |
||
65 | self::setAttribute($request, self::KEY, $username), |
||
66 | $response |
||
67 | ); |
||
68 | } |
||
69 | |||
70 | return $response |
||
71 | ->withStatus(401) |
||
72 | ->withHeader('WWW-Authenticate', 'Digest realm="'.$this->realm.'",qop="auth",nonce="'.($this->nonce ?: uniqid()).'",opaque="'.md5($this->realm).'"'); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Login or check the user credentials. |
||
77 | * |
||
78 | * @param ServerRequestInterface $request |
||
79 | * @param string|null $username |
||
80 | * |
||
81 | * @return bool |
||
82 | */ |
||
83 | private function login(ServerRequestInterface $request, &$username) |
||
84 | { |
||
85 | //Check header |
||
86 | $authorization = self::parseAuthorizationHeader($request->getHeaderLine('Authorization')); |
||
87 | |||
88 | if (!$authorization) { |
||
89 | return false; |
||
90 | } |
||
91 | |||
92 | //Check whether user exists |
||
93 | if (!isset($this->users[$authorization['username']])) { |
||
94 | return false; |
||
95 | } |
||
96 | |||
97 | $username = $authorization['username']; |
||
98 | |||
99 | //Check authentication |
||
100 | return $this->checkAuthentication($authorization, $request->getMethod(), $this->users[$username]); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Validates the user authentication. |
||
105 | * |
||
106 | * @param array $authorization |
||
107 | * @param string $method |
||
108 | * @param string $password |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | private function checkAuthentication(array $authorization, $method, $password) |
||
121 | |||
122 | /** |
||
123 | * Parses the authorization header for a basic authentication. |
||
124 | * |
||
125 | * @param string $header |
||
126 | * |
||
127 | * @return false|array |
||
128 | */ |
||
129 | private static function parseAuthorizationHeader($header) |
||
149 | } |
||
150 |