2016-05-11 12:23:25 +03:00
|
|
|
<?php
|
2016-12-13 12:30:08 +03:00
|
|
|
|
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>
|
|
|
|
*
|
|
|
|
* @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;
|
2016-05-11 12:23:25 +03:00
|
|
|
use OC;
|
|
|
|
use OC\App\AppManager;
|
2016-06-16 11:08:49 +03:00
|
|
|
use OC_App;
|
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;
|
2016-05-11 12:23:25 +03:00
|
|
|
use OCP\AppFramework\QueryException;
|
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;
|
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;
|
|
|
|
|
|
|
|
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-08-29 20:19:44 +03:00
|
|
|
const BACKUP_CODES_APP_ID = 'twofactor_backupcodes';
|
|
|
|
const BACKUP_CODES_PROVIDER_ID = 'backup_codes';
|
2016-11-02 15:08:09 +03:00
|
|
|
const REMEMBER_LOGIN = 'two_factor_remember_login';
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
/** @var AppManager */
|
|
|
|
private $appManager;
|
|
|
|
|
|
|
|
/** @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;
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
/**
|
|
|
|
* @param AppManager $appManager
|
|
|
|
* @param ISession $session
|
2016-05-17 16:48:41 +03:00
|
|
|
* @param IConfig $config
|
2016-12-13 17:15:14 +03:00
|
|
|
* @param IManager $activityManager
|
|
|
|
* @param ILogger $logger
|
2017-08-10 13:46:33 +03:00
|
|
|
* @param TokenProvider $tokenProvider
|
|
|
|
* @param ITimeFactory $timeFactory
|
2016-05-11 12:23:25 +03:00
|
|
|
*/
|
2017-08-10 13:46:33 +03:00
|
|
|
public function __construct(AppManager $appManager,
|
|
|
|
ISession $session,
|
|
|
|
IConfig $config,
|
|
|
|
IManager $activityManager,
|
|
|
|
ILogger $logger,
|
|
|
|
TokenProvider $tokenProvider,
|
|
|
|
ITimeFactory $timeFactory) {
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->appManager = $appManager;
|
|
|
|
$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;
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether the user must provide a second factor challenge
|
|
|
|
*
|
|
|
|
* @param IUser $user
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isTwoFactorAuthenticated(IUser $user) {
|
2016-05-17 16:48:41 +03:00
|
|
|
$twoFactorEnabled = ((int) $this->config->getUserValue($user->getUID(), 'core', 'two_factor_auth_disabled', 0)) === 0;
|
|
|
|
return $twoFactorEnabled && count($this->getProviders($user)) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public function getProvider(IUser $user, $challengeProviderId) {
|
2016-08-29 20:19:44 +03:00
|
|
|
$providers = $this->getProviders($user, true);
|
2016-05-11 12:23:25 +03:00
|
|
|
return isset($providers[$challengeProviderId]) ? $providers[$challengeProviderId] : null;
|
|
|
|
}
|
|
|
|
|
2016-08-29 20:19:44 +03:00
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @return IProvider|null the backup provider, if enabled for the given user
|
|
|
|
*/
|
|
|
|
public function getBackupProvider(IUser $user) {
|
|
|
|
$providers = $this->getProviders($user, true);
|
2016-11-28 10:47:34 +03:00
|
|
|
if (!isset($providers[self::BACKUP_CODES_PROVIDER_ID])) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-08-29 20:19:44 +03:00
|
|
|
return $providers[self::BACKUP_CODES_PROVIDER_ID];
|
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
/**
|
|
|
|
* Get the list of 2FA providers for the given user
|
|
|
|
*
|
|
|
|
* @param IUser $user
|
2016-08-29 20:19:44 +03:00
|
|
|
* @param bool $includeBackupApp
|
2016-05-11 12:23:25 +03:00
|
|
|
* @return IProvider[]
|
2016-11-02 15:07:49 +03:00
|
|
|
* @throws Exception
|
2016-05-11 12:23:25 +03:00
|
|
|
*/
|
2016-08-29 20:19:44 +03:00
|
|
|
public function getProviders(IUser $user, $includeBackupApp = false) {
|
2016-05-11 12:23:25 +03:00
|
|
|
$allApps = $this->appManager->getEnabledAppsForUser($user);
|
|
|
|
$providers = [];
|
|
|
|
|
|
|
|
foreach ($allApps as $appId) {
|
2016-08-29 20:19:44 +03:00
|
|
|
if (!$includeBackupApp && $appId === self::BACKUP_CODES_APP_ID) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
$info = $this->appManager->getAppInfo($appId);
|
2016-07-23 14:26:57 +03:00
|
|
|
if (isset($info['two-factor-providers'])) {
|
|
|
|
$providerClasses = $info['two-factor-providers'];
|
|
|
|
foreach ($providerClasses as $class) {
|
|
|
|
try {
|
|
|
|
$this->loadTwoFactorApp($appId);
|
|
|
|
$provider = OC::$server->query($class);
|
|
|
|
$providers[$provider->getId()] = $provider;
|
|
|
|
} catch (QueryException $exc) {
|
|
|
|
// Provider class can not be resolved
|
|
|
|
throw new Exception("Could not load two-factor auth provider $class");
|
|
|
|
}
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_filter($providers, function ($provider) use ($user) {
|
|
|
|
/* @var $provider IProvider */
|
|
|
|
return $provider->isTwoFactorAuthEnabledForUser($user);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-16 11:08:49 +03:00
|
|
|
/**
|
|
|
|
* Load an app by ID if it has not been loaded yet
|
|
|
|
*
|
|
|
|
* @param string $appId
|
|
|
|
*/
|
|
|
|
protected function loadTwoFactorApp($appId) {
|
|
|
|
if (!OC_App::isAppLoaded($appId)) {
|
|
|
|
OC_App::loadApp($appId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
/**
|
|
|
|
* Verify the given challenge
|
|
|
|
*
|
|
|
|
* @param string $providerId
|
|
|
|
* @param IUser $user
|
|
|
|
* @param string $challenge
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function verifyChallenge($providerId, IUser $user, $challenge) {
|
|
|
|
$provider = $this->getProvider($user, $providerId);
|
|
|
|
if (is_null($provider)) {
|
|
|
|
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
|
|
|
|
|
|
|
$this->publishEvent($user, 'twofactor_success', [
|
|
|
|
'provider' => $provider->getDisplayName(),
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
private function publishEvent(IUser $user, $event, array $params) {
|
|
|
|
$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) {
|
|
|
|
$this->logger->warning('could not publish backup code creation activity', ['app' => 'core']);
|
|
|
|
$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
|
|
|
|
*/
|
2016-08-24 11:42:07 +03:00
|
|
|
public function needsSecondFactor(IUser $user = null) {
|
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');
|
|
|
|
|
|
|
|
if (!in_array($tokenId, $tokensNeeding2FA, true)) {
|
|
|
|
$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
|
|
|
*/
|
2016-09-06 22:41:15 +03:00
|
|
|
public function prepareTwoFactorLogin(IUser $user, $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
|
|
|
}
|
|
|
|
|
|
|
|
}
|