2016-05-11 12:23:25 +03:00
|
|
|
<?php
|
2018-05-22 09:52:16 +03:00
|
|
|
|
|
|
|
declare(strict_types = 1);
|
2016-05-11 12:23:25 +03:00
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-11 12:23:25 +03:00
|
|
|
* @author Christoph Wurst <christoph@owncloud.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-05-11 12:23:25 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Authentication\TwoFactorAuth;
|
|
|
|
|
2017-05-16 10:37:21 +03:00
|
|
|
use BadMethodCallException;
|
2016-06-13 13:46:45 +03:00
|
|
|
use Exception;
|
2017-08-10 13:46:33 +03:00
|
|
|
use OC\Authentication\Exceptions\InvalidTokenException;
|
|
|
|
use OC\Authentication\Token\IProvider as TokenProvider;
|
2016-12-13 12:30:08 +03:00
|
|
|
use OCP\Activity\IManager;
|
2017-08-10 13:46:33 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2016-05-11 12:23:25 +03:00
|
|
|
use OCP\Authentication\TwoFactorAuth\IProvider;
|
2018-05-22 09:52:16 +03:00
|
|
|
use OCP\Authentication\TwoFactorAuth\IRegistry;
|
2016-05-17 16:48:41 +03:00
|
|
|
use OCP\IConfig;
|
2016-12-13 17:15:14 +03:00
|
|
|
use OCP\ILogger;
|
2016-05-11 12:23:25 +03:00
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\IUser;
|
2018-01-17 16:50:27 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent;
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
class Manager {
|
|
|
|
|
|
|
|
const SESSION_UID_KEY = 'two_factor_auth_uid';
|
2017-08-10 13:46:33 +03:00
|
|
|
const SESSION_UID_DONE = 'two_factor_auth_passed';
|
2016-11-02 15:08:09 +03:00
|
|
|
const REMEMBER_LOGIN = 'two_factor_remember_login';
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2018-05-22 09:52:16 +03:00
|
|
|
/** @var ProviderLoader */
|
|
|
|
private $providerLoader;
|
|
|
|
|
|
|
|
/** @var IRegistry */
|
|
|
|
private $providerRegistry;
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
/** @var ISession */
|
|
|
|
private $session;
|
|
|
|
|
2016-05-17 16:48:41 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
|
2016-12-13 12:30:08 +03:00
|
|
|
/** @var IManager */
|
|
|
|
private $activityManager;
|
|
|
|
|
2016-12-13 17:15:14 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
|
|
|
|
2017-08-10 13:46:33 +03:00
|
|
|
/** @var TokenProvider */
|
|
|
|
private $tokenProvider;
|
|
|
|
|
|
|
|
/** @var ITimeFactory */
|
|
|
|
private $timeFactory;
|
|
|
|
|
2018-01-17 16:50:27 +03:00
|
|
|
/** @var EventDispatcherInterface */
|
|
|
|
private $dispatcher;
|
|
|
|
|
2018-05-22 09:52:16 +03:00
|
|
|
public function __construct(ProviderLoader $providerLoader,
|
|
|
|
IRegistry $providerRegistry, ISession $session, IConfig $config,
|
|
|
|
IManager $activityManager, ILogger $logger, TokenProvider $tokenProvider,
|
|
|
|
ITimeFactory $timeFactory, EventDispatcherInterface $eventDispatcher) {
|
|
|
|
$this->providerLoader = $providerLoader;
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->session = $session;
|
2016-05-17 16:48:41 +03:00
|
|
|
$this->config = $config;
|
2016-12-13 12:30:08 +03:00
|
|
|
$this->activityManager = $activityManager;
|
2016-12-13 17:15:14 +03:00
|
|
|
$this->logger = $logger;
|
2017-08-10 13:46:33 +03:00
|
|
|
$this->tokenProvider = $tokenProvider;
|
|
|
|
$this->timeFactory = $timeFactory;
|
2018-01-17 16:50:27 +03:00
|
|
|
$this->dispatcher = $eventDispatcher;
|
2018-05-22 09:52:16 +03:00
|
|
|
$this->providerRegistry = $providerRegistry;
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether the user must provide a second factor challenge
|
|
|
|
*
|
|
|
|
* @param IUser $user
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2018-01-17 16:50:27 +03:00
|
|
|
public function isTwoFactorAuthenticated(IUser $user): bool {
|
2016-05-17 16:48:41 +03:00
|
|
|
$twoFactorEnabled = ((int) $this->config->getUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 0)) === 0;
|
2018-05-22 09:52:16 +03:00
|
|
|
|
|
|
|
if (!$twoFactorEnabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$providerStates = $this->providerRegistry->getProviderStates($user);
|
2018-08-08 07:57:32 +03:00
|
|
|
$providers = $this->providerLoader->getProviders($user);
|
|
|
|
$fixedStates = $this->fixMissingProviderStates($providerStates, $providers, $user);
|
|
|
|
$enabled = array_filter($fixedStates);
|
2018-05-22 09:52:16 +03:00
|
|
|
|
|
|
|
return $twoFactorEnabled && !empty($enabled);
|
2016-05-17 16:48:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable 2FA checks for the given user
|
|
|
|
*
|
|
|
|
* @param IUser $user
|
|
|
|
*/
|
|
|
|
public function disableTwoFactorAuthentication(IUser $user) {
|
|
|
|
$this->config->setUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable all 2FA checks for the given user
|
|
|
|
*
|
|
|
|
* @param IUser $user
|
|
|
|
*/
|
|
|
|
public function enableTwoFactorAuthentication(IUser $user) {
|
|
|
|
$this->config->deleteUserValue($user->getUID(), 'core', 'two_factor_auth_disabled');
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a 2FA provider by its ID
|
|
|
|
*
|
|
|
|
* @param IUser $user
|
|
|
|
* @param string $challengeProviderId
|
|
|
|
* @return IProvider|null
|
|
|
|
*/
|
2018-01-17 16:50:27 +03:00
|
|
|
public function getProvider(IUser $user, string $challengeProviderId) {
|
2018-05-22 09:52:16 +03:00
|
|
|
$providers = $this->getProviderSet($user)->getProviders();
|
2018-01-17 16:50:27 +03:00
|
|
|
return $providers[$challengeProviderId] ?? null;
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
2016-08-29 20:19:44 +03:00
|
|
|
/**
|
2018-05-22 09:52:16 +03:00
|
|
|
* Check if the persistant mapping of enabled/disabled state of each available
|
|
|
|
* provider is missing an entry and add it to the registry in that case.
|
|
|
|
*
|
|
|
|
* @todo remove in Nextcloud 17 as by then all providers should have been updated
|
|
|
|
*
|
|
|
|
* @param string[] $providerStates
|
|
|
|
* @param IProvider[] $providers
|
2016-08-29 20:19:44 +03:00
|
|
|
* @param IUser $user
|
2018-05-22 09:52:16 +03:00
|
|
|
* @return string[] the updated $providerStates variable
|
2016-08-29 20:19:44 +03:00
|
|
|
*/
|
2018-05-22 09:52:16 +03:00
|
|
|
private function fixMissingProviderStates(array $providerStates,
|
|
|
|
array $providers, IUser $user): array {
|
|
|
|
|
|
|
|
foreach ($providers as $provider) {
|
|
|
|
if (isset($providerStates[$provider->getId()])) {
|
|
|
|
// All good
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$enabled = $provider->isTwoFactorAuthEnabledForUser($user);
|
|
|
|
if ($enabled) {
|
|
|
|
$this->providerRegistry->enableProviderFor($provider, $user);
|
|
|
|
} else {
|
|
|
|
$this->providerRegistry->disableProviderFor($provider, $user);
|
|
|
|
}
|
|
|
|
$providerStates[$provider->getId()] = $enabled;
|
2016-11-28 10:47:34 +03:00
|
|
|
}
|
2018-05-22 09:52:16 +03:00
|
|
|
|
|
|
|
return $providerStates;
|
2016-08-29 20:19:44 +03:00
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
/**
|
2018-05-22 09:52:16 +03:00
|
|
|
* @param array $states
|
|
|
|
* @param IProvider $providers
|
2016-05-11 12:23:25 +03:00
|
|
|
*/
|
2018-05-22 09:52:16 +03:00
|
|
|
private function isProviderMissing(array $states, array $providers): bool {
|
|
|
|
$indexed = [];
|
|
|
|
foreach ($providers as $provider) {
|
|
|
|
$indexed[$provider->getId()] = $provider;
|
|
|
|
}
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2018-05-22 09:52:16 +03:00
|
|
|
$missing = [];
|
|
|
|
foreach ($states as $providerId => $enabled) {
|
|
|
|
if (!$enabled) {
|
|
|
|
// Don't care
|
2016-08-29 20:19:44 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-05-22 09:52:16 +03:00
|
|
|
if (!isset($indexed[$providerId])) {
|
|
|
|
$missing[] = $providerId;
|
|
|
|
$this->logger->alert("two-factor auth provider '$providerId' failed to load",
|
|
|
|
[
|
|
|
|
'app' => 'core',
|
|
|
|
]);
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 09:52:16 +03:00
|
|
|
if (!empty($missing)) {
|
|
|
|
// There was at least one provider missing
|
|
|
|
$this->logger->alert(count($missing) . " two-factor auth providers failed to load", ['app' => 'core']);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we reach this, there was not a single provider missing
|
|
|
|
return false;
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
2016-06-16 11:08:49 +03:00
|
|
|
/**
|
2018-05-22 09:52:16 +03:00
|
|
|
* Get the list of 2FA providers for the given user
|
2016-06-16 11:08:49 +03:00
|
|
|
*
|
2018-05-22 09:52:16 +03:00
|
|
|
* @param IUser $user
|
|
|
|
* @throws Exception
|
2016-06-16 11:08:49 +03:00
|
|
|
*/
|
2018-05-22 09:52:16 +03:00
|
|
|
public function getProviderSet(IUser $user): ProviderSet {
|
|
|
|
$providerStates = $this->providerRegistry->getProviderStates($user);
|
|
|
|
$providers = $this->providerLoader->getProviders($user);
|
|
|
|
|
|
|
|
$fixedStates = $this->fixMissingProviderStates($providerStates, $providers, $user);
|
|
|
|
$isProviderMissing = $this->isProviderMissing($fixedStates, $providers);
|
|
|
|
|
|
|
|
$enabled = array_filter($providers, function (IProvider $provider) use ($fixedStates) {
|
|
|
|
return $fixedStates[$provider->getId()];
|
|
|
|
});
|
|
|
|
return new ProviderSet($enabled, $isProviderMissing);
|
2016-06-16 11:08:49 +03:00
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
/**
|
|
|
|
* Verify the given challenge
|
|
|
|
*
|
|
|
|
* @param string $providerId
|
|
|
|
* @param IUser $user
|
|
|
|
* @param string $challenge
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2018-01-17 16:50:27 +03:00
|
|
|
public function verifyChallenge(string $providerId, IUser $user, string $challenge): bool {
|
2016-05-11 12:23:25 +03:00
|
|
|
$provider = $this->getProvider($user, $providerId);
|
2018-01-17 16:50:27 +03:00
|
|
|
if ($provider === null) {
|
2016-05-11 12:23:25 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-06 22:41:15 +03:00
|
|
|
$passed = $provider->verifyChallenge($user, $challenge);
|
|
|
|
if ($passed) {
|
2016-11-02 15:08:09 +03:00
|
|
|
if ($this->session->get(self::REMEMBER_LOGIN) === true) {
|
2016-09-06 22:41:15 +03:00
|
|
|
// TODO: resolve cyclic dependency and use DI
|
|
|
|
\OC::$server->getUserSession()->createRememberMeToken($user);
|
|
|
|
}
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->session->remove(self::SESSION_UID_KEY);
|
2016-11-02 15:08:09 +03:00
|
|
|
$this->session->remove(self::REMEMBER_LOGIN);
|
2017-08-10 13:46:33 +03:00
|
|
|
$this->session->set(self::SESSION_UID_DONE, $user->getUID());
|
|
|
|
|
|
|
|
// Clear token from db
|
|
|
|
$sessionId = $this->session->getId();
|
|
|
|
$token = $this->tokenProvider->getToken($sessionId);
|
|
|
|
$tokenId = $token->getId();
|
|
|
|
$this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $tokenId);
|
2016-12-13 12:30:08 +03:00
|
|
|
|
2018-01-17 16:50:27 +03:00
|
|
|
$dispatchEvent = new GenericEvent($user, ['provider' => $provider->getDisplayName()]);
|
|
|
|
$this->dispatcher->dispatch(IProvider::EVENT_SUCCESS, $dispatchEvent);
|
|
|
|
|
2016-12-13 12:30:08 +03:00
|
|
|
$this->publishEvent($user, 'twofactor_success', [
|
|
|
|
'provider' => $provider->getDisplayName(),
|
|
|
|
]);
|
|
|
|
} else {
|
2018-01-17 16:50:27 +03:00
|
|
|
$dispatchEvent = new GenericEvent($user, ['provider' => $provider->getDisplayName()]);
|
|
|
|
$this->dispatcher->dispatch(IProvider::EVENT_FAILED, $dispatchEvent);
|
|
|
|
|
2016-12-13 12:30:08 +03:00
|
|
|
$this->publishEvent($user, 'twofactor_failed', [
|
|
|
|
'provider' => $provider->getDisplayName(),
|
|
|
|
]);
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
2016-09-06 22:41:15 +03:00
|
|
|
return $passed;
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
2016-12-13 12:30:08 +03:00
|
|
|
/**
|
|
|
|
* Push a 2fa event the user's activity stream
|
|
|
|
*
|
|
|
|
* @param IUser $user
|
|
|
|
* @param string $event
|
2018-01-17 16:50:27 +03:00
|
|
|
* @param array $params
|
2016-12-13 12:30:08 +03:00
|
|
|
*/
|
2018-01-17 16:50:27 +03:00
|
|
|
private function publishEvent(IUser $user, string $event, array $params) {
|
2016-12-13 12:30:08 +03:00
|
|
|
$activity = $this->activityManager->generateEvent();
|
2017-05-16 10:37:21 +03:00
|
|
|
$activity->setApp('core')
|
|
|
|
->setType('security')
|
2016-12-13 12:30:08 +03:00
|
|
|
->setAuthor($user->getUID())
|
2016-12-13 17:15:14 +03:00
|
|
|
->setAffectedUser($user->getUID())
|
|
|
|
->setSubject($event, $params);
|
|
|
|
try {
|
|
|
|
$this->activityManager->publish($activity);
|
2017-05-16 10:37:21 +03:00
|
|
|
} catch (BadMethodCallException $e) {
|
2018-05-22 09:52:16 +03:00
|
|
|
$this->logger->warning('could not publish activity', ['app' => 'core']);
|
2017-05-16 10:37:21 +03:00
|
|
|
$this->logger->logException($e, ['app' => 'core']);
|
2016-12-13 17:15:14 +03:00
|
|
|
}
|
2016-12-13 12:30:08 +03:00
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
/**
|
|
|
|
* Check if the currently logged in user needs to pass 2FA
|
|
|
|
*
|
2016-08-24 11:42:07 +03:00
|
|
|
* @param IUser $user the currently logged in user
|
2016-05-11 12:23:25 +03:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2018-01-17 16:50:27 +03:00
|
|
|
public function needsSecondFactor(IUser $user = null): bool {
|
2017-08-10 13:46:33 +03:00
|
|
|
if ($user === null) {
|
2016-08-24 11:42:07 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-12 23:28:43 +03:00
|
|
|
// If we are authenticated using an app password skip all this
|
|
|
|
if ($this->session->exists('app_password')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-10 13:46:33 +03:00
|
|
|
// First check if the session tells us we should do 2FA (99% case)
|
|
|
|
if (!$this->session->exists(self::SESSION_UID_KEY)) {
|
|
|
|
|
|
|
|
// Check if the session tells us it is 2FA authenticated already
|
|
|
|
if ($this->session->exists(self::SESSION_UID_DONE) &&
|
|
|
|
$this->session->get(self::SESSION_UID_DONE) === $user->getUID()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the session is expired check if we are not logged in by a token
|
|
|
|
* that still needs 2FA auth
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
$sessionId = $this->session->getId();
|
|
|
|
$token = $this->tokenProvider->getToken($sessionId);
|
|
|
|
$tokenId = $token->getId();
|
|
|
|
$tokensNeeding2FA = $this->config->getUserKeys($user->getUID(), 'login_token_2fa');
|
|
|
|
|
2018-01-17 16:50:27 +03:00
|
|
|
if (!\in_array($tokenId, $tokensNeeding2FA, true)) {
|
2017-08-10 13:46:33 +03:00
|
|
|
$this->session->set(self::SESSION_UID_DONE, $user->getUID());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (InvalidTokenException $e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 11:42:07 +03:00
|
|
|
if (!$this->isTwoFactorAuthenticated($user)) {
|
|
|
|
// There is no second factor any more -> let the user pass
|
|
|
|
// This prevents infinite redirect loops when a user is about
|
|
|
|
// to solve the 2FA challenge, and the provider app is
|
|
|
|
// disabled the same time
|
|
|
|
$this->session->remove(self::SESSION_UID_KEY);
|
2017-08-10 13:46:33 +03:00
|
|
|
|
|
|
|
$keys = $this->config->getUserKeys($user->getUID(), 'login_token_2fa');
|
|
|
|
foreach ($keys as $key) {
|
|
|
|
$this->config->deleteUserValue($user->getUID(), 'login_token_2fa', $key);
|
|
|
|
}
|
2016-08-24 11:42:07 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-06 22:41:15 +03:00
|
|
|
* Prepare the 2FA login
|
2016-05-11 12:23:25 +03:00
|
|
|
*
|
|
|
|
* @param IUser $user
|
2016-09-06 22:41:15 +03:00
|
|
|
* @param boolean $rememberMe
|
2016-05-11 12:23:25 +03:00
|
|
|
*/
|
2018-01-17 16:50:27 +03:00
|
|
|
public function prepareTwoFactorLogin(IUser $user, bool $rememberMe) {
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->session->set(self::SESSION_UID_KEY, $user->getUID());
|
2016-11-02 15:08:09 +03:00
|
|
|
$this->session->set(self::REMEMBER_LOGIN, $rememberMe);
|
2017-08-10 13:46:33 +03:00
|
|
|
|
|
|
|
$id = $this->session->getId();
|
|
|
|
$token = $this->tokenProvider->getToken($id);
|
|
|
|
$this->config->setUserValue($user->getUID(), 'login_token_2fa', $token->getId(), $this->timeFactory->getTime());
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|