Passed
Pull Request — master (#37)
by Aleksei
03:02 queued 45s
created

HttpHeader::withTokenType()   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\IdentityWithTokenRepositoryInterface;
12
13
use function reset;
14
15
/**
16
 * HttpHeader supports HTTP authentication through HTTP Headers.
17
 *
18
 * The default implementation of HttpHeader uses the
19
 * {@see \Yiisoft\Auth\IdentityWithTokenRepositoryInterface::findIdentityByToken()}
20
 * and passes the value of the `X-Api-Key` header. This implementation is used mainly for authenticating API clients.
21
 */
22
class HttpHeader implements AuthenticationMethodInterface
23
{
24
    protected string $headerName = 'X-Api-Key';
25
    private ?string $tokenType = null;
26
27
    /**
28
     * @var string A pattern to use to extract the HTTP authentication value.
29
     */
30
    protected string $pattern = '/(.*)/';
31
32
    protected IdentityWithTokenRepositoryInterface $identityRepository;
33
34 18
    public function __construct(IdentityWithTokenRepositoryInterface $identityRepository)
35
    {
36 18
        $this->identityRepository = $identityRepository;
37 18
    }
38
39 12
    public function authenticate(ServerRequestInterface $request): ?IdentityInterface
40
    {
41 12
        $authToken = $this->getAuthenticationToken($request);
42 12
        if ($authToken !== null) {
43 10
            return $this->identityRepository->findIdentityByToken($authToken, $this->tokenType);
44
        }
45
46 3
        return null;
47
    }
48
49 1
    public function challenge(ResponseInterface $response): ResponseInterface
50
    {
51 1
        return $response;
52
    }
53
54
    /**
55
     * @param string $name The HTTP header name.
56
     *
57
     * @return $this
58
     *
59
     * @psalm-immutable
60
     */
61 3
    public function withHeaderName(string $name): self
62
    {
63 3
        $new = clone $this;
64 3
        $new->headerName = $name;
65 3
        return $new;
66
    }
67
68
    /**
69
     * @param string|null $type Identity token type
70
     *
71
     * @return $this
72
     *
73
     * @psalm-immutable
74
     */
75 4
    public function withTokenType(?string $type): self
76
    {
77 4
        $new = clone $this;
78 4
        $new->tokenType = $type;
79 4
        return $new;
80
    }
81
82
    /**
83
     * @param string $pattern A pattern to use to extract the HTTP authentication value.
84
     *
85
     * @return self
86
     *
87
     * @psalm-immutable
88
     */
89 4
    public function withPattern(string $pattern): self
90
    {
91 4
        $new = clone $this;
92 4
        $new->pattern = $pattern;
93 4
        return $new;
94
    }
95
96 12
    protected function getAuthenticationToken(ServerRequestInterface $request): ?string
97
    {
98 12
        $authHeaders = $request->getHeader($this->headerName);
99 12
        $authHeader = reset($authHeaders);
100 12
        if (!empty($authHeader)) {
101 11
            if (preg_match($this->pattern, $authHeader, $matches)) {
102 10
                $authHeader = $matches[1];
103
            } else {
104 1
                return null;
105
            }
106 10
            return $authHeader;
107
        }
108 2
        return null;
109
    }
110
}
111