-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHttpAuthenticator.php
More file actions
53 lines (38 loc) · 1.07 KB
/
HttpAuthenticator.php
File metadata and controls
53 lines (38 loc) · 1.07 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
51
52
53
<?php
declare(strict_types = 1);
/**
* This file is part of the HttpAuthExtension package
*
* @license MIT
* @author Petr Kessler (https://kesspess.cz)
* @link https://github.com/uestla/HttpAuthExtension
*/
namespace HttpAuthExtension;
use Nette\Http\IResponse;
final class HttpAuthenticator
{
/** @var IResponse */
private $response;
/** @var string */
private $username;
/** @var string */
private $password;
/** @var string */
private $title;
public function __construct(IResponse $response, string $username, string $password, string $title)
{
$this->response = $response;
$this->username = $username;
$this->password = $password;
$this->title = $title;
}
public function run(): void
{
if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] !== $this->username || $_SERVER['PHP_AUTH_PW'] !== $this->password) {
$this->response->setHeader('WWW-Authenticate', sprintf('Basic realm="%s"', $this->title));
$this->response->setCode(IResponse::S401_UNAUTHORIZED);
echo '<h1>Authentication failed.</h1>';
exit(1);
}
}
}