1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Foundation\Security; |
13
|
|
|
|
14
|
|
|
use Jitamin\Bus\Event\AuthFailureEvent; |
15
|
|
|
use Jitamin\Bus\Event\AuthSuccessEvent; |
16
|
|
|
use Jitamin\Foundation\Base; |
17
|
|
|
use LogicException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Authentication Manager. |
21
|
|
|
*/ |
22
|
|
|
class AuthenticationManager extends Base |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Event names. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
const EVENT_SUCCESS = 'auth.success'; |
30
|
|
|
const EVENT_FAILURE = 'auth.failure'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* List of authentication providers. |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $providers = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Register a new authentication provider. |
41
|
|
|
* |
42
|
|
|
* @param AuthenticationProviderInterface $provider |
43
|
|
|
* |
44
|
|
|
* @return AuthenticationManager |
45
|
|
|
*/ |
46
|
|
|
public function register(AuthenticationProviderInterface $provider) |
47
|
|
|
{ |
48
|
|
|
$this->providers[$provider->getName()] = $provider; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Register a new authentication provider. |
55
|
|
|
* |
56
|
|
|
* @param string $name |
57
|
|
|
* |
58
|
|
|
* @return AuthenticationProviderInterface|OAuthAuthenticationProviderInterface|PasswordAuthenticationProviderInterface|PreAuthenticationProviderInterface|OAuthAuthenticationProviderInterface |
59
|
|
|
*/ |
60
|
|
|
public function getProvider($name) |
61
|
|
|
{ |
62
|
|
|
if (!isset($this->providers[$name])) { |
63
|
|
|
throw new LogicException('Authentication provider not found: '.$name); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->providers[$name]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Execute providers that are able to validate the current session. |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function checkCurrentSession() |
75
|
|
|
{ |
76
|
|
|
if ($this->userSession->isLogged()) { |
|
|
|
|
77
|
|
|
foreach ($this->filterProviders('SessionCheckProviderInterface') as $provider) { |
78
|
|
|
if (!$provider->isValidSession()) { |
79
|
|
|
$this->logger->debug('Invalidate session for '.$this->userSession->getUsername()); |
|
|
|
|
80
|
|
|
$this->sessionStorage->flush(); |
|
|
|
|
81
|
|
|
$this->preAuthentication(); |
82
|
|
|
|
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Execute pre-authentication providers. |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function preAuthentication() |
97
|
|
|
{ |
98
|
|
|
foreach ($this->filterProviders('PreAuthenticationProviderInterface') as $provider) { |
99
|
|
View Code Duplication |
if ($provider->authenticate() && $this->userProfile->initialize($provider->getUser())) { |
|
|
|
|
100
|
|
|
$this->dispatcher->dispatch(self::EVENT_SUCCESS, new AuthSuccessEvent($provider->getName())); |
|
|
|
|
101
|
|
|
|
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Execute username/password authentication providers. |
111
|
|
|
* |
112
|
|
|
* @param string $username |
113
|
|
|
* @param string $password |
114
|
|
|
* @param bool $fireEvent |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function passwordAuthentication($username, $password, $fireEvent = true) |
119
|
|
|
{ |
120
|
|
|
foreach ($this->filterProviders('PasswordAuthenticationProviderInterface') as $provider) { |
121
|
|
|
$provider->setUsername($username); |
122
|
|
|
$provider->setPassword($password); |
123
|
|
|
|
124
|
|
View Code Duplication |
if ($provider->authenticate() && $this->userProfile->initialize($provider->getUser())) { |
|
|
|
|
125
|
|
|
if ($fireEvent) { |
126
|
|
|
$this->dispatcher->dispatch(self::EVENT_SUCCESS, new AuthSuccessEvent($provider->getName())); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($fireEvent) { |
134
|
|
|
$this->dispatcher->dispatch(self::EVENT_FAILURE, new AuthFailureEvent($username)); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Perform OAuth2 authentication. |
142
|
|
|
* |
143
|
|
|
* @param string $name |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function oauthAuthentication($name) |
148
|
|
|
{ |
149
|
|
|
$provider = $this->getProvider($name); |
150
|
|
|
|
151
|
|
View Code Duplication |
if ($provider->authenticate() && $this->userProfile->initialize($provider->getUser())) { |
|
|
|
|
152
|
|
|
$this->dispatcher->dispatch(self::EVENT_SUCCESS, new AuthSuccessEvent($provider->getName())); |
|
|
|
|
153
|
|
|
|
154
|
|
|
return true; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$this->dispatcher->dispatch(self::EVENT_FAILURE, new AuthFailureEvent()); |
|
|
|
|
158
|
|
|
|
159
|
|
|
return false; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get the last Post-Authentication provider. |
164
|
|
|
* |
165
|
|
|
* @return PostAuthenticationProviderInterface |
166
|
|
|
*/ |
167
|
|
|
public function getPostAuthenticationProvider() |
168
|
|
|
{ |
169
|
|
|
$providers = $this->filterProviders('PostAuthenticationProviderInterface'); |
170
|
|
|
|
171
|
|
|
if (empty($providers)) { |
172
|
|
|
throw new LogicException('You must have at least one Post-Authentication Provider configured'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return array_pop($providers); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Filter registered providers by interface type. |
180
|
|
|
* |
181
|
|
|
* @param string $interface |
182
|
|
|
* |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
|
private function filterProviders($interface) |
186
|
|
|
{ |
187
|
|
|
$interface = '\Jitamin\Foundation\Security\\'.$interface; |
188
|
|
|
|
189
|
|
|
return array_filter($this->providers, function (AuthenticationProviderInterface $provider) use ($interface) { |
190
|
|
|
return is_a($provider, $interface); |
191
|
|
|
}); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.