Completed
Pull Request — master (#86)
by Michal
05:52
created

BasicAuthentication::authorized()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
c 0
b 0
f 0
rs 9.9666
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Tomaj\NetteApi\Authorization;
4
5
use Nette\Http\IRequest;
6
7
class BasicAuthentication implements ApiAuthorizationInterface
8
{
9
    /** @var array */
10
    private $authentications;
11
12
    /** @var IRequest */
13
    private $httpRequest;
14
15
    /**
16
     * @param array<string, string> $autentications - available username - password pairs
17
     * @param IRequest $httpRequest
18
     */
19
    public function __construct(array $autentications, IRequest $httpRequest)
20
    {
21
        $this->authentications = $autentications;
22
        $this->httpRequest = $httpRequest;
23
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function authorized(): bool
29
    {
30
        $urlScript = $this->httpRequest->getUrl();
31
        $authentication = $this->authentications[$urlScript->getUser()] ?? null;
32
        if (!$authentication) {
33
            return false;
34
        }
35
        return $authentication === $urlScript->getPassword();
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getErrorMessage(): ?string
42
    {
43
        return 'Incorrect username or password';
44
    }
45
}
46