Passed
Push — master ( 318797...999269 )
by Alexander
14:33 queued 12:46
created

HttpHeader::getAuthToken()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 3
rs 9.9666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Auth\Method;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Yiisoft\Auth\AuthInterface;
10
use Yiisoft\Auth\IdentityInterface;
11
use Yiisoft\Auth\IdentityRepositoryInterface;
12
13
/**
14
 * HttpHeaderAuth supports HTTP authentication through HTTP Headers.
15
 *
16
 * The default implementation of HttpHeaderAuth uses the [[Yiisoft\Yii\Web\User\IdentityRepositoryInterface::findIdentityByToken()|findIdentityByToken()]]
17
 * method of the `user` application component and passes the value of the `X-Api-Key` header. This implementation is used
18
 * for authenticating API clients.
19
 */
20
class HttpHeader implements AuthInterface
21
{
22
    private const HEADER_NAME = 'X-Api-Key';
23
    private const PATTERN = '/(.*)/';
24
25
    /**
26
     * @var string the HTTP header name
27
     */
28
    protected string $headerName = self::HEADER_NAME;
29
30
    /**
31
     * @var string a pattern to use to extract the HTTP authentication value
32
     */
33
    protected string $pattern = self::PATTERN;
34
35
    protected IdentityRepositoryInterface $identityRepository;
36
37 7
    public function __construct(IdentityRepositoryInterface $identityRepository)
38
    {
39 7
        $this->identityRepository = $identityRepository;
40 7
    }
41
42 6
    public function authenticate(ServerRequestInterface $request): ?IdentityInterface
43
    {
44 6
        $authToken = $this->getAuthToken($request);
45 6
        if ($authToken !== null) {
46 4
            return $this->identityRepository->findIdentityByToken($authToken, get_class($this));
47
        }
48
49 2
        return null;
50
    }
51
52 1
    public function challenge(ResponseInterface $response): ResponseInterface
53
    {
54 1
        return $response;
55
    }
56
57 1
    public function setHeaderName(string $name): void
58
    {
59 1
        $this->headerName = $name;
60 1
    }
61
62 2
    public function setPattern(string $pattern): void
63
    {
64 2
        $this->pattern = $pattern;
65 2
    }
66
67 6
    protected function getAuthToken(ServerRequestInterface $request): ?string
68
    {
69 6
        $authHeaders = $request->getHeader($this->headerName);
70 6
        $authHeader = \reset($authHeaders);
71 6
        if (!empty($authHeader)) {
72 5
            if (preg_match($this->pattern, $authHeader, $matches)) {
73 4
                $authHeader = $matches[1];
74
            } else {
75 1
                return null;
76
            }
77 4
            return $authHeader;
78
        }
79 1
        return null;
80
    }
81
}
82