Completed
Push — master ( 0728e2...7bf439 )
by Tomas
10:58 queued 10s
created

BearerTokenAuthorization::isValidIp()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 8.6186
c 0
b 0
f 0
cc 7
nc 6
nop 1
crap 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tomaj\NetteApi\Authorization;
6
7
use Tomaj\NetteApi\Misc\BearerTokenRepositoryInterface;
8
use Tomaj\NetteApi\Misc\IpDetectorInterface;
9
10
class BearerTokenAuthorization extends TokenAuthorization
11
{
12
   /**
13
     * BearerTokenAuthorization constructor.
14
     *
15
     * @param BearerTokenRepositoryInterface $tokenRepository
16
     * @param IpDetectorInterface            $ipDetector
17
     */
18
    public function __construct(BearerTokenRepositoryInterface $tokenRepository, IpDetectorInterface $ipDetector)
19
    {
20
        parent::__construct($tokenRepository, $ipDetector);
21
    }
22
23
    /**
24
     * Read HTTP reader with authorization token
25
     * If everything is ok, it return token. In other situations returns false and set errorMessage.
26
     *
27
     * @return string|null
28
     */
29
    protected function readAuthorizationToken(): ?string
30
    {
31
        if (!isset($_SERVER['HTTP_AUTHORIZATION'])) {
32
            $this->errorMessage = 'Authorization header HTTP_Authorization is not set';
33 33
            return null;
34
        }
35 33
        $parts = explode(' ', $_SERVER['HTTP_AUTHORIZATION']);
36 33
        if (count($parts) !== 2) {
37 33
            $this->errorMessage = 'Authorization header contains invalid structure';
38
            return null;
39
        }
40
        if (strtolower($parts[0]) !== 'bearer') {
41
            $this->errorMessage = 'Authorization header doesn\'t contain bearer token';
42 33
            return null;
43
        }
44 33
        return $parts[1];
45 33
    }
46
}
47