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 Exception;
|
|
|
|
use OC;
|
|
|
|
use OC\App\AppManager;
|
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;
|
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;
|
2016-08-29 20:19:44 +03:00
|
|
|
use OCP\Authentication\TwoFactorAuth\IProvider;
|
|
|
|
use OCP\IConfig;
|
2016-12-20 17:23:36 +03:00
|
|
|
use OCP\ILogger;
|
2016-08-29 20:19:44 +03:00
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\IUser;
|
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 {
|
|
|
|
|
2017-08-10 13:46:33 +03:00
|
|
|
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $user;
|
|
|
|
|
2017-08-10 13:46:33 +03:00
|
|
|
/** @var AppManager|\PHPUnit_Framework_MockObject_MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $appManager;
|
|
|
|
|
2017-08-10 13:46:33 +03:00
|
|
|
/** @var ISession|\PHPUnit_Framework_MockObject_MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $session;
|
|
|
|
|
|
|
|
/** @var Manager */
|
|
|
|
private $manager;
|
|
|
|
|
2017-08-10 13:46:33 +03:00
|
|
|
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $config;
|
|
|
|
|
2017-08-10 13:46:33 +03:00
|
|
|
/** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
|
2016-12-20 17:23:36 +03:00
|
|
|
private $activityManager;
|
|
|
|
|
2017-08-10 13:46:33 +03:00
|
|
|
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
|
2016-12-20 17:23:36 +03:00
|
|
|
private $logger;
|
|
|
|
|
2017-08-10 13:46:33 +03:00
|
|
|
/** @var IProvider|\PHPUnit_Framework_MockObject_MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $fakeProvider;
|
|
|
|
|
2017-08-10 13:46:33 +03:00
|
|
|
/** @var IProvider|\PHPUnit_Framework_MockObject_MockObject */
|
2016-08-29 20:19:44 +03:00
|
|
|
private $backupProvider;
|
|
|
|
|
2017-08-10 13:46:33 +03:00
|
|
|
/** @var TokenProvider|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $tokenProvider;
|
|
|
|
|
|
|
|
/** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $timeFactory;
|
|
|
|
|
2018-01-24 10:48:28 +03:00
|
|
|
/** @var EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $eventDispatcher;
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2016-09-02 12:07:26 +03:00
|
|
|
$this->user = $this->createMock(IUser::class);
|
2017-03-22 12:33:30 +03:00
|
|
|
$this->appManager = $this->createMock(AppManager::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);
|
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
2017-08-10 13:46:33 +03:00
|
|
|
$this->tokenProvider = $this->createMock(TokenProvider::class);
|
|
|
|
$this->timeFactory = $this->createMock(ITimeFactory::class);
|
2018-01-24 10:48:28 +03:00
|
|
|
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
|
2017-08-10 13:46:33 +03:00
|
|
|
|
|
|
|
$this->manager = $this->getMockBuilder(Manager::class)
|
|
|
|
->setConstructorArgs([
|
|
|
|
$this->appManager,
|
|
|
|
$this->session,
|
|
|
|
$this->config,
|
|
|
|
$this->activityManager,
|
|
|
|
$this->logger,
|
|
|
|
$this->tokenProvider,
|
2018-01-24 10:48:28 +03:00
|
|
|
$this->timeFactory,
|
|
|
|
$this->eventDispatcher
|
2017-08-10 13:46:33 +03:00
|
|
|
])
|
2016-06-16 11:08:49 +03:00
|
|
|
->setMethods(['loadTwoFactorApp']) // Do not actually load the apps
|
|
|
|
->getMock();
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2016-09-02 12:07:26 +03:00
|
|
|
$this->fakeProvider = $this->createMock(IProvider::class);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->fakeProvider->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue('email'));
|
|
|
|
$this->fakeProvider->expects($this->any())
|
|
|
|
->method('isTwoFactorAuthEnabledForUser')
|
|
|
|
->will($this->returnValue(true));
|
2016-08-29 20:19:44 +03:00
|
|
|
OC::$server->registerService('\OCA\MyCustom2faApp\FakeProvider', function() {
|
2016-05-11 12:23:25 +03:00
|
|
|
return $this->fakeProvider;
|
|
|
|
});
|
2016-08-29 20:19:44 +03:00
|
|
|
|
|
|
|
$this->backupProvider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')->getMock();
|
|
|
|
$this->backupProvider->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue('backup_codes'));
|
|
|
|
$this->backupProvider->expects($this->any())
|
|
|
|
->method('isTwoFactorAuthEnabledForUser')
|
|
|
|
->will($this->returnValue(true));
|
2016-10-03 22:47:33 +03:00
|
|
|
OC::$server->registerService('\OCA\TwoFactorBackupCodes\Provider\FakeBackupCodesProvider', function () {
|
2016-08-29 20:19:44 +03:00
|
|
|
return $this->backupProvider;
|
|
|
|
});
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
2016-08-24 11:42:07 +03:00
|
|
|
private function prepareNoProviders() {
|
|
|
|
$this->appManager->expects($this->any())
|
|
|
|
->method('getEnabledAppsForUser')
|
|
|
|
->with($this->user)
|
|
|
|
->will($this->returnValue([]));
|
|
|
|
|
|
|
|
$this->appManager->expects($this->never())
|
|
|
|
->method('getAppInfo');
|
|
|
|
|
|
|
|
$this->manager->expects($this->never())
|
|
|
|
->method('loadTwoFactorApp');
|
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
private function prepareProviders() {
|
|
|
|
$this->appManager->expects($this->any())
|
|
|
|
->method('getEnabledAppsForUser')
|
|
|
|
->with($this->user)
|
|
|
|
->will($this->returnValue(['mycustom2faapp']));
|
|
|
|
|
|
|
|
$this->appManager->expects($this->once())
|
|
|
|
->method('getAppInfo')
|
|
|
|
->with('mycustom2faapp')
|
|
|
|
->will($this->returnValue([
|
|
|
|
'two-factor-providers' => [
|
|
|
|
'\OCA\MyCustom2faApp\FakeProvider',
|
|
|
|
],
|
|
|
|
]));
|
2016-06-16 12:07:44 +03:00
|
|
|
|
|
|
|
$this->manager->expects($this->once())
|
|
|
|
->method('loadTwoFactorApp')
|
|
|
|
->with('mycustom2faapp');
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
2016-08-29 20:19:44 +03:00
|
|
|
private function prepareProvidersWitBackupProvider() {
|
|
|
|
$this->appManager->expects($this->any())
|
|
|
|
->method('getEnabledAppsForUser')
|
|
|
|
->with($this->user)
|
|
|
|
->will($this->returnValue([
|
|
|
|
'mycustom2faapp',
|
|
|
|
'twofactor_backupcodes',
|
|
|
|
]));
|
|
|
|
|
|
|
|
$this->appManager->expects($this->exactly(2))
|
|
|
|
->method('getAppInfo')
|
|
|
|
->will($this->returnValueMap([
|
|
|
|
[
|
2018-01-30 18:17:51 +03:00
|
|
|
'mycustom2faapp', false, null,
|
2016-08-29 20:19:44 +03:00
|
|
|
['two-factor-providers' => [
|
|
|
|
'\OCA\MyCustom2faApp\FakeProvider',
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
[
|
2018-01-30 18:17:51 +03:00
|
|
|
'twofactor_backupcodes', false, null,
|
2016-08-29 20:19:44 +03:00
|
|
|
['two-factor-providers' => [
|
2016-10-03 22:47:33 +03:00
|
|
|
'\OCA\TwoFactorBackupCodes\Provider\FakeBackupCodesProvider',
|
2016-08-29 20:19:44 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
|
|
|
]));
|
|
|
|
|
|
|
|
$this->manager->expects($this->exactly(2))
|
|
|
|
->method('loadTwoFactorApp');
|
|
|
|
}
|
|
|
|
|
2016-06-13 13:46:45 +03:00
|
|
|
/**
|
2016-08-29 20:19:44 +03:00
|
|
|
* @expectedException Exception
|
2016-06-13 13:46:45 +03:00
|
|
|
* @expectedExceptionMessage Could not load two-factor auth provider \OCA\MyFaulty2faApp\DoesNotExist
|
|
|
|
*/
|
|
|
|
public function testFailHardIfProviderCanNotBeLoaded() {
|
|
|
|
$this->appManager->expects($this->once())
|
|
|
|
->method('getEnabledAppsForUser')
|
|
|
|
->with($this->user)
|
|
|
|
->will($this->returnValue(['faulty2faapp']));
|
2016-06-16 12:07:44 +03:00
|
|
|
$this->manager->expects($this->once())
|
|
|
|
->method('loadTwoFactorApp')
|
|
|
|
->with('faulty2faapp');
|
2016-06-13 13:46:45 +03:00
|
|
|
|
|
|
|
$this->appManager->expects($this->once())
|
|
|
|
->method('getAppInfo')
|
|
|
|
->with('faulty2faapp')
|
|
|
|
->will($this->returnValue([
|
|
|
|
'two-factor-providers' => [
|
|
|
|
'\OCA\MyFaulty2faApp\DoesNotExist',
|
|
|
|
],
|
|
|
|
]));
|
|
|
|
|
|
|
|
$this->manager->getProviders($this->user);
|
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
public function testIsTwoFactorAuthenticated() {
|
|
|
|
$this->prepareProviders();
|
|
|
|
|
2016-06-13 13:46:45 +03:00
|
|
|
$this->user->expects($this->once())
|
2016-05-11 12:23:25 +03:00
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('user123'));
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getUserValue')
|
|
|
|
->with('user123', 'core', 'two_factor_auth_disabled', 0)
|
|
|
|
->will($this->returnValue(0));
|
|
|
|
|
2016-06-13 13:46:45 +03:00
|
|
|
$this->assertTrue($this->manager->isTwoFactorAuthenticated($this->user));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetProvider() {
|
|
|
|
$this->prepareProviders();
|
|
|
|
|
|
|
|
$this->assertSame($this->fakeProvider, $this->manager->getProvider($this->user, 'email'));
|
|
|
|
}
|
|
|
|
|
2016-08-29 20:19:44 +03:00
|
|
|
public function testGetBackupProvider() {
|
|
|
|
$this->prepareProvidersWitBackupProvider();
|
|
|
|
|
|
|
|
$this->assertSame($this->backupProvider, $this->manager->getBackupProvider($this->user));
|
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
public function testGetInvalidProvider() {
|
|
|
|
$this->prepareProviders();
|
|
|
|
|
|
|
|
$this->assertSame(null, $this->manager->getProvider($this->user, 'nonexistent'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetProviders() {
|
|
|
|
$this->prepareProviders();
|
|
|
|
$expectedProviders = [
|
|
|
|
'email' => $this->fakeProvider,
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($expectedProviders, $this->manager->getProviders($this->user));
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
->will($this->returnValue(true));
|
2017-08-10 13:46:33 +03:00
|
|
|
|
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')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
$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);
|
2017-08-10 13:46:33 +03:00
|
|
|
|
2016-12-20 17:23:36 +03:00
|
|
|
$this->user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('jos');
|
2017-08-10 13:46:33 +03:00
|
|
|
|
2016-12-20 17:23:36 +03:00
|
|
|
$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();
|
2016-05-11 12:23:25 +03:00
|
|
|
|
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')
|
|
|
|
->with('jos', 'login_token_2fa', 42);
|
|
|
|
|
2016-08-24 11:42:07 +03:00
|
|
|
$this->assertTrue($this->manager->verifyChallenge('email', $this->user, $challenge));
|
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)
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
$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')
|
|
|
|
->will($this->returnValue(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([
|
|
|
|
42
|
|
|
|
]);
|
|
|
|
|
|
|
|
$manager = $this->getMockBuilder(Manager::class)
|
|
|
|
->setConstructorArgs([
|
|
|
|
$this->appManager,
|
|
|
|
$this->session,
|
|
|
|
$this->config,
|
|
|
|
$this->activityManager,
|
|
|
|
$this->logger,
|
|
|
|
$this->tokenProvider,
|
2018-01-24 10:48:28 +03:00
|
|
|
$this->timeFactory,
|
|
|
|
$this->eventDispatcher
|
2017-08-10 13:46:33 +03:00
|
|
|
])
|
|
|
|
->setMethods(['loadTwoFactorApp','isTwoFactorAuthenticated']) // Do not actually load the apps
|
|
|
|
->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')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$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')
|
2016-05-11 12:23:25 +03:00
|
|
|
->will($this->returnValue('ferdinand'));
|
|
|
|
|
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')
|
|
|
|
->with('ferdinand', 'login_token_2fa', 42, 1337);
|
|
|
|
|
|
|
|
|
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')
|
2016-09-06 22:41:15 +03:00
|
|
|
->will($this->returnValue('ferdinand'));
|
|
|
|
|
|
|
|
$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')
|
|
|
|
->with('ferdinand', 'login_token_2fa', 42, 1337);
|
|
|
|
|
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')
|
|
|
|
->will($this->returnCallback(function($var) {
|
|
|
|
if ($var === Manager::SESSION_UID_KEY) {
|
|
|
|
return false;
|
2017-09-12 23:28:43 +03:00
|
|
|
} else if ($var === 'app_password') {
|
|
|
|
return false;
|
2017-08-10 13:46:33 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}));
|
|
|
|
$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([
|
|
|
|
42, 43, 44
|
|
|
|
]);
|
|
|
|
|
|
|
|
$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
|
|
|
}
|