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

BasicAuthentication   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 0
cts 18
cp 0
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A authorized() 0 9 2
A getErrorMessage() 0 4 1
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