Passed
Pull Request — master (#18)
by Alexander
01:12
created

HttpHeader::withHeaderName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\AuthenticationMethodInterface;
10
use Yiisoft\Auth\IdentityInterface;
11
use Yiisoft\Auth\IdentityRepositoryInterface;
12
13
/**
14
 * HttpHeader 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 AuthenticationMethodInterface
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 14
    public function __construct(IdentityRepositoryInterface $identityRepository)
38
    {
39 14
        $this->identityRepository = $identityRepository;
40 14
    }
41
42 10
    public function authenticate(ServerRequestInterface $request): ?IdentityInterface
43
    {
44 10
        $authToken = $this->getAuthToken($request);
45 10
        if ($authToken !== null) {
46 8
            return $this->identityRepository->findIdentityByToken($authToken, get_class($this));
47
        }
48
49 3
        return null;
50
    }
51
52 1
    public function challenge(ResponseInterface $response): ResponseInterface
53
    {
54 1
        return $response;
55
    }
56
57 1
    public function withHeaderName(string $name): self
58
    {
59 1
        $new = clone $this;
60 1
        $new->headerName = $name;
61 1
        return $new;
62
    }
63
64 2
    public function withPattern(string $pattern): self
65
    {
66 2
        $new = clone $this;
67 2
        $new->pattern = $pattern;
68 2
        return $new;
69
    }
70
71 10
    protected function getAuthToken(ServerRequestInterface $request): ?string
72
    {
73 10
        $authHeaders = $request->getHeader($this->headerName);
74 10
        $authHeader = \reset($authHeaders);
75 10
        if (!empty($authHeader)) {
76 9
            if (preg_match($this->pattern, $authHeader, $matches)) {
77 8
                $authHeader = $matches[1];
78
            } else {
79 1
                return null;
80
            }
81 8
            return $authHeader;
82
        }
83 2
        return null;
84
    }
85
}
86