@@ -164,8 +164,8 @@ |
||
164 | 164 | } |
165 | 165 | |
166 | 166 | /** |
167 | - * @param array $states |
|
168 | - * @param IProvider $providers |
|
167 | + * @param string[] $states |
|
168 | + * @param IProvider[] $providers |
|
169 | 169 | */ |
170 | 170 | private function isProviderMissing(array $states, array $providers): bool { |
171 | 171 | $indexed = []; |
@@ -44,331 +44,331 @@ |
||
44 | 44 | |
45 | 45 | class Manager { |
46 | 46 | |
47 | - const SESSION_UID_KEY = 'two_factor_auth_uid'; |
|
48 | - const SESSION_UID_DONE = 'two_factor_auth_passed'; |
|
49 | - const REMEMBER_LOGIN = 'two_factor_remember_login'; |
|
50 | - |
|
51 | - /** @var ProviderLoader */ |
|
52 | - private $providerLoader; |
|
53 | - |
|
54 | - /** @var IRegistry */ |
|
55 | - private $providerRegistry; |
|
56 | - |
|
57 | - /** @var ISession */ |
|
58 | - private $session; |
|
59 | - |
|
60 | - /** @var IConfig */ |
|
61 | - private $config; |
|
62 | - |
|
63 | - /** @var IManager */ |
|
64 | - private $activityManager; |
|
65 | - |
|
66 | - /** @var ILogger */ |
|
67 | - private $logger; |
|
68 | - |
|
69 | - /** @var TokenProvider */ |
|
70 | - private $tokenProvider; |
|
71 | - |
|
72 | - /** @var ITimeFactory */ |
|
73 | - private $timeFactory; |
|
74 | - |
|
75 | - /** @var EventDispatcherInterface */ |
|
76 | - private $dispatcher; |
|
77 | - |
|
78 | - public function __construct(ProviderLoader $providerLoader, |
|
79 | - IRegistry $providerRegistry, ISession $session, IConfig $config, |
|
80 | - IManager $activityManager, ILogger $logger, TokenProvider $tokenProvider, |
|
81 | - ITimeFactory $timeFactory, EventDispatcherInterface $eventDispatcher) { |
|
82 | - $this->providerLoader = $providerLoader; |
|
83 | - $this->session = $session; |
|
84 | - $this->config = $config; |
|
85 | - $this->activityManager = $activityManager; |
|
86 | - $this->logger = $logger; |
|
87 | - $this->tokenProvider = $tokenProvider; |
|
88 | - $this->timeFactory = $timeFactory; |
|
89 | - $this->dispatcher = $eventDispatcher; |
|
90 | - $this->providerRegistry = $providerRegistry; |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Determine whether the user must provide a second factor challenge |
|
95 | - * |
|
96 | - * @param IUser $user |
|
97 | - * @return boolean |
|
98 | - */ |
|
99 | - public function isTwoFactorAuthenticated(IUser $user): bool { |
|
100 | - $twoFactorEnabled = ((int) $this->config->getUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 0)) === 0; |
|
101 | - |
|
102 | - if (!$twoFactorEnabled) { |
|
103 | - return false; |
|
104 | - } |
|
105 | - |
|
106 | - $providerStates = $this->providerRegistry->getProviderStates($user); |
|
107 | - $enabled = array_filter($providerStates); |
|
108 | - |
|
109 | - return $twoFactorEnabled && !empty($enabled); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Disable 2FA checks for the given user |
|
114 | - * |
|
115 | - * @param IUser $user |
|
116 | - */ |
|
117 | - public function disableTwoFactorAuthentication(IUser $user) { |
|
118 | - $this->config->setUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 1); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Enable all 2FA checks for the given user |
|
123 | - * |
|
124 | - * @param IUser $user |
|
125 | - */ |
|
126 | - public function enableTwoFactorAuthentication(IUser $user) { |
|
127 | - $this->config->deleteUserValue($user->getUID(), 'core', 'two_factor_auth_disabled'); |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Get a 2FA provider by its ID |
|
132 | - * |
|
133 | - * @param IUser $user |
|
134 | - * @param string $challengeProviderId |
|
135 | - * @return IProvider|null |
|
136 | - */ |
|
137 | - public function getProvider(IUser $user, string $challengeProviderId) { |
|
138 | - $providers = $this->getProviderSet($user)->getProviders(); |
|
139 | - return $providers[$challengeProviderId] ?? null; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Check if the persistant mapping of enabled/disabled state of each available |
|
144 | - * provider is missing an entry and add it to the registry in that case. |
|
145 | - * |
|
146 | - * @todo remove in Nextcloud 17 as by then all providers should have been updated |
|
147 | - * |
|
148 | - * @param string[] $providerStates |
|
149 | - * @param IProvider[] $providers |
|
150 | - * @param IUser $user |
|
151 | - * @return string[] the updated $providerStates variable |
|
152 | - */ |
|
153 | - private function fixMissingProviderStates(array $providerStates, |
|
154 | - array $providers, IUser $user): array { |
|
155 | - |
|
156 | - foreach ($providers as $provider) { |
|
157 | - if (isset($providerStates[$provider->getId()])) { |
|
158 | - // All good |
|
159 | - continue; |
|
160 | - } |
|
161 | - |
|
162 | - $enabled = $provider->isTwoFactorAuthEnabledForUser($user); |
|
163 | - if ($enabled) { |
|
164 | - $this->providerRegistry->enableProviderFor($provider, $user); |
|
165 | - } else { |
|
166 | - $this->providerRegistry->disableProviderFor($provider, $user); |
|
167 | - } |
|
168 | - $providerStates[$provider->getId()] = $enabled; |
|
169 | - } |
|
170 | - |
|
171 | - return $providerStates; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * @param array $states |
|
176 | - * @param IProvider $providers |
|
177 | - */ |
|
178 | - private function isProviderMissing(array $states, array $providers): bool { |
|
179 | - $indexed = []; |
|
180 | - foreach ($providers as $provider) { |
|
181 | - $indexed[$provider->getId()] = $provider; |
|
182 | - } |
|
183 | - |
|
184 | - $missing = []; |
|
185 | - foreach ($states as $providerId => $enabled) { |
|
186 | - if (!$enabled) { |
|
187 | - // Don't care |
|
188 | - continue; |
|
189 | - } |
|
190 | - |
|
191 | - if (!isset($indexed[$providerId])) { |
|
192 | - $missing[] = $providerId; |
|
193 | - $this->logger->alert("two-factor auth provider '$providerId' failed to load", |
|
194 | - [ |
|
195 | - 'app' => 'core', |
|
196 | - ]); |
|
197 | - } |
|
198 | - } |
|
199 | - |
|
200 | - if (!empty($missing)) { |
|
201 | - // There was at least one provider missing |
|
202 | - $this->logger->alert(count($missing) . " two-factor auth providers failed to load", ['app' => 'core']); |
|
203 | - |
|
204 | - return true; |
|
205 | - } |
|
206 | - |
|
207 | - // If we reach this, there was not a single provider missing |
|
208 | - return false; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Get the list of 2FA providers for the given user |
|
213 | - * |
|
214 | - * @param IUser $user |
|
215 | - * @throws Exception |
|
216 | - */ |
|
217 | - public function getProviderSet(IUser $user): ProviderSet { |
|
218 | - $providerStates = $this->providerRegistry->getProviderStates($user); |
|
219 | - $providers = $this->providerLoader->getProviders($user); |
|
220 | - |
|
221 | - $fixedStates = $this->fixMissingProviderStates($providerStates, $providers, $user); |
|
222 | - $isProviderMissing = $this->isProviderMissing($fixedStates, $providers); |
|
223 | - |
|
224 | - $enabled = array_filter($providers, function (IProvider $provider) use ($fixedStates) { |
|
225 | - return $fixedStates[$provider->getId()]; |
|
226 | - }); |
|
227 | - return new ProviderSet($enabled, $isProviderMissing); |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Verify the given challenge |
|
232 | - * |
|
233 | - * @param string $providerId |
|
234 | - * @param IUser $user |
|
235 | - * @param string $challenge |
|
236 | - * @return boolean |
|
237 | - */ |
|
238 | - public function verifyChallenge(string $providerId, IUser $user, string $challenge): bool { |
|
239 | - $provider = $this->getProvider($user, $providerId); |
|
240 | - if ($provider === null) { |
|
241 | - return false; |
|
242 | - } |
|
243 | - |
|
244 | - $passed = $provider->verifyChallenge($user, $challenge); |
|
245 | - if ($passed) { |
|
246 | - if ($this->session->get(self::REMEMBER_LOGIN) === true) { |
|
247 | - // TODO: resolve cyclic dependency and use DI |
|
248 | - \OC::$server->getUserSession()->createRememberMeToken($user); |
|
249 | - } |
|
250 | - $this->session->remove(self::SESSION_UID_KEY); |
|
251 | - $this->session->remove(self::REMEMBER_LOGIN); |
|
252 | - $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
|
253 | - |
|
254 | - // Clear token from db |
|
255 | - $sessionId = $this->session->getId(); |
|
256 | - $token = $this->tokenProvider->getToken($sessionId); |
|
257 | - $tokenId = $token->getId(); |
|
258 | - $this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $tokenId); |
|
259 | - |
|
260 | - $dispatchEvent = new GenericEvent($user, ['provider' => $provider->getDisplayName()]); |
|
261 | - $this->dispatcher->dispatch(IProvider::EVENT_SUCCESS, $dispatchEvent); |
|
262 | - |
|
263 | - $this->publishEvent($user, 'twofactor_success', [ |
|
264 | - 'provider' => $provider->getDisplayName(), |
|
265 | - ]); |
|
266 | - } else { |
|
267 | - $dispatchEvent = new GenericEvent($user, ['provider' => $provider->getDisplayName()]); |
|
268 | - $this->dispatcher->dispatch(IProvider::EVENT_FAILED, $dispatchEvent); |
|
269 | - |
|
270 | - $this->publishEvent($user, 'twofactor_failed', [ |
|
271 | - 'provider' => $provider->getDisplayName(), |
|
272 | - ]); |
|
273 | - } |
|
274 | - return $passed; |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * Push a 2fa event the user's activity stream |
|
279 | - * |
|
280 | - * @param IUser $user |
|
281 | - * @param string $event |
|
282 | - * @param array $params |
|
283 | - */ |
|
284 | - private function publishEvent(IUser $user, string $event, array $params) { |
|
285 | - $activity = $this->activityManager->generateEvent(); |
|
286 | - $activity->setApp('core') |
|
287 | - ->setType('security') |
|
288 | - ->setAuthor($user->getUID()) |
|
289 | - ->setAffectedUser($user->getUID()) |
|
290 | - ->setSubject($event, $params); |
|
291 | - try { |
|
292 | - $this->activityManager->publish($activity); |
|
293 | - } catch (BadMethodCallException $e) { |
|
294 | - $this->logger->warning('could not publish activity', ['app' => 'core']); |
|
295 | - $this->logger->logException($e, ['app' => 'core']); |
|
296 | - } |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * Check if the currently logged in user needs to pass 2FA |
|
301 | - * |
|
302 | - * @param IUser $user the currently logged in user |
|
303 | - * @return boolean |
|
304 | - */ |
|
305 | - public function needsSecondFactor(IUser $user = null): bool { |
|
306 | - if ($user === null) { |
|
307 | - return false; |
|
308 | - } |
|
309 | - |
|
310 | - // If we are authenticated using an app password skip all this |
|
311 | - if ($this->session->exists('app_password')) { |
|
312 | - return false; |
|
313 | - } |
|
314 | - |
|
315 | - // First check if the session tells us we should do 2FA (99% case) |
|
316 | - if (!$this->session->exists(self::SESSION_UID_KEY)) { |
|
317 | - |
|
318 | - // Check if the session tells us it is 2FA authenticated already |
|
319 | - if ($this->session->exists(self::SESSION_UID_DONE) && |
|
320 | - $this->session->get(self::SESSION_UID_DONE) === $user->getUID()) { |
|
321 | - return false; |
|
322 | - } |
|
323 | - |
|
324 | - /* |
|
47 | + const SESSION_UID_KEY = 'two_factor_auth_uid'; |
|
48 | + const SESSION_UID_DONE = 'two_factor_auth_passed'; |
|
49 | + const REMEMBER_LOGIN = 'two_factor_remember_login'; |
|
50 | + |
|
51 | + /** @var ProviderLoader */ |
|
52 | + private $providerLoader; |
|
53 | + |
|
54 | + /** @var IRegistry */ |
|
55 | + private $providerRegistry; |
|
56 | + |
|
57 | + /** @var ISession */ |
|
58 | + private $session; |
|
59 | + |
|
60 | + /** @var IConfig */ |
|
61 | + private $config; |
|
62 | + |
|
63 | + /** @var IManager */ |
|
64 | + private $activityManager; |
|
65 | + |
|
66 | + /** @var ILogger */ |
|
67 | + private $logger; |
|
68 | + |
|
69 | + /** @var TokenProvider */ |
|
70 | + private $tokenProvider; |
|
71 | + |
|
72 | + /** @var ITimeFactory */ |
|
73 | + private $timeFactory; |
|
74 | + |
|
75 | + /** @var EventDispatcherInterface */ |
|
76 | + private $dispatcher; |
|
77 | + |
|
78 | + public function __construct(ProviderLoader $providerLoader, |
|
79 | + IRegistry $providerRegistry, ISession $session, IConfig $config, |
|
80 | + IManager $activityManager, ILogger $logger, TokenProvider $tokenProvider, |
|
81 | + ITimeFactory $timeFactory, EventDispatcherInterface $eventDispatcher) { |
|
82 | + $this->providerLoader = $providerLoader; |
|
83 | + $this->session = $session; |
|
84 | + $this->config = $config; |
|
85 | + $this->activityManager = $activityManager; |
|
86 | + $this->logger = $logger; |
|
87 | + $this->tokenProvider = $tokenProvider; |
|
88 | + $this->timeFactory = $timeFactory; |
|
89 | + $this->dispatcher = $eventDispatcher; |
|
90 | + $this->providerRegistry = $providerRegistry; |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Determine whether the user must provide a second factor challenge |
|
95 | + * |
|
96 | + * @param IUser $user |
|
97 | + * @return boolean |
|
98 | + */ |
|
99 | + public function isTwoFactorAuthenticated(IUser $user): bool { |
|
100 | + $twoFactorEnabled = ((int) $this->config->getUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 0)) === 0; |
|
101 | + |
|
102 | + if (!$twoFactorEnabled) { |
|
103 | + return false; |
|
104 | + } |
|
105 | + |
|
106 | + $providerStates = $this->providerRegistry->getProviderStates($user); |
|
107 | + $enabled = array_filter($providerStates); |
|
108 | + |
|
109 | + return $twoFactorEnabled && !empty($enabled); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Disable 2FA checks for the given user |
|
114 | + * |
|
115 | + * @param IUser $user |
|
116 | + */ |
|
117 | + public function disableTwoFactorAuthentication(IUser $user) { |
|
118 | + $this->config->setUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 1); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Enable all 2FA checks for the given user |
|
123 | + * |
|
124 | + * @param IUser $user |
|
125 | + */ |
|
126 | + public function enableTwoFactorAuthentication(IUser $user) { |
|
127 | + $this->config->deleteUserValue($user->getUID(), 'core', 'two_factor_auth_disabled'); |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Get a 2FA provider by its ID |
|
132 | + * |
|
133 | + * @param IUser $user |
|
134 | + * @param string $challengeProviderId |
|
135 | + * @return IProvider|null |
|
136 | + */ |
|
137 | + public function getProvider(IUser $user, string $challengeProviderId) { |
|
138 | + $providers = $this->getProviderSet($user)->getProviders(); |
|
139 | + return $providers[$challengeProviderId] ?? null; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Check if the persistant mapping of enabled/disabled state of each available |
|
144 | + * provider is missing an entry and add it to the registry in that case. |
|
145 | + * |
|
146 | + * @todo remove in Nextcloud 17 as by then all providers should have been updated |
|
147 | + * |
|
148 | + * @param string[] $providerStates |
|
149 | + * @param IProvider[] $providers |
|
150 | + * @param IUser $user |
|
151 | + * @return string[] the updated $providerStates variable |
|
152 | + */ |
|
153 | + private function fixMissingProviderStates(array $providerStates, |
|
154 | + array $providers, IUser $user): array { |
|
155 | + |
|
156 | + foreach ($providers as $provider) { |
|
157 | + if (isset($providerStates[$provider->getId()])) { |
|
158 | + // All good |
|
159 | + continue; |
|
160 | + } |
|
161 | + |
|
162 | + $enabled = $provider->isTwoFactorAuthEnabledForUser($user); |
|
163 | + if ($enabled) { |
|
164 | + $this->providerRegistry->enableProviderFor($provider, $user); |
|
165 | + } else { |
|
166 | + $this->providerRegistry->disableProviderFor($provider, $user); |
|
167 | + } |
|
168 | + $providerStates[$provider->getId()] = $enabled; |
|
169 | + } |
|
170 | + |
|
171 | + return $providerStates; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * @param array $states |
|
176 | + * @param IProvider $providers |
|
177 | + */ |
|
178 | + private function isProviderMissing(array $states, array $providers): bool { |
|
179 | + $indexed = []; |
|
180 | + foreach ($providers as $provider) { |
|
181 | + $indexed[$provider->getId()] = $provider; |
|
182 | + } |
|
183 | + |
|
184 | + $missing = []; |
|
185 | + foreach ($states as $providerId => $enabled) { |
|
186 | + if (!$enabled) { |
|
187 | + // Don't care |
|
188 | + continue; |
|
189 | + } |
|
190 | + |
|
191 | + if (!isset($indexed[$providerId])) { |
|
192 | + $missing[] = $providerId; |
|
193 | + $this->logger->alert("two-factor auth provider '$providerId' failed to load", |
|
194 | + [ |
|
195 | + 'app' => 'core', |
|
196 | + ]); |
|
197 | + } |
|
198 | + } |
|
199 | + |
|
200 | + if (!empty($missing)) { |
|
201 | + // There was at least one provider missing |
|
202 | + $this->logger->alert(count($missing) . " two-factor auth providers failed to load", ['app' => 'core']); |
|
203 | + |
|
204 | + return true; |
|
205 | + } |
|
206 | + |
|
207 | + // If we reach this, there was not a single provider missing |
|
208 | + return false; |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Get the list of 2FA providers for the given user |
|
213 | + * |
|
214 | + * @param IUser $user |
|
215 | + * @throws Exception |
|
216 | + */ |
|
217 | + public function getProviderSet(IUser $user): ProviderSet { |
|
218 | + $providerStates = $this->providerRegistry->getProviderStates($user); |
|
219 | + $providers = $this->providerLoader->getProviders($user); |
|
220 | + |
|
221 | + $fixedStates = $this->fixMissingProviderStates($providerStates, $providers, $user); |
|
222 | + $isProviderMissing = $this->isProviderMissing($fixedStates, $providers); |
|
223 | + |
|
224 | + $enabled = array_filter($providers, function (IProvider $provider) use ($fixedStates) { |
|
225 | + return $fixedStates[$provider->getId()]; |
|
226 | + }); |
|
227 | + return new ProviderSet($enabled, $isProviderMissing); |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Verify the given challenge |
|
232 | + * |
|
233 | + * @param string $providerId |
|
234 | + * @param IUser $user |
|
235 | + * @param string $challenge |
|
236 | + * @return boolean |
|
237 | + */ |
|
238 | + public function verifyChallenge(string $providerId, IUser $user, string $challenge): bool { |
|
239 | + $provider = $this->getProvider($user, $providerId); |
|
240 | + if ($provider === null) { |
|
241 | + return false; |
|
242 | + } |
|
243 | + |
|
244 | + $passed = $provider->verifyChallenge($user, $challenge); |
|
245 | + if ($passed) { |
|
246 | + if ($this->session->get(self::REMEMBER_LOGIN) === true) { |
|
247 | + // TODO: resolve cyclic dependency and use DI |
|
248 | + \OC::$server->getUserSession()->createRememberMeToken($user); |
|
249 | + } |
|
250 | + $this->session->remove(self::SESSION_UID_KEY); |
|
251 | + $this->session->remove(self::REMEMBER_LOGIN); |
|
252 | + $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
|
253 | + |
|
254 | + // Clear token from db |
|
255 | + $sessionId = $this->session->getId(); |
|
256 | + $token = $this->tokenProvider->getToken($sessionId); |
|
257 | + $tokenId = $token->getId(); |
|
258 | + $this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $tokenId); |
|
259 | + |
|
260 | + $dispatchEvent = new GenericEvent($user, ['provider' => $provider->getDisplayName()]); |
|
261 | + $this->dispatcher->dispatch(IProvider::EVENT_SUCCESS, $dispatchEvent); |
|
262 | + |
|
263 | + $this->publishEvent($user, 'twofactor_success', [ |
|
264 | + 'provider' => $provider->getDisplayName(), |
|
265 | + ]); |
|
266 | + } else { |
|
267 | + $dispatchEvent = new GenericEvent($user, ['provider' => $provider->getDisplayName()]); |
|
268 | + $this->dispatcher->dispatch(IProvider::EVENT_FAILED, $dispatchEvent); |
|
269 | + |
|
270 | + $this->publishEvent($user, 'twofactor_failed', [ |
|
271 | + 'provider' => $provider->getDisplayName(), |
|
272 | + ]); |
|
273 | + } |
|
274 | + return $passed; |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * Push a 2fa event the user's activity stream |
|
279 | + * |
|
280 | + * @param IUser $user |
|
281 | + * @param string $event |
|
282 | + * @param array $params |
|
283 | + */ |
|
284 | + private function publishEvent(IUser $user, string $event, array $params) { |
|
285 | + $activity = $this->activityManager->generateEvent(); |
|
286 | + $activity->setApp('core') |
|
287 | + ->setType('security') |
|
288 | + ->setAuthor($user->getUID()) |
|
289 | + ->setAffectedUser($user->getUID()) |
|
290 | + ->setSubject($event, $params); |
|
291 | + try { |
|
292 | + $this->activityManager->publish($activity); |
|
293 | + } catch (BadMethodCallException $e) { |
|
294 | + $this->logger->warning('could not publish activity', ['app' => 'core']); |
|
295 | + $this->logger->logException($e, ['app' => 'core']); |
|
296 | + } |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * Check if the currently logged in user needs to pass 2FA |
|
301 | + * |
|
302 | + * @param IUser $user the currently logged in user |
|
303 | + * @return boolean |
|
304 | + */ |
|
305 | + public function needsSecondFactor(IUser $user = null): bool { |
|
306 | + if ($user === null) { |
|
307 | + return false; |
|
308 | + } |
|
309 | + |
|
310 | + // If we are authenticated using an app password skip all this |
|
311 | + if ($this->session->exists('app_password')) { |
|
312 | + return false; |
|
313 | + } |
|
314 | + |
|
315 | + // First check if the session tells us we should do 2FA (99% case) |
|
316 | + if (!$this->session->exists(self::SESSION_UID_KEY)) { |
|
317 | + |
|
318 | + // Check if the session tells us it is 2FA authenticated already |
|
319 | + if ($this->session->exists(self::SESSION_UID_DONE) && |
|
320 | + $this->session->get(self::SESSION_UID_DONE) === $user->getUID()) { |
|
321 | + return false; |
|
322 | + } |
|
323 | + |
|
324 | + /* |
|
325 | 325 | * If the session is expired check if we are not logged in by a token |
326 | 326 | * that still needs 2FA auth |
327 | 327 | */ |
328 | - try { |
|
329 | - $sessionId = $this->session->getId(); |
|
330 | - $token = $this->tokenProvider->getToken($sessionId); |
|
331 | - $tokenId = $token->getId(); |
|
332 | - $tokensNeeding2FA = $this->config->getUserKeys($user->getUID(), 'login_token_2fa'); |
|
333 | - |
|
334 | - if (!\in_array($tokenId, $tokensNeeding2FA, true)) { |
|
335 | - $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
|
336 | - return false; |
|
337 | - } |
|
338 | - } catch (InvalidTokenException $e) { |
|
339 | - } |
|
340 | - } |
|
341 | - |
|
342 | - if (!$this->isTwoFactorAuthenticated($user)) { |
|
343 | - // There is no second factor any more -> let the user pass |
|
344 | - // This prevents infinite redirect loops when a user is about |
|
345 | - // to solve the 2FA challenge, and the provider app is |
|
346 | - // disabled the same time |
|
347 | - $this->session->remove(self::SESSION_UID_KEY); |
|
348 | - |
|
349 | - $keys = $this->config->getUserKeys($user->getUID(), 'login_token_2fa'); |
|
350 | - foreach ($keys as $key) { |
|
351 | - $this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $key); |
|
352 | - } |
|
353 | - return false; |
|
354 | - } |
|
355 | - |
|
356 | - return true; |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * Prepare the 2FA login |
|
361 | - * |
|
362 | - * @param IUser $user |
|
363 | - * @param boolean $rememberMe |
|
364 | - */ |
|
365 | - public function prepareTwoFactorLogin(IUser $user, bool $rememberMe) { |
|
366 | - $this->session->set(self::SESSION_UID_KEY, $user->getUID()); |
|
367 | - $this->session->set(self::REMEMBER_LOGIN, $rememberMe); |
|
368 | - |
|
369 | - $id = $this->session->getId(); |
|
370 | - $token = $this->tokenProvider->getToken($id); |
|
371 | - $this->config->setUserValue($user->getUID(), 'login_token_2fa', $token->getId(), $this->timeFactory->getTime()); |
|
372 | - } |
|
328 | + try { |
|
329 | + $sessionId = $this->session->getId(); |
|
330 | + $token = $this->tokenProvider->getToken($sessionId); |
|
331 | + $tokenId = $token->getId(); |
|
332 | + $tokensNeeding2FA = $this->config->getUserKeys($user->getUID(), 'login_token_2fa'); |
|
333 | + |
|
334 | + if (!\in_array($tokenId, $tokensNeeding2FA, true)) { |
|
335 | + $this->session->set(self::SESSION_UID_DONE, $user->getUID()); |
|
336 | + return false; |
|
337 | + } |
|
338 | + } catch (InvalidTokenException $e) { |
|
339 | + } |
|
340 | + } |
|
341 | + |
|
342 | + if (!$this->isTwoFactorAuthenticated($user)) { |
|
343 | + // There is no second factor any more -> let the user pass |
|
344 | + // This prevents infinite redirect loops when a user is about |
|
345 | + // to solve the 2FA challenge, and the provider app is |
|
346 | + // disabled the same time |
|
347 | + $this->session->remove(self::SESSION_UID_KEY); |
|
348 | + |
|
349 | + $keys = $this->config->getUserKeys($user->getUID(), 'login_token_2fa'); |
|
350 | + foreach ($keys as $key) { |
|
351 | + $this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $key); |
|
352 | + } |
|
353 | + return false; |
|
354 | + } |
|
355 | + |
|
356 | + return true; |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * Prepare the 2FA login |
|
361 | + * |
|
362 | + * @param IUser $user |
|
363 | + * @param boolean $rememberMe |
|
364 | + */ |
|
365 | + public function prepareTwoFactorLogin(IUser $user, bool $rememberMe) { |
|
366 | + $this->session->set(self::SESSION_UID_KEY, $user->getUID()); |
|
367 | + $this->session->set(self::REMEMBER_LOGIN, $rememberMe); |
|
368 | + |
|
369 | + $id = $this->session->getId(); |
|
370 | + $token = $this->tokenProvider->getToken($id); |
|
371 | + $this->config->setUserValue($user->getUID(), 'login_token_2fa', $token->getId(), $this->timeFactory->getTime()); |
|
372 | + } |
|
373 | 373 | |
374 | 374 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | /** |
5 | 5 | * @copyright Copyright (c) 2016, ownCloud, Inc. |
6 | 6 | * |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | |
200 | 200 | if (!empty($missing)) { |
201 | 201 | // There was at least one provider missing |
202 | - $this->logger->alert(count($missing) . " two-factor auth providers failed to load", ['app' => 'core']); |
|
202 | + $this->logger->alert(count($missing)." two-factor auth providers failed to load", ['app' => 'core']); |
|
203 | 203 | |
204 | 204 | return true; |
205 | 205 | } |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | $fixedStates = $this->fixMissingProviderStates($providerStates, $providers, $user); |
222 | 222 | $isProviderMissing = $this->isProviderMissing($fixedStates, $providers); |
223 | 223 | |
224 | - $enabled = array_filter($providers, function (IProvider $provider) use ($fixedStates) { |
|
224 | + $enabled = array_filter($providers, function(IProvider $provider) use ($fixedStates) { |
|
225 | 225 | return $fixedStates[$provider->getId()]; |
226 | 226 | }); |
227 | 227 | return new ProviderSet($enabled, $isProviderMissing); |
@@ -58,299 +58,299 @@ |
||
58 | 58 | use OCP\Util; |
59 | 59 | |
60 | 60 | class LoginController extends Controller { |
61 | - /** @var IUserManager */ |
|
62 | - private $userManager; |
|
63 | - /** @var IConfig */ |
|
64 | - private $config; |
|
65 | - /** @var ISession */ |
|
66 | - private $session; |
|
67 | - /** @var IUserSession|Session */ |
|
68 | - private $userSession; |
|
69 | - /** @var IURLGenerator */ |
|
70 | - private $urlGenerator; |
|
71 | - /** @var ILogger */ |
|
72 | - private $logger; |
|
73 | - /** @var Manager */ |
|
74 | - private $twoFactorManager; |
|
75 | - /** @var Defaults */ |
|
76 | - private $defaults; |
|
77 | - /** @var Throttler */ |
|
78 | - private $throttler; |
|
79 | - |
|
80 | - /** |
|
81 | - * @param string $appName |
|
82 | - * @param IRequest $request |
|
83 | - * @param IUserManager $userManager |
|
84 | - * @param IConfig $config |
|
85 | - * @param ISession $session |
|
86 | - * @param IUserSession $userSession |
|
87 | - * @param IURLGenerator $urlGenerator |
|
88 | - * @param ILogger $logger |
|
89 | - * @param Manager $twoFactorManager |
|
90 | - * @param Defaults $defaults |
|
91 | - * @param Throttler $throttler |
|
92 | - */ |
|
93 | - public function __construct($appName, |
|
94 | - IRequest $request, |
|
95 | - IUserManager $userManager, |
|
96 | - IConfig $config, |
|
97 | - ISession $session, |
|
98 | - IUserSession $userSession, |
|
99 | - IURLGenerator $urlGenerator, |
|
100 | - ILogger $logger, |
|
101 | - Manager $twoFactorManager, |
|
102 | - Defaults $defaults, |
|
103 | - Throttler $throttler) { |
|
104 | - parent::__construct($appName, $request); |
|
105 | - $this->userManager = $userManager; |
|
106 | - $this->config = $config; |
|
107 | - $this->session = $session; |
|
108 | - $this->userSession = $userSession; |
|
109 | - $this->urlGenerator = $urlGenerator; |
|
110 | - $this->logger = $logger; |
|
111 | - $this->twoFactorManager = $twoFactorManager; |
|
112 | - $this->defaults = $defaults; |
|
113 | - $this->throttler = $throttler; |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * @NoAdminRequired |
|
118 | - * @UseSession |
|
119 | - * |
|
120 | - * @return RedirectResponse |
|
121 | - */ |
|
122 | - public function logout() { |
|
123 | - $loginToken = $this->request->getCookie('nc_token'); |
|
124 | - if (!is_null($loginToken)) { |
|
125 | - $this->config->deleteUserValue($this->userSession->getUser()->getUID(), 'login_token', $loginToken); |
|
126 | - } |
|
127 | - $this->userSession->logout(); |
|
128 | - |
|
129 | - $response = new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm')); |
|
130 | - $response->addHeader('Clear-Site-Data', '"cache", "cookies", "storage", "executionContexts"'); |
|
131 | - return $response; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * @PublicPage |
|
136 | - * @NoCSRFRequired |
|
137 | - * @UseSession |
|
138 | - * |
|
139 | - * @param string $user |
|
140 | - * @param string $redirect_url |
|
141 | - * |
|
142 | - * @return TemplateResponse|RedirectResponse |
|
143 | - */ |
|
144 | - public function showLoginForm(string $user = null, string $redirect_url = null): Http\Response { |
|
145 | - |
|
146 | - if ($this->userSession->isLoggedIn()) { |
|
147 | - return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
148 | - } |
|
149 | - |
|
150 | - $parameters = array(); |
|
151 | - $loginMessages = $this->session->get('loginMessages'); |
|
152 | - $errors = []; |
|
153 | - $messages = []; |
|
154 | - if (is_array($loginMessages)) { |
|
155 | - list($errors, $messages) = $loginMessages; |
|
156 | - } |
|
157 | - $this->session->remove('loginMessages'); |
|
158 | - foreach ($errors as $value) { |
|
159 | - $parameters[$value] = true; |
|
160 | - } |
|
161 | - |
|
162 | - $parameters['messages'] = $messages; |
|
163 | - if ($user !== null && $user !== '') { |
|
164 | - $parameters['loginName'] = $user; |
|
165 | - $parameters['user_autofocus'] = false; |
|
166 | - } else { |
|
167 | - $parameters['loginName'] = ''; |
|
168 | - $parameters['user_autofocus'] = true; |
|
169 | - } |
|
170 | - if (!empty($redirect_url)) { |
|
171 | - $parameters['redirect_url'] = $redirect_url; |
|
172 | - } |
|
173 | - |
|
174 | - $parameters['canResetPassword'] = true; |
|
175 | - $parameters['resetPasswordLink'] = $this->config->getSystemValue('lost_password_link', ''); |
|
176 | - if (!$parameters['resetPasswordLink']) { |
|
177 | - if ($user !== null && $user !== '') { |
|
178 | - $userObj = $this->userManager->get($user); |
|
179 | - if ($userObj instanceof IUser) { |
|
180 | - $parameters['canResetPassword'] = $userObj->canChangePassword(); |
|
181 | - } |
|
182 | - } |
|
183 | - } elseif ($parameters['resetPasswordLink'] === 'disabled') { |
|
184 | - $parameters['canResetPassword'] = false; |
|
185 | - } |
|
186 | - |
|
187 | - $parameters['alt_login'] = OC_App::getAlternativeLogIns(); |
|
188 | - |
|
189 | - if ($user !== null && $user !== '') { |
|
190 | - $parameters['loginName'] = $user; |
|
191 | - $parameters['user_autofocus'] = false; |
|
192 | - } else { |
|
193 | - $parameters['loginName'] = ''; |
|
194 | - $parameters['user_autofocus'] = true; |
|
195 | - } |
|
196 | - |
|
197 | - $parameters['throttle_delay'] = $this->throttler->getDelay($this->request->getRemoteAddress()); |
|
198 | - |
|
199 | - // OpenGraph Support: http://ogp.me/ |
|
200 | - Util::addHeader('meta', ['property' => 'og:title', 'content' => Util::sanitizeHTML($this->defaults->getName())]); |
|
201 | - Util::addHeader('meta', ['property' => 'og:description', 'content' => Util::sanitizeHTML($this->defaults->getSlogan())]); |
|
202 | - Util::addHeader('meta', ['property' => 'og:site_name', 'content' => Util::sanitizeHTML($this->defaults->getName())]); |
|
203 | - Util::addHeader('meta', ['property' => 'og:url', 'content' => $this->urlGenerator->getAbsoluteURL('/')]); |
|
204 | - Util::addHeader('meta', ['property' => 'og:type', 'content' => 'website']); |
|
205 | - Util::addHeader('meta', ['property' => 'og:image', 'content' => $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core','favicon-touch.png'))]); |
|
206 | - |
|
207 | - return new TemplateResponse( |
|
208 | - $this->appName, 'login', $parameters, 'guest' |
|
209 | - ); |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * @param string $redirectUrl |
|
214 | - * @return RedirectResponse |
|
215 | - */ |
|
216 | - private function generateRedirect($redirectUrl) { |
|
217 | - if (!is_null($redirectUrl) && $this->userSession->isLoggedIn()) { |
|
218 | - $location = $this->urlGenerator->getAbsoluteURL(urldecode($redirectUrl)); |
|
219 | - // Deny the redirect if the URL contains a @ |
|
220 | - // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
|
221 | - if (strpos($location, '@') === false) { |
|
222 | - return new RedirectResponse($location); |
|
223 | - } |
|
224 | - } |
|
225 | - return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * @PublicPage |
|
230 | - * @UseSession |
|
231 | - * @NoCSRFRequired |
|
232 | - * @BruteForceProtection(action=login) |
|
233 | - * |
|
234 | - * @param string $user |
|
235 | - * @param string $password |
|
236 | - * @param string $redirect_url |
|
237 | - * @param boolean $remember_login |
|
238 | - * @param string $timezone |
|
239 | - * @param string $timezone_offset |
|
240 | - * @return RedirectResponse |
|
241 | - */ |
|
242 | - public function tryLogin($user, $password, $redirect_url, $remember_login = true, $timezone = '', $timezone_offset = '') { |
|
243 | - if(!is_string($user)) { |
|
244 | - throw new \InvalidArgumentException('Username must be string'); |
|
245 | - } |
|
246 | - |
|
247 | - // If the user is already logged in and the CSRF check does not pass then |
|
248 | - // simply redirect the user to the correct page as required. This is the |
|
249 | - // case when an user has already logged-in, in another tab. |
|
250 | - if(!$this->request->passesCSRFCheck()) { |
|
251 | - return $this->generateRedirect($redirect_url); |
|
252 | - } |
|
253 | - |
|
254 | - if ($this->userManager instanceof PublicEmitter) { |
|
255 | - $this->userManager->emit('\OC\User', 'preLogin', array($user, $password)); |
|
256 | - } |
|
257 | - |
|
258 | - $originalUser = $user; |
|
259 | - // TODO: Add all the insane error handling |
|
260 | - /* @var $loginResult IUser */ |
|
261 | - $loginResult = $this->userManager->checkPasswordNoLogging($user, $password); |
|
262 | - if ($loginResult === false) { |
|
263 | - $users = $this->userManager->getByEmail($user); |
|
264 | - // we only allow login by email if unique |
|
265 | - if (count($users) === 1) { |
|
266 | - $previousUser = $user; |
|
267 | - $user = $users[0]->getUID(); |
|
268 | - if($user !== $previousUser) { |
|
269 | - $loginResult = $this->userManager->checkPassword($user, $password); |
|
270 | - } |
|
271 | - } |
|
272 | - } |
|
273 | - if ($loginResult === false) { |
|
274 | - $this->logger->warning('Login failed: \''. $user .'\' (Remote IP: \''. $this->request->getRemoteAddress(). '\')', ['app' => 'core']); |
|
275 | - // Read current user and append if possible - we need to return the unmodified user otherwise we will leak the login name |
|
276 | - $args = !is_null($user) ? ['user' => $originalUser] : []; |
|
277 | - if (!is_null($redirect_url)) { |
|
278 | - $args['redirect_url'] = $redirect_url; |
|
279 | - } |
|
280 | - $response = new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', $args)); |
|
281 | - $response->throttle(['user' => $user]); |
|
282 | - $this->session->set('loginMessages', [ |
|
283 | - ['invalidpassword'], [] |
|
284 | - ]); |
|
285 | - return $response; |
|
286 | - } |
|
287 | - // TODO: remove password checks from above and let the user session handle failures |
|
288 | - // requires https://github.com/owncloud/core/pull/24616 |
|
289 | - $this->userSession->completeLogin($loginResult, ['loginName' => $user, 'password' => $password]); |
|
290 | - $this->userSession->createSessionToken($this->request, $loginResult->getUID(), $user, $password, IToken::REMEMBER); |
|
291 | - |
|
292 | - // User has successfully logged in, now remove the password reset link, when it is available |
|
293 | - $this->config->deleteUserValue($loginResult->getUID(), 'core', 'lostpassword'); |
|
294 | - |
|
295 | - $this->session->set('last-password-confirm', $loginResult->getLastLogin()); |
|
296 | - |
|
297 | - if ($timezone_offset !== '') { |
|
298 | - $this->config->setUserValue($loginResult->getUID(), 'core', 'timezone', $timezone); |
|
299 | - $this->session->set('timezone', $timezone_offset); |
|
300 | - } |
|
301 | - |
|
302 | - if ($this->twoFactorManager->isTwoFactorAuthenticated($loginResult)) { |
|
303 | - $this->twoFactorManager->prepareTwoFactorLogin($loginResult, $remember_login); |
|
304 | - |
|
305 | - $providers = $this->twoFactorManager->getProviderSet($loginResult)->getProviders(); |
|
306 | - if (count($providers) === 1) { |
|
307 | - // Single provider, hence we can redirect to that provider's challenge page directly |
|
308 | - /* @var $provider IProvider */ |
|
309 | - $provider = array_pop($providers); |
|
310 | - $url = 'core.TwoFactorChallenge.showChallenge'; |
|
311 | - $urlParams = [ |
|
312 | - 'challengeProviderId' => $provider->getId(), |
|
313 | - ]; |
|
314 | - } else { |
|
315 | - $url = 'core.TwoFactorChallenge.selectChallenge'; |
|
316 | - $urlParams = []; |
|
317 | - } |
|
318 | - |
|
319 | - if (!is_null($redirect_url)) { |
|
320 | - $urlParams['redirect_url'] = $redirect_url; |
|
321 | - } |
|
322 | - |
|
323 | - return new RedirectResponse($this->urlGenerator->linkToRoute($url, $urlParams)); |
|
324 | - } |
|
325 | - |
|
326 | - if ($remember_login) { |
|
327 | - $this->userSession->createRememberMeToken($loginResult); |
|
328 | - } |
|
329 | - |
|
330 | - return $this->generateRedirect($redirect_url); |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * @NoAdminRequired |
|
335 | - * @UseSession |
|
336 | - * @BruteForceProtection(action=sudo) |
|
337 | - * |
|
338 | - * @license GNU AGPL version 3 or any later version |
|
339 | - * |
|
340 | - * @param string $password |
|
341 | - * @return DataResponse |
|
342 | - */ |
|
343 | - public function confirmPassword($password) { |
|
344 | - $loginName = $this->userSession->getLoginName(); |
|
345 | - $loginResult = $this->userManager->checkPassword($loginName, $password); |
|
346 | - if ($loginResult === false) { |
|
347 | - $response = new DataResponse([], Http::STATUS_FORBIDDEN); |
|
348 | - $response->throttle(); |
|
349 | - return $response; |
|
350 | - } |
|
351 | - |
|
352 | - $confirmTimestamp = time(); |
|
353 | - $this->session->set('last-password-confirm', $confirmTimestamp); |
|
354 | - return new DataResponse(['lastLogin' => $confirmTimestamp], Http::STATUS_OK); |
|
355 | - } |
|
61 | + /** @var IUserManager */ |
|
62 | + private $userManager; |
|
63 | + /** @var IConfig */ |
|
64 | + private $config; |
|
65 | + /** @var ISession */ |
|
66 | + private $session; |
|
67 | + /** @var IUserSession|Session */ |
|
68 | + private $userSession; |
|
69 | + /** @var IURLGenerator */ |
|
70 | + private $urlGenerator; |
|
71 | + /** @var ILogger */ |
|
72 | + private $logger; |
|
73 | + /** @var Manager */ |
|
74 | + private $twoFactorManager; |
|
75 | + /** @var Defaults */ |
|
76 | + private $defaults; |
|
77 | + /** @var Throttler */ |
|
78 | + private $throttler; |
|
79 | + |
|
80 | + /** |
|
81 | + * @param string $appName |
|
82 | + * @param IRequest $request |
|
83 | + * @param IUserManager $userManager |
|
84 | + * @param IConfig $config |
|
85 | + * @param ISession $session |
|
86 | + * @param IUserSession $userSession |
|
87 | + * @param IURLGenerator $urlGenerator |
|
88 | + * @param ILogger $logger |
|
89 | + * @param Manager $twoFactorManager |
|
90 | + * @param Defaults $defaults |
|
91 | + * @param Throttler $throttler |
|
92 | + */ |
|
93 | + public function __construct($appName, |
|
94 | + IRequest $request, |
|
95 | + IUserManager $userManager, |
|
96 | + IConfig $config, |
|
97 | + ISession $session, |
|
98 | + IUserSession $userSession, |
|
99 | + IURLGenerator $urlGenerator, |
|
100 | + ILogger $logger, |
|
101 | + Manager $twoFactorManager, |
|
102 | + Defaults $defaults, |
|
103 | + Throttler $throttler) { |
|
104 | + parent::__construct($appName, $request); |
|
105 | + $this->userManager = $userManager; |
|
106 | + $this->config = $config; |
|
107 | + $this->session = $session; |
|
108 | + $this->userSession = $userSession; |
|
109 | + $this->urlGenerator = $urlGenerator; |
|
110 | + $this->logger = $logger; |
|
111 | + $this->twoFactorManager = $twoFactorManager; |
|
112 | + $this->defaults = $defaults; |
|
113 | + $this->throttler = $throttler; |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * @NoAdminRequired |
|
118 | + * @UseSession |
|
119 | + * |
|
120 | + * @return RedirectResponse |
|
121 | + */ |
|
122 | + public function logout() { |
|
123 | + $loginToken = $this->request->getCookie('nc_token'); |
|
124 | + if (!is_null($loginToken)) { |
|
125 | + $this->config->deleteUserValue($this->userSession->getUser()->getUID(), 'login_token', $loginToken); |
|
126 | + } |
|
127 | + $this->userSession->logout(); |
|
128 | + |
|
129 | + $response = new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm')); |
|
130 | + $response->addHeader('Clear-Site-Data', '"cache", "cookies", "storage", "executionContexts"'); |
|
131 | + return $response; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * @PublicPage |
|
136 | + * @NoCSRFRequired |
|
137 | + * @UseSession |
|
138 | + * |
|
139 | + * @param string $user |
|
140 | + * @param string $redirect_url |
|
141 | + * |
|
142 | + * @return TemplateResponse|RedirectResponse |
|
143 | + */ |
|
144 | + public function showLoginForm(string $user = null, string $redirect_url = null): Http\Response { |
|
145 | + |
|
146 | + if ($this->userSession->isLoggedIn()) { |
|
147 | + return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
148 | + } |
|
149 | + |
|
150 | + $parameters = array(); |
|
151 | + $loginMessages = $this->session->get('loginMessages'); |
|
152 | + $errors = []; |
|
153 | + $messages = []; |
|
154 | + if (is_array($loginMessages)) { |
|
155 | + list($errors, $messages) = $loginMessages; |
|
156 | + } |
|
157 | + $this->session->remove('loginMessages'); |
|
158 | + foreach ($errors as $value) { |
|
159 | + $parameters[$value] = true; |
|
160 | + } |
|
161 | + |
|
162 | + $parameters['messages'] = $messages; |
|
163 | + if ($user !== null && $user !== '') { |
|
164 | + $parameters['loginName'] = $user; |
|
165 | + $parameters['user_autofocus'] = false; |
|
166 | + } else { |
|
167 | + $parameters['loginName'] = ''; |
|
168 | + $parameters['user_autofocus'] = true; |
|
169 | + } |
|
170 | + if (!empty($redirect_url)) { |
|
171 | + $parameters['redirect_url'] = $redirect_url; |
|
172 | + } |
|
173 | + |
|
174 | + $parameters['canResetPassword'] = true; |
|
175 | + $parameters['resetPasswordLink'] = $this->config->getSystemValue('lost_password_link', ''); |
|
176 | + if (!$parameters['resetPasswordLink']) { |
|
177 | + if ($user !== null && $user !== '') { |
|
178 | + $userObj = $this->userManager->get($user); |
|
179 | + if ($userObj instanceof IUser) { |
|
180 | + $parameters['canResetPassword'] = $userObj->canChangePassword(); |
|
181 | + } |
|
182 | + } |
|
183 | + } elseif ($parameters['resetPasswordLink'] === 'disabled') { |
|
184 | + $parameters['canResetPassword'] = false; |
|
185 | + } |
|
186 | + |
|
187 | + $parameters['alt_login'] = OC_App::getAlternativeLogIns(); |
|
188 | + |
|
189 | + if ($user !== null && $user !== '') { |
|
190 | + $parameters['loginName'] = $user; |
|
191 | + $parameters['user_autofocus'] = false; |
|
192 | + } else { |
|
193 | + $parameters['loginName'] = ''; |
|
194 | + $parameters['user_autofocus'] = true; |
|
195 | + } |
|
196 | + |
|
197 | + $parameters['throttle_delay'] = $this->throttler->getDelay($this->request->getRemoteAddress()); |
|
198 | + |
|
199 | + // OpenGraph Support: http://ogp.me/ |
|
200 | + Util::addHeader('meta', ['property' => 'og:title', 'content' => Util::sanitizeHTML($this->defaults->getName())]); |
|
201 | + Util::addHeader('meta', ['property' => 'og:description', 'content' => Util::sanitizeHTML($this->defaults->getSlogan())]); |
|
202 | + Util::addHeader('meta', ['property' => 'og:site_name', 'content' => Util::sanitizeHTML($this->defaults->getName())]); |
|
203 | + Util::addHeader('meta', ['property' => 'og:url', 'content' => $this->urlGenerator->getAbsoluteURL('/')]); |
|
204 | + Util::addHeader('meta', ['property' => 'og:type', 'content' => 'website']); |
|
205 | + Util::addHeader('meta', ['property' => 'og:image', 'content' => $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core','favicon-touch.png'))]); |
|
206 | + |
|
207 | + return new TemplateResponse( |
|
208 | + $this->appName, 'login', $parameters, 'guest' |
|
209 | + ); |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * @param string $redirectUrl |
|
214 | + * @return RedirectResponse |
|
215 | + */ |
|
216 | + private function generateRedirect($redirectUrl) { |
|
217 | + if (!is_null($redirectUrl) && $this->userSession->isLoggedIn()) { |
|
218 | + $location = $this->urlGenerator->getAbsoluteURL(urldecode($redirectUrl)); |
|
219 | + // Deny the redirect if the URL contains a @ |
|
220 | + // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
|
221 | + if (strpos($location, '@') === false) { |
|
222 | + return new RedirectResponse($location); |
|
223 | + } |
|
224 | + } |
|
225 | + return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * @PublicPage |
|
230 | + * @UseSession |
|
231 | + * @NoCSRFRequired |
|
232 | + * @BruteForceProtection(action=login) |
|
233 | + * |
|
234 | + * @param string $user |
|
235 | + * @param string $password |
|
236 | + * @param string $redirect_url |
|
237 | + * @param boolean $remember_login |
|
238 | + * @param string $timezone |
|
239 | + * @param string $timezone_offset |
|
240 | + * @return RedirectResponse |
|
241 | + */ |
|
242 | + public function tryLogin($user, $password, $redirect_url, $remember_login = true, $timezone = '', $timezone_offset = '') { |
|
243 | + if(!is_string($user)) { |
|
244 | + throw new \InvalidArgumentException('Username must be string'); |
|
245 | + } |
|
246 | + |
|
247 | + // If the user is already logged in and the CSRF check does not pass then |
|
248 | + // simply redirect the user to the correct page as required. This is the |
|
249 | + // case when an user has already logged-in, in another tab. |
|
250 | + if(!$this->request->passesCSRFCheck()) { |
|
251 | + return $this->generateRedirect($redirect_url); |
|
252 | + } |
|
253 | + |
|
254 | + if ($this->userManager instanceof PublicEmitter) { |
|
255 | + $this->userManager->emit('\OC\User', 'preLogin', array($user, $password)); |
|
256 | + } |
|
257 | + |
|
258 | + $originalUser = $user; |
|
259 | + // TODO: Add all the insane error handling |
|
260 | + /* @var $loginResult IUser */ |
|
261 | + $loginResult = $this->userManager->checkPasswordNoLogging($user, $password); |
|
262 | + if ($loginResult === false) { |
|
263 | + $users = $this->userManager->getByEmail($user); |
|
264 | + // we only allow login by email if unique |
|
265 | + if (count($users) === 1) { |
|
266 | + $previousUser = $user; |
|
267 | + $user = $users[0]->getUID(); |
|
268 | + if($user !== $previousUser) { |
|
269 | + $loginResult = $this->userManager->checkPassword($user, $password); |
|
270 | + } |
|
271 | + } |
|
272 | + } |
|
273 | + if ($loginResult === false) { |
|
274 | + $this->logger->warning('Login failed: \''. $user .'\' (Remote IP: \''. $this->request->getRemoteAddress(). '\')', ['app' => 'core']); |
|
275 | + // Read current user and append if possible - we need to return the unmodified user otherwise we will leak the login name |
|
276 | + $args = !is_null($user) ? ['user' => $originalUser] : []; |
|
277 | + if (!is_null($redirect_url)) { |
|
278 | + $args['redirect_url'] = $redirect_url; |
|
279 | + } |
|
280 | + $response = new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', $args)); |
|
281 | + $response->throttle(['user' => $user]); |
|
282 | + $this->session->set('loginMessages', [ |
|
283 | + ['invalidpassword'], [] |
|
284 | + ]); |
|
285 | + return $response; |
|
286 | + } |
|
287 | + // TODO: remove password checks from above and let the user session handle failures |
|
288 | + // requires https://github.com/owncloud/core/pull/24616 |
|
289 | + $this->userSession->completeLogin($loginResult, ['loginName' => $user, 'password' => $password]); |
|
290 | + $this->userSession->createSessionToken($this->request, $loginResult->getUID(), $user, $password, IToken::REMEMBER); |
|
291 | + |
|
292 | + // User has successfully logged in, now remove the password reset link, when it is available |
|
293 | + $this->config->deleteUserValue($loginResult->getUID(), 'core', 'lostpassword'); |
|
294 | + |
|
295 | + $this->session->set('last-password-confirm', $loginResult->getLastLogin()); |
|
296 | + |
|
297 | + if ($timezone_offset !== '') { |
|
298 | + $this->config->setUserValue($loginResult->getUID(), 'core', 'timezone', $timezone); |
|
299 | + $this->session->set('timezone', $timezone_offset); |
|
300 | + } |
|
301 | + |
|
302 | + if ($this->twoFactorManager->isTwoFactorAuthenticated($loginResult)) { |
|
303 | + $this->twoFactorManager->prepareTwoFactorLogin($loginResult, $remember_login); |
|
304 | + |
|
305 | + $providers = $this->twoFactorManager->getProviderSet($loginResult)->getProviders(); |
|
306 | + if (count($providers) === 1) { |
|
307 | + // Single provider, hence we can redirect to that provider's challenge page directly |
|
308 | + /* @var $provider IProvider */ |
|
309 | + $provider = array_pop($providers); |
|
310 | + $url = 'core.TwoFactorChallenge.showChallenge'; |
|
311 | + $urlParams = [ |
|
312 | + 'challengeProviderId' => $provider->getId(), |
|
313 | + ]; |
|
314 | + } else { |
|
315 | + $url = 'core.TwoFactorChallenge.selectChallenge'; |
|
316 | + $urlParams = []; |
|
317 | + } |
|
318 | + |
|
319 | + if (!is_null($redirect_url)) { |
|
320 | + $urlParams['redirect_url'] = $redirect_url; |
|
321 | + } |
|
322 | + |
|
323 | + return new RedirectResponse($this->urlGenerator->linkToRoute($url, $urlParams)); |
|
324 | + } |
|
325 | + |
|
326 | + if ($remember_login) { |
|
327 | + $this->userSession->createRememberMeToken($loginResult); |
|
328 | + } |
|
329 | + |
|
330 | + return $this->generateRedirect($redirect_url); |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * @NoAdminRequired |
|
335 | + * @UseSession |
|
336 | + * @BruteForceProtection(action=sudo) |
|
337 | + * |
|
338 | + * @license GNU AGPL version 3 or any later version |
|
339 | + * |
|
340 | + * @param string $password |
|
341 | + * @return DataResponse |
|
342 | + */ |
|
343 | + public function confirmPassword($password) { |
|
344 | + $loginName = $this->userSession->getLoginName(); |
|
345 | + $loginResult = $this->userManager->checkPassword($loginName, $password); |
|
346 | + if ($loginResult === false) { |
|
347 | + $response = new DataResponse([], Http::STATUS_FORBIDDEN); |
|
348 | + $response->throttle(); |
|
349 | + return $response; |
|
350 | + } |
|
351 | + |
|
352 | + $confirmTimestamp = time(); |
|
353 | + $this->session->set('last-password-confirm', $confirmTimestamp); |
|
354 | + return new DataResponse(['lastLogin' => $confirmTimestamp], Http::STATUS_OK); |
|
355 | + } |
|
356 | 356 | } |
@@ -35,76 +35,76 @@ |
||
35 | 35 | |
36 | 36 | class State extends Base { |
37 | 37 | |
38 | - /** @var IRegistry */ |
|
39 | - private $registry; |
|
40 | - |
|
41 | - /** @var IUserManager */ |
|
42 | - private $userManager; |
|
43 | - |
|
44 | - public function __construct(IRegistry $registry, IUserManager $userManager) { |
|
45 | - parent::__construct('twofactorauth:state'); |
|
46 | - |
|
47 | - $this->registry = $registry; |
|
48 | - $this->userManager = $userManager; |
|
49 | - } |
|
50 | - |
|
51 | - protected function configure() { |
|
52 | - parent::configure(); |
|
53 | - |
|
54 | - $this->setName('twofactorauth:state'); |
|
55 | - $this->setDescription('Get the two-factor authentication (2FA) state of a user'); |
|
56 | - $this->addArgument('uid', InputArgument::REQUIRED); |
|
57 | - } |
|
58 | - |
|
59 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
60 | - $uid = $input->getArgument('uid'); |
|
61 | - $user = $this->userManager->get($uid); |
|
62 | - if (is_null($user)) { |
|
63 | - $output->writeln("<error>Invalid UID</error>"); |
|
64 | - return; |
|
65 | - } |
|
66 | - |
|
67 | - $providerStates = $this->registry->getProviderStates($user); |
|
68 | - $filtered = $this->filterEnabledDisabledUnknownProviders($providerStates); |
|
69 | - list ($enabled, $disabled) = $filtered; |
|
70 | - |
|
71 | - if (!empty($enabled)) { |
|
72 | - $output->writeln("Two-factor authentication is enabled for user $uid"); |
|
73 | - } else { |
|
74 | - $output->writeln("Two-factor authentication is not enabled for user $uid"); |
|
75 | - } |
|
76 | - |
|
77 | - $output->writeln(""); |
|
78 | - $this->printProviders("Enabled providers", $enabled, $output); |
|
79 | - $this->printProviders("Disabled providers", $disabled, $output); |
|
80 | - } |
|
81 | - |
|
82 | - private function filterEnabledDisabledUnknownProviders(array $providerStates): array { |
|
83 | - $enabled = []; |
|
84 | - $disabled = []; |
|
85 | - |
|
86 | - foreach ($providerStates as $providerId => $isEnabled) { |
|
87 | - if ($isEnabled) { |
|
88 | - $enabled[] = $providerId; |
|
89 | - } else { |
|
90 | - $disabled[] = $providerId; |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - return [$enabled, $disabled]; |
|
95 | - } |
|
96 | - |
|
97 | - private function printProviders(string $title, array $providers, |
|
98 | - OutputInterface $output) { |
|
99 | - if (empty($providers)) { |
|
100 | - // Ignore and don't print anything |
|
101 | - return; |
|
102 | - } |
|
103 | - |
|
104 | - $output->writeln($title . ":"); |
|
105 | - foreach ($providers as $provider) { |
|
106 | - $output->writeln("- " . $provider); |
|
107 | - } |
|
108 | - } |
|
38 | + /** @var IRegistry */ |
|
39 | + private $registry; |
|
40 | + |
|
41 | + /** @var IUserManager */ |
|
42 | + private $userManager; |
|
43 | + |
|
44 | + public function __construct(IRegistry $registry, IUserManager $userManager) { |
|
45 | + parent::__construct('twofactorauth:state'); |
|
46 | + |
|
47 | + $this->registry = $registry; |
|
48 | + $this->userManager = $userManager; |
|
49 | + } |
|
50 | + |
|
51 | + protected function configure() { |
|
52 | + parent::configure(); |
|
53 | + |
|
54 | + $this->setName('twofactorauth:state'); |
|
55 | + $this->setDescription('Get the two-factor authentication (2FA) state of a user'); |
|
56 | + $this->addArgument('uid', InputArgument::REQUIRED); |
|
57 | + } |
|
58 | + |
|
59 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
60 | + $uid = $input->getArgument('uid'); |
|
61 | + $user = $this->userManager->get($uid); |
|
62 | + if (is_null($user)) { |
|
63 | + $output->writeln("<error>Invalid UID</error>"); |
|
64 | + return; |
|
65 | + } |
|
66 | + |
|
67 | + $providerStates = $this->registry->getProviderStates($user); |
|
68 | + $filtered = $this->filterEnabledDisabledUnknownProviders($providerStates); |
|
69 | + list ($enabled, $disabled) = $filtered; |
|
70 | + |
|
71 | + if (!empty($enabled)) { |
|
72 | + $output->writeln("Two-factor authentication is enabled for user $uid"); |
|
73 | + } else { |
|
74 | + $output->writeln("Two-factor authentication is not enabled for user $uid"); |
|
75 | + } |
|
76 | + |
|
77 | + $output->writeln(""); |
|
78 | + $this->printProviders("Enabled providers", $enabled, $output); |
|
79 | + $this->printProviders("Disabled providers", $disabled, $output); |
|
80 | + } |
|
81 | + |
|
82 | + private function filterEnabledDisabledUnknownProviders(array $providerStates): array { |
|
83 | + $enabled = []; |
|
84 | + $disabled = []; |
|
85 | + |
|
86 | + foreach ($providerStates as $providerId => $isEnabled) { |
|
87 | + if ($isEnabled) { |
|
88 | + $enabled[] = $providerId; |
|
89 | + } else { |
|
90 | + $disabled[] = $providerId; |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + return [$enabled, $disabled]; |
|
95 | + } |
|
96 | + |
|
97 | + private function printProviders(string $title, array $providers, |
|
98 | + OutputInterface $output) { |
|
99 | + if (empty($providers)) { |
|
100 | + // Ignore and don't print anything |
|
101 | + return; |
|
102 | + } |
|
103 | + |
|
104 | + $output->writeln($title . ":"); |
|
105 | + foreach ($providers as $provider) { |
|
106 | + $output->writeln("- " . $provider); |
|
107 | + } |
|
108 | + } |
|
109 | 109 | |
110 | 110 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | /** |
6 | 6 | * @copyright 2018 Christoph Wurst <[email protected]> |
@@ -101,9 +101,9 @@ discard block |
||
101 | 101 | return; |
102 | 102 | } |
103 | 103 | |
104 | - $output->writeln($title . ":"); |
|
104 | + $output->writeln($title.":"); |
|
105 | 105 | foreach ($providers as $provider) { |
106 | - $output->writeln("- " . $provider); |
|
106 | + $output->writeln("- ".$provider); |
|
107 | 107 | } |
108 | 108 | } |
109 | 109 |
@@ -43,124 +43,124 @@ |
||
43 | 43 | $application->add(new OC\Core\Command\App\CheckCode()); |
44 | 44 | $application->add(new OC\Core\Command\L10n\CreateJs()); |
45 | 45 | $application->add(new \OC\Core\Command\Integrity\SignApp( |
46 | - \OC::$server->getIntegrityCodeChecker(), |
|
47 | - new \OC\IntegrityCheck\Helpers\FileAccessHelper(), |
|
48 | - \OC::$server->getURLGenerator() |
|
46 | + \OC::$server->getIntegrityCodeChecker(), |
|
47 | + new \OC\IntegrityCheck\Helpers\FileAccessHelper(), |
|
48 | + \OC::$server->getURLGenerator() |
|
49 | 49 | )); |
50 | 50 | $application->add(new \OC\Core\Command\Integrity\SignCore( |
51 | - \OC::$server->getIntegrityCodeChecker(), |
|
52 | - new \OC\IntegrityCheck\Helpers\FileAccessHelper() |
|
51 | + \OC::$server->getIntegrityCodeChecker(), |
|
52 | + new \OC\IntegrityCheck\Helpers\FileAccessHelper() |
|
53 | 53 | )); |
54 | 54 | $application->add(new \OC\Core\Command\Integrity\CheckApp( |
55 | - \OC::$server->getIntegrityCodeChecker() |
|
55 | + \OC::$server->getIntegrityCodeChecker() |
|
56 | 56 | )); |
57 | 57 | $application->add(new \OC\Core\Command\Integrity\CheckCore( |
58 | - \OC::$server->getIntegrityCodeChecker() |
|
58 | + \OC::$server->getIntegrityCodeChecker() |
|
59 | 59 | )); |
60 | 60 | |
61 | 61 | |
62 | 62 | if (\OC::$server->getConfig()->getSystemValue('installed', false)) { |
63 | - $application->add(new OC\Core\Command\App\Disable(\OC::$server->getAppManager())); |
|
64 | - $application->add(new OC\Core\Command\App\Enable(\OC::$server->getAppManager())); |
|
65 | - $application->add(new OC\Core\Command\App\Install()); |
|
66 | - $application->add(new OC\Core\Command\App\GetPath()); |
|
67 | - $application->add(new OC\Core\Command\App\ListApps(\OC::$server->getAppManager())); |
|
63 | + $application->add(new OC\Core\Command\App\Disable(\OC::$server->getAppManager())); |
|
64 | + $application->add(new OC\Core\Command\App\Enable(\OC::$server->getAppManager())); |
|
65 | + $application->add(new OC\Core\Command\App\Install()); |
|
66 | + $application->add(new OC\Core\Command\App\GetPath()); |
|
67 | + $application->add(new OC\Core\Command\App\ListApps(\OC::$server->getAppManager())); |
|
68 | 68 | |
69 | - $application->add(new OC\Core\Command\TwoFactorAuth\Enable( |
|
70 | - \OC::$server->getTwoFactorAuthManager(), \OC::$server->getUserManager() |
|
71 | - )); |
|
72 | - $application->add(new OC\Core\Command\TwoFactorAuth\Disable( |
|
73 | - \OC::$server->getTwoFactorAuthManager(), \OC::$server->getUserManager() |
|
74 | - )); |
|
75 | - $application->add(\OC::$server->query(\OC\Core\Command\TwoFactorAuth\State::class)); |
|
69 | + $application->add(new OC\Core\Command\TwoFactorAuth\Enable( |
|
70 | + \OC::$server->getTwoFactorAuthManager(), \OC::$server->getUserManager() |
|
71 | + )); |
|
72 | + $application->add(new OC\Core\Command\TwoFactorAuth\Disable( |
|
73 | + \OC::$server->getTwoFactorAuthManager(), \OC::$server->getUserManager() |
|
74 | + )); |
|
75 | + $application->add(\OC::$server->query(\OC\Core\Command\TwoFactorAuth\State::class)); |
|
76 | 76 | |
77 | - $application->add(new OC\Core\Command\Background\Cron(\OC::$server->getConfig())); |
|
78 | - $application->add(new OC\Core\Command\Background\WebCron(\OC::$server->getConfig())); |
|
79 | - $application->add(new OC\Core\Command\Background\Ajax(\OC::$server->getConfig())); |
|
77 | + $application->add(new OC\Core\Command\Background\Cron(\OC::$server->getConfig())); |
|
78 | + $application->add(new OC\Core\Command\Background\WebCron(\OC::$server->getConfig())); |
|
79 | + $application->add(new OC\Core\Command\Background\Ajax(\OC::$server->getConfig())); |
|
80 | 80 | |
81 | - $application->add(new OC\Core\Command\Config\App\DeleteConfig(\OC::$server->getConfig())); |
|
82 | - $application->add(new OC\Core\Command\Config\App\GetConfig(\OC::$server->getConfig())); |
|
83 | - $application->add(new OC\Core\Command\Config\App\SetConfig(\OC::$server->getConfig())); |
|
84 | - $application->add(new OC\Core\Command\Config\Import(\OC::$server->getConfig())); |
|
85 | - $application->add(new OC\Core\Command\Config\ListConfigs(\OC::$server->getSystemConfig(), \OC::$server->getAppConfig())); |
|
86 | - $application->add(new OC\Core\Command\Config\System\DeleteConfig(\OC::$server->getSystemConfig())); |
|
87 | - $application->add(new OC\Core\Command\Config\System\GetConfig(\OC::$server->getSystemConfig())); |
|
88 | - $application->add(new OC\Core\Command\Config\System\SetConfig(\OC::$server->getSystemConfig())); |
|
81 | + $application->add(new OC\Core\Command\Config\App\DeleteConfig(\OC::$server->getConfig())); |
|
82 | + $application->add(new OC\Core\Command\Config\App\GetConfig(\OC::$server->getConfig())); |
|
83 | + $application->add(new OC\Core\Command\Config\App\SetConfig(\OC::$server->getConfig())); |
|
84 | + $application->add(new OC\Core\Command\Config\Import(\OC::$server->getConfig())); |
|
85 | + $application->add(new OC\Core\Command\Config\ListConfigs(\OC::$server->getSystemConfig(), \OC::$server->getAppConfig())); |
|
86 | + $application->add(new OC\Core\Command\Config\System\DeleteConfig(\OC::$server->getSystemConfig())); |
|
87 | + $application->add(new OC\Core\Command\Config\System\GetConfig(\OC::$server->getSystemConfig())); |
|
88 | + $application->add(new OC\Core\Command\Config\System\SetConfig(\OC::$server->getSystemConfig())); |
|
89 | 89 | |
90 | - $application->add(new OC\Core\Command\Db\ConvertType(\OC::$server->getConfig(), new \OC\DB\ConnectionFactory(\OC::$server->getSystemConfig()))); |
|
91 | - $application->add(new OC\Core\Command\Db\ConvertMysqlToMB4(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection(), \OC::$server->getURLGenerator(), \OC::$server->getLogger())); |
|
92 | - $application->add(new OC\Core\Command\Db\ConvertFilecacheBigInt(\OC::$server->getDatabaseConnection())); |
|
93 | - $application->add(new OC\Core\Command\Db\AddMissingIndices(\OC::$server->getDatabaseConnection(), \OC::$server->getEventDispatcher())); |
|
94 | - $application->add(new OC\Core\Command\Db\Migrations\StatusCommand(\OC::$server->getDatabaseConnection())); |
|
95 | - $application->add(new OC\Core\Command\Db\Migrations\MigrateCommand(\OC::$server->getDatabaseConnection())); |
|
96 | - $application->add(new OC\Core\Command\Db\Migrations\GenerateCommand(\OC::$server->getDatabaseConnection(), \OC::$server->getAppManager())); |
|
97 | - $application->add(new OC\Core\Command\Db\Migrations\GenerateFromSchemaFileCommand(\OC::$server->getConfig(), \OC::$server->getAppManager(), \OC::$server->getDatabaseConnection())); |
|
98 | - $application->add(new OC\Core\Command\Db\Migrations\ExecuteCommand(\OC::$server->getDatabaseConnection(), \OC::$server->getAppManager(), \OC::$server->getConfig())); |
|
90 | + $application->add(new OC\Core\Command\Db\ConvertType(\OC::$server->getConfig(), new \OC\DB\ConnectionFactory(\OC::$server->getSystemConfig()))); |
|
91 | + $application->add(new OC\Core\Command\Db\ConvertMysqlToMB4(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection(), \OC::$server->getURLGenerator(), \OC::$server->getLogger())); |
|
92 | + $application->add(new OC\Core\Command\Db\ConvertFilecacheBigInt(\OC::$server->getDatabaseConnection())); |
|
93 | + $application->add(new OC\Core\Command\Db\AddMissingIndices(\OC::$server->getDatabaseConnection(), \OC::$server->getEventDispatcher())); |
|
94 | + $application->add(new OC\Core\Command\Db\Migrations\StatusCommand(\OC::$server->getDatabaseConnection())); |
|
95 | + $application->add(new OC\Core\Command\Db\Migrations\MigrateCommand(\OC::$server->getDatabaseConnection())); |
|
96 | + $application->add(new OC\Core\Command\Db\Migrations\GenerateCommand(\OC::$server->getDatabaseConnection(), \OC::$server->getAppManager())); |
|
97 | + $application->add(new OC\Core\Command\Db\Migrations\GenerateFromSchemaFileCommand(\OC::$server->getConfig(), \OC::$server->getAppManager(), \OC::$server->getDatabaseConnection())); |
|
98 | + $application->add(new OC\Core\Command\Db\Migrations\ExecuteCommand(\OC::$server->getDatabaseConnection(), \OC::$server->getAppManager(), \OC::$server->getConfig())); |
|
99 | 99 | |
100 | - $application->add(new OC\Core\Command\Encryption\Disable(\OC::$server->getConfig())); |
|
101 | - $application->add(new OC\Core\Command\Encryption\Enable(\OC::$server->getConfig(), \OC::$server->getEncryptionManager())); |
|
102 | - $application->add(new OC\Core\Command\Encryption\ListModules(\OC::$server->getEncryptionManager())); |
|
103 | - $application->add(new OC\Core\Command\Encryption\SetDefaultModule(\OC::$server->getEncryptionManager())); |
|
104 | - $application->add(new OC\Core\Command\Encryption\Status(\OC::$server->getEncryptionManager())); |
|
105 | - $application->add(new OC\Core\Command\Encryption\EncryptAll(\OC::$server->getEncryptionManager(), \OC::$server->getAppManager(), \OC::$server->getConfig(), new \Symfony\Component\Console\Helper\QuestionHelper())); |
|
106 | - $application->add(new OC\Core\Command\Encryption\DecryptAll( |
|
107 | - \OC::$server->getEncryptionManager(), |
|
108 | - \OC::$server->getAppManager(), |
|
109 | - \OC::$server->getConfig(), |
|
110 | - new \OC\Encryption\DecryptAll(\OC::$server->getEncryptionManager(), \OC::$server->getUserManager(), new \OC\Files\View()), |
|
111 | - new \Symfony\Component\Console\Helper\QuestionHelper()) |
|
112 | - ); |
|
100 | + $application->add(new OC\Core\Command\Encryption\Disable(\OC::$server->getConfig())); |
|
101 | + $application->add(new OC\Core\Command\Encryption\Enable(\OC::$server->getConfig(), \OC::$server->getEncryptionManager())); |
|
102 | + $application->add(new OC\Core\Command\Encryption\ListModules(\OC::$server->getEncryptionManager())); |
|
103 | + $application->add(new OC\Core\Command\Encryption\SetDefaultModule(\OC::$server->getEncryptionManager())); |
|
104 | + $application->add(new OC\Core\Command\Encryption\Status(\OC::$server->getEncryptionManager())); |
|
105 | + $application->add(new OC\Core\Command\Encryption\EncryptAll(\OC::$server->getEncryptionManager(), \OC::$server->getAppManager(), \OC::$server->getConfig(), new \Symfony\Component\Console\Helper\QuestionHelper())); |
|
106 | + $application->add(new OC\Core\Command\Encryption\DecryptAll( |
|
107 | + \OC::$server->getEncryptionManager(), |
|
108 | + \OC::$server->getAppManager(), |
|
109 | + \OC::$server->getConfig(), |
|
110 | + new \OC\Encryption\DecryptAll(\OC::$server->getEncryptionManager(), \OC::$server->getUserManager(), new \OC\Files\View()), |
|
111 | + new \Symfony\Component\Console\Helper\QuestionHelper()) |
|
112 | + ); |
|
113 | 113 | |
114 | - $application->add(new OC\Core\Command\Log\Manage(\OC::$server->getConfig())); |
|
115 | - $application->add(new OC\Core\Command\Log\File(\OC::$server->getConfig())); |
|
114 | + $application->add(new OC\Core\Command\Log\Manage(\OC::$server->getConfig())); |
|
115 | + $application->add(new OC\Core\Command\Log\File(\OC::$server->getConfig())); |
|
116 | 116 | |
117 | - $view = new \OC\Files\View(); |
|
118 | - $util = new \OC\Encryption\Util( |
|
119 | - $view, |
|
120 | - \OC::$server->getUserManager(), |
|
121 | - \OC::$server->getGroupManager(), |
|
122 | - \OC::$server->getConfig() |
|
123 | - ); |
|
124 | - $application->add(new OC\Core\Command\Encryption\ChangeKeyStorageRoot( |
|
125 | - $view, |
|
126 | - \OC::$server->getUserManager(), |
|
127 | - \OC::$server->getConfig(), |
|
128 | - $util, |
|
129 | - new \Symfony\Component\Console\Helper\QuestionHelper() |
|
130 | - ) |
|
131 | - ); |
|
132 | - $application->add(new OC\Core\Command\Encryption\ShowKeyStorageRoot($util)); |
|
117 | + $view = new \OC\Files\View(); |
|
118 | + $util = new \OC\Encryption\Util( |
|
119 | + $view, |
|
120 | + \OC::$server->getUserManager(), |
|
121 | + \OC::$server->getGroupManager(), |
|
122 | + \OC::$server->getConfig() |
|
123 | + ); |
|
124 | + $application->add(new OC\Core\Command\Encryption\ChangeKeyStorageRoot( |
|
125 | + $view, |
|
126 | + \OC::$server->getUserManager(), |
|
127 | + \OC::$server->getConfig(), |
|
128 | + $util, |
|
129 | + new \Symfony\Component\Console\Helper\QuestionHelper() |
|
130 | + ) |
|
131 | + ); |
|
132 | + $application->add(new OC\Core\Command\Encryption\ShowKeyStorageRoot($util)); |
|
133 | 133 | |
134 | - $application->add(new OC\Core\Command\Maintenance\DataFingerprint(\OC::$server->getConfig(), new \OC\AppFramework\Utility\TimeFactory())); |
|
135 | - $application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateDB(\OC::$server->getMimeTypeDetector(), \OC::$server->getMimeTypeLoader())); |
|
136 | - $application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateJS(\OC::$server->getMimeTypeDetector())); |
|
137 | - $application->add(new OC\Core\Command\Maintenance\Mode(\OC::$server->getConfig())); |
|
138 | - $application->add(new OC\Core\Command\Maintenance\UpdateHtaccess()); |
|
139 | - $application->add(new OC\Core\Command\Maintenance\UpdateTheme(\OC::$server->getMimeTypeDetector(), \OC::$server->getMemCacheFactory())); |
|
134 | + $application->add(new OC\Core\Command\Maintenance\DataFingerprint(\OC::$server->getConfig(), new \OC\AppFramework\Utility\TimeFactory())); |
|
135 | + $application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateDB(\OC::$server->getMimeTypeDetector(), \OC::$server->getMimeTypeLoader())); |
|
136 | + $application->add(new OC\Core\Command\Maintenance\Mimetype\UpdateJS(\OC::$server->getMimeTypeDetector())); |
|
137 | + $application->add(new OC\Core\Command\Maintenance\Mode(\OC::$server->getConfig())); |
|
138 | + $application->add(new OC\Core\Command\Maintenance\UpdateHtaccess()); |
|
139 | + $application->add(new OC\Core\Command\Maintenance\UpdateTheme(\OC::$server->getMimeTypeDetector(), \OC::$server->getMemCacheFactory())); |
|
140 | 140 | |
141 | - $application->add(new OC\Core\Command\Upgrade(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->query(\OC\Installer::class))); |
|
142 | - $application->add(new OC\Core\Command\Maintenance\Repair( |
|
143 | - new \OC\Repair(\OC\Repair::getRepairSteps(), \OC::$server->getEventDispatcher()), \OC::$server->getConfig(), |
|
144 | - \OC::$server->getEventDispatcher(), \OC::$server->getAppManager())); |
|
141 | + $application->add(new OC\Core\Command\Upgrade(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->query(\OC\Installer::class))); |
|
142 | + $application->add(new OC\Core\Command\Maintenance\Repair( |
|
143 | + new \OC\Repair(\OC\Repair::getRepairSteps(), \OC::$server->getEventDispatcher()), \OC::$server->getConfig(), |
|
144 | + \OC::$server->getEventDispatcher(), \OC::$server->getAppManager())); |
|
145 | 145 | |
146 | - $application->add(new OC\Core\Command\User\Add(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); |
|
147 | - $application->add(new OC\Core\Command\User\Delete(\OC::$server->getUserManager())); |
|
148 | - $application->add(new OC\Core\Command\User\Disable(\OC::$server->getUserManager())); |
|
149 | - $application->add(new OC\Core\Command\User\Enable(\OC::$server->getUserManager())); |
|
150 | - $application->add(new OC\Core\Command\User\LastSeen(\OC::$server->getUserManager())); |
|
151 | - $application->add(new OC\Core\Command\User\Report(\OC::$server->getUserManager())); |
|
152 | - $application->add(new OC\Core\Command\User\ResetPassword(\OC::$server->getUserManager())); |
|
153 | - $application->add(new OC\Core\Command\User\Setting(\OC::$server->getUserManager(), \OC::$server->getConfig(), \OC::$server->getDatabaseConnection())); |
|
154 | - $application->add(new OC\Core\Command\User\ListCommand(\OC::$server->getUserManager())); |
|
155 | - $application->add(new OC\Core\Command\User\Info(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); |
|
146 | + $application->add(new OC\Core\Command\User\Add(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); |
|
147 | + $application->add(new OC\Core\Command\User\Delete(\OC::$server->getUserManager())); |
|
148 | + $application->add(new OC\Core\Command\User\Disable(\OC::$server->getUserManager())); |
|
149 | + $application->add(new OC\Core\Command\User\Enable(\OC::$server->getUserManager())); |
|
150 | + $application->add(new OC\Core\Command\User\LastSeen(\OC::$server->getUserManager())); |
|
151 | + $application->add(new OC\Core\Command\User\Report(\OC::$server->getUserManager())); |
|
152 | + $application->add(new OC\Core\Command\User\ResetPassword(\OC::$server->getUserManager())); |
|
153 | + $application->add(new OC\Core\Command\User\Setting(\OC::$server->getUserManager(), \OC::$server->getConfig(), \OC::$server->getDatabaseConnection())); |
|
154 | + $application->add(new OC\Core\Command\User\ListCommand(\OC::$server->getUserManager())); |
|
155 | + $application->add(new OC\Core\Command\User\Info(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); |
|
156 | 156 | |
157 | - $application->add(new OC\Core\Command\Group\ListCommand(\OC::$server->getGroupManager())); |
|
158 | - $application->add(new OC\Core\Command\Group\AddUser(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); |
|
159 | - $application->add(new OC\Core\Command\Group\RemoveUser(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); |
|
157 | + $application->add(new OC\Core\Command\Group\ListCommand(\OC::$server->getGroupManager())); |
|
158 | + $application->add(new OC\Core\Command\Group\AddUser(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); |
|
159 | + $application->add(new OC\Core\Command\Group\RemoveUser(\OC::$server->getUserManager(), \OC::$server->getGroupManager())); |
|
160 | 160 | |
161 | - $application->add(new OC\Core\Command\Security\ListCertificates(\OC::$server->getCertificateManager(null), \OC::$server->getL10N('core'))); |
|
162 | - $application->add(new OC\Core\Command\Security\ImportCertificate(\OC::$server->getCertificateManager(null))); |
|
163 | - $application->add(new OC\Core\Command\Security\RemoveCertificate(\OC::$server->getCertificateManager(null))); |
|
161 | + $application->add(new OC\Core\Command\Security\ListCertificates(\OC::$server->getCertificateManager(null), \OC::$server->getL10N('core'))); |
|
162 | + $application->add(new OC\Core\Command\Security\ImportCertificate(\OC::$server->getCertificateManager(null))); |
|
163 | + $application->add(new OC\Core\Command\Security\RemoveCertificate(\OC::$server->getCertificateManager(null))); |
|
164 | 164 | } else { |
165 | - $application->add(new OC\Core\Command\Maintenance\Install(\OC::$server->getSystemConfig())); |
|
165 | + $application->add(new OC\Core\Command\Maintenance\Install(\OC::$server->getSystemConfig())); |
|
166 | 166 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | /** |
6 | 6 | * @copyright 2018 Christoph Wurst <[email protected]> |
@@ -35,52 +35,52 @@ |
||
35 | 35 | */ |
36 | 36 | class ProviderUserAssignmentDao { |
37 | 37 | |
38 | - const TABLE_NAME = 'twofactor_providers'; |
|
38 | + const TABLE_NAME = 'twofactor_providers'; |
|
39 | 39 | |
40 | - /** @var IDBConnection */ |
|
41 | - private $conn; |
|
40 | + /** @var IDBConnection */ |
|
41 | + private $conn; |
|
42 | 42 | |
43 | - public function __construct(IDBConnection $dbConn) { |
|
44 | - $this->conn = $dbConn; |
|
45 | - } |
|
43 | + public function __construct(IDBConnection $dbConn) { |
|
44 | + $this->conn = $dbConn; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Get all assigned provider IDs for the given user ID |
|
49 | - * |
|
50 | - * @return string[] where the array key is the provider ID (string) and the |
|
51 | - * value is the enabled state (bool) |
|
52 | - */ |
|
53 | - public function getState(string $uid): array { |
|
54 | - $qb = $this->conn->getQueryBuilder(); |
|
47 | + /** |
|
48 | + * Get all assigned provider IDs for the given user ID |
|
49 | + * |
|
50 | + * @return string[] where the array key is the provider ID (string) and the |
|
51 | + * value is the enabled state (bool) |
|
52 | + */ |
|
53 | + public function getState(string $uid): array { |
|
54 | + $qb = $this->conn->getQueryBuilder(); |
|
55 | 55 | |
56 | - $query = $qb->select('provider_id', 'enabled') |
|
57 | - ->from(self::TABLE_NAME) |
|
58 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))); |
|
59 | - $result = $query->execute(); |
|
60 | - $providers = []; |
|
61 | - foreach ($result->fetchAll() as $row) { |
|
62 | - $providers[$row['provider_id']] = 1 === (int) $row['enabled']; |
|
63 | - } |
|
64 | - $result->closeCursor(); |
|
56 | + $query = $qb->select('provider_id', 'enabled') |
|
57 | + ->from(self::TABLE_NAME) |
|
58 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))); |
|
59 | + $result = $query->execute(); |
|
60 | + $providers = []; |
|
61 | + foreach ($result->fetchAll() as $row) { |
|
62 | + $providers[$row['provider_id']] = 1 === (int) $row['enabled']; |
|
63 | + } |
|
64 | + $result->closeCursor(); |
|
65 | 65 | |
66 | - return $providers; |
|
67 | - } |
|
66 | + return $providers; |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * Persist a new/updated (provider_id, uid, enabled) tuple |
|
71 | - */ |
|
72 | - public function persist(string $providerId, string $uid, int $enabled) { |
|
73 | - $qb = $this->conn->getQueryBuilder(); |
|
69 | + /** |
|
70 | + * Persist a new/updated (provider_id, uid, enabled) tuple |
|
71 | + */ |
|
72 | + public function persist(string $providerId, string $uid, int $enabled) { |
|
73 | + $qb = $this->conn->getQueryBuilder(); |
|
74 | 74 | |
75 | - // TODO: concurrency? What if (providerId, uid) private key is inserted |
|
76 | - // twice at the same time? |
|
77 | - $query = $qb->insert(self::TABLE_NAME)->values([ |
|
78 | - 'provider_id' => $qb->createNamedParameter($providerId), |
|
79 | - 'uid' => $qb->createNamedParameter($uid), |
|
80 | - 'enabled' => $qb->createNamedParameter($enabled, IQueryBuilder::PARAM_INT), |
|
81 | - ]); |
|
75 | + // TODO: concurrency? What if (providerId, uid) private key is inserted |
|
76 | + // twice at the same time? |
|
77 | + $query = $qb->insert(self::TABLE_NAME)->values([ |
|
78 | + 'provider_id' => $qb->createNamedParameter($providerId), |
|
79 | + 'uid' => $qb->createNamedParameter($uid), |
|
80 | + 'enabled' => $qb->createNamedParameter($enabled, IQueryBuilder::PARAM_INT), |
|
81 | + ]); |
|
82 | 82 | |
83 | - $query->execute(); |
|
84 | - } |
|
83 | + $query->execute(); |
|
84 | + } |
|
85 | 85 | |
86 | 86 | } |
@@ -34,55 +34,55 @@ |
||
34 | 34 | |
35 | 35 | class ProviderLoader { |
36 | 36 | |
37 | - const BACKUP_CODES_APP_ID = 'twofactor_backupcodes'; |
|
37 | + const BACKUP_CODES_APP_ID = 'twofactor_backupcodes'; |
|
38 | 38 | |
39 | - /** @var IAppManager */ |
|
40 | - private $appManager; |
|
39 | + /** @var IAppManager */ |
|
40 | + private $appManager; |
|
41 | 41 | |
42 | - public function __construct(IAppManager $appManager) { |
|
43 | - $this->appManager = $appManager; |
|
44 | - } |
|
42 | + public function __construct(IAppManager $appManager) { |
|
43 | + $this->appManager = $appManager; |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Get the list of 2FA providers for the given user |
|
48 | - * |
|
49 | - * @return IProvider[] |
|
50 | - * @throws Exception |
|
51 | - */ |
|
52 | - public function getProviders(IUser $user): array { |
|
53 | - $allApps = $this->appManager->getEnabledAppsForUser($user); |
|
54 | - $providers = []; |
|
46 | + /** |
|
47 | + * Get the list of 2FA providers for the given user |
|
48 | + * |
|
49 | + * @return IProvider[] |
|
50 | + * @throws Exception |
|
51 | + */ |
|
52 | + public function getProviders(IUser $user): array { |
|
53 | + $allApps = $this->appManager->getEnabledAppsForUser($user); |
|
54 | + $providers = []; |
|
55 | 55 | |
56 | - foreach ($allApps as $appId) { |
|
57 | - $info = $this->appManager->getAppInfo($appId); |
|
58 | - if (isset($info['two-factor-providers'])) { |
|
59 | - /** @var string[] $providerClasses */ |
|
60 | - $providerClasses = $info['two-factor-providers']; |
|
61 | - foreach ($providerClasses as $class) { |
|
62 | - try { |
|
63 | - $this->loadTwoFactorApp($appId); |
|
64 | - $provider = OC::$server->query($class); |
|
65 | - $providers[$provider->getId()] = $provider; |
|
66 | - } catch (QueryException $exc) { |
|
67 | - // Provider class can not be resolved |
|
68 | - throw new Exception("Could not load two-factor auth provider $class"); |
|
69 | - } |
|
70 | - } |
|
71 | - } |
|
72 | - } |
|
56 | + foreach ($allApps as $appId) { |
|
57 | + $info = $this->appManager->getAppInfo($appId); |
|
58 | + if (isset($info['two-factor-providers'])) { |
|
59 | + /** @var string[] $providerClasses */ |
|
60 | + $providerClasses = $info['two-factor-providers']; |
|
61 | + foreach ($providerClasses as $class) { |
|
62 | + try { |
|
63 | + $this->loadTwoFactorApp($appId); |
|
64 | + $provider = OC::$server->query($class); |
|
65 | + $providers[$provider->getId()] = $provider; |
|
66 | + } catch (QueryException $exc) { |
|
67 | + // Provider class can not be resolved |
|
68 | + throw new Exception("Could not load two-factor auth provider $class"); |
|
69 | + } |
|
70 | + } |
|
71 | + } |
|
72 | + } |
|
73 | 73 | |
74 | - return $providers; |
|
75 | - } |
|
74 | + return $providers; |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Load an app by ID if it has not been loaded yet |
|
79 | - * |
|
80 | - * @param string $appId |
|
81 | - */ |
|
82 | - protected function loadTwoFactorApp(string $appId) { |
|
83 | - if (!OC_App::isAppLoaded($appId)) { |
|
84 | - OC_App::loadApp($appId); |
|
85 | - } |
|
86 | - } |
|
77 | + /** |
|
78 | + * Load an app by ID if it has not been loaded yet |
|
79 | + * |
|
80 | + * @param string $appId |
|
81 | + */ |
|
82 | + protected function loadTwoFactorApp(string $appId) { |
|
83 | + if (!OC_App::isAppLoaded($appId)) { |
|
84 | + OC_App::loadApp($appId); |
|
85 | + } |
|
86 | + } |
|
87 | 87 | |
88 | 88 | } |
@@ -39,27 +39,27 @@ |
||
39 | 39 | */ |
40 | 40 | interface IRegistry { |
41 | 41 | |
42 | - /** |
|
43 | - * Get a key-value map of providers and their enabled/disabled state for |
|
44 | - * the given user. |
|
45 | - * |
|
46 | - * @since 14.0.0 |
|
47 | - * @return string[] where the array key is the provider ID (string) and the |
|
48 | - * value is the enabled state (bool) |
|
49 | - */ |
|
50 | - public function getProviderStates(IUser $user): array; |
|
42 | + /** |
|
43 | + * Get a key-value map of providers and their enabled/disabled state for |
|
44 | + * the given user. |
|
45 | + * |
|
46 | + * @since 14.0.0 |
|
47 | + * @return string[] where the array key is the provider ID (string) and the |
|
48 | + * value is the enabled state (bool) |
|
49 | + */ |
|
50 | + public function getProviderStates(IUser $user): array; |
|
51 | 51 | |
52 | - /** |
|
53 | - * Enable the given 2FA provider for the given user |
|
54 | - * |
|
55 | - * @since 14.0.0 |
|
56 | - */ |
|
57 | - public function enableProviderFor(IProvider $provider, IUser $user); |
|
52 | + /** |
|
53 | + * Enable the given 2FA provider for the given user |
|
54 | + * |
|
55 | + * @since 14.0.0 |
|
56 | + */ |
|
57 | + public function enableProviderFor(IProvider $provider, IUser $user); |
|
58 | 58 | |
59 | - /** |
|
60 | - * Disable the given 2FA provider for the given user |
|
61 | - * |
|
62 | - * @since 14.0.0 |
|
63 | - */ |
|
64 | - public function disableProviderFor(IProvider $provider, IUser $user); |
|
59 | + /** |
|
60 | + * Disable the given 2FA provider for the given user |
|
61 | + * |
|
62 | + * @since 14.0.0 |
|
63 | + */ |
|
64 | + public function disableProviderFor(IProvider $provider, IUser $user); |
|
65 | 65 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | /** |
6 | 6 | * @copyright 2018 Christoph Wurst <[email protected]> |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | /** |
6 | 6 | * @copyright 2018 Christoph Wurst <[email protected]> |
@@ -33,23 +33,23 @@ |
||
33 | 33 | |
34 | 34 | class Registry implements IRegistry { |
35 | 35 | |
36 | - /** @var ProviderUserAssignmentDao */ |
|
37 | - private $assignmentDao; |
|
36 | + /** @var ProviderUserAssignmentDao */ |
|
37 | + private $assignmentDao; |
|
38 | 38 | |
39 | - public function __construct(ProviderUserAssignmentDao $assignmentDao) { |
|
40 | - $this->assignmentDao = $assignmentDao; |
|
41 | - } |
|
39 | + public function __construct(ProviderUserAssignmentDao $assignmentDao) { |
|
40 | + $this->assignmentDao = $assignmentDao; |
|
41 | + } |
|
42 | 42 | |
43 | - public function getProviderStates(IUser $user): array { |
|
44 | - return $this->assignmentDao->getState($user->getUID()); |
|
45 | - } |
|
43 | + public function getProviderStates(IUser $user): array { |
|
44 | + return $this->assignmentDao->getState($user->getUID()); |
|
45 | + } |
|
46 | 46 | |
47 | - public function enableProviderFor(IProvider $provider, IUser $user) { |
|
48 | - $this->assignmentDao->persist($provider->getId(), $user->getUID(), 1); |
|
49 | - } |
|
47 | + public function enableProviderFor(IProvider $provider, IUser $user) { |
|
48 | + $this->assignmentDao->persist($provider->getId(), $user->getUID(), 1); |
|
49 | + } |
|
50 | 50 | |
51 | - public function disableProviderFor(IProvider $provider, IUser $user) { |
|
52 | - $this->assignmentDao->persist($provider->getId(), $user->getUID(), 0); |
|
53 | - } |
|
51 | + public function disableProviderFor(IProvider $provider, IUser $user) { |
|
52 | + $this->assignmentDao->persist($provider->getId(), $user->getUID(), 0); |
|
53 | + } |
|
54 | 54 | |
55 | 55 | } |
@@ -42,171 +42,171 @@ |
||
42 | 42 | |
43 | 43 | class TwoFactorChallengeController extends Controller { |
44 | 44 | |
45 | - /** @var Manager */ |
|
46 | - private $twoFactorManager; |
|
47 | - |
|
48 | - /** @var IUserSession */ |
|
49 | - private $userSession; |
|
50 | - |
|
51 | - /** @var ISession */ |
|
52 | - private $session; |
|
53 | - |
|
54 | - /** @var IURLGenerator */ |
|
55 | - private $urlGenerator; |
|
56 | - |
|
57 | - /** |
|
58 | - * @param string $appName |
|
59 | - * @param IRequest $request |
|
60 | - * @param Manager $twoFactorManager |
|
61 | - * @param IUserSession $userSession |
|
62 | - * @param ISession $session |
|
63 | - * @param IURLGenerator $urlGenerator |
|
64 | - */ |
|
65 | - public function __construct($appName, IRequest $request, Manager $twoFactorManager, IUserSession $userSession, |
|
66 | - ISession $session, IURLGenerator $urlGenerator) { |
|
67 | - parent::__construct($appName, $request); |
|
68 | - $this->twoFactorManager = $twoFactorManager; |
|
69 | - $this->userSession = $userSession; |
|
70 | - $this->session = $session; |
|
71 | - $this->urlGenerator = $urlGenerator; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * @return string |
|
76 | - */ |
|
77 | - protected function getLogoutUrl() { |
|
78 | - return OC_User::getLogoutUrl($this->urlGenerator); |
|
79 | - } |
|
45 | + /** @var Manager */ |
|
46 | + private $twoFactorManager; |
|
47 | + |
|
48 | + /** @var IUserSession */ |
|
49 | + private $userSession; |
|
50 | + |
|
51 | + /** @var ISession */ |
|
52 | + private $session; |
|
53 | + |
|
54 | + /** @var IURLGenerator */ |
|
55 | + private $urlGenerator; |
|
56 | + |
|
57 | + /** |
|
58 | + * @param string $appName |
|
59 | + * @param IRequest $request |
|
60 | + * @param Manager $twoFactorManager |
|
61 | + * @param IUserSession $userSession |
|
62 | + * @param ISession $session |
|
63 | + * @param IURLGenerator $urlGenerator |
|
64 | + */ |
|
65 | + public function __construct($appName, IRequest $request, Manager $twoFactorManager, IUserSession $userSession, |
|
66 | + ISession $session, IURLGenerator $urlGenerator) { |
|
67 | + parent::__construct($appName, $request); |
|
68 | + $this->twoFactorManager = $twoFactorManager; |
|
69 | + $this->userSession = $userSession; |
|
70 | + $this->session = $session; |
|
71 | + $this->urlGenerator = $urlGenerator; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * @return string |
|
76 | + */ |
|
77 | + protected function getLogoutUrl() { |
|
78 | + return OC_User::getLogoutUrl($this->urlGenerator); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * @param IProvider[] $providers |
|
83 | - */ |
|
84 | - private function splitProvidersAndBackupCodes(array $providers): array { |
|
85 | - $regular = []; |
|
86 | - $backup = null; |
|
87 | - foreach ($providers as $provider) { |
|
88 | - if ($provider->getId() === 'backup_codes') { |
|
89 | - $backup = $provider; |
|
90 | - } else { |
|
91 | - $regular[] = $provider; |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - return [$regular, $backup]; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * @NoAdminRequired |
|
100 | - * @NoCSRFRequired |
|
101 | - * |
|
102 | - * @param string $redirect_url |
|
103 | - * @return TemplateResponse |
|
104 | - */ |
|
105 | - public function selectChallenge($redirect_url) { |
|
106 | - $user = $this->userSession->getUser(); |
|
107 | - $providerSet = $this->twoFactorManager->getProviderSet($user); |
|
108 | - $allProviders = $providerSet->getProviders(); |
|
109 | - list($providers, $backupProvider) = $this->splitProvidersAndBackupCodes($allProviders); |
|
110 | - |
|
111 | - $data = [ |
|
112 | - 'providers' => $providers, |
|
113 | - 'backupProvider' => $backupProvider, |
|
114 | - 'providerMissing' => $providerSet->isProviderMissing(), |
|
115 | - 'redirect_url' => $redirect_url, |
|
116 | - 'logout_url' => $this->getLogoutUrl(), |
|
117 | - ]; |
|
118 | - return new TemplateResponse($this->appName, 'twofactorselectchallenge', $data, 'guest'); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * @NoAdminRequired |
|
123 | - * @NoCSRFRequired |
|
124 | - * @UseSession |
|
125 | - * |
|
126 | - * @param string $challengeProviderId |
|
127 | - * @param string $redirect_url |
|
128 | - * @return TemplateResponse|RedirectResponse |
|
129 | - */ |
|
130 | - public function showChallenge($challengeProviderId, $redirect_url) { |
|
131 | - $user = $this->userSession->getUser(); |
|
132 | - $providerSet = $this->twoFactorManager->getProviderSet($user); |
|
133 | - $provider = $providerSet->getProvider($challengeProviderId); |
|
134 | - if (is_null($provider)) { |
|
135 | - return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge')); |
|
136 | - } |
|
137 | - |
|
138 | - $backupProvider = $providerSet->getProvider('backup_codes'); |
|
139 | - if (!is_null($backupProvider) && $backupProvider->getId() === $provider->getId()) { |
|
140 | - // Don't show the backup provider link if we're already showing that provider's challenge |
|
141 | - $backupProvider = null; |
|
142 | - } |
|
143 | - |
|
144 | - $errorMessage = ''; |
|
145 | - $error = false; |
|
146 | - if ($this->session->exists('two_factor_auth_error')) { |
|
147 | - $this->session->remove('two_factor_auth_error'); |
|
148 | - $error = true; |
|
149 | - $errorMessage = $this->session->get("two_factor_auth_error_message"); |
|
150 | - $this->session->remove('two_factor_auth_error_message'); |
|
151 | - } |
|
152 | - $tmpl = $provider->getTemplate($user); |
|
153 | - $tmpl->assign('redirect_url', $redirect_url); |
|
154 | - $data = [ |
|
155 | - 'error' => $error, |
|
156 | - 'error_message' => $errorMessage, |
|
157 | - 'provider' => $provider, |
|
158 | - 'backupProvider' => $backupProvider, |
|
159 | - 'logout_url' => $this->getLogoutUrl(), |
|
160 | - 'redirect_url' => $redirect_url, |
|
161 | - 'template' => $tmpl->fetchPage(), |
|
162 | - ]; |
|
163 | - $response = new TemplateResponse($this->appName, 'twofactorshowchallenge', $data, 'guest'); |
|
164 | - if ($provider instanceof IProvidesCustomCSP) { |
|
165 | - $response->setContentSecurityPolicy($provider->getCSP()); |
|
166 | - } |
|
167 | - return $response; |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * @NoAdminRequired |
|
172 | - * @NoCSRFRequired |
|
173 | - * @UseSession |
|
174 | - * |
|
175 | - * @UserRateThrottle(limit=5, period=100) |
|
176 | - * |
|
177 | - * @param string $challengeProviderId |
|
178 | - * @param string $challenge |
|
179 | - * @param string $redirect_url |
|
180 | - * @return RedirectResponse |
|
181 | - */ |
|
182 | - public function solveChallenge($challengeProviderId, $challenge, $redirect_url = null) { |
|
183 | - $user = $this->userSession->getUser(); |
|
184 | - $provider = $this->twoFactorManager->getProvider($user, $challengeProviderId); |
|
185 | - if (is_null($provider)) { |
|
186 | - return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge')); |
|
187 | - } |
|
188 | - |
|
189 | - try { |
|
190 | - if ($this->twoFactorManager->verifyChallenge($challengeProviderId, $user, $challenge)) { |
|
191 | - if (!is_null($redirect_url)) { |
|
192 | - return new RedirectResponse($this->urlGenerator->getAbsoluteURL(urldecode($redirect_url))); |
|
193 | - } |
|
194 | - return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
195 | - } |
|
196 | - } catch (TwoFactorException $e) { |
|
197 | - /* |
|
81 | + /** |
|
82 | + * @param IProvider[] $providers |
|
83 | + */ |
|
84 | + private function splitProvidersAndBackupCodes(array $providers): array { |
|
85 | + $regular = []; |
|
86 | + $backup = null; |
|
87 | + foreach ($providers as $provider) { |
|
88 | + if ($provider->getId() === 'backup_codes') { |
|
89 | + $backup = $provider; |
|
90 | + } else { |
|
91 | + $regular[] = $provider; |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + return [$regular, $backup]; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * @NoAdminRequired |
|
100 | + * @NoCSRFRequired |
|
101 | + * |
|
102 | + * @param string $redirect_url |
|
103 | + * @return TemplateResponse |
|
104 | + */ |
|
105 | + public function selectChallenge($redirect_url) { |
|
106 | + $user = $this->userSession->getUser(); |
|
107 | + $providerSet = $this->twoFactorManager->getProviderSet($user); |
|
108 | + $allProviders = $providerSet->getProviders(); |
|
109 | + list($providers, $backupProvider) = $this->splitProvidersAndBackupCodes($allProviders); |
|
110 | + |
|
111 | + $data = [ |
|
112 | + 'providers' => $providers, |
|
113 | + 'backupProvider' => $backupProvider, |
|
114 | + 'providerMissing' => $providerSet->isProviderMissing(), |
|
115 | + 'redirect_url' => $redirect_url, |
|
116 | + 'logout_url' => $this->getLogoutUrl(), |
|
117 | + ]; |
|
118 | + return new TemplateResponse($this->appName, 'twofactorselectchallenge', $data, 'guest'); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * @NoAdminRequired |
|
123 | + * @NoCSRFRequired |
|
124 | + * @UseSession |
|
125 | + * |
|
126 | + * @param string $challengeProviderId |
|
127 | + * @param string $redirect_url |
|
128 | + * @return TemplateResponse|RedirectResponse |
|
129 | + */ |
|
130 | + public function showChallenge($challengeProviderId, $redirect_url) { |
|
131 | + $user = $this->userSession->getUser(); |
|
132 | + $providerSet = $this->twoFactorManager->getProviderSet($user); |
|
133 | + $provider = $providerSet->getProvider($challengeProviderId); |
|
134 | + if (is_null($provider)) { |
|
135 | + return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge')); |
|
136 | + } |
|
137 | + |
|
138 | + $backupProvider = $providerSet->getProvider('backup_codes'); |
|
139 | + if (!is_null($backupProvider) && $backupProvider->getId() === $provider->getId()) { |
|
140 | + // Don't show the backup provider link if we're already showing that provider's challenge |
|
141 | + $backupProvider = null; |
|
142 | + } |
|
143 | + |
|
144 | + $errorMessage = ''; |
|
145 | + $error = false; |
|
146 | + if ($this->session->exists('two_factor_auth_error')) { |
|
147 | + $this->session->remove('two_factor_auth_error'); |
|
148 | + $error = true; |
|
149 | + $errorMessage = $this->session->get("two_factor_auth_error_message"); |
|
150 | + $this->session->remove('two_factor_auth_error_message'); |
|
151 | + } |
|
152 | + $tmpl = $provider->getTemplate($user); |
|
153 | + $tmpl->assign('redirect_url', $redirect_url); |
|
154 | + $data = [ |
|
155 | + 'error' => $error, |
|
156 | + 'error_message' => $errorMessage, |
|
157 | + 'provider' => $provider, |
|
158 | + 'backupProvider' => $backupProvider, |
|
159 | + 'logout_url' => $this->getLogoutUrl(), |
|
160 | + 'redirect_url' => $redirect_url, |
|
161 | + 'template' => $tmpl->fetchPage(), |
|
162 | + ]; |
|
163 | + $response = new TemplateResponse($this->appName, 'twofactorshowchallenge', $data, 'guest'); |
|
164 | + if ($provider instanceof IProvidesCustomCSP) { |
|
165 | + $response->setContentSecurityPolicy($provider->getCSP()); |
|
166 | + } |
|
167 | + return $response; |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * @NoAdminRequired |
|
172 | + * @NoCSRFRequired |
|
173 | + * @UseSession |
|
174 | + * |
|
175 | + * @UserRateThrottle(limit=5, period=100) |
|
176 | + * |
|
177 | + * @param string $challengeProviderId |
|
178 | + * @param string $challenge |
|
179 | + * @param string $redirect_url |
|
180 | + * @return RedirectResponse |
|
181 | + */ |
|
182 | + public function solveChallenge($challengeProviderId, $challenge, $redirect_url = null) { |
|
183 | + $user = $this->userSession->getUser(); |
|
184 | + $provider = $this->twoFactorManager->getProvider($user, $challengeProviderId); |
|
185 | + if (is_null($provider)) { |
|
186 | + return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge')); |
|
187 | + } |
|
188 | + |
|
189 | + try { |
|
190 | + if ($this->twoFactorManager->verifyChallenge($challengeProviderId, $user, $challenge)) { |
|
191 | + if (!is_null($redirect_url)) { |
|
192 | + return new RedirectResponse($this->urlGenerator->getAbsoluteURL(urldecode($redirect_url))); |
|
193 | + } |
|
194 | + return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
195 | + } |
|
196 | + } catch (TwoFactorException $e) { |
|
197 | + /* |
|
198 | 198 | * The 2FA App threw an TwoFactorException. Now we display more |
199 | 199 | * information to the user. The exception text is stored in the |
200 | 200 | * session to be used in showChallenge() |
201 | 201 | */ |
202 | - $this->session->set('two_factor_auth_error_message', $e->getMessage()); |
|
203 | - } |
|
204 | - |
|
205 | - $this->session->set('two_factor_auth_error', true); |
|
206 | - return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.showChallenge', [ |
|
207 | - 'challengeProviderId' => $provider->getId(), |
|
208 | - 'redirect_url' => $redirect_url, |
|
209 | - ])); |
|
210 | - } |
|
202 | + $this->session->set('two_factor_auth_error_message', $e->getMessage()); |
|
203 | + } |
|
204 | + |
|
205 | + $this->session->set('two_factor_auth_error', true); |
|
206 | + return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.showChallenge', [ |
|
207 | + 'challengeProviderId' => $provider->getId(), |
|
208 | + 'redirect_url' => $redirect_url, |
|
209 | + ])); |
|
210 | + } |
|
211 | 211 | |
212 | 212 | } |