|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Http\Message\Authentication; |
|
4
|
|
|
|
|
5
|
|
|
use Http\Message\Authentication; |
|
6
|
|
|
use Psr\Http\Message\RequestInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Authenticate a PSR-7 Request if the reuqest is matching. |
|
10
|
|
|
* |
|
11
|
|
|
* @author Márk Sági-Kazár <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
final class Matching implements Authentication |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Authentication |
|
17
|
|
|
*/ |
|
18
|
|
|
private $authentication; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var callable |
|
22
|
|
|
*/ |
|
23
|
|
|
private $matcher; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param Authentication $authentication |
|
27
|
|
|
* @param callable|null $matcher |
|
28
|
|
|
*/ |
|
29
|
9 |
|
public function __construct(Authentication $authentication, callable $matcher = null) |
|
30
|
|
|
{ |
|
31
|
9 |
|
if (is_null($matcher)) { |
|
32
|
|
|
$matcher = function () { |
|
33
|
|
|
return true; |
|
34
|
|
|
}; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
9 |
|
$this->authentication = $authentication; |
|
38
|
9 |
|
$this->matcher = $matcher; |
|
39
|
9 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Returns the authentication. |
|
43
|
|
|
* |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function getAuthentication() |
|
47
|
|
|
{ |
|
48
|
2 |
|
return $this->authentication; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Sets the authentication. |
|
53
|
|
|
* |
|
54
|
|
|
* @param Authentication $authentication |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function setAuthentication(Authentication $authentication) |
|
57
|
|
|
{ |
|
58
|
1 |
|
$this->authentication = $authentication; |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns the matcher. |
|
63
|
|
|
* |
|
64
|
|
|
* @return callable |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public function getMatcher() |
|
67
|
|
|
{ |
|
68
|
2 |
|
return $this->matcher; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Sets the matcher. |
|
73
|
|
|
* |
|
74
|
|
|
* @param callable $matcher |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public function setMatcher(callable $matcher) |
|
77
|
|
|
{ |
|
78
|
2 |
|
$this->matcher = $matcher; |
|
79
|
2 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
2 |
|
public function authenticate(RequestInterface $request) |
|
85
|
|
|
{ |
|
86
|
2 |
|
if (!call_user_func($this->matcher, $request)) { |
|
87
|
1 |
|
return $request; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
return $this->authentication->authenticate($request); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Creates a matching authentication for an URL. |
|
95
|
|
|
* |
|
96
|
|
|
* @param Authentication $authentication |
|
97
|
|
|
* @param string $url |
|
98
|
|
|
* |
|
99
|
|
|
* @return self |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function createUrlMatcher(Authentication $authentication, $url) |
|
102
|
|
|
{ |
|
103
|
1 |
|
$matcher = function ($request) use ($url) { |
|
104
|
|
|
return preg_match($url, $request->getRequestTarget()); |
|
105
|
1 |
|
}; |
|
106
|
|
|
|
|
107
|
1 |
|
return new static($authentication, $matcher); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|