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/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-25 13:02:05 +03:00
|
|
|
namespace Test\Core\Controller;
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2016-05-25 13:02:05 +03:00
|
|
|
use OC\Core\Controller\TwoFactorChallengeController;
|
2016-05-11 12:23:25 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class TwoFactorChallengeControllerTest extends TestCase {
|
|
|
|
|
|
|
|
private $request;
|
|
|
|
private $twoFactorManager;
|
|
|
|
private $userSession;
|
|
|
|
private $session;
|
|
|
|
private $urlGenerator;
|
|
|
|
|
2016-06-07 18:53:00 +03:00
|
|
|
/** @var TwoFactorChallengeController|\PHPUnit_Framework_MockObject_MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $controller;
|
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2016-08-30 14:28:04 +03:00
|
|
|
$this->request = $this->getMockBuilder('\OCP\IRequest')->getMock();
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->twoFactorManager = $this->getMockBuilder('\OC\Authentication\TwoFactorAuth\Manager')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-08-30 14:28:04 +03:00
|
|
|
$this->userSession = $this->getMockBuilder('\OCP\IUserSession')->getMock();
|
|
|
|
$this->session = $this->getMockBuilder('\OCP\ISession')->getMock();
|
|
|
|
$this->urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')->getMock();
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2016-06-07 18:53:00 +03:00
|
|
|
$this->controller = $this->getMockBuilder('OC\Core\Controller\TwoFactorChallengeController')
|
|
|
|
->setConstructorArgs([
|
|
|
|
'core',
|
|
|
|
$this->request,
|
|
|
|
$this->twoFactorManager,
|
|
|
|
$this->userSession,
|
|
|
|
$this->session,
|
|
|
|
$this->urlGenerator,
|
|
|
|
])
|
|
|
|
->setMethods(['getLogoutAttribute'])
|
|
|
|
->getMock();
|
|
|
|
$this->controller->expects($this->any())
|
|
|
|
->method('getLogoutAttribute')
|
|
|
|
->willReturn('logoutAttribute');
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSelectChallenge() {
|
2016-08-30 14:28:04 +03:00
|
|
|
$user = $this->getMockBuilder('\OCP\IUser')->getMock();
|
2016-05-11 12:23:25 +03:00
|
|
|
$providers = [
|
|
|
|
'prov1',
|
|
|
|
'prov2',
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getProviders')
|
|
|
|
->with($user)
|
|
|
|
->will($this->returnValue($providers));
|
2016-08-29 20:19:44 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getBackupProvider')
|
|
|
|
->with($user)
|
|
|
|
->will($this->returnValue('backup'));
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
$expected = new \OCP\AppFramework\Http\TemplateResponse('core', 'twofactorselectchallenge', [
|
|
|
|
'providers' => $providers,
|
2016-08-29 20:19:44 +03:00
|
|
|
'backupProvider' => 'backup',
|
2016-06-01 14:54:08 +03:00
|
|
|
'redirect_url' => '/some/url',
|
2016-06-07 18:53:00 +03:00
|
|
|
'logout_attribute' => 'logoutAttribute',
|
2016-06-01 14:54:08 +03:00
|
|
|
], 'guest');
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2016-06-01 14:54:08 +03:00
|
|
|
$this->assertEquals($expected, $this->controller->selectChallenge('/some/url'));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testShowChallenge() {
|
2016-08-30 14:28:04 +03:00
|
|
|
$user = $this->getMockBuilder('\OCP\IUser')->getMock();
|
2016-05-11 12:23:25 +03:00
|
|
|
$provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-08-29 20:19:44 +03:00
|
|
|
$backupProvider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-05-11 12:23:25 +03:00
|
|
|
$tmpl = $this->getMockBuilder('\OCP\Template')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getProvider')
|
|
|
|
->with($user, 'myprovider')
|
|
|
|
->will($this->returnValue($provider));
|
2016-08-29 20:19:44 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getBackupProvider')
|
|
|
|
->with($user)
|
|
|
|
->will($this->returnValue($backupProvider));
|
|
|
|
$provider->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue('u2f'));
|
|
|
|
$backupProvider->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue('backup_codes'));
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('exists')
|
|
|
|
->with('two_factor_auth_error')
|
|
|
|
->will($this->returnValue(true));
|
2016-11-15 10:51:07 +03:00
|
|
|
$this->session->expects($this->exactly(2))
|
2016-05-11 12:23:25 +03:00
|
|
|
->method('remove')
|
2016-11-15 10:51:07 +03:00
|
|
|
->with($this->logicalOr(
|
|
|
|
$this->equalTo('two_factor_auth_error'),
|
|
|
|
$this->equalTo('two_factor_auth_error_message')));
|
2016-05-11 12:23:25 +03:00
|
|
|
$provider->expects($this->once())
|
|
|
|
->method('getTemplate')
|
|
|
|
->with($user)
|
|
|
|
->will($this->returnValue($tmpl));
|
|
|
|
$tmpl->expects($this->once())
|
|
|
|
->method('fetchPage')
|
|
|
|
->will($this->returnValue('<html/>'));
|
|
|
|
|
|
|
|
$expected = new \OCP\AppFramework\Http\TemplateResponse('core', 'twofactorshowchallenge', [
|
|
|
|
'error' => true,
|
|
|
|
'provider' => $provider,
|
2016-08-29 20:19:44 +03:00
|
|
|
'backupProvider' => $backupProvider,
|
2016-06-07 18:53:00 +03:00
|
|
|
'logout_attribute' => 'logoutAttribute',
|
2016-05-11 12:23:25 +03:00
|
|
|
'template' => '<html/>',
|
2016-12-23 22:53:26 +03:00
|
|
|
'redirect_url' => '/re/dir/ect/url',
|
2016-11-15 10:51:07 +03:00
|
|
|
'error_message' => null,
|
2016-05-11 12:23:25 +03:00
|
|
|
], 'guest');
|
|
|
|
|
2016-06-01 14:54:08 +03:00
|
|
|
$this->assertEquals($expected, $this->controller->showChallenge('myprovider', '/re/dir/ect/url'));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testShowInvalidChallenge() {
|
2016-08-30 14:28:04 +03:00
|
|
|
$user = $this->getMockBuilder('\OCP\IUser')->getMock();
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getProvider')
|
|
|
|
->with($user, 'myprovider')
|
|
|
|
->will($this->returnValue(null));
|
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('core.TwoFactorChallenge.selectChallenge')
|
|
|
|
->will($this->returnValue('select/challenge/url'));
|
|
|
|
|
|
|
|
$expected = new \OCP\AppFramework\Http\RedirectResponse('select/challenge/url');
|
|
|
|
|
2016-06-01 14:54:08 +03:00
|
|
|
$this->assertEquals($expected, $this->controller->showChallenge('myprovider', 'redirect/url'));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSolveChallenge() {
|
2016-08-30 14:28:04 +03:00
|
|
|
$user = $this->getMockBuilder('\OCP\IUser')->getMock();
|
2016-05-11 12:23:25 +03:00
|
|
|
$provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getProvider')
|
|
|
|
->with($user, 'myprovider')
|
|
|
|
->will($this->returnValue($provider));
|
|
|
|
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('verifyChallenge')
|
|
|
|
->with('myprovider', $user, 'token')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2016-08-30 14:28:04 +03:00
|
|
|
$expected = new \OCP\AppFramework\Http\RedirectResponse(\OC_Util::getDefaultPageUrl());
|
|
|
|
$this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token'));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSolveChallengeInvalidProvider() {
|
2016-08-30 14:28:04 +03:00
|
|
|
$user = $this->getMockBuilder('\OCP\IUser')->getMock();
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getProvider')
|
|
|
|
->with($user, 'myprovider')
|
|
|
|
->will($this->returnValue(null));
|
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('core.TwoFactorChallenge.selectChallenge')
|
|
|
|
->will($this->returnValue('select/challenge/url'));
|
|
|
|
|
|
|
|
$expected = new \OCP\AppFramework\Http\RedirectResponse('select/challenge/url');
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSolveInvalidChallenge() {
|
2016-08-30 14:28:04 +03:00
|
|
|
$user = $this->getMockBuilder('\OCP\IUser')->getMock();
|
2016-05-11 12:23:25 +03:00
|
|
|
$provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('getProvider')
|
|
|
|
->with($user, 'myprovider')
|
|
|
|
->will($this->returnValue($provider));
|
|
|
|
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('verifyChallenge')
|
|
|
|
->with('myprovider', $user, 'token')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
$this->session->expects($this->once())
|
|
|
|
->method('set')
|
|
|
|
->with('two_factor_auth_error', true);
|
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('core.TwoFactorChallenge.showChallenge', [
|
|
|
|
'challengeProviderId' => 'myprovider',
|
2016-06-01 14:54:08 +03:00
|
|
|
'redirect_url' => '/url',
|
2016-05-11 12:23:25 +03:00
|
|
|
])
|
|
|
|
->will($this->returnValue('files/index/url'));
|
|
|
|
$provider->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue('myprovider'));
|
|
|
|
|
|
|
|
$expected = new \OCP\AppFramework\Http\RedirectResponse('files/index/url');
|
2016-06-01 14:54:08 +03:00
|
|
|
$this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token', '/url'));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|