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 with a multiple authentication methods. |
10
|
|
|
* |
11
|
|
|
* @author Márk Sági-Kazár <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
final class Chain implements Authentication |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Authentication[] |
17
|
|
|
*/ |
18
|
|
|
private $authenticationChain = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Authentication[] $authenticationChain |
22
|
|
|
*/ |
23
|
7 |
|
public function __construct(array $authenticationChain = []) |
24
|
|
|
{ |
25
|
7 |
|
$this->setAuthenticationChain($authenticationChain); |
26
|
7 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Adds an Authentication method to the chain. |
30
|
|
|
* |
31
|
|
|
* The order of authentication methods SHOULD NOT matter. |
32
|
|
|
* |
33
|
|
|
* @param Authentication $authentication |
34
|
|
|
*/ |
35
|
5 |
|
public function addAuthentication(Authentication $authentication) |
36
|
|
|
{ |
37
|
5 |
|
$this->authenticationChain[] = $authentication; |
38
|
5 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns the current authentication chain. |
42
|
|
|
* |
43
|
|
|
* @return Authentication[] |
44
|
|
|
*/ |
45
|
4 |
|
public function getAuthenticationChain() |
46
|
|
|
{ |
47
|
4 |
|
return $this->authenticationChain; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Replaces the current authentication chain. |
52
|
|
|
* |
53
|
|
|
* @param array $authenticationChain |
54
|
|
|
*/ |
55
|
7 |
|
public function setAuthenticationChain(array $authenticationChain) |
56
|
|
|
{ |
57
|
7 |
|
$this->clearAuthenticationChain(); |
58
|
|
|
|
59
|
7 |
|
foreach ($authenticationChain as $authentication) { |
60
|
5 |
|
$this->addAuthentication($authentication); |
61
|
7 |
|
} |
62
|
7 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Clears the authentication chain. |
66
|
|
|
*/ |
67
|
7 |
|
public function clearAuthenticationChain() |
68
|
|
|
{ |
69
|
7 |
|
$this->authenticationChain = []; |
70
|
7 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
1 |
|
public function authenticate(RequestInterface $request) |
76
|
|
|
{ |
77
|
1 |
|
foreach ($this->authenticationChain as $authentication) { |
78
|
1 |
|
$request = $authentication->authenticate($request); |
79
|
1 |
|
} |
80
|
|
|
|
81
|
1 |
|
return $request; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|