2016-04-29 17:31:27 +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\Token;
|
|
|
|
|
|
|
|
use OC\Authentication\Token\DefaultToken;
|
|
|
|
use OC\Authentication\Token\DefaultTokenProvider;
|
|
|
|
use OC\Authentication\Token\IToken;
|
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class DefaultTokenProviderTest extends TestCase {
|
|
|
|
|
|
|
|
/** @var DefaultTokenProvider */
|
|
|
|
private $tokenProvider;
|
|
|
|
private $mapper;
|
|
|
|
private $crypto;
|
|
|
|
private $config;
|
|
|
|
private $logger;
|
2016-05-06 17:31:40 +03:00
|
|
|
private $timeFactory;
|
|
|
|
private $time;
|
2016-04-29 17:31:27 +03:00
|
|
|
|
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2016-05-06 17:31:40 +03:00
|
|
|
$this->mapper = $this->getMockBuilder('\OC\Authentication\Token\DefaultTokenMapper')
|
2016-05-02 11:58:44 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-04-29 17:31:27 +03:00
|
|
|
$this->crypto = $this->getMock('\OCP\Security\ICrypto');
|
|
|
|
$this->config = $this->getMock('\OCP\IConfig');
|
|
|
|
$this->logger = $this->getMock('\OCP\ILogger');
|
2016-05-06 17:31:40 +03:00
|
|
|
$this->timeFactory = $this->getMock('\OCP\AppFramework\Utility\ITimeFactory');
|
|
|
|
$this->time = 1313131;
|
|
|
|
$this->timeFactory->expects($this->any())
|
|
|
|
->method('getTime')
|
|
|
|
->will($this->returnValue($this->time));
|
2016-04-29 17:31:27 +03:00
|
|
|
|
2016-05-08 20:31:42 +03:00
|
|
|
$this->tokenProvider = new DefaultTokenProvider($this->mapper, $this->crypto, $this->config, $this->logger,
|
|
|
|
$this->timeFactory);
|
2016-04-29 17:31:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGenerateToken() {
|
|
|
|
$token = 'token';
|
|
|
|
$uid = 'user';
|
2016-05-24 11:50:18 +03:00
|
|
|
$user = 'User';
|
2016-04-29 17:31:27 +03:00
|
|
|
$password = 'passme';
|
2016-05-12 12:19:17 +03:00
|
|
|
$name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
|
|
|
|
. 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
|
|
|
|
. 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
|
|
|
|
. 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
|
2016-04-29 17:31:27 +03:00
|
|
|
$type = IToken::PERMANENT_TOKEN;
|
|
|
|
|
|
|
|
$toInsert = new DefaultToken();
|
|
|
|
$toInsert->setUid($uid);
|
2016-05-24 11:50:18 +03:00
|
|
|
$toInsert->setLoginName($user);
|
2016-04-29 17:31:27 +03:00
|
|
|
$toInsert->setPassword('encryptedpassword');
|
|
|
|
$toInsert->setName($name);
|
2016-05-06 17:31:40 +03:00
|
|
|
$toInsert->setToken(hash('sha512', $token . '1f4h9s'));
|
2016-04-29 17:31:27 +03:00
|
|
|
$toInsert->setType($type);
|
2016-05-06 17:31:40 +03:00
|
|
|
$toInsert->setLastActivity($this->time);
|
2016-04-29 17:31:27 +03:00
|
|
|
|
2016-05-06 17:31:40 +03:00
|
|
|
$this->config->expects($this->any())
|
2016-04-29 17:31:27 +03:00
|
|
|
->method('getSystemValue')
|
|
|
|
->with('secret')
|
|
|
|
->will($this->returnValue('1f4h9s'));
|
|
|
|
$this->crypto->expects($this->once())
|
|
|
|
->method('encrypt')
|
|
|
|
->with($password, $token . '1f4h9s')
|
|
|
|
->will($this->returnValue('encryptedpassword'));
|
|
|
|
$this->mapper->expects($this->once())
|
|
|
|
->method('insert')
|
|
|
|
->with($this->equalTo($toInsert));
|
|
|
|
|
2016-05-24 11:50:18 +03:00
|
|
|
$actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type);
|
2016-04-29 17:31:27 +03:00
|
|
|
|
|
|
|
$this->assertEquals($toInsert, $actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateToken() {
|
2016-05-06 17:31:40 +03:00
|
|
|
$tk = new DefaultToken();
|
2016-04-29 17:31:27 +03:00
|
|
|
$this->mapper->expects($this->once())
|
|
|
|
->method('update')
|
|
|
|
->with($tk);
|
|
|
|
|
|
|
|
$this->tokenProvider->updateToken($tk);
|
2016-05-06 17:31:40 +03:00
|
|
|
|
|
|
|
$this->assertEquals($this->time, $tk->getLastActivity());
|
2016-04-29 17:31:27 +03:00
|
|
|
}
|
2016-05-18 12:33:56 +03:00
|
|
|
|
|
|
|
public function testGetTokenByUser() {
|
|
|
|
$user = $this->getMock('\OCP\IUser');
|
|
|
|
$this->mapper->expects($this->once())
|
|
|
|
->method('getTokenByUser')
|
|
|
|
->with($user)
|
|
|
|
->will($this->returnValue(['token']));
|
|
|
|
|
|
|
|
$this->assertEquals(['token'], $this->tokenProvider->getTokenByUser($user));
|
|
|
|
}
|
2016-04-29 17:31:27 +03:00
|
|
|
|
|
|
|
public function testGetPassword() {
|
|
|
|
$token = 'token1234';
|
2016-05-06 17:31:40 +03:00
|
|
|
$tk = new DefaultToken();
|
|
|
|
$tk->setPassword('someencryptedvalue');
|
2016-04-29 17:31:27 +03:00
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('secret')
|
|
|
|
->will($this->returnValue('1f4h9s'));
|
|
|
|
$this->crypto->expects($this->once())
|
|
|
|
->method('decrypt')
|
|
|
|
->with('someencryptedvalue', $token . '1f4h9s')
|
|
|
|
->will($this->returnValue('passme'));
|
|
|
|
|
|
|
|
$actual = $this->tokenProvider->getPassword($tk, $token);
|
|
|
|
|
|
|
|
$this->assertEquals('passme', $actual);
|
|
|
|
}
|
|
|
|
|
2016-05-31 11:48:14 +03:00
|
|
|
/**
|
|
|
|
* @expectedException \OC\Authentication\Exceptions\PasswordlessTokenException
|
|
|
|
*/
|
|
|
|
public function testGetPasswordPasswordLessToken() {
|
|
|
|
$token = 'token1234';
|
|
|
|
$tk = new DefaultToken();
|
|
|
|
$tk->setPassword(null);
|
|
|
|
|
|
|
|
$this->tokenProvider->getPassword($tk, $token);
|
|
|
|
}
|
|
|
|
|
2016-05-08 20:31:42 +03:00
|
|
|
/**
|
|
|
|
* @expectedException \OC\Authentication\Exceptions\InvalidTokenException
|
|
|
|
*/
|
|
|
|
public function testGetPasswordDeletesInvalidToken() {
|
|
|
|
$token = 'token1234';
|
|
|
|
$tk = new DefaultToken();
|
|
|
|
$tk->setPassword('someencryptedvalue');
|
|
|
|
/* @var $tokenProvider DefaultTokenProvider */
|
|
|
|
$tokenProvider = $this->getMockBuilder('\OC\Authentication\Token\DefaultTokenProvider')
|
|
|
|
->setMethods([
|
|
|
|
'invalidateToken'
|
|
|
|
])
|
|
|
|
->setConstructorArgs([$this->mapper, $this->crypto, $this->config, $this->logger,
|
|
|
|
$this->timeFactory])
|
|
|
|
->getMock();
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('secret')
|
|
|
|
->will($this->returnValue('1f4h9s'));
|
|
|
|
$this->crypto->expects($this->once())
|
|
|
|
->method('decrypt')
|
|
|
|
->with('someencryptedvalue', $token . '1f4h9s')
|
|
|
|
->will($this->throwException(new \Exception('some crypto error occurred')));
|
|
|
|
$tokenProvider->expects($this->once())
|
|
|
|
->method('invalidateToken')
|
|
|
|
->with($token);
|
|
|
|
|
|
|
|
$tokenProvider->getPassword($tk, $token);
|
|
|
|
}
|
|
|
|
|
2016-04-29 17:31:27 +03:00
|
|
|
public function testInvalidateToken() {
|
|
|
|
$this->mapper->expects($this->once())
|
|
|
|
->method('invalidate')
|
|
|
|
->with(hash('sha512', 'token7'));
|
|
|
|
|
|
|
|
$this->tokenProvider->invalidateToken('token7');
|
|
|
|
}
|
|
|
|
|
2016-05-19 12:20:22 +03:00
|
|
|
public function testInvaildateTokenById() {
|
|
|
|
$id = 123;
|
|
|
|
$user = $this->getMock('\OCP\IUser');
|
|
|
|
|
|
|
|
$this->mapper->expects($this->once())
|
|
|
|
->method('deleteById')
|
|
|
|
->with($user, $id);
|
|
|
|
|
|
|
|
$this->tokenProvider->invalidateTokenById($user, $id);
|
|
|
|
}
|
|
|
|
|
2016-04-29 17:31:27 +03:00
|
|
|
public function testInvalidateOldTokens() {
|
|
|
|
$defaultSessionLifetime = 60 * 60 * 24;
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('session_lifetime', $defaultSessionLifetime)
|
|
|
|
->will($this->returnValue(150));
|
|
|
|
$this->mapper->expects($this->once())
|
|
|
|
->method('invalidateOld')
|
2016-05-06 17:31:40 +03:00
|
|
|
->with($this->time - 150);
|
2016-04-29 17:31:27 +03:00
|
|
|
|
|
|
|
$this->tokenProvider->invalidateOldTokens();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateToken() {
|
|
|
|
$token = 'sometoken';
|
|
|
|
$dbToken = new DefaultToken();
|
|
|
|
$this->mapper->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->with(hash('sha512', $token))
|
|
|
|
->will($this->returnValue($dbToken));
|
|
|
|
|
|
|
|
$actual = $this->tokenProvider->validateToken($token);
|
|
|
|
|
|
|
|
$this->assertEquals($dbToken, $actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OC\Authentication\Exceptions\InvalidTokenException
|
|
|
|
*/
|
|
|
|
public function testValidateInvalidToken() {
|
|
|
|
$token = 'sometoken';
|
|
|
|
$this->mapper->expects($this->once())
|
|
|
|
->method('getToken')
|
|
|
|
->with(hash('sha512', $token))
|
|
|
|
->will($this->throwException(new DoesNotExistException('')));
|
|
|
|
|
|
|
|
$this->tokenProvider->validateToken($token);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|