2016-05-11 12:23:25 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Christoph Wurst <christoph@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
* @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 Test\Authentication\TwoFactorAuth;
|
|
|
|
|
2016-08-29 20:19:44 +03:00
|
|
|
use OC;
|
2017-08-10 13:46:33 +03:00
|
|
|
use OC\Authentication\Token\IProvider as TokenProvider;
|
2016-05-11 12:23:25 +03:00
|
|
|
use OC\Authentication\TwoFactorAuth\Manager;
|
2018-09-29 19:57:00 +03:00
|
|
|
use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor;
|
2018-05-22 09:52:16 +03:00
|
|
|
use OC\Authentication\TwoFactorAuth\ProviderLoader;
|
2016-12-20 17:23:36 +03:00
|
|
|
use OCP\Activity\IEvent;
|
|
|
|
use OCP\Activity\IManager;
|
2017-08-10 13:46:33 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2019-04-05 19:21:08 +03:00
|
|
|
use OCP\Authentication\TwoFactorAuth\IActivatableAtLogin;
|
2016-08-29 20:19:44 +03:00
|
|
|
use OCP\Authentication\TwoFactorAuth\IProvider;
|
2018-05-22 09:52:16 +03:00
|
|
|
use OCP\Authentication\TwoFactorAuth\IRegistry;
|
2021-03-05 18:02:17 +03:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2016-08-29 20:19:44 +03:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\IUser;
|
2018-10-11 13:20:18 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2020-10-12 18:14:25 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2019-04-05 19:21:08 +03:00
|
|
|
use function reset;
|
2018-01-24 10:48:28 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2016-08-29 20:19:44 +03:00
|
|
|
use Test\TestCase;
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
class ManagerTest extends TestCase {
|
|
|
|
|
2018-10-11 13:20:18 +03:00
|
|
|
/** @var IUser|MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $user;
|
|
|
|
|
2018-10-11 13:20:18 +03:00
|
|
|
/** @var ProviderLoader|MockObject */
|
2018-05-22 09:52:16 +03:00
|
|
|
private $providerLoader;
|
|
|
|
|
2018-10-11 13:20:18 +03:00
|
|
|
/** @var IRegistry|MockObject */
|
2018-05-22 09:52:16 +03:00
|
|
|
private $providerRegistry;
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2018-10-11 13:20:18 +03:00
|
|
|
/** @var MandatoryTwoFactor|MockObject */
|
2018-09-29 19:57:00 +03:00
|
|
|
private $mandatoryTwoFactor;
|
|
|
|
|
2018-10-11 13:20:18 +03:00
|
|
|
/** @var ISession|MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $session;
|
|
|
|
|
|
|
|
/** @var Manager */
|
|
|
|
private $manager;
|
|
|
|
|
2018-10-11 13:20:18 +03:00
|
|
|
/** @var IConfig|MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $config;
|
|
|
|
|
2018-10-11 13:20:18 +03:00
|
|
|
/** @var IManager|MockObject */
|
2016-12-20 17:23:36 +03:00
|
|
|
private $activityManager;
|
|
|
|
|
2020-10-12 18:14:25 +03:00
|
|
|
/** @var LoggerInterface|MockObject */
|
2016-12-20 17:23:36 +03:00
|
|
|
private $logger;
|
|
|
|
|
2018-10-11 13:20:18 +03:00
|
|
|
/** @var IProvider|MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $fakeProvider;
|
|
|
|
|
2018-10-11 13:20:18 +03:00
|
|
|
/** @var IProvider|MockObject */
|
2016-08-29 20:19:44 +03:00
|
|
|
private $backupProvider;
|
|
|
|
|
2018-10-11 13:20:18 +03:00
|
|
|
/** @var TokenProvider|MockObject */
|
2017-08-10 13:46:33 +03:00
|
|
|
private $tokenProvider;
|
|
|
|
|
2018-10-11 13:20:18 +03:00
|
|
|
/** @var ITimeFactory|MockObject */
|
2017-08-10 13:46:33 +03:00
|
|
|
private $timeFactory;
|
|
|
|
|
2021-03-05 18:02:17 +03:00
|
|
|
/** @var IEventDispatcher|MockObject */
|
|
|
|
private $newDispatcher;
|
|
|
|
|
2018-10-11 13:20:18 +03:00
|
|
|
/** @var EventDispatcherInterface|MockObject */
|
2018-01-24 10:48:28 +03:00
|
|
|
private $eventDispatcher;
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2016-05-11 12:23:25 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2016-09-02 12:07:26 +03:00
|
|
|
$this->user = $this->createMock(IUser::class);
|
2018-10-11 13:20:18 +03:00
|
|
|
$this->providerLoader = $this->createMock(ProviderLoader::class);
|
2018-05-22 09:52:16 +03:00
|
|
|
$this->providerRegistry = $this->createMock(IRegistry::class);
|
2018-09-29 19:57:00 +03:00
|
|
|
$this->mandatoryTwoFactor = $this->createMock(MandatoryTwoFactor::class);
|
2016-09-02 12:07:26 +03:00
|
|
|
$this->session = $this->createMock(ISession::class);
|
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2016-12-20 17:23:36 +03:00
|
|
|
$this->activityManager = $this->createMock(IManager::class);
|
2020-10-12 18:14:25 +03:00
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
2017-08-10 13:46:33 +03:00
|
|
|
$this->tokenProvider = $this->createMock(TokenProvider::class);
|
|
|
|
$this->timeFactory = $this->createMock(ITimeFactory::class);
|
2021-03-05 18:02:17 +03:00
|
|
|
$this->newDispatcher = $this->createMock(IEventDispatcher::class);
|
2018-01-24 10:48:28 +03:00
|
|
|
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
2017-08-10 13:46:33 +03:00
|
|
|
|
2018-05-22 09:52:16 +03:00
|
|
|
$this->manager = new Manager(
|
|
|
|
$this->providerLoader,
|
|
|
|
$this->providerRegistry,
|
2018-09-29 19:57:00 +03:00
|
|
|
$this->mandatoryTwoFactor,
|
2018-05-22 09:52:16 +03:00
|
|
|
$this->session,
|
|
|
|
$this->config,
|
|
|
|
$this->activityManager,
|
|
|
|
$this->logger,
|
|
|
|
$this->tokenProvider,
|
|
|
|
$this->timeFactory,
|
2021-03-05 18:02:17 +03:00
|
|
|
$this->newDispatcher,
|
2018-05-22 09:52:16 +03:00
|
|
|
$this->eventDispatcher
|
|
|
|
);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2016-09-02 12:07:26 +03:00
|
|
|
$this->fakeProvider = $this->createMock(IProvider::class);
|
2018-05-22 09:52:16 +03:00
|
|
|
$this->fakeProvider->method('getId')->willReturn('email');
|
2016-08-29 20:19:44 +03:00
|
|
|
|
|
|
|
$this->backupProvider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')->getMock();
|
2018-05-22 09:52:16 +03:00
|
|
|
$this->backupProvider->method('getId')->willReturn('backup_codes');
|
|
|
|
$this->backupProvider->method('isTwoFactorAuthEnabledForUser')->willReturn(true);
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
2016-08-24 11:42:07 +03:00
|
|
|
private function prepareNoProviders() {
|
2018-05-22 09:52:16 +03:00
|
|
|
$this->providerLoader->method('getProviders')
|
2016-08-24 11:42:07 +03:00
|
|
|
->with($this->user)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn([]);
|
2016-08-24 11:42:07 +03:00
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
private function prepareProviders() {
|
2018-05-22 09:52:16 +03:00
|
|
|
$this->providerRegistry->expects($this->once())
|
|
|
|
->method('getProviderStates')
|
2016-05-11 12:23:25 +03:00
|
|
|
->with($this->user)
|
2018-05-22 09:52:16 +03:00
|
|
|
->willReturn([
|
|
|
|
$this->fakeProvider->getId() => true,
|
|
|
|
]);
|
|
|
|
$this->providerLoader->expects($this->once())
|
|
|
|
->method('getProviders')
|
2016-08-29 20:19:44 +03:00
|
|
|
->with($this->user)
|
2018-05-22 09:52:16 +03:00
|
|
|
->willReturn([$this->fakeProvider]);
|
2016-08-29 20:19:44 +03:00
|
|
|
}
|
|
|
|
|
2018-05-22 09:52:16 +03:00
|
|
|
private function prepareProvidersWitBackupProvider() {
|
|
|
|
$this->providerLoader->method('getProviders')
|
2016-06-13 13:46:45 +03:00
|
|
|
->with($this->user)
|
2018-05-22 09:52:16 +03:00
|
|
|
->willReturn([
|
|
|
|
$this->fakeProvider,
|
|
|
|
$this->backupProvider,
|
|
|
|
]);
|
2016-06-13 13:46:45 +03:00
|
|
|
}
|
|
|
|
|
2018-09-29 19:57:00 +03:00
|
|
|
public function testIsTwoFactorAuthenticatedEnforced() {
|
|
|
|
$this->mandatoryTwoFactor->expects($this->once())
|
2018-10-11 13:20:18 +03:00
|
|
|
->method('isEnforcedFor')
|
|
|
|
->with($this->user)
|
2018-09-29 19:57:00 +03:00
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$enabled = $this->manager->isTwoFactorAuthenticated($this->user);
|
|
|
|
|
|
|
|
$this->assertTrue($enabled);
|
|
|
|
}
|
|
|
|
|
2018-08-08 07:57:32 +03:00
|
|
|
public function testIsTwoFactorAuthenticatedNoProviders() {
|
2018-09-29 19:57:00 +03:00
|
|
|
$this->mandatoryTwoFactor->expects($this->once())
|
2018-10-11 13:20:18 +03:00
|
|
|
->method('isEnforcedFor')
|
|
|
|
->with($this->user)
|
2018-09-29 19:57:00 +03:00
|
|
|
->willReturn(false);
|
2018-08-08 07:57:32 +03:00
|
|
|
$this->providerRegistry->expects($this->once())
|
|
|
|
->method('getProviderStates')
|
|
|
|
->willReturn([]); // No providers registered
|
|
|
|
$this->providerLoader->expects($this->once())
|
|
|
|
->method('getProviders')
|
|
|
|
->willReturn([]); // No providers loadable
|
|
|
|
|
|
|
|
$this->assertFalse($this->manager->isTwoFactorAuthenticated($this->user));
|
2018-08-10 10:26:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsTwoFactorAuthenticatedOnlyBackupCodes() {
|
2018-09-29 19:57:00 +03:00
|
|
|
$this->mandatoryTwoFactor->expects($this->once())
|
2018-10-11 13:20:18 +03:00
|
|
|
->method('isEnforcedFor')
|
|
|
|
->with($this->user)
|
2018-09-29 19:57:00 +03:00
|
|
|
->willReturn(false);
|
2018-08-10 10:26:40 +03:00
|
|
|
$this->providerRegistry->expects($this->once())
|
|
|
|
->method('getProviderStates')
|
|
|
|
->willReturn([
|
|
|
|
'backup_codes' => true,
|
|
|
|
]);
|
|
|
|
$backupCodesProvider = $this->createMock(IProvider::class);
|
|
|
|
$backupCodesProvider
|
|
|
|
->method('getId')
|
|
|
|
->willReturn('backup_codes');
|
|
|
|
$this->providerLoader->expects($this->once())
|
|
|
|
->method('getProviders')
|
|
|
|
->willReturn([
|
|
|
|
$backupCodesProvider,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertFalse($this->manager->isTwoFactorAuthenticated($this->user));
|
2018-08-08 07:57:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsTwoFactorAuthenticatedFailingProviders() {
|
2018-09-29 19:57:00 +03:00
|
|
|
$this->mandatoryTwoFactor->expects($this->once())
|
2018-10-11 13:20:18 +03:00
|
|
|
->method('isEnforcedFor')
|
|
|
|
->with($this->user)
|
2018-09-29 19:57:00 +03:00
|
|
|
->willReturn(false);
|
2018-05-22 09:52:16 +03:00
|
|
|
$this->providerRegistry->expects($this->once())
|
|
|
|
->method('getProviderStates')
|
|
|
|
->willReturn([
|
|
|
|
'twofactor_totp' => true,
|
|
|
|
'twofactor_u2f' => false,
|
2018-08-08 07:57:32 +03:00
|
|
|
]); // Two providers registered, but …
|
|
|
|
$this->providerLoader->expects($this->once())
|
|
|
|
->method('getProviders')
|
|
|
|
->willReturn([]); // … none of them is able to load, however …
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2018-08-08 07:57:32 +03:00
|
|
|
// … 2FA is still enforced
|
2016-06-13 13:46:45 +03:00
|
|
|
$this->assertTrue($this->manager->isTwoFactorAuthenticated($this->user));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
2018-08-08 07:57:32 +03:00
|
|
|
public function providerStatesFixData(): array {
|
|
|
|
return [
|
|
|
|
[false, false],
|
|
|
|
[true, true],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the 2FA registry has not been populated when a user logs in,
|
|
|
|
* the 2FA manager has to first fix the state before it checks for
|
|
|
|
* enabled providers.
|
|
|
|
*
|
|
|
|
* If any of these providers is active, 2FA is enabled
|
|
|
|
*
|
|
|
|
* @dataProvider providerStatesFixData
|
|
|
|
*/
|
|
|
|
public function testIsTwoFactorAuthenticatedFixesProviderStates(bool $providerEnabled, bool $expected) {
|
|
|
|
$this->providerRegistry->expects($this->once())
|
|
|
|
->method('getProviderStates')
|
|
|
|
->willReturn([]); // Nothing registered yet
|
|
|
|
$this->providerLoader->expects($this->once())
|
|
|
|
->method('getProviders')
|
|
|
|
->willReturn([
|
|
|
|
$this->fakeProvider
|
|
|
|
]);
|
|
|
|
$this->fakeProvider->expects($this->once())
|
|
|
|
->method('isTwoFactorAuthEnabledForUser')
|
|
|
|
->with($this->user)
|
|
|
|
->willReturn($providerEnabled);
|
|
|
|
if ($providerEnabled) {
|
|
|
|
$this->providerRegistry->expects($this->once())
|
|
|
|
->method('enableProviderFor')
|
|
|
|
->with(
|
|
|
|
$this->fakeProvider,
|
|
|
|
$this->user
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$this->providerRegistry->expects($this->once())
|
|
|
|
->method('disableProviderFor')
|
|
|
|
->with(
|
|
|
|
$this->fakeProvider,
|
|
|
|
$this->user
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $this->manager->isTwoFactorAuthenticated($this->user));
|
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
public function testGetProvider() {
|
2018-05-22 09:52:16 +03:00
|
|
|
$this->providerRegistry->expects($this->once())
|
|
|
|
->method('getProviderStates')
|
|
|
|
->with($this->user)
|
|
|
|
->willReturn([
|
|
|
|
$this->fakeProvider->getId() => true,
|
|
|
|
]);
|
|
|
|
$this->providerLoader->expects($this->once())
|
|
|
|
->method('getProviders')
|
|
|
|
->with($this->user)
|
|
|
|
->willReturn([$this->fakeProvider]);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2018-05-22 09:52:16 +03:00
|
|
|
$provider = $this->manager->getProvider($this->user, $this->fakeProvider->getId());
|
2016-08-29 20:19:44 +03:00
|
|
|
|
2018-05-22 09:52:16 +03:00
|
|
|
$this->assertSame($this->fakeProvider, $provider);
|
2016-08-29 20:19:44 +03:00
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
public function testGetInvalidProvider() {
|
2018-05-22 09:52:16 +03:00
|
|
|
$this->providerRegistry->expects($this->once())
|
|
|
|
->method('getProviderStates')
|
|
|
|
->with($this->user)
|
|
|
|
->willReturn([]);
|
|
|
|
$this->providerLoader->expects($this->once())
|
|
|
|
->method('getProviders')
|
|
|
|
->with($this->user)
|
|
|
|
->willReturn([]);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2018-05-22 09:52:16 +03:00
|
|
|
$provider = $this->manager->getProvider($this->user, 'nonexistent');
|
|
|
|
|
|
|
|
$this->assertNull($provider);
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
2019-04-05 19:21:08 +03:00
|
|
|
public function testGetLoginSetupProviders() {
|
|
|
|
$provider1 = $this->createMock(IProvider::class);
|
|
|
|
$provider2 = $this->createMock(IActivatableAtLogin::class);
|
|
|
|
$this->providerLoader->expects($this->once())
|
|
|
|
->method('getProviders')
|
|
|
|
->with($this->user)
|
|
|
|
->willReturn([
|
|
|
|
$provider1,
|
|
|
|
$provider2,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$providers = $this->manager->getLoginSetupProviders($this->user);
|
|
|
|
|
|
|
|
$this->assertCount(1, $providers);
|
|
|
|
$this->assertSame($provider2, reset($providers));
|
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
public function testGetProviders() {
|
2018-05-22 09:52:16 +03:00
|
|
|
$this->providerRegistry->expects($this->once())
|
|
|
|
->method('getProviderStates')
|
|
|
|
->with($this->user)
|
|
|
|
->willReturn([
|
|
|
|
$this->fakeProvider->getId() => true,
|
|
|
|
]);
|
|
|
|
$this->providerLoader->expects($this->once())
|
|
|
|
->method('getProviders')
|
|
|
|
->with($this->user)
|
|
|
|
->willReturn([$this->fakeProvider]);
|
2016-05-11 12:23:25 +03:00
|
|
|
$expectedProviders = [
|
|
|
|
'email' => $this->fakeProvider,
|
|
|
|
];
|
|
|
|
|
2018-05-22 09:52:16 +03:00
|
|
|
$providerSet = $this->manager->getProviderSet($this->user);
|
|
|
|
$providers = $providerSet->getProviders();
|
|
|
|
|
|
|
|
$this->assertEquals($expectedProviders, $providers);
|
|
|
|
$this->assertFalse($providerSet->isProviderMissing());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetProvidersOneMissing() {
|
|
|
|
$this->providerRegistry->expects($this->once())
|
|
|
|
->method('getProviderStates')
|
|
|
|
->with($this->user)
|
|
|
|
->willReturn([
|
|
|
|
$this->fakeProvider->getId() => true,
|
|
|
|
]);
|
|
|
|
$this->providerLoader->expects($this->once())
|
|
|
|
->method('getProviders')
|
|
|
|
->with($this->user)
|
|
|
|
->willReturn([]);
|
|
|
|
$expectedProviders = [
|
|
|
|
'email' => $this->fakeProvider,
|
|
|
|
];
|
|
|
|
|
|
|
|
$providerSet = $this->manager->getProviderSet($this->user);
|
|
|
|
|
|
|
|
$this->assertTrue($providerSet->isProviderMissing());
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testVerifyChallenge() {
|
|
|
|
$this->prepareProviders();
|
|
|
|
|
|
|
|
$challenge = 'passme';
|
2016-12-20 17:23:36 +03:00
|
|
|
$event = $this->createMock(IEvent::class);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->fakeProvider->expects($this->once())
|
|
|
|
->method('verifyChallenge')
|
|
|
|
->with($this->user, $challenge)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->session->expects($this->once())
|
2016-09-06 22:41:15 +03:00
|
|
|
->method('get')
|
|
|
|
->with('two_factor_remember_login')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->session->expects($this->at(1))
|
2016-05-11 12:23:25 +03:00
|
|
|
->method('remove')
|
|
|
|
->with('two_factor_auth_uid');
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->session->expects($this->at(2))
|
|
|
|
->method('remove')
|
|
|
|
->with('two_factor_remember_login');
|
2017-08-10 13:46:33 +03:00
|
|
|
$this->session->expects($this->at(3))
|
|
|
|
->method('set')
|
|
|
|
->with(Manager::SESSION_UID_DONE, 'jos');
|
|
|
|
$this->session->method('getId')
|
|
|
|
->willReturn('mysessionid');
|
2016-12-20 17:23:36 +03:00
|
|
|
$this->activityManager->expects($this->once())
|
|
|
|
->method('generateEvent')
|
|
|
|
->willReturn($event);
|
|
|
|
$this->user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('jos');
|
|
|
|
$event->expects($this->once())
|
|
|
|
->method('setApp')
|
2017-05-18 23:10:51 +03:00
|
|
|
->with($this->equalTo('core'))
|
2016-12-20 17:23:36 +03:00
|
|
|
->willReturnSelf();
|
|
|
|
$event->expects($this->once())
|
|
|
|
->method('setType')
|
2017-05-18 23:10:51 +03:00
|
|
|
->with($this->equalTo('security'))
|
2016-12-20 17:23:36 +03:00
|
|
|
->willReturnSelf();
|
|
|
|
$event->expects($this->once())
|
|
|
|
->method('setAuthor')
|
|
|
|
->with($this->equalTo('jos'))
|
|
|
|
->willReturnSelf();
|
|
|
|
$event->expects($this->once())
|
|
|
|
->method('setAffectedUser')
|
|
|
|
->with($this->equalTo('jos'))
|
|
|
|
->willReturnSelf();
|
2018-01-24 10:48:28 +03:00
|
|
|
$this->fakeProvider
|
2016-12-20 17:23:36 +03:00
|
|
|
->method('getDisplayName')
|
|
|
|
->willReturn('Fake 2FA');
|
|
|
|
$event->expects($this->once())
|
|
|
|
->method('setSubject')
|
|
|
|
->with($this->equalTo('twofactor_success'), $this->equalTo([
|
|
|
|
'provider' => 'Fake 2FA',
|
|
|
|
]))
|
|
|
|
->willReturnSelf();
|
2017-08-10 13:46:33 +03:00
|
|
|
$token = $this->createMock(OC\Authentication\Token\IToken::class);
|
|
|
|
$this->tokenProvider->method('getToken')
|
|
|
|
->with('mysessionid')
|
|
|
|
->willReturn($token);
|
|
|
|
$token->method('getId')
|
|
|
|
->willReturn(42);
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('deleteUserValue')
|
2021-01-22 16:09:37 +03:00
|
|
|
->with('jos', 'login_token_2fa', '42');
|
2017-08-10 13:46:33 +03:00
|
|
|
|
2018-05-22 09:52:16 +03:00
|
|
|
$result = $this->manager->verifyChallenge('email', $this->user, $challenge);
|
|
|
|
|
|
|
|
$this->assertTrue($result);
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testVerifyChallengeInvalidProviderId() {
|
|
|
|
$this->prepareProviders();
|
|
|
|
|
|
|
|
$challenge = 'passme';
|
|
|
|
$this->fakeProvider->expects($this->never())
|
|
|
|
->method('verifyChallenge')
|
|
|
|
->with($this->user, $challenge);
|
|
|
|
$this->session->expects($this->never())
|
|
|
|
->method('remove');
|
|
|
|
|
2016-08-24 11:42:07 +03:00
|
|
|
$this->assertFalse($this->manager->verifyChallenge('dontexist', $this->user, $challenge));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testVerifyInvalidChallenge() {
|
|
|
|
$this->prepareProviders();
|
|
|
|
|
|
|
|
$challenge = 'dontpassme';
|
2016-12-20 17:23:36 +03:00
|
|
|
$event = $this->createMock(IEvent::class);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->fakeProvider->expects($this->once())
|
|
|
|
->method('verifyChallenge')
|
|
|
|
->with($this->user, $challenge)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->session->expects($this->never())
|
|
|
|
->method('remove');
|
2016-12-20 17:23:36 +03:00
|
|
|
$this->activityManager->expects($this->once())
|
|
|
|
->method('generateEvent')
|
|
|
|
->willReturn($event);
|
|
|
|
$this->user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('jos');
|
|
|
|
$event->expects($this->once())
|
|
|
|
->method('setApp')
|
2017-05-18 23:10:51 +03:00
|
|
|
->with($this->equalTo('core'))
|
2016-12-20 17:23:36 +03:00
|
|
|
->willReturnSelf();
|
|
|
|
$event->expects($this->once())
|
|
|
|
->method('setType')
|
2017-05-18 23:10:51 +03:00
|
|
|
->with($this->equalTo('security'))
|
2016-12-20 17:23:36 +03:00
|
|
|
->willReturnSelf();
|
|
|
|
$event->expects($this->once())
|
|
|
|
->method('setAuthor')
|
|
|
|
->with($this->equalTo('jos'))
|
|
|
|
->willReturnSelf();
|
|
|
|
$event->expects($this->once())
|
|
|
|
->method('setAffectedUser')
|
|
|
|
->with($this->equalTo('jos'))
|
|
|
|
->willReturnSelf();
|
2018-01-24 10:48:28 +03:00
|
|
|
$this->fakeProvider
|
2016-12-20 17:23:36 +03:00
|
|
|
->method('getDisplayName')
|
|
|
|
->willReturn('Fake 2FA');
|
|
|
|
$event->expects($this->once())
|
|
|
|
->method('setSubject')
|
|
|
|
->with($this->equalTo('twofactor_failed'), $this->equalTo([
|
|
|
|
'provider' => 'Fake 2FA',
|
|
|
|
]))
|
|
|
|
->willReturnSelf();
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2016-08-24 11:42:07 +03:00
|
|
|
$this->assertFalse($this->manager->verifyChallenge('email', $this->user, $challenge));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNeedsSecondFactor() {
|
2016-09-02 12:07:26 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
2017-08-10 13:46:33 +03:00
|
|
|
$this->session->expects($this->at(0))
|
2017-09-12 23:28:43 +03:00
|
|
|
->method('exists')
|
|
|
|
->with('app_password')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->session->expects($this->at(1))
|
2016-05-11 12:23:25 +03:00
|
|
|
->method('exists')
|
|
|
|
->with('two_factor_auth_uid')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2017-09-12 23:28:43 +03:00
|
|
|
$this->session->expects($this->at(2))
|
2017-08-10 13:46:33 +03:00
|
|
|
->method('exists')
|
|
|
|
->with(Manager::SESSION_UID_DONE)
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->session->method('getId')
|
|
|
|
->willReturn('mysessionid');
|
|
|
|
$token = $this->createMock(OC\Authentication\Token\IToken::class);
|
|
|
|
$this->tokenProvider->method('getToken')
|
|
|
|
->with('mysessionid')
|
|
|
|
->willReturn($token);
|
|
|
|
$token->method('getId')
|
|
|
|
->willReturn(42);
|
|
|
|
|
|
|
|
$user->method('getUID')
|
|
|
|
->willReturn('user');
|
|
|
|
$this->config->method('getUserKeys')
|
|
|
|
->with('user', 'login_token_2fa')
|
|
|
|
->willReturn([
|
2021-01-22 16:09:37 +03:00
|
|
|
'42'
|
2017-08-10 13:46:33 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$manager = $this->getMockBuilder(Manager::class)
|
|
|
|
->setConstructorArgs([
|
2018-05-22 09:52:16 +03:00
|
|
|
$this->providerLoader,
|
|
|
|
$this->providerRegistry,
|
2018-09-29 19:57:00 +03:00
|
|
|
$this->mandatoryTwoFactor,
|
2017-08-10 13:46:33 +03:00
|
|
|
$this->session,
|
|
|
|
$this->config,
|
|
|
|
$this->activityManager,
|
|
|
|
$this->logger,
|
|
|
|
$this->tokenProvider,
|
2018-01-24 10:48:28 +03:00
|
|
|
$this->timeFactory,
|
2021-03-05 18:02:17 +03:00
|
|
|
$this->newDispatcher,
|
2018-01-24 10:48:28 +03:00
|
|
|
$this->eventDispatcher
|
2017-08-10 13:46:33 +03:00
|
|
|
])
|
2018-05-22 09:52:16 +03:00
|
|
|
->setMethods(['loadTwoFactorApp', 'isTwoFactorAuthenticated'])// Do not actually load the apps
|
2017-08-10 13:46:33 +03:00
|
|
|
->getMock();
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2017-08-10 13:46:33 +03:00
|
|
|
$manager->method('isTwoFactorAuthenticated')
|
|
|
|
->with($user)
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->assertTrue($manager->needsSecondFactor($user));
|
2016-08-24 11:42:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNeedsSecondFactorUserIsNull() {
|
|
|
|
$user = null;
|
|
|
|
$this->session->expects($this->never())
|
|
|
|
->method('exists');
|
|
|
|
|
|
|
|
$this->assertFalse($this->manager->needsSecondFactor($user));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNeedsSecondFactorWithNoProviderAvailableAnymore() {
|
|
|
|
$this->prepareNoProviders();
|
|
|
|
|
|
|
|
$user = null;
|
|
|
|
$this->session->expects($this->never())
|
|
|
|
->method('exists')
|
|
|
|
->with('two_factor_auth_uid')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2016-08-24 11:42:07 +03:00
|
|
|
$this->session->expects($this->never())
|
|
|
|
->method('remove')
|
|
|
|
->with('two_factor_auth_uid');
|
|
|
|
|
|
|
|
$this->assertFalse($this->manager->needsSecondFactor($user));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPrepareTwoFactorLogin() {
|
2017-08-10 13:46:33 +03:00
|
|
|
$this->user->method('getUID')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('ferdinand');
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->session->expects($this->at(0))
|
|
|
|
->method('set')
|
|
|
|
->with('two_factor_auth_uid', 'ferdinand');
|
|
|
|
$this->session->expects($this->at(1))
|
|
|
|
->method('set')
|
|
|
|
->with('two_factor_remember_login', true);
|
|
|
|
|
2017-08-10 13:46:33 +03:00
|
|
|
$this->session->method('getId')
|
|
|
|
->willReturn('mysessionid');
|
|
|
|
$token = $this->createMock(OC\Authentication\Token\IToken::class);
|
|
|
|
$this->tokenProvider->method('getToken')
|
|
|
|
->with('mysessionid')
|
|
|
|
->willReturn($token);
|
|
|
|
$token->method('getId')
|
|
|
|
->willReturn(42);
|
|
|
|
|
|
|
|
$this->timeFactory->method('getTime')
|
|
|
|
->willReturn(1337);
|
|
|
|
|
|
|
|
$this->config->method('setUserValue')
|
2021-01-22 16:09:37 +03:00
|
|
|
->with('ferdinand', 'login_token_2fa', '42', '1337');
|
2017-08-10 13:46:33 +03:00
|
|
|
|
|
|
|
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->manager->prepareTwoFactorLogin($this->user, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPrepareTwoFactorLoginDontRemember() {
|
2017-08-10 13:46:33 +03:00
|
|
|
$this->user->method('getUID')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('ferdinand');
|
2016-09-06 22:41:15 +03:00
|
|
|
|
|
|
|
$this->session->expects($this->at(0))
|
2016-05-11 12:23:25 +03:00
|
|
|
->method('set')
|
|
|
|
->with('two_factor_auth_uid', 'ferdinand');
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->session->expects($this->at(1))
|
|
|
|
->method('set')
|
|
|
|
->with('two_factor_remember_login', false);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2017-08-10 13:46:33 +03:00
|
|
|
$this->session->method('getId')
|
|
|
|
->willReturn('mysessionid');
|
|
|
|
$token = $this->createMock(OC\Authentication\Token\IToken::class);
|
|
|
|
$this->tokenProvider->method('getToken')
|
|
|
|
->with('mysessionid')
|
|
|
|
->willReturn($token);
|
|
|
|
$token->method('getId')
|
|
|
|
->willReturn(42);
|
|
|
|
|
|
|
|
$this->timeFactory->method('getTime')
|
|
|
|
->willReturn(1337);
|
|
|
|
|
|
|
|
$this->config->method('setUserValue')
|
2021-01-22 16:09:37 +03:00
|
|
|
->with('ferdinand', 'login_token_2fa', '42', '1337');
|
2017-08-10 13:46:33 +03:00
|
|
|
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->manager->prepareTwoFactorLogin($this->user, false);
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
2017-08-10 13:46:33 +03:00
|
|
|
public function testNeedsSecondFactorSessionAuth() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$user->method('getUID')
|
|
|
|
->willReturn('user');
|
|
|
|
|
|
|
|
$this->session->method('exists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function ($var) {
|
2017-08-10 13:46:33 +03:00
|
|
|
if ($var === Manager::SESSION_UID_KEY) {
|
|
|
|
return false;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($var === 'app_password') {
|
2017-09-12 23:28:43 +03:00
|
|
|
return false;
|
2017-08-10 13:46:33 +03:00
|
|
|
}
|
|
|
|
return true;
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2017-08-10 13:46:33 +03:00
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with(Manager::SESSION_UID_DONE)
|
|
|
|
->willReturn('user');
|
|
|
|
|
|
|
|
$this->assertFalse($this->manager->needsSecondFactor($user));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNeedsSecondFactorSessionAuthFailDBPass() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$user->method('getUID')
|
|
|
|
->willReturn('user');
|
|
|
|
|
|
|
|
$this->session->method('exists')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->session->method('getId')
|
|
|
|
->willReturn('mysessionid');
|
|
|
|
|
|
|
|
$token = $this->createMock(OC\Authentication\Token\IToken::class);
|
|
|
|
$token->method('getId')
|
|
|
|
->willReturn(40);
|
|
|
|
|
|
|
|
$this->tokenProvider->method('getToken')
|
|
|
|
->with('mysessionid')
|
|
|
|
->willReturn($token);
|
|
|
|
|
|
|
|
$this->config->method('getUserKeys')
|
|
|
|
->with('user', 'login_token_2fa')
|
|
|
|
->willReturn([
|
2021-01-22 16:09:37 +03:00
|
|
|
'42', '43', '44'
|
2017-08-10 13:46:33 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('set')
|
|
|
|
->with(Manager::SESSION_UID_DONE, 'user');
|
|
|
|
|
|
|
|
$this->assertFalse($this->manager->needsSecondFactor($user));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNeedsSecondFactorInvalidToken() {
|
2017-09-05 13:14:28 +03:00
|
|
|
$this->prepareNoProviders();
|
|
|
|
|
2017-08-10 13:46:33 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$user->method('getUID')
|
|
|
|
->willReturn('user');
|
|
|
|
|
|
|
|
$this->session->method('exists')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->session->method('getId')
|
|
|
|
->willReturn('mysessionid');
|
|
|
|
|
|
|
|
$this->tokenProvider->method('getToken')
|
|
|
|
->with('mysessionid')
|
|
|
|
->willThrowException(new OC\Authentication\Exceptions\InvalidTokenException());
|
|
|
|
|
2017-09-05 13:14:28 +03:00
|
|
|
$this->config->method('getUserKeys')->willReturn([]);
|
|
|
|
|
|
|
|
$this->assertFalse($this->manager->needsSecondFactor($user));
|
2017-08-10 13:46:33 +03:00
|
|
|
}
|
2017-09-12 23:28:43 +03:00
|
|
|
|
|
|
|
public function testNeedsSecondFactorAppPassword() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$this->session->method('exists')
|
|
|
|
->with('app_password')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->assertFalse($this->manager->needsSecondFactor($user));
|
|
|
|
}
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|