• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

contributte / http / #120

pending completion
#120

push

github

f3l1x
Composer: allow codesniffer

60 of 97 relevant lines covered (61.86%)

0.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

27.27
/src/Auth/BasicAuthenticator.php
1
<?php declare(strict_types = 1);
2

3
namespace Contributte\Http\Auth;
4

5
use Nette\Http\IRequest;
6
use Nette\Http\IResponse;
7
use Tracy\Debugger;
8

9
class BasicAuthenticator
10
{
11

12
        /** @var string */
13
        private $title;
14

15
        /** @var mixed[] */
16
        private $users = [];
17

18
        public function __construct(string $title)
19
        {
20
                $this->title = $title;
1✔
21
        }
1✔
22

23
        public function addUser(string $user, string $password, bool $unsecured): self
24
        {
25
                $this->users[$user] = [
1✔
26
                        'password' => $password,
1✔
27
                        'unsecured' => $unsecured,
1✔
28
                ];
29
                return $this;
1✔
30
        }
31

32
        public function authenticate(IRequest $request, IResponse $response): void
33
        {
34
                $user = $request->getUrl()->getUser();
×
35
                $password = $request->getUrl()->getPassword();
×
36

37
                if (!$this->auth($user, $password)) {
×
38
                        if (class_exists(Debugger::class)) {
×
39
                                Debugger::$productionMode = true;
×
40
                        }
41

42
                        $response->setHeader('WWW-Authenticate', sprintf('Basic realm="%s"', $this->title));
×
43
                        $response->setCode(IResponse::S401_UNAUTHORIZED);
×
44

45
                        echo '<h1>Authentication failed.</h1>';
×
46
                        die;
×
47
                }
48
        }
×
49

50
        protected function auth(string $user, string $password): bool
51
        {
52
                if (!isset($this->users[$user])) {
×
53
                        return false;
×
54
                }
55

56
                if (
57
                        ($this->users[$user]['unsecured'] === true && !hash_equals($password, $this->users[$user]['password'])) ||
×
58
                        ($this->users[$user]['unsecured'] === false && !password_verify($password, $this->users[$user]['password']))
×
59
                ) {
60
                        return false;
×
61
                }
62

63
                return true;
×
64
        }
65

66
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc