1 | <?php |
||
29 | final class SecurityAuthenticator implements |
||
30 | SimplePreAuthenticatorInterface, |
||
31 | AuthenticationFailureHandlerInterface |
||
32 | { |
||
33 | |||
34 | /** |
||
35 | * Authentication can be required to use any service |
||
36 | * @var bool, |
||
37 | */ |
||
38 | protected $securityRequired; |
||
39 | |||
40 | /** |
||
41 | * Authentication can use a test user if no user found |
||
42 | * @var bool, |
||
43 | */ |
||
44 | protected $testUsername; |
||
45 | |||
46 | /** |
||
47 | * Authentication can allow not identified users to get information |
||
48 | * @var bool, |
||
49 | */ |
||
50 | protected $allowAnonymous; |
||
51 | |||
52 | /** |
||
53 | * @var AuthenticationProvider |
||
54 | */ |
||
55 | protected $userProvider; |
||
56 | |||
57 | /** |
||
58 | * @var StrategyInterface |
||
59 | */ |
||
60 | protected $extractionStrategy; |
||
61 | |||
62 | /** |
||
63 | * @var Logger |
||
64 | */ |
||
65 | protected $logger; |
||
66 | |||
67 | |||
68 | /** |
||
69 | * @param boolean $securityRequired user provider to use |
||
70 | * @param string $securityTestUsername user for testing |
||
71 | * @param boolean $allowAnonymous user provider to use |
||
72 | * @param AuthenticationProvider $userProvider user provider to use |
||
73 | * @param StrategyInterface $extractionStrategy auth strategy to use |
||
74 | * @param Logger $logger logger to user for logging errors |
||
75 | */ |
||
76 | 20 | public function __construct( |
|
93 | |||
94 | /** |
||
95 | * @param Request $request request to authenticate |
||
96 | * @param string $providerKey provider key to auth with |
||
97 | * |
||
98 | * @return PreAuthenticatedToken |
||
99 | */ |
||
100 | 10 | public function createToken(Request $request, $providerKey) |
|
101 | { |
||
102 | // look for an apikey query parameter |
||
103 | 10 | $apiKey = $this->extractionStrategy->apply($request); |
|
104 | |||
105 | 10 | $token = new PreAuthenticatedToken( |
|
106 | 10 | 'anon.', |
|
107 | $apiKey, |
||
108 | $providerKey, |
||
109 | 10 | $this->extractionStrategy->getRoles() |
|
110 | ); |
||
111 | |||
112 | 10 | $token->setAttribute('ipAddress', $request->getClientIp()); |
|
113 | |||
114 | 10 | return $token; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * Tries to authenticate the provided token |
||
119 | * |
||
120 | * @param TokenInterface $token token to authenticate |
||
121 | * @param UserProviderInterface $userProvider provider to auth against |
||
122 | * @param string $providerKey key to auth with |
||
123 | * |
||
124 | * @return PreAuthenticatedToken |
||
125 | */ |
||
126 | 4 | public function authenticateToken( |
|
127 | TokenInterface $token, |
||
128 | UserProviderInterface $userProvider, |
||
129 | $providerKey |
||
130 | ) { |
||
131 | 4 | $username = $token->getCredentials(); |
|
132 | 4 | $roles = array_map(array($this, 'objectRolesToArray'), $token->getRoles()); |
|
133 | 4 | $user = false; |
|
134 | |||
135 | // If no username in Strategy, check if required. |
||
136 | 4 | if ($this->securityRequired && !$username) { |
|
137 | $this->logger->warning('Authentication key is required.'); |
||
138 | throw new AuthenticationException('Authentication key is required.'); |
||
139 | } |
||
140 | |||
141 | 4 | if ($username) { |
|
142 | 4 | if (in_array(SecurityUser::ROLE_SUBNET, $roles)) { |
|
143 | $this->logger->info('Authentication, subnet user IP address: ' . $token->getAttribute('ipAddress')); |
||
144 | $user = new SubnetUser($username); |
||
145 | 4 | } elseif ($user = $this->userProvider->loadUserByUsername($username)) { |
|
146 | 2 | $roles[] = SecurityUser::ROLE_CONSULTANT; |
|
147 | } |
||
148 | } |
||
149 | |||
150 | // If no user, try to fetch the test user, else check if anonymous is enabled |
||
151 | 4 | if (!$user) { |
|
152 | 2 | if ($this->testUsername && $user = $this->userProvider->loadUserByUsername($this->testUsername)) { |
|
153 | $this->logger->info('Authentication, test user: ' . $this->testUsername); |
||
154 | $roles[] = SecurityUser::ROLE_TEST; |
||
155 | 2 | } elseif ($this->allowAnonymous) { |
|
156 | $this->logger->info('Authentication, loading anonymous user.'); |
||
157 | $user = new AnonymousUser(); |
||
158 | $roles[] = SecurityUser::ROLE_ANONYMOUS; |
||
159 | } |
||
160 | } |
||
161 | |||
162 | /** @var SecurityUser $securityUser */ |
||
163 | 4 | if ($user) { |
|
164 | 2 | $securityUser = new SecurityUser($user, $roles); |
|
165 | } else { |
||
166 | 2 | $this->logger->warning(sprintf('Authentication key "%s" could not be resolved.', $username)); |
|
167 | 2 | throw new AuthenticationException( |
|
168 | 2 | sprintf('Authentication key "%s" could not be resolved.', $username) |
|
169 | ); |
||
170 | } |
||
171 | |||
172 | 2 | return new PreAuthenticatedToken( |
|
173 | $securityUser, |
||
174 | $username, |
||
175 | $providerKey, |
||
176 | 2 | $securityUser->getRoles() |
|
177 | ); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Convert object role to string role. |
||
182 | * |
||
183 | * @param RoleInterface $role Object role |
||
184 | * @return null|string |
||
185 | */ |
||
186 | private function objectRolesToArray(RoleInterface $role) |
||
190 | |||
191 | /** |
||
192 | * @param TokenInterface $token token to check |
||
193 | * @param string $providerKey provider to check against |
||
194 | * |
||
195 | * @return bool |
||
196 | */ |
||
197 | 2 | public function supportsToken(TokenInterface $token, $providerKey) |
|
201 | |||
202 | /** |
||
203 | * This is called when an interactive authentication attempt fails. This is |
||
204 | * called by authentication listeners inheriting from |
||
205 | * AbstractAuthenticationListener. |
||
206 | * |
||
207 | * @param Request $request original request |
||
208 | * @param AuthenticationException $exception exception from auth attempt |
||
209 | * |
||
210 | * @return Response|null |
||
211 | */ |
||
212 | 2 | public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
|
219 | } |
||
220 |