|
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 HttpHeader uses the {@see \Yiisoft\Auth\IdentityRepositoryInterface::findIdentityByToken()} |
|
17
|
|
|
* and passes the value of the `X-Api-Key` header. This implementation is used mainly for authenticating API clients. |
|
18
|
|
|
*/ |
|
19
|
|
|
class HttpHeader implements AuthenticationMethodInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var string The HTTP header name. |
|
23
|
|
|
*/ |
|
24
|
|
|
protected string $headerName = 'X-Api-Key'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string A pattern to use to extract the HTTP authentication value. |
|
28
|
|
|
*/ |
|
29
|
|
|
protected string $pattern = '/(.*)/'; |
|
30
|
|
|
|
|
31
|
|
|
protected IdentityRepositoryInterface $identityRepository; |
|
32
|
|
|
|
|
33
|
16 |
|
public function __construct(IdentityRepositoryInterface $identityRepository) |
|
34
|
|
|
{ |
|
35
|
16 |
|
$this->identityRepository = $identityRepository; |
|
36
|
16 |
|
} |
|
37
|
|
|
|
|
38
|
10 |
|
public function authenticate(ServerRequestInterface $request): ?IdentityInterface |
|
39
|
|
|
{ |
|
40
|
10 |
|
$authToken = $this->getAuthenticationToken($request); |
|
41
|
10 |
|
if ($authToken !== null) { |
|
42
|
8 |
|
return $this->identityRepository->findIdentityByToken($authToken, static::class); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
3 |
|
return null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
public function challenge(ResponseInterface $response): ResponseInterface |
|
49
|
|
|
{ |
|
50
|
1 |
|
return $response; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
3 |
|
public function withHeaderName(string $name): self |
|
54
|
|
|
{ |
|
55
|
3 |
|
$new = clone $this; |
|
56
|
3 |
|
$new->headerName = $name; |
|
57
|
3 |
|
return $new; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $pattern A pattern to use to extract the HTTP authentication value. |
|
62
|
|
|
* @return self |
|
63
|
|
|
*/ |
|
64
|
4 |
|
public function withPattern(string $pattern): self |
|
65
|
|
|
{ |
|
66
|
4 |
|
$new = clone $this; |
|
67
|
4 |
|
$new->pattern = $pattern; |
|
68
|
4 |
|
return $new; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
10 |
|
protected function getAuthenticationToken(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
|
|
|
|