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