aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Authentication/Token
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/Authentication/Token')
-rw-r--r--tests/lib/Authentication/Token/DefaultTokenCleanupJobTest.php52
-rw-r--r--tests/lib/Authentication/Token/DefaultTokenMapperTest.php233
-rw-r--r--tests/lib/Authentication/Token/DefaultTokenProviderTest.php552
-rw-r--r--tests/lib/Authentication/Token/DefaultTokenTest.php41
-rw-r--r--tests/lib/Authentication/Token/ManagerTest.php395
-rw-r--r--tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php106
-rw-r--r--tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php461
-rw-r--r--tests/lib/Authentication/Token/PublicKeyTokenTest.php30
-rw-r--r--tests/lib/Authentication/Token/RemoteWipeTest.php27
9 files changed, 470 insertions, 1427 deletions
diff --git a/tests/lib/Authentication/Token/DefaultTokenCleanupJobTest.php b/tests/lib/Authentication/Token/DefaultTokenCleanupJobTest.php
deleted file mode 100644
index 0991c8b1fc8..00000000000
--- a/tests/lib/Authentication/Token/DefaultTokenCleanupJobTest.php
+++ /dev/null
@@ -1,52 +0,0 @@
-<?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\DefaultTokenCleanupJob;
-use OC\Authentication\Token\IProvider;
-use OC\Authentication\Token\Manager;
-use Test\TestCase;
-
-class DefaultTokenCleanupJobTest extends TestCase {
-
- /** @var DefaultTokenCleanupJob */
- private $job;
- private $tokenProvider;
-
- protected function setUp(): void {
- parent::setUp();
-
- $this->tokenProvider = $this->getMockBuilder(Manager::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->overwriteService(IProvider::class, $this->tokenProvider);
- $this->job = new DefaultTokenCleanupJob();
- }
-
- public function testRun() {
- $this->tokenProvider->expects($this->once())
- ->method('invalidateOldTokens')
- ->with();
- $this->invokePrivate($this->job, 'run', [null]);
- }
-}
diff --git a/tests/lib/Authentication/Token/DefaultTokenMapperTest.php b/tests/lib/Authentication/Token/DefaultTokenMapperTest.php
deleted file mode 100644
index da779be0807..00000000000
--- a/tests/lib/Authentication/Token/DefaultTokenMapperTest.php
+++ /dev/null
@@ -1,233 +0,0 @@
-<?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;
-use OC\Authentication\Token\DefaultToken;
-use OC\Authentication\Token\DefaultTokenMapper;
-use OC\Authentication\Token\IToken;
-use OCP\DB\QueryBuilder\IQueryBuilder;
-use OCP\IDBConnection;
-use OCP\IUser;
-use Test\TestCase;
-
-/**
- * Class DefaultTokenMapperTest
- *
- * @group DB
- * @package Test\Authentication
- */
-class DefaultTokenMapperTest extends TestCase {
-
- /** @var DefaultTokenMapper */
- private $mapper;
-
- /** @var IDBConnection */
- private $dbConnection;
- private $time;
-
- protected function setUp(): void {
- parent::setUp();
-
- $this->dbConnection = OC::$server->getDatabaseConnection();
- $this->time = time();
- $this->resetDatabase();
-
- $this->mapper = new DefaultTokenMapper($this->dbConnection);
- }
-
- private function resetDatabase() {
- $qb = $this->dbConnection->getQueryBuilder();
- $qb->delete('authtoken')->execute();
- $qb->insert('authtoken')->values([
- 'uid' => $qb->createNamedParameter('user1'),
- 'login_name' => $qb->createNamedParameter('User1'),
- 'password' => $qb->createNamedParameter('a75c7116460c082912d8f6860a850904|3nz5qbG1nNSLLi6V|c55365a0e54cfdfac4a175bcf11a7612aea74492277bba6e5d96a24497fa9272488787cb2f3ad34d8b9b8060934fce02f008d371df3ff3848f4aa61944851ff0'),
- 'name' => $qb->createNamedParameter('Firefox on Linux'),
- 'token' => $qb->createNamedParameter('9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206'),
- 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN),
- 'last_activity' => $qb->createNamedParameter($this->time - 120, IQueryBuilder::PARAM_INT), // Two minutes ago
- 'last_check' => $this->time - 60 * 10, // 10mins ago
- ])->execute();
- $qb->insert('authtoken')->values([
- 'uid' => $qb->createNamedParameter('user2'),
- 'login_name' => $qb->createNamedParameter('User2'),
- 'password' => $qb->createNamedParameter('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f'),
- 'name' => $qb->createNamedParameter('Firefox on Android'),
- 'token' => $qb->createNamedParameter('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b'),
- 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN),
- 'last_activity' => $qb->createNamedParameter($this->time - 60 * 60 * 24 * 3, IQueryBuilder::PARAM_INT), // Three days ago
- 'last_check' => $this->time - 10, // 10secs ago
- ])->execute();
- $qb->insert('authtoken')->values([
- 'uid' => $qb->createNamedParameter('user1'),
- 'login_name' => $qb->createNamedParameter('User1'),
- 'password' => $qb->createNamedParameter('063de945d6f6b26862d9b6f40652f2d5|DZ/z520tfdXPtd0T|395f6b89be8d9d605e409e20b9d9abe477fde1be38a3223f9e508f979bf906e50d9eaa4dca983ca4fb22a241eb696c3f98654e7775f78c4caf13108f98642b53'),
- 'name' => $qb->createNamedParameter('Iceweasel on Linux'),
- 'token' => $qb->createNamedParameter('47af8697ba590fb82579b5f1b3b6e8066773a62100abbe0db09a289a62f5d980dc300fa3d98b01d7228468d1ab05c1aa14c8d14bd5b6eee9cdf1ac14864680c3'),
- 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN),
- 'last_activity' => $qb->createNamedParameter($this->time - 120, IQueryBuilder::PARAM_INT), // Two minutes ago
- 'last_check' => $this->time - 60 * 10, // 10mins ago
- ])->execute();
- }
-
- private function getNumberOfTokens() {
- $qb = $this->dbConnection->getQueryBuilder();
- $result = $qb->select($qb->func()->count('*', 'count'))
- ->from('authtoken')
- ->execute()
- ->fetch();
- return (int) $result['count'];
- }
-
- public function testInvalidate() {
- $token = '9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206';
-
- $this->mapper->invalidate($token);
-
- $this->assertSame(2, $this->getNumberOfTokens());
- }
-
- public function testInvalidateInvalid() {
- $token = 'youwontfindthisoneinthedatabase';
-
- $this->mapper->invalidate($token);
-
- $this->assertSame(3, $this->getNumberOfTokens());
- }
-
- public function testInvalidateOld() {
- $olderThan = $this->time - 60 * 60; // One hour
-
- $this->mapper->invalidateOld($olderThan);
-
- $this->assertSame(2, $this->getNumberOfTokens());
- }
-
- public function testGetToken() {
- $token = new DefaultToken();
- $token->setUid('user2');
- $token->setLoginName('User2');
- $token->setPassword('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f');
- $token->setName('Firefox on Android');
- $token->setToken('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b');
- $token->setType(IToken::TEMPORARY_TOKEN);
- $token->setRemember(IToken::DO_NOT_REMEMBER);
- $token->setLastActivity($this->time - 60 * 60 * 24 * 3);
- $token->setLastCheck($this->time - 10);
- $token->setVersion(DefaultToken::VERSION);
-
- $dbToken = $this->mapper->getToken($token->getToken());
-
- $token->setId($dbToken->getId()); // We don't know the ID
- $token->resetUpdatedFields();
-
- $this->assertEquals($token, $dbToken);
- }
-
-
- public function testGetInvalidToken() {
- $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class);
-
- $token = 'thisisaninvalidtokenthatisnotinthedatabase';
-
- $this->mapper->getToken($token);
- }
-
- public function testGetTokenById() {
- $token = new DefaultToken();
- $token->setUid('user2');
- $token->setLoginName('User2');
- $token->setPassword('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f');
- $token->setName('Firefox on Android');
- $token->setToken('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b');
- $token->setType(IToken::TEMPORARY_TOKEN);
- $token->setRemember(IToken::DO_NOT_REMEMBER);
- $token->setLastActivity($this->time - 60 * 60 * 24 * 3);
- $token->setLastCheck($this->time - 10);
- $token->setVersion(DefaultToken::VERSION);
-
- $dbToken = $this->mapper->getToken($token->getToken());
- $token->setId($dbToken->getId()); // We don't know the ID
- $token->resetUpdatedFields();
-
- $dbToken = $this->mapper->getTokenById($token->getId());
- $this->assertEquals($token, $dbToken);
- }
-
-
- public function testGetTokenByIdNotFound() {
- $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class);
-
- $this->mapper->getTokenById(-1);
- }
-
-
- public function testGetInvalidTokenById() {
- $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class);
-
- $id = 42;
-
- $this->mapper->getToken($id);
- }
-
- public function testGetTokenByUser() {
- $this->assertCount(2, $this->mapper->getTokenByUser('user1'));
- }
-
- public function testGetTokenByUserNotFound() {
- $this->assertCount(0, $this->mapper->getTokenByUser('user1000'));
- }
-
- public function testDeleteById() {
- /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
- $user = $this->createMock(IUser::class);
- $qb = $this->dbConnection->getQueryBuilder();
- $qb->select('id')
- ->from('authtoken')
- ->where($qb->expr()->eq('token', $qb->createNamedParameter('9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206')));
- $result = $qb->execute();
- $id = $result->fetch()['id'];
-
- $this->mapper->deleteById('user1', $id);
- $this->assertEquals(2, $this->getNumberOfTokens());
- }
-
- public function testDeleteByIdWrongUser() {
- $id = 33;
-
- $this->mapper->deleteById('user1000', $id);
- $this->assertEquals(3, $this->getNumberOfTokens());
- }
-
- public function testDeleteByName() {
- $qb = $this->dbConnection->getQueryBuilder();
- $qb->select('name')
- ->from('authtoken')
- ->where($qb->expr()->eq('token', $qb->createNamedParameter('9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206')));
- $result = $qb->execute();
- $name = $result->fetch()['name'];
- $this->mapper->deleteByName($name);
- $this->assertEquals(2, $this->getNumberOfTokens());
- }
-}
diff --git a/tests/lib/Authentication/Token/DefaultTokenProviderTest.php b/tests/lib/Authentication/Token/DefaultTokenProviderTest.php
deleted file mode 100644
index 7a8915a7c14..00000000000
--- a/tests/lib/Authentication/Token/DefaultTokenProviderTest.php
+++ /dev/null
@@ -1,552 +0,0 @@
-<?php
-/**
- * @author Christoph Wurst <christoph@owncloud.com>
- *
- * @copyright Copyright (c) 2016, Lukas Reschke <lukas@statuscode.ch>
- * @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\Exceptions\ExpiredTokenException;
-use OC\Authentication\Exceptions\InvalidTokenException;
-use OC\Authentication\Token\DefaultToken;
-use OC\Authentication\Token\DefaultTokenMapper;
-use OC\Authentication\Token\DefaultTokenProvider;
-use OC\Authentication\Token\IToken;
-use OC\Authentication\Token\PublicKeyToken;
-use OCP\AppFramework\Db\DoesNotExistException;
-use OCP\AppFramework\Utility\ITimeFactory;
-use OCP\IConfig;
-use OCP\Security\ICrypto;
-use Psr\Log\LoggerInterface;
-use Test\TestCase;
-
-class DefaultTokenProviderTest extends TestCase {
-
- /** @var DefaultTokenProvider|\PHPUnit\Framework\MockObject\MockObject */
- private $tokenProvider;
- /** @var DefaultTokenMapper|\PHPUnit\Framework\MockObject\MockObject */
- private $mapper;
- /** @var ICrypto|\PHPUnit\Framework\MockObject\MockObject */
- private $crypto;
- /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
- private $config;
- /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
- private $logger;
- /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
- private $timeFactory;
- /** @var int */
- private $time;
-
- protected function setUp(): void {
- parent::setUp();
-
- $this->mapper = $this->createMock(DefaultTokenMapper::class);
- $this->crypto = $this->createMock(ICrypto::class);
- $this->config = $this->createMock(IConfig::class);
- $this->logger = $this->createMock(LoggerInterface::class);
- $this->timeFactory = $this->createMock(ITimeFactory::class);
- $this->time = 1313131;
- $this->timeFactory->expects($this->any())
- ->method('getTime')
- ->willReturn($this->time);
-
- $this->tokenProvider = new DefaultTokenProvider($this->mapper, $this->crypto, $this->config, $this->logger,
- $this->timeFactory);
- }
-
- public function testGenerateToken() {
- $token = 'token';
- $uid = 'user';
- $user = 'User';
- $password = 'passme';
- $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';
- $type = IToken::PERMANENT_TOKEN;
-
- $toInsert = new DefaultToken();
- $toInsert->setUid($uid);
- $toInsert->setLoginName($user);
- $toInsert->setPassword('encryptedpassword');
- $toInsert->setName($name);
- $toInsert->setToken(hash('sha512', $token . '1f4h9s'));
- $toInsert->setType($type);
- $toInsert->setRemember(IToken::DO_NOT_REMEMBER);
- $toInsert->setLastActivity($this->time);
- $toInsert->setLastCheck($this->time);
- $toInsert->setVersion(DefaultToken::VERSION);
-
- $this->config->expects($this->any())
- ->method('getSystemValue')
- ->with('secret')
- ->willReturn('1f4h9s');
- $this->crypto->expects($this->once())
- ->method('encrypt')
- ->with($password, $token . '1f4h9s')
- ->willReturn('encryptedpassword');
- $this->mapper->expects($this->once())
- ->method('insert')
- ->with($this->equalTo($toInsert));
-
- $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
-
- $this->assertEquals($toInsert, $actual);
- }
-
- public function testUpdateToken() {
- $tk = new DefaultToken();
- $tk->setLastActivity($this->time - 200);
- $this->mapper->expects($this->once())
- ->method('update')
- ->with($tk);
-
- $this->tokenProvider->updateTokenActivity($tk);
-
- $this->assertEquals($this->time, $tk->getLastActivity());
- }
-
- public function testUpdateTokenDebounce() {
- $tk = new DefaultToken();
- $tk->setLastActivity($this->time - 30);
- $this->mapper->expects($this->never())
- ->method('update')
- ->with($tk);
-
- $this->tokenProvider->updateTokenActivity($tk);
- }
-
- public function testGetTokenByUser() {
- $this->mapper->expects($this->once())
- ->method('getTokenByUser')
- ->with('uid')
- ->willReturn(['token']);
-
- $this->assertEquals(['token'], $this->tokenProvider->getTokenByUser('uid'));
- }
-
- public function testGetPassword() {
- $token = 'token1234';
- $tk = new DefaultToken();
- $tk->setPassword('someencryptedvalue');
- $this->config->expects($this->once())
- ->method('getSystemValue')
- ->with('secret')
- ->willReturn('1f4h9s');
- $this->crypto->expects($this->once())
- ->method('decrypt')
- ->with('someencryptedvalue', $token . '1f4h9s')
- ->willReturn('passme');
-
- $actual = $this->tokenProvider->getPassword($tk, $token);
-
- $this->assertEquals('passme', $actual);
- }
-
-
- public function testGetPasswordPasswordLessToken() {
- $this->expectException(\OC\Authentication\Exceptions\PasswordlessTokenException::class);
-
- $token = 'token1234';
- $tk = new DefaultToken();
- $tk->setPassword(null);
-
- $this->tokenProvider->getPassword($tk, $token);
- }
-
-
- public function testGetPasswordDeletesInvalidToken() {
- $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class);
-
- $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')
- ->willReturn('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);
- }
-
- public function testSetPassword() {
- $token = new DefaultToken();
- $tokenId = 'token123';
- $password = '123456';
-
- $this->config->expects($this->once())
- ->method('getSystemValue')
- ->with('secret')
- ->willReturn('ocsecret');
- $this->crypto->expects($this->once())
- ->method('encrypt')
- ->with($password, $tokenId . 'ocsecret')
- ->willReturn('encryptedpassword');
- $this->mapper->expects($this->once())
- ->method('update')
- ->with($token);
-
- $this->tokenProvider->setPassword($token, $tokenId, $password);
-
- $this->assertEquals('encryptedpassword', $token->getPassword());
- }
-
-
- public function testSetPasswordInvalidToken() {
- $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class);
-
- $token = $this->createMock(IToken::class);
- $tokenId = 'token123';
- $password = '123456';
-
- $this->tokenProvider->setPassword($token, $tokenId, $password);
- }
-
- public function testInvalidateToken() {
- $this->mapper->expects($this->once())
- ->method('invalidate')
- ->with(hash('sha512', 'token7'));
-
- $this->tokenProvider->invalidateToken('token7');
- }
-
- public function testInvaildateTokenById() {
- $id = 123;
-
- $this->mapper->expects($this->once())
- ->method('deleteById')
- ->with('uid', $id);
-
- $this->tokenProvider->invalidateTokenById('uid', $id);
- }
-
- public function testInvalidateOldTokens() {
- $defaultSessionLifetime = 60 * 60 * 24;
- $defaultRememberMeLifetime = 60 * 60 * 24 * 15;
- $this->config->expects($this->exactly(2))
- ->method('getSystemValue')
- ->willReturnMap([
- ['session_lifetime', $defaultSessionLifetime, 150],
- ['remember_login_cookie_lifetime', $defaultRememberMeLifetime, 300],
- ]);
- $this->mapper->expects($this->at(0))
- ->method('invalidateOld')
- ->with($this->time - 150);
- $this->mapper->expects($this->at(1))
- ->method('invalidateOld')
- ->with($this->time - 300);
-
- $this->tokenProvider->invalidateOldTokens();
- }
-
- public function testRenewSessionTokenWithoutPassword() {
- $token = $this->getMockBuilder(DefaultToken::class)
- ->disableOriginalConstructor()
- ->setMethods(['getUID', 'getLoginName', 'getPassword', 'getName', 'getRemember'])
- ->getMock();
- $token
- ->expects($this->at(0))
- ->method('getUID')
- ->willReturn('UserUid');
- $token
- ->expects($this->at(1))
- ->method('getLoginName')
- ->willReturn('UserLoginName');
- $token
- ->expects($this->at(2))
- ->method('getPassword')
- ->willReturn(null);
- $token
- ->expects($this->at(3))
- ->method('getName')
- ->willReturn('MyTokenName');
- $token
- ->expects($this->at(4))
- ->method('getRemember')
- ->willReturn(IToken::DO_NOT_REMEMBER);
- $this->config
- ->expects($this->exactly(2))
- ->method('getSystemValue')
- ->with('secret')
- ->willReturn('MyInstanceSecret');
- $this->mapper
- ->expects($this->at(0))
- ->method('getToken')
- ->with(hash('sha512', 'oldId' . 'MyInstanceSecret'))
- ->willReturn($token);
- $newToken = new DefaultToken();
- $newToken->setUid('UserUid');
- $newToken->setLoginName('UserLoginName');
- $newToken->setName('MyTokenName');
- $newToken->setToken(hash('sha512', 'newId' . 'MyInstanceSecret'));
- $newToken->setType(IToken::TEMPORARY_TOKEN);
- $newToken->setRemember(IToken::DO_NOT_REMEMBER);
- $newToken->setLastActivity(1313131);
- $this->mapper
- ->expects($this->at(1))
- ->method('insert')
- ->with($newToken);
- $this->mapper
- ->expects($this->at(2))
- ->method('delete')
- ->with($token);
-
- $this->tokenProvider->renewSessionToken('oldId', 'newId');
- }
-
- public function testRenewSessionTokenWithPassword() {
- $token = $this->getMockBuilder(DefaultToken::class)
- ->disableOriginalConstructor()
- ->setMethods(['getUID', 'getLoginName', 'getPassword', 'getName', 'getRemember'])
- ->getMock();
- $token
- ->expects($this->at(0))
- ->method('getUID')
- ->willReturn('UserUid');
- $token
- ->expects($this->at(1))
- ->method('getLoginName')
- ->willReturn('UserLoginName');
- $token
- ->expects($this->at(2))
- ->method('getPassword')
- ->willReturn('EncryptedPassword');
- $token
- ->expects($this->at(3))
- ->method('getPassword')
- ->willReturn('EncryptedPassword');
- $token
- ->expects($this->at(4))
- ->method('getName')
- ->willReturn('MyTokenName');
- $token
- ->expects($this->at(5))
- ->method('getRemember')
- ->willReturn(IToken::REMEMBER);
- $this->crypto
- ->expects($this->any(0))
- ->method('decrypt')
- ->with('EncryptedPassword', 'oldIdMyInstanceSecret')
- ->willReturn('ClearTextPassword');
- $this->crypto
- ->expects($this->any(1))
- ->method('encrypt')
- ->with('ClearTextPassword', 'newIdMyInstanceSecret')
- ->willReturn('EncryptedPassword');
- $this->config
- ->expects($this->exactly(4))
- ->method('getSystemValue')
- ->with('secret')
- ->willReturn('MyInstanceSecret');
- $this->mapper
- ->expects($this->at(0))
- ->method('getToken')
- ->with(hash('sha512', 'oldId' . 'MyInstanceSecret'))
- ->willReturn($token);
- $newToken = new DefaultToken();
- $newToken->setUid('UserUid');
- $newToken->setLoginName('UserLoginName');
- $newToken->setName('MyTokenName');
- $newToken->setToken(hash('sha512', 'newId' . 'MyInstanceSecret'));
- $newToken->setType(IToken::TEMPORARY_TOKEN);
- $newToken->setRemember(IToken::REMEMBER);
- $newToken->setLastActivity(1313131);
- $newToken->setPassword('EncryptedPassword');
- $this->mapper
- ->expects($this->at(1))
- ->method('insert')
- ->with($this->equalTo($newToken));
- $this->mapper
- ->expects($this->at(2))
- ->method('delete')
- ->with($token);
-
- $this->tokenProvider->renewSessionToken('oldId', 'newId');
- }
-
- public function testGetToken() {
- $token = new DefaultToken();
-
- $this->config->method('getSystemValue')
- ->with('secret')
- ->willReturn('mysecret');
-
- $this->mapper->method('getToken')
- ->with(
- $this->callback(function (string $token) {
- return hash('sha512', 'unhashedTokenmysecret') === $token;
- })
- )->willReturn($token);
-
- $this->assertSame($token, $this->tokenProvider->getToken('unhashedToken'));
- }
-
- public function testGetInvalidToken() {
- $this->expectException(InvalidTokenException::class);
-
- $this->config->method('getSystemValue')
- ->with('secret')
- ->willReturn('mysecret');
-
- $this->mapper->method('getToken')
- ->with(
- $this->callback(function (string $token) {
- return hash('sha512', 'unhashedTokenmysecret') === $token;
- })
- )->willThrowException(new InvalidTokenException());
-
- $this->tokenProvider->getToken('unhashedToken');
- }
-
- public function testGetExpiredToken() {
- $token = new DefaultToken();
- $token->setExpires(42);
-
- $this->config->method('getSystemValue')
- ->with('secret')
- ->willReturn('mysecret');
-
- $this->mapper->method('getToken')
- ->with(
- $this->callback(function (string $token) {
- return hash('sha512', 'unhashedTokenmysecret') === $token;
- })
- )->willReturn($token);
-
- try {
- $this->tokenProvider->getToken('unhashedToken');
- } catch (ExpiredTokenException $e) {
- $this->assertSame($token, $e->getToken());
- }
- }
-
- public function testGetTokenById() {
- $token = $this->createMock(DefaultToken::class);
-
- $this->mapper->expects($this->once())
- ->method('getTokenById')
- ->with($this->equalTo(42))
- ->willReturn($token);
-
- $this->assertSame($token, $this->tokenProvider->getTokenById(42));
- }
-
- public function testGetInvalidTokenById() {
- $this->expectException(InvalidTokenException::class);
-
- $this->mapper->expects($this->once())
- ->method('getTokenById')
- ->with($this->equalTo(42))
- ->willThrowException(new DoesNotExistException('nope'));
-
- $this->tokenProvider->getTokenById(42);
- }
-
- public function testGetExpiredTokenById() {
- $token = new DefaultToken();
- $token->setExpires(42);
-
- $this->mapper->expects($this->once())
- ->method('getTokenById')
- ->with($this->equalTo(42))
- ->willReturn($token);
-
- try {
- $this->tokenProvider->getTokenById(42);
- $this->fail();
- } catch (ExpiredTokenException $e) {
- $this->assertSame($token, $e->getToken());
- }
- }
-
- public function testRotate() {
- $token = new DefaultToken();
- $token->setPassword('oldencryptedpassword');
-
- $this->config->method('getSystemValue')
- ->with('secret')
- ->willReturn('mysecret');
-
- $this->crypto->method('decrypt')
- ->with('oldencryptedpassword', 'oldtokenmysecret')
- ->willReturn('mypassword');
- $this->crypto->method('encrypt')
- ->with('mypassword', 'newtokenmysecret')
- ->willReturn('newencryptedpassword');
-
- $this->mapper->expects($this->once())
- ->method('update')
- ->with($this->callback(function (DefaultToken $token) {
- return $token->getPassword() === 'newencryptedpassword' &&
- $token->getToken() === hash('sha512', 'newtokenmysecret');
- }));
-
- $this->tokenProvider->rotate($token, 'oldtoken', 'newtoken');
- }
-
- public function testRotateNoPassword() {
- $token = new DefaultToken();
-
- $this->config->method('getSystemValue')
- ->with('secret')
- ->willReturn('mysecret');
-
- $this->mapper->expects($this->once())
- ->method('update')
- ->with($this->callback(function (DefaultToken $token) {
- return $token->getPassword() === null &&
- $token->getToken() === hash('sha512', 'newtokenmysecret');
- }));
-
- $this->tokenProvider->rotate($token, 'oldtoken', 'newtoken');
- }
-
- public function testMarkPasswordInvalidInvalidToken() {
- $token = $this->createMock(PublicKeyToken::class);
-
- $this->expectException(InvalidTokenException::class);
-
- $this->tokenProvider->markPasswordInvalid($token, 'tokenId');
- }
-
- public function testMarkPasswordInvalid() {
- $token = $this->createMock(DefaultToken::class);
-
- $this->mapper->expects($this->once())
- ->method('invalidate')
- ->with('0c7db0098fe8ddba6032b22719ec18867c69a1820fa36d71c28bf96d52843bdc44a112bd24093b049be5bb54769bcb72d67190a4a9690e51aac263cba38186fb');
-
- $this->tokenProvider->markPasswordInvalid($token, 'tokenId');
- }
-}
diff --git a/tests/lib/Authentication/Token/DefaultTokenTest.php b/tests/lib/Authentication/Token/DefaultTokenTest.php
deleted file mode 100644
index 76b976586a9..00000000000
--- a/tests/lib/Authentication/Token/DefaultTokenTest.php
+++ /dev/null
@@ -1,41 +0,0 @@
-<?php
-/**
- * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * 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
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-namespace Test\Authentication\Token;
-
-use OC\Authentication\Token\DefaultToken;
-use Test\TestCase;
-
-class DefaultTokenTest extends TestCase {
- public function testSetScopeAsArray() {
- $scope = ['filesystem' => false];
- $token = new DefaultToken();
- $token->setScope($scope);
- $this->assertEquals(json_encode($scope), $token->getScope());
- $this->assertEquals($scope, $token->getScopeAsArray());
- }
-
- public function testDefaultScope() {
- $scope = ['filesystem' => true];
- $token = new DefaultToken();
- $this->assertEquals($scope, $token->getScopeAsArray());
- }
-}
diff --git a/tests/lib/Authentication/Token/ManagerTest.php b/tests/lib/Authentication/Token/ManagerTest.php
index fb92b3e5018..58bbe236248 100644
--- a/tests/lib/Authentication/Token/ManagerTest.php
+++ b/tests/lib/Authentication/Token/ManagerTest.php
@@ -3,34 +3,14 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * 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
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Authentication\Token;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OC\Authentication\Exceptions\InvalidTokenException;
-use OC\Authentication\Exceptions\PasswordlessTokenException;
-use OC\Authentication\Token\DefaultToken;
-use OC\Authentication\Token\DefaultTokenProvider;
use OC\Authentication\Token\IToken;
use OC\Authentication\Token\Manager;
use OC\Authentication\Token\PublicKeyToken;
@@ -39,11 +19,8 @@ use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class ManagerTest extends TestCase {
-
/** @var PublicKeyTokenProvider|MockObject */
private $publicKeyTokenProvider;
- /** @var DefaultTokenProvider|MockObject */
- private $defaultTokenProvider;
/** @var Manager */
private $manager;
@@ -51,17 +28,12 @@ class ManagerTest extends TestCase {
parent::setUp();
$this->publicKeyTokenProvider = $this->createMock(PublicKeyTokenProvider::class);
- $this->defaultTokenProvider = $this->createMock(DefaultTokenProvider::class);
$this->manager = new Manager(
- $this->defaultTokenProvider,
$this->publicKeyTokenProvider
);
}
- public function testGenerateToken() {
- $this->defaultTokenProvider->expects($this->never())
- ->method('generateToken');
-
+ public function testGenerateToken(): void {
$token = new PublicKeyToken();
$this->publicKeyTokenProvider->expects($this->once())
@@ -89,11 +61,9 @@ class ManagerTest extends TestCase {
$this->assertSame($token, $actual);
}
- public function testGenerateConflictingToken() {
+ public function testGenerateConflictingToken(): void {
/** @var MockObject|UniqueConstraintViolationException $exception */
$exception = $this->createMock(UniqueConstraintViolationException::class);
- $this->defaultTokenProvider->expects($this->never())
- ->method('generateToken');
$token = new PublicKeyToken();
$token->setUid('uid');
@@ -127,20 +97,45 @@ class ManagerTest extends TestCase {
$this->assertSame($token, $actual);
}
- public function tokenData(): array {
+ public function testGenerateTokenTooLongName(): void {
+ $token = $this->createMock(IToken::class);
+ $token->method('getName')
+ ->willReturn(str_repeat('a', 120) . '…');
+
+
+ $this->publicKeyTokenProvider->expects($this->once())
+ ->method('generateToken')
+ ->with(
+ 'token',
+ 'uid',
+ 'loginName',
+ 'password',
+ str_repeat('a', 120) . '…',
+ IToken::TEMPORARY_TOKEN,
+ IToken::REMEMBER
+ )->willReturn($token);
+
+ $actual = $this->manager->generateToken(
+ 'token',
+ 'uid',
+ 'loginName',
+ 'password',
+ str_repeat('a', 200),
+ IToken::TEMPORARY_TOKEN,
+ IToken::REMEMBER
+ );
+
+ $this->assertSame(121, mb_strlen($actual->getName()));
+ }
+
+ public static function tokenData(): array {
return [
- [new DefaultToken()],
[new PublicKeyToken()],
- [$this->createMock(IToken::class)],
+ [IToken::class],
];
}
protected function setNoCall(IToken $token) {
- if (!($token instanceof DefaultToken)) {
- $this->defaultTokenProvider->expects($this->never())
- ->method($this->anything());
- }
-
if (!($token instanceof PublicKeyToken)) {
$this->publicKeyTokenProvider->expects($this->never())
->method($this->anything());
@@ -148,13 +143,6 @@ class ManagerTest extends TestCase {
}
protected function setCall(IToken $token, string $function, $return = null) {
- if ($token instanceof DefaultToken) {
- $this->defaultTokenProvider->expects($this->once())
- ->method($function)
- ->with($token)
- ->willReturn($return);
- }
-
if ($token instanceof PublicKeyToken) {
$this->publicKeyTokenProvider->expects($this->once())
->method($function)
@@ -164,15 +152,17 @@ class ManagerTest extends TestCase {
}
protected function setException(IToken $token) {
- if (!($token instanceof DefaultToken) && !($token instanceof PublicKeyToken)) {
+ if (!($token instanceof PublicKeyToken)) {
$this->expectException(InvalidTokenException::class);
}
}
- /**
- * @dataProvider tokenData
- */
- public function testUpdateToken(IToken $token) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('tokenData')]
+ public function testUpdateToken(IToken|string $token): void {
+ if (is_string($token)) {
+ $token = $this->createMock($token);
+ }
+
$this->setNoCall($token);
$this->setCall($token, 'updateToken');
$this->setException($token);
@@ -180,10 +170,12 @@ class ManagerTest extends TestCase {
$this->manager->updateToken($token);
}
- /**
- * @dataProvider tokenData
- */
- public function testUpdateTokenActivity(IToken $token) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('tokenData')]
+ public function testUpdateTokenActivity(IToken|string $token): void {
+ if (is_string($token)) {
+ $token = $this->createMock($token);
+ }
+
$this->setNoCall($token);
$this->setCall($token, 'updateTokenActivity');
$this->setException($token);
@@ -191,10 +183,12 @@ class ManagerTest extends TestCase {
$this->manager->updateTokenActivity($token);
}
- /**
- * @dataProvider tokenData
- */
- public function testGetPassword(IToken $token) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('tokenData')]
+ public function testGetPassword(IToken|string $token): void {
+ if (is_string($token)) {
+ $token = $this->createMock($token);
+ }
+
$this->setNoCall($token);
$this->setCall($token, 'getPassword', 'password');
$this->setException($token);
@@ -204,10 +198,12 @@ class ManagerTest extends TestCase {
$this->assertSame('password', $result);
}
- /**
- * @dataProvider tokenData
- */
- public function testSetPassword(IToken $token) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('tokenData')]
+ public function testSetPassword(IToken|string $token): void {
+ if (is_string($token)) {
+ $token = $this->createMock($token);
+ }
+
$this->setNoCall($token);
$this->setCall($token, 'setPassword');
$this->setException($token);
@@ -215,11 +211,7 @@ class ManagerTest extends TestCase {
$this->manager->setPassword($token, 'tokenId', 'password');
}
- public function testInvalidateTokens() {
- $this->defaultTokenProvider->expects($this->once())
- ->method('invalidateToken')
- ->with('token');
-
+ public function testInvalidateTokens(): void {
$this->publicKeyTokenProvider->expects($this->once())
->method('invalidateToken')
->with('token');
@@ -227,11 +219,7 @@ class ManagerTest extends TestCase {
$this->manager->invalidateToken('token');
}
- public function testInvalidateTokenById() {
- $this->defaultTokenProvider->expects($this->once())
- ->method('invalidateTokenById')
- ->with('uid', 42);
-
+ public function testInvalidateTokenById(): void {
$this->publicKeyTokenProvider->expects($this->once())
->method('invalidateTokenById')
->with('uid', 42);
@@ -239,39 +227,35 @@ class ManagerTest extends TestCase {
$this->manager->invalidateTokenById('uid', 42);
}
- public function testInvalidateOldTokens() {
- $this->defaultTokenProvider->expects($this->once())
- ->method('invalidateOldTokens');
-
+ public function testInvalidateOldTokens(): void {
$this->publicKeyTokenProvider->expects($this->once())
->method('invalidateOldTokens');
$this->manager->invalidateOldTokens();
}
- public function testGetTokenByUser() {
- $t1 = new DefaultToken();
- $t2 = new DefaultToken();
- $t3 = new PublicKeyToken();
- $t4 = new PublicKeyToken();
+ public function testInvalidateLastUsedBefore(): void {
+ $this->publicKeyTokenProvider->expects($this->once())
+ ->method('invalidateLastUsedBefore')
+ ->with('user', 946684800);
- $this->defaultTokenProvider
- ->method('getTokenByUser')
- ->willReturn([$t1, $t2]);
+ $this->manager->invalidateLastUsedBefore('user', 946684800);
+ }
+
+ public function testGetTokenByUser(): void {
+ $t1 = new PublicKeyToken();
+ $t2 = new PublicKeyToken();
$this->publicKeyTokenProvider
->method('getTokenByUser')
- ->willReturn([$t3, $t4]);
+ ->willReturn([$t1, $t2]);
$result = $this->manager->getTokenByUser('uid');
- $this->assertEquals([$t1, $t2, $t3, $t4], $result);
+ $this->assertEquals([$t1, $t2], $result);
}
- public function testRenewSessionTokenPublicKey() {
- $this->defaultTokenProvider->expects($this->never())
- ->method($this->anything());
-
+ public function testRenewSessionTokenPublicKey(): void {
$this->publicKeyTokenProvider->expects($this->once())
->method('renewSessionToken')
->with('oldId', 'newId');
@@ -279,35 +263,17 @@ class ManagerTest extends TestCase {
$this->manager->renewSessionToken('oldId', 'newId');
}
- public function testRenewSessionTokenDefault() {
+ public function testRenewSessionInvalid(): void {
$this->publicKeyTokenProvider->expects($this->once())
->method('renewSessionToken')
->with('oldId', 'newId')
->willThrowException(new InvalidTokenException());
- $this->defaultTokenProvider->expects($this->once())
- ->method('renewSessionToken')
- ->with('oldId', 'newId');
-
- $this->manager->renewSessionToken('oldId', 'newId');
- }
-
- public function testRenewSessionInvalid() {
- $this->publicKeyTokenProvider->expects($this->once())
- ->method('renewSessionToken')
- ->with('oldId', 'newId')
- ->willThrowException(new InvalidTokenException());
-
- $this->defaultTokenProvider->expects($this->once())
- ->method('renewSessionToken')
- ->with('oldId', 'newId')
- ->willThrowException(new InvalidTokenException());
-
$this->expectException(InvalidTokenException::class);
$this->manager->renewSessionToken('oldId', 'newId');
}
- public function testGetTokenByIdPublicKey() {
+ public function testGetTokenByIdPublicKey(): void {
$token = $this->createMock(IToken::class);
$this->publicKeyTokenProvider->expects($this->once())
@@ -315,50 +281,22 @@ class ManagerTest extends TestCase {
->with(42)
->willReturn($token);
- $this->defaultTokenProvider->expects($this->never())
- ->method($this->anything());
-
-
$this->assertSame($token, $this->manager->getTokenById(42));
}
- public function testGetTokenByIdDefault() {
- $token = $this->createMock(IToken::class);
-
+ public function testGetTokenByIdInvalid(): void {
$this->publicKeyTokenProvider->expects($this->once())
->method('getTokenById')
->with(42)
->willThrowException(new InvalidTokenException());
- $this->defaultTokenProvider->expects($this->once())
- ->method('getTokenById')
- ->with(42)
- ->willReturn($token);
-
- $this->assertSame($token, $this->manager->getTokenById(42));
- }
-
- public function testGetTokenByIdInvalid() {
- $this->publicKeyTokenProvider->expects($this->once())
- ->method('getTokenById')
- ->with(42)
- ->willThrowException(new InvalidTokenException());
-
- $this->defaultTokenProvider->expects($this->once())
- ->method('getTokenById')
- ->with(42)
- ->willThrowException(new InvalidTokenException());
-
$this->expectException(InvalidTokenException::class);
$this->manager->getTokenById(42);
}
- public function testGetTokenPublicKey() {
+ public function testGetTokenPublicKey(): void {
$token = new PublicKeyToken();
- $this->defaultTokenProvider->expects($this->never())
- ->method($this->anything());
-
$this->publicKeyTokenProvider
->method('getToken')
->with('tokenId')
@@ -367,12 +305,7 @@ class ManagerTest extends TestCase {
$this->assertSame($token, $this->manager->getToken('tokenId'));
}
- public function testGetTokenInvalid() {
- $this->defaultTokenProvider
- ->method('getToken')
- ->with('tokenId')
- ->willThrowException(new InvalidTokenException());
-
+ public function testGetTokenInvalid(): void {
$this->publicKeyTokenProvider
->method('getToken')
->with('tokenId')
@@ -382,64 +315,12 @@ class ManagerTest extends TestCase {
$this->manager->getToken('tokenId');
}
- public function testGetTokenConvertPassword() {
- $oldToken = new DefaultToken();
- $newToken = new PublicKeyToken();
-
- $this->publicKeyTokenProvider
- ->method('getToken')
- ->with('tokenId')
- ->willThrowException(new InvalidTokenException());
-
- $this->defaultTokenProvider
- ->method('getToken')
- ->willReturn($oldToken);
-
- $this->defaultTokenProvider
- ->method('getPassword')
- ->with($oldToken, 'tokenId')
- ->willReturn('password');
-
- $this->publicKeyTokenProvider
- ->method('convertToken')
- ->with($oldToken, 'tokenId', 'password')
- ->willReturn($newToken);
-
- $this->assertSame($newToken, $this->manager->getToken('tokenId'));
- }
-
- public function testGetTokenConvertNoPassword() {
- $oldToken = new DefaultToken();
- $newToken = new PublicKeyToken();
-
- $this->publicKeyTokenProvider
- ->method('getToken')
- ->with('tokenId')
- ->willThrowException(new InvalidTokenException());
-
- $this->defaultTokenProvider
- ->method('getToken')
- ->willReturn($oldToken);
-
- $this->defaultTokenProvider
- ->method('getPassword')
- ->with($oldToken, 'tokenId')
- ->willThrowException(new PasswordlessTokenException());
-
- $this->publicKeyTokenProvider
- ->method('convertToken')
- ->with($oldToken, 'tokenId', null)
- ->willReturn($newToken);
-
- $this->assertSame($newToken, $this->manager->getToken('tokenId'));
- }
-
- public function testRotateInvalid() {
+ public function testRotateInvalid(): void {
$this->expectException(InvalidTokenException::class);
$this->manager->rotate($this->createMock(IToken::class), 'oldId', 'newId');
}
- public function testRotatePublicKey() {
+ public function testRotatePublicKey(): void {
$token = new PublicKeyToken();
$this->publicKeyTokenProvider
@@ -450,78 +331,76 @@ class ManagerTest extends TestCase {
$this->assertSame($token, $this->manager->rotate($token, 'oldId', 'newId'));
}
- public function testRotateConvertPassword() {
- $oldToken = new DefaultToken();
- $newToken = new PublicKeyToken();
-
- $this->defaultTokenProvider
- ->method('getPassword')
- ->with($oldToken, 'oldId')
- ->willReturn('password');
-
- $this->publicKeyTokenProvider
- ->method('convertToken')
- ->with($oldToken, 'newId', 'password')
- ->willReturn($newToken);
-
- $this->assertSame($newToken, $this->manager->rotate($oldToken, 'oldId', 'newId'));
- }
-
- public function testRotateConvertNoPassword() {
- $oldToken = new DefaultToken();
- $newToken = new PublicKeyToken();
-
- $this->defaultTokenProvider
- ->method('getPassword')
- ->with($oldToken, 'oldId')
- ->willThrowException(new PasswordlessTokenException());
-
- $this->publicKeyTokenProvider
- ->method('convertToken')
- ->with($oldToken, 'newId', null)
- ->willReturn($newToken);
-
- $this->assertSame($newToken, $this->manager->rotate($oldToken, 'oldId', 'newId'));
- }
-
- public function testMarkPasswordInvalidDefault() {
- $token = $this->createMock(DefaultToken::class);
-
- $this->defaultTokenProvider->expects($this->once())
- ->method('markPasswordInvalid')
- ->with($token, 'tokenId');
- $this->publicKeyTokenProvider->expects($this->never())
- ->method($this->anything());
-
- $this->manager->markPasswordInvalid($token, 'tokenId');
- }
-
- public function testMarkPasswordInvalidPublicKey() {
+ public function testMarkPasswordInvalidPublicKey(): void {
$token = $this->createMock(PublicKeyToken::class);
$this->publicKeyTokenProvider->expects($this->once())
->method('markPasswordInvalid')
->with($token, 'tokenId');
- $this->defaultTokenProvider->expects($this->never())
- ->method($this->anything());
$this->manager->markPasswordInvalid($token, 'tokenId');
}
- public function testMarkPasswordInvalidInvalidToken() {
+ public function testMarkPasswordInvalidInvalidToken(): void {
$this->expectException(InvalidTokenException::class);
$this->manager->markPasswordInvalid($this->createMock(IToken::class), 'tokenId');
}
- public function testUpdatePasswords() {
- $this->defaultTokenProvider->expects($this->once())
- ->method('updatePasswords')
- ->with('uid', 'pass');
+ public function testUpdatePasswords(): void {
$this->publicKeyTokenProvider->expects($this->once())
->method('updatePasswords')
->with('uid', 'pass');
$this->manager->updatePasswords('uid', 'pass');
}
+
+ public function testInvalidateTokensOfUserNoClientName(): void {
+ $t1 = new PublicKeyToken();
+ $t2 = new PublicKeyToken();
+ $t1->setId(123);
+ $t2->setId(456);
+
+ $this->publicKeyTokenProvider
+ ->expects($this->once())
+ ->method('getTokenByUser')
+ ->with('theUser')
+ ->willReturn([$t1, $t2]);
+
+ $calls = [
+ ['theUser', 123],
+ ['theUser', 456],
+ ];
+ $this->publicKeyTokenProvider
+ ->expects($this->exactly(2))
+ ->method('invalidateTokenById')
+ ->willReturnCallback(function () use (&$calls): void {
+ $expected = array_shift($calls);
+ $this->assertEquals($expected, func_get_args());
+ });
+ $this->manager->invalidateTokensOfUser('theUser', null);
+ }
+
+ public function testInvalidateTokensOfUserClientNameGiven(): void {
+ $t1 = new PublicKeyToken();
+ $t2 = new PublicKeyToken();
+ $t3 = new PublicKeyToken();
+ $t1->setId(123);
+ $t1->setName('Firefox session');
+ $t2->setId(456);
+ $t2->setName('My Client Name');
+ $t3->setId(789);
+ $t3->setName('mobile client');
+
+ $this->publicKeyTokenProvider
+ ->expects($this->once())
+ ->method('getTokenByUser')
+ ->with('theUser')
+ ->willReturn([$t1, $t2, $t3]);
+ $this->publicKeyTokenProvider
+ ->expects($this->once())
+ ->method('invalidateTokenById')
+ ->with('theUser', 456);
+ $this->manager->invalidateTokensOfUser('theUser', 'My Client Name');
+ }
}
diff --git a/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php b/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php
index bfb92932e81..d1585dadc26 100644
--- a/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php
+++ b/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php
@@ -2,43 +2,26 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * 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
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Authentication\Token;
-use OC;
-use OC\Authentication\Token\IToken;
use OC\Authentication\Token\PublicKeyToken;
use OC\Authentication\Token\PublicKeyTokenMapper;
+use OCP\AppFramework\Db\DoesNotExistException;
+use OCP\Authentication\Token\IToken;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IUser;
+use OCP\Server;
use Test\TestCase;
/**
* @group DB
*/
class PublicKeyTokenMapperTest extends TestCase {
-
/** @var PublicKeyTokenMapper */
private $mapper;
@@ -51,7 +34,7 @@ class PublicKeyTokenMapperTest extends TestCase {
protected function setUp(): void {
parent::setUp();
- $this->dbConnection = OC::$server->getDatabaseConnection();
+ $this->dbConnection = Server::get(IDBConnection::class);
$this->time = time();
$this->resetDatabase();
@@ -114,6 +97,20 @@ class PublicKeyTokenMapperTest extends TestCase {
'version' => $qb->createNamedParameter(2),
'password_invalid' => $qb->createNamedParameter(1),
])->execute();
+ $qb->insert('authtoken')->values([
+ 'uid' => $qb->createNamedParameter('user3'),
+ 'login_name' => $qb->createNamedParameter('User3'),
+ 'password' => $qb->createNamedParameter('063de945d6f6b26862d9b6f40652f2d5|DZ/z520tfdXPtd0T|395f6b89be8d9d605e409e20b9d9abe477fde1be38a3223f9e508f979bf906e50d9eaa4dca983ca4fb22a241eb696c3f98654e7775f78c4caf13108f98642b53'),
+ 'name' => $qb->createNamedParameter('Iceweasel on Linux'),
+ 'token' => $qb->createNamedParameter('84c5808c6445b6d65b8aa5b03840f09b27de603f0fb970906fb14ea4b115b7bf5ec53fada5c093fe46afdcd7bbc9617253a4d105f7dfb32719f9973d72412f31'),
+ 'type' => $qb->createNamedParameter(IToken::PERMANENT_TOKEN),
+ 'last_activity' => $qb->createNamedParameter($this->time - 60 * 3, IQueryBuilder::PARAM_INT), // Three minutes ago
+ 'last_check' => $this->time - 60 * 10, // 10mins ago
+ 'public_key' => $qb->createNamedParameter('public key'),
+ 'private_key' => $qb->createNamedParameter('private key'),
+ 'version' => $qb->createNamedParameter(2),
+ 'password_invalid' => $qb->createNamedParameter(1),
+ ])->execute();
}
private function getNumberOfTokens() {
@@ -122,34 +119,42 @@ class PublicKeyTokenMapperTest extends TestCase {
->from('authtoken')
->execute()
->fetch();
- return (int) $result['count'];
+ return (int)$result['count'];
}
- public function testInvalidate() {
+ public function testInvalidate(): void {
$token = '9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206';
$this->mapper->invalidate($token);
- $this->assertSame(3, $this->getNumberOfTokens());
+ $this->assertSame(4, $this->getNumberOfTokens());
}
- public function testInvalidateInvalid() {
+ public function testInvalidateInvalid(): void {
$token = 'youwontfindthisoneinthedatabase';
$this->mapper->invalidate($token);
- $this->assertSame(4, $this->getNumberOfTokens());
+ $this->assertSame(5, $this->getNumberOfTokens());
}
- public function testInvalidateOld() {
+ public function testInvalidateOld(): void {
$olderThan = $this->time - 60 * 60; // One hour
$this->mapper->invalidateOld($olderThan);
- $this->assertSame(3, $this->getNumberOfTokens());
+ $this->assertSame(4, $this->getNumberOfTokens());
}
- public function testGetToken() {
+ public function testInvalidateLastUsedBefore(): void {
+ $before = $this->time - 60 * 2; // Two minutes
+
+ $this->mapper->invalidateLastUsedBefore('user3', $before);
+
+ $this->assertSame(4, $this->getNumberOfTokens());
+ }
+
+ public function testGetToken(): void {
$token = new PublicKeyToken();
$token->setUid('user2');
$token->setLoginName('User2');
@@ -173,15 +178,15 @@ class PublicKeyTokenMapperTest extends TestCase {
}
- public function testGetInvalidToken() {
- $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class);
+ public function testGetInvalidToken(): void {
+ $this->expectException(DoesNotExistException::class);
$token = 'thisisaninvalidtokenthatisnotinthedatabase';
$this->mapper->getToken($token);
}
- public function testGetTokenById() {
+ public function testGetTokenById(): void {
$token = new PublicKeyToken();
$token->setUid('user2');
$token->setLoginName('User2');
@@ -205,30 +210,30 @@ class PublicKeyTokenMapperTest extends TestCase {
}
- public function testGetTokenByIdNotFound() {
- $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class);
+ public function testGetTokenByIdNotFound(): void {
+ $this->expectException(DoesNotExistException::class);
$this->mapper->getTokenById(-1);
}
- public function testGetInvalidTokenById() {
- $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class);
+ public function testGetInvalidTokenById(): void {
+ $this->expectException(DoesNotExistException::class);
$id = '42';
$this->mapper->getToken($id);
}
- public function testGetTokenByUser() {
+ public function testGetTokenByUser(): void {
$this->assertCount(2, $this->mapper->getTokenByUser('user1'));
}
- public function testGetTokenByUserNotFound() {
+ public function testGetTokenByUserNotFound(): void {
$this->assertCount(0, $this->mapper->getTokenByUser('user1000'));
}
- public function testDeleteById() {
+ public function testGetById(): void {
/** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
$user = $this->createMock(IUser::class);
$qb = $this->dbConnection->getQueryBuilder();
@@ -238,20 +243,11 @@ class PublicKeyTokenMapperTest extends TestCase {
$result = $qb->execute();
$id = $result->fetch()['id'];
- $this->mapper->deleteById('user1', (int)$id);
- $this->assertEquals(3, $this->getNumberOfTokens());
+ $token = $this->mapper->getTokenById((int)$id);
+ $this->assertEquals('user1', $token->getUID());
}
- public function testDeleteByIdWrongUser() {
- /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
- $user = $this->createMock(IUser::class);
- $id = 33;
-
- $this->mapper->deleteById('user1000', $id);
- $this->assertEquals(4, $this->getNumberOfTokens());
- }
-
- public function testDeleteByName() {
+ public function testDeleteByName(): void {
$qb = $this->dbConnection->getQueryBuilder();
$qb->select('name')
->from('authtoken')
@@ -259,10 +255,10 @@ class PublicKeyTokenMapperTest extends TestCase {
$result = $qb->execute();
$name = $result->fetch()['name'];
$this->mapper->deleteByName($name);
- $this->assertEquals(3, $this->getNumberOfTokens());
+ $this->assertEquals(4, $this->getNumberOfTokens());
}
- public function testHasExpiredTokens() {
+ public function testHasExpiredTokens(): void {
$this->assertFalse($this->mapper->hasExpiredTokens('user1'));
$this->assertTrue($this->mapper->hasExpiredTokens('user3'));
}
diff --git a/tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php b/tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php
index f27100b5d78..7e7f949965f 100644
--- a/tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php
+++ b/tests/lib/Authentication/Token/PublicKeyTokenProviderTest.php
@@ -1,56 +1,52 @@
<?php
+
+declare(strict_types=1);
+
/**
- * @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * 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
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Authentication\Token;
use OC\Authentication\Exceptions\ExpiredTokenException;
use OC\Authentication\Exceptions\InvalidTokenException;
-use OC\Authentication\Token\DefaultToken;
-use OC\Authentication\Token\IToken;
+use OC\Authentication\Exceptions\PasswordlessTokenException;
use OC\Authentication\Token\PublicKeyToken;
use OC\Authentication\Token\PublicKeyTokenMapper;
use OC\Authentication\Token\PublicKeyTokenProvider;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\Authentication\Token\IToken;
+use OCP\ICacheFactory;
use OCP\IConfig;
+use OCP\IDBConnection;
use OCP\Security\ICrypto;
+use OCP\Security\IHasher;
+use OCP\Server;
+use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
class PublicKeyTokenProviderTest extends TestCase {
-
/** @var PublicKeyTokenProvider|\PHPUnit\Framework\MockObject\MockObject */
private $tokenProvider;
/** @var PublicKeyTokenMapper|\PHPUnit\Framework\MockObject\MockObject */
private $mapper;
+ /** @var IHasher|\PHPUnit\Framework\MockObject\MockObject */
+ private $hasher;
/** @var ICrypto */
private $crypto;
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
private $config;
+ /** @var IDBConnection|MockObject */
+ private IDBConnection $db;
/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
private $logger;
/** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
private $timeFactory;
+ /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
+ private $cacheFactory;
/** @var int */
private $time;
@@ -58,27 +54,103 @@ class PublicKeyTokenProviderTest extends TestCase {
parent::setUp();
$this->mapper = $this->createMock(PublicKeyTokenMapper::class);
- $this->crypto = \OC::$server->getCrypto();
+ $this->hasher = Server::get(IHasher::class);
+ $this->crypto = Server::get(ICrypto::class);
$this->config = $this->createMock(IConfig::class);
$this->config->method('getSystemValue')
->willReturnMap([
- ['session_lifetime', 60 * 60 * 24, 150],
- ['remember_login_cookie_lifetime', 60 * 60 * 24 * 15, 300],
- ['secret', '', '1f4h9s'],
['openssl', [], []],
]);
+ $this->config->method('getSystemValueString')
+ ->willReturnMap([
+ ['secret', '', '1f4h9s'],
+ ]);
+ $this->db = $this->createMock(IDBConnection::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->time = 1313131;
$this->timeFactory->method('getTime')
->willReturn($this->time);
+ $this->cacheFactory = $this->createMock(ICacheFactory::class);
+
+ $this->tokenProvider = new PublicKeyTokenProvider(
+ $this->mapper,
+ $this->crypto,
+ $this->config,
+ $this->db,
+ $this->logger,
+ $this->timeFactory,
+ $this->hasher,
+ $this->cacheFactory,
+ );
+ }
+
+ public function testGenerateToken(): void {
+ $token = 'tokentokentokentokentoken';
+ $uid = 'user';
+ $user = 'User';
+ $password = 'passme';
+ $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';
+ $type = IToken::PERMANENT_TOKEN;
+
+ $this->config->method('getSystemValueBool')
+ ->willReturnMap([
+ ['auth.storeCryptedPassword', true, true],
+ ]);
+ $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
- $this->tokenProvider = new PublicKeyTokenProvider($this->mapper, $this->crypto, $this->config, $this->logger,
- $this->timeFactory);
+ $this->assertInstanceOf(PublicKeyToken::class, $actual);
+ $this->assertSame($uid, $actual->getUID());
+ $this->assertSame($user, $actual->getLoginName());
+ $this->assertSame($name, $actual->getName());
+ $this->assertSame(IToken::DO_NOT_REMEMBER, $actual->getRemember());
+ $this->assertSame($password, $this->tokenProvider->getPassword($actual, $token));
}
- public function testGenerateToken() {
- $token = 'token';
+ public function testGenerateTokenNoPassword(): void {
+ $token = 'tokentokentokentokentoken';
+ $uid = 'user';
+ $user = 'User';
+ $password = 'passme';
+ $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';
+ $type = IToken::PERMANENT_TOKEN;
+ $this->config->method('getSystemValueBool')
+ ->willReturnMap([
+ ['auth.storeCryptedPassword', true, false],
+ ]);
+ $this->expectException(PasswordlessTokenException::class);
+
+ $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
+
+ $this->assertInstanceOf(PublicKeyToken::class, $actual);
+ $this->assertSame($uid, $actual->getUID());
+ $this->assertSame($user, $actual->getLoginName());
+ $this->assertSame($name, $actual->getName());
+ $this->assertSame(IToken::DO_NOT_REMEMBER, $actual->getRemember());
+ $this->tokenProvider->getPassword($actual, $token);
+ }
+
+ public function testGenerateTokenLongPassword(): void {
+ $token = 'tokentokentokentokentoken';
+ $uid = 'user';
+ $user = 'User';
+ $password = '';
+ for ($i = 0; $i < 500; $i++) {
+ $password .= 'e';
+ }
+ $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';
+ $type = IToken::PERMANENT_TOKEN;
+ $this->config->method('getSystemValueBool')
+ ->willReturnMap([
+ ['auth.storeCryptedPassword', true, true],
+ ]);
+ $this->expectException(\RuntimeException::class);
+
+ $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
+ }
+
+ public function testGenerateTokenInvalidName(): void {
+ $token = 'tokentokentokentokentoken';
$uid = 'user';
$user = 'User';
$password = 'passme';
@@ -87,46 +159,53 @@ class PublicKeyTokenProviderTest extends TestCase {
. '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';
$type = IToken::PERMANENT_TOKEN;
+ $this->config->method('getSystemValueBool')
+ ->willReturnMap([
+ ['auth.storeCryptedPassword', true, true],
+ ]);
$actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
$this->assertInstanceOf(PublicKeyToken::class, $actual);
$this->assertSame($uid, $actual->getUID());
$this->assertSame($user, $actual->getLoginName());
- $this->assertSame($name, $actual->getName());
+ $this->assertSame('User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12User-Agent: Mozill…', $actual->getName());
$this->assertSame(IToken::DO_NOT_REMEMBER, $actual->getRemember());
$this->assertSame($password, $this->tokenProvider->getPassword($actual, $token));
}
- public function testUpdateToken() {
+ public function testUpdateToken(): void {
$tk = new PublicKeyToken();
- $tk->setLastActivity($this->time - 200);
$this->mapper->expects($this->once())
- ->method('update')
- ->with($tk);
+ ->method('updateActivity')
+ ->with($tk, $this->time);
+ $tk->setLastActivity($this->time - 200);
+ $this->config->method('getSystemValueBool')
+ ->willReturnMap([
+ ['auth.storeCryptedPassword', true, true],
+ ]);
$this->tokenProvider->updateTokenActivity($tk);
$this->assertEquals($this->time, $tk->getLastActivity());
}
- public function testUpdateTokenDebounce() {
+ public function testUpdateTokenDebounce(): void {
$tk = new PublicKeyToken();
-
$this->config->method('getSystemValueInt')
->willReturnCallback(function ($value, $default) {
return $default;
});
-
$tk->setLastActivity($this->time - 30);
+
$this->mapper->expects($this->never())
- ->method('update')
- ->with($tk);
+ ->method('updateActivity')
+ ->with($tk, $this->time);
$this->tokenProvider->updateTokenActivity($tk);
}
- public function testGetTokenByUser() {
+ public function testGetTokenByUser(): void {
$this->mapper->expects($this->once())
->method('getTokenByUser')
->with('uid')
@@ -135,16 +214,17 @@ class PublicKeyTokenProviderTest extends TestCase {
$this->assertEquals(['token'], $this->tokenProvider->getTokenByUser('uid'));
}
- public function testGetPassword() {
- $token = 'token';
+ public function testGetPassword(): void {
+ $token = 'tokentokentokentokentoken';
$uid = 'user';
$user = 'User';
$password = 'passme';
- $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';
+ $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';
$type = IToken::PERMANENT_TOKEN;
+ $this->config->method('getSystemValueBool')
+ ->willReturnMap([
+ ['auth.storeCryptedPassword', true, true],
+ ]);
$actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
@@ -152,8 +232,8 @@ class PublicKeyTokenProviderTest extends TestCase {
}
- public function testGetPasswordPasswordLessToken() {
- $this->expectException(\OC\Authentication\Exceptions\PasswordlessTokenException::class);
+ public function testGetPasswordPasswordLessToken(): void {
+ $this->expectException(PasswordlessTokenException::class);
$token = 'token1234';
$tk = new PublicKeyToken();
@@ -163,34 +243,36 @@ class PublicKeyTokenProviderTest extends TestCase {
}
- public function testGetPasswordInvalidToken() {
- $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class);
+ public function testGetPasswordInvalidToken(): void {
+ $this->expectException(InvalidTokenException::class);
- $token = 'token';
+ $token = 'tokentokentokentokentoken';
$uid = 'user';
$user = 'User';
$password = 'passme';
- $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';
+ $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';
$type = IToken::PERMANENT_TOKEN;
+ $this->config->method('getSystemValueBool')
+ ->willReturnMap([
+ ['auth.storeCryptedPassword', true, true],
+ ]);
$actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
$this->tokenProvider->getPassword($actual, 'wrongtoken');
}
- public function testSetPassword() {
- $token = 'token';
+ public function testSetPassword(): void {
+ $token = 'tokentokentokentokentoken';
$uid = 'user';
$user = 'User';
$password = 'passme';
- $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';
+ $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';
$type = IToken::PERMANENT_TOKEN;
+ $this->config->method('getSystemValueBool')
+ ->willReturnMap([
+ ['auth.storeCryptedPassword', true, true],
+ ]);
$actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
@@ -202,18 +284,18 @@ class PublicKeyTokenProviderTest extends TestCase {
$this->mapper->expects($this->once())
->method('update')
->with($this->callback(function ($token) use ($newpass) {
- return $newpass === $this->tokenProvider->getPassword($token, 'token');
+ return $newpass === $this->tokenProvider->getPassword($token, 'tokentokentokentokentoken');
}));
$this->tokenProvider->setPassword($actual, $token, $newpass);
- $this->assertSame($newpass, $this->tokenProvider->getPassword($actual, 'token'));
+ $this->assertSame($newpass, $this->tokenProvider->getPassword($actual, 'tokentokentokentokentoken'));
}
- public function testSetPasswordInvalidToken() {
- $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class);
+ public function testSetPasswordInvalidToken(): void {
+ $this->expectException(InvalidTokenException::class);
$token = $this->createMock(IToken::class);
$tokenId = 'token123';
@@ -222,123 +304,147 @@ class PublicKeyTokenProviderTest extends TestCase {
$this->tokenProvider->setPassword($token, $tokenId, $password);
}
- public function testInvalidateToken() {
- $this->mapper->expects($this->once())
+ public function testInvalidateToken(): void {
+ $calls = [
+ [hash('sha512', 'token7' . '1f4h9s')],
+ [hash('sha512', 'token7')]
+ ];
+
+ $this->mapper->expects($this->exactly(2))
->method('invalidate')
- ->with(hash('sha512', 'token7'.'1f4h9s'));
+ ->willReturnCallback(function () use (&$calls): void {
+ $expected = array_shift($calls);
+ $this->assertEquals($expected, func_get_args());
+ });
$this->tokenProvider->invalidateToken('token7');
}
- public function testInvaildateTokenById() {
+ public function testInvalidateTokenById(): void {
$id = 123;
$this->mapper->expects($this->once())
- ->method('deleteById')
- ->with('uid', $id);
+ ->method('getTokenById')
+ ->with($id);
$this->tokenProvider->invalidateTokenById('uid', $id);
}
- public function testInvalidateOldTokens() {
+ public function testInvalidateOldTokens(): void {
$defaultSessionLifetime = 60 * 60 * 24;
$defaultRememberMeLifetime = 60 * 60 * 24 * 15;
- $this->config->expects($this->exactly(2))
- ->method('getSystemValue')
+ $wipeTokenLifetime = 60 * 60 * 24 * 60;
+ $this->config->expects($this->exactly(4))
+ ->method('getSystemValueInt')
->willReturnMap([
['session_lifetime', $defaultSessionLifetime, 150],
['remember_login_cookie_lifetime', $defaultRememberMeLifetime, 300],
+ ['token_auth_wipe_token_retention', $wipeTokenLifetime, 500],
+ ['token_auth_token_retention', 60 * 60 * 24 * 365, 800],
]);
- $this->mapper->expects($this->at(0))
- ->method('invalidateOld')
- ->with($this->time - 150);
- $this->mapper->expects($this->at(1))
+
+ $calls = [
+ [$this->time - 150, IToken::TEMPORARY_TOKEN, IToken::DO_NOT_REMEMBER],
+ [$this->time - 300, IToken::TEMPORARY_TOKEN, IToken::REMEMBER],
+ [$this->time - 500, IToken::WIPE_TOKEN, null],
+ [$this->time - 800, IToken::PERMANENT_TOKEN, null],
+ ];
+ $this->mapper->expects($this->exactly(4))
->method('invalidateOld')
- ->with($this->time - 300);
+ ->willReturnCallback(function () use (&$calls): void {
+ $expected = array_shift($calls);
+ $this->assertEquals($expected, func_get_args());
+ });
$this->tokenProvider->invalidateOldTokens();
}
- public function testRenewSessionTokenWithoutPassword() {
- $token = 'oldId';
+ public function testInvalidateLastUsedBefore(): void {
+ $this->mapper->expects($this->once())
+ ->method('invalidateLastUsedBefore')
+ ->with('user', 946684800);
+
+ $this->tokenProvider->invalidateLastUsedBefore('user', 946684800);
+ }
+
+ public function testRenewSessionTokenWithoutPassword(): void {
+ $token = 'oldIdtokentokentokentoken';
$uid = 'user';
$user = 'User';
$password = null;
- $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';
+ $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';
$type = IToken::PERMANENT_TOKEN;
$oldToken = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
$this->mapper
- ->expects($this->at(0))
+ ->expects($this->once())
->method('getToken')
- ->with(hash('sha512', 'oldId' . '1f4h9s'))
+ ->with(hash('sha512', 'oldIdtokentokentokentoken' . '1f4h9s'))
->willReturn($oldToken);
$this->mapper
- ->expects($this->at(1))
+ ->expects($this->once())
->method('insert')
->with($this->callback(function (PublicKeyToken $token) use ($user, $uid, $name) {
- return $token->getUID() === $uid &&
- $token->getLoginName() === $user &&
- $token->getName() === $name &&
- $token->getType() === IToken::DO_NOT_REMEMBER &&
- $token->getLastActivity() === $this->time &&
- $token->getPassword() === null;
+ return $token->getUID() === $uid
+ && $token->getLoginName() === $user
+ && $token->getName() === $name
+ && $token->getType() === IToken::DO_NOT_REMEMBER
+ && $token->getLastActivity() === $this->time
+ && $token->getPassword() === null;
}));
$this->mapper
- ->expects($this->at(2))
+ ->expects($this->once())
->method('delete')
->with($this->callback(function ($token) use ($oldToken) {
return $token === $oldToken;
}));
- $this->tokenProvider->renewSessionToken('oldId', 'newId');
+ $this->tokenProvider->renewSessionToken('oldIdtokentokentokentoken', 'newIdtokentokentokentoken');
}
- public function testRenewSessionTokenWithPassword() {
- $token = 'oldId';
+ public function testRenewSessionTokenWithPassword(): void {
+ $token = 'oldIdtokentokentokentoken';
$uid = 'user';
$user = 'User';
$password = 'password';
- $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';
+ $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';
$type = IToken::PERMANENT_TOKEN;
+ $this->config->method('getSystemValueBool')
+ ->willReturnMap([
+ ['auth.storeCryptedPassword', true, true],
+ ]);
$oldToken = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
$this->mapper
- ->expects($this->at(0))
+ ->expects($this->once())
->method('getToken')
- ->with(hash('sha512', 'oldId' . '1f4h9s'))
+ ->with(hash('sha512', 'oldIdtokentokentokentoken' . '1f4h9s'))
->willReturn($oldToken);
$this->mapper
- ->expects($this->at(1))
+ ->expects($this->once())
->method('insert')
- ->with($this->callback(function (PublicKeyToken $token) use ($user, $uid, $name) {
- return $token->getUID() === $uid &&
- $token->getLoginName() === $user &&
- $token->getName() === $name &&
- $token->getType() === IToken::DO_NOT_REMEMBER &&
- $token->getLastActivity() === $this->time &&
- $token->getPassword() !== null &&
- $this->tokenProvider->getPassword($token, 'newId') === 'password';
+ ->with($this->callback(function (PublicKeyToken $token) use ($user, $uid, $name): bool {
+ return $token->getUID() === $uid
+ && $token->getLoginName() === $user
+ && $token->getName() === $name
+ && $token->getType() === IToken::DO_NOT_REMEMBER
+ && $token->getLastActivity() === $this->time
+ && $token->getPassword() !== null
+ && $this->tokenProvider->getPassword($token, 'newIdtokentokentokentoken') === 'password';
}));
$this->mapper
- ->expects($this->at(2))
+ ->expects($this->once())
->method('delete')
- ->with($this->callback(function ($token) use ($oldToken) {
+ ->with($this->callback(function ($token) use ($oldToken): bool {
return $token === $oldToken;
}));
- $this->tokenProvider->renewSessionToken('oldId', 'newId');
+ $this->tokenProvider->renewSessionToken('oldIdtokentokentokentoken', 'newIdtokentokentokentoken');
}
- public function testGetToken() {
+ public function testGetToken(): void {
$token = new PublicKeyToken();
$this->config->method('getSystemValue')
@@ -348,35 +454,37 @@ class PublicKeyTokenProviderTest extends TestCase {
$this->mapper->method('getToken')
->with(
$this->callback(function (string $token) {
- return hash('sha512', 'unhashedToken'.'1f4h9s') === $token;
+ return hash('sha512', 'unhashedTokentokentokentokentoken' . '1f4h9s') === $token;
})
)->willReturn($token);
- $this->assertSame($token, $this->tokenProvider->getToken('unhashedToken'));
+ $this->assertSame($token, $this->tokenProvider->getToken('unhashedTokentokentokentokentoken'));
}
- public function testGetInvalidToken() {
+ public function testGetInvalidToken(): void {
$this->expectException(InvalidTokenException::class);
- $this->mapper->method('getToken')
- ->with(
- $this->callback(function (string $token) {
- return hash('sha512', 'unhashedToken'.'1f4h9s') === $token;
- })
- )->willThrowException(new DoesNotExistException('nope'));
+ $calls = [
+ 'unhashedTokentokentokentokentoken' . '1f4h9s',
+ 'unhashedTokentokentokentokentoken',
+ ];
+ $this->mapper->expects($this->exactly(2))
+ ->method('getToken')
+ ->willReturnCallback(function (string $token) use (&$calls): void {
+ $expected = array_shift($calls);
+ $this->assertEquals(hash('sha512', $expected), $token);
+ throw new DoesNotExistException('nope');
+ });
- $this->tokenProvider->getToken('unhashedToken');
+ $this->tokenProvider->getToken('unhashedTokentokentokentokentoken');
}
- public function testGetExpiredToken() {
- $token = 'token';
+ public function testGetExpiredToken(): void {
+ $token = 'tokentokentokentokentoken';
$uid = 'user';
$user = 'User';
$password = 'passme';
- $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';
+ $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';
$type = IToken::PERMANENT_TOKEN;
$actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
@@ -385,19 +493,19 @@ class PublicKeyTokenProviderTest extends TestCase {
$this->mapper->method('getToken')
->with(
$this->callback(function (string $token) {
- return hash('sha512', 'token'.'1f4h9s') === $token;
+ return hash('sha512', 'tokentokentokentokentoken' . '1f4h9s') === $token;
})
)->willReturn($actual);
try {
- $this->tokenProvider->getToken('token');
+ $this->tokenProvider->getToken('tokentokentokentokentoken');
$this->fail();
} catch (ExpiredTokenException $e) {
$this->assertSame($actual, $e->getToken());
}
}
- public function testGetTokenById() {
+ public function testGetTokenById(): void {
$token = $this->createMock(PublicKeyToken::class);
$this->mapper->expects($this->once())
@@ -408,7 +516,7 @@ class PublicKeyTokenProviderTest extends TestCase {
$this->assertSame($token, $this->tokenProvider->getTokenById(42));
}
- public function testGetInvalidTokenById() {
+ public function testGetInvalidTokenById(): void {
$this->expectException(InvalidTokenException::class);
$this->mapper->expects($this->once())
@@ -419,7 +527,7 @@ class PublicKeyTokenProviderTest extends TestCase {
$this->tokenProvider->getTokenById(42);
}
- public function testGetExpiredTokenById() {
+ public function testGetExpiredTokenById(): void {
$token = new PublicKeyToken();
$token->setExpires(42);
@@ -436,40 +544,38 @@ class PublicKeyTokenProviderTest extends TestCase {
}
}
- public function testRotate() {
- $token = 'oldtoken';
+ public function testRotate(): void {
+ $token = 'oldtokentokentokentokentoken';
$uid = 'user';
$user = 'User';
$password = 'password';
- $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';
+ $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';
$type = IToken::PERMANENT_TOKEN;
+ $this->config->method('getSystemValueBool')
+ ->willReturnMap([
+ ['auth.storeCryptedPassword', true, true],
+ ]);
$actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
- $new = $this->tokenProvider->rotate($actual, 'oldtoken', 'newtoken');
+ $new = $this->tokenProvider->rotate($actual, 'oldtokentokentokentokentoken', 'newtokentokentokentokentoken');
- $this->assertSame('password', $this->tokenProvider->getPassword($new, 'newtoken'));
+ $this->assertSame('password', $this->tokenProvider->getPassword($new, 'newtokentokentokentokentoken'));
}
- public function testRotateNoPassword() {
- $token = 'oldtoken';
+ public function testRotateNoPassword(): void {
+ $token = 'oldtokentokentokentokentoken';
$uid = 'user';
$user = 'User';
$password = null;
- $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';
+ $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';
$type = IToken::PERMANENT_TOKEN;
$actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
$oldPrivate = $actual->getPrivateKey();
- $new = $this->tokenProvider->rotate($actual, 'oldtoken', 'newtoken');
+ $new = $this->tokenProvider->rotate($actual, 'oldtokentokentokentokentoken', 'newtokentokentokentokentoken');
$newPrivate = $new->getPrivateKey();
@@ -477,46 +583,15 @@ class PublicKeyTokenProviderTest extends TestCase {
$this->assertNull($new->getPassword());
}
- public function testConvertToken() {
- $defaultToken = new DefaultToken();
- $defaultToken->setId(42);
- $defaultToken->setPassword('oldPass');
- $defaultToken->setExpires(1337);
- $defaultToken->setToken('oldToken');
- $defaultToken->setUid('uid');
- $defaultToken->setLoginName('loginName');
- $defaultToken->setLastActivity(999);
- $defaultToken->setName('name');
- $defaultToken->setRemember(IToken::REMEMBER);
- $defaultToken->setType(IToken::PERMANENT_TOKEN);
-
- $this->mapper->expects($this->once())
- ->method('update')
- ->willReturnArgument(0);
-
- $newToken = $this->tokenProvider->convertToken($defaultToken, 'newToken', 'newPassword');
-
- $this->assertSame(42, $newToken->getId());
- $this->assertSame('newPassword', $this->tokenProvider->getPassword($newToken, 'newToken'));
- $this->assertSame(1337, $newToken->getExpires());
- $this->assertSame('uid', $newToken->getUID());
- $this->assertSame('loginName', $newToken->getLoginName());
- $this->assertSame(1313131, $newToken->getLastActivity());
- $this->assertSame(1313131, $newToken->getLastCheck());
- $this->assertSame('name', $newToken->getName());
- $this->assertSame(IToken::REMEMBER, $newToken->getRemember());
- $this->assertSame(IToken::PERMANENT_TOKEN, $newToken->getType());
- }
-
- public function testMarkPasswordInvalidInvalidToken() {
- $token = $this->createMock(DefaultToken::class);
+ public function testMarkPasswordInvalidInvalidToken(): void {
+ $token = $this->createMock(IToken::class);
$this->expectException(InvalidTokenException::class);
$this->tokenProvider->markPasswordInvalid($token, 'tokenId');
}
- public function testMarkPasswordInvalid() {
+ public function testMarkPasswordInvalid(): void {
$token = $this->createMock(PublicKeyToken::class);
$token->expects($this->once())
@@ -529,10 +604,10 @@ class PublicKeyTokenProviderTest extends TestCase {
$this->tokenProvider->markPasswordInvalid($token, 'tokenId');
}
- public function testUpdatePasswords() {
+ public function testUpdatePasswords(): void {
$uid = 'myUID';
$token1 = $this->tokenProvider->generateToken(
- 'foo',
+ 'foobetokentokentokentoken',
$uid,
$uid,
'bar',
@@ -540,13 +615,17 @@ class PublicKeyTokenProviderTest extends TestCase {
IToken::PERMANENT_TOKEN,
IToken::REMEMBER);
$token2 = $this->tokenProvider->generateToken(
- 'foobar',
+ 'foobartokentokentokentoken',
$uid,
$uid,
'bar',
'random2',
IToken::PERMANENT_TOKEN,
IToken::REMEMBER);
+ $this->config->method('getSystemValueBool')
+ ->willReturnMap([
+ ['auth.storeCryptedPassword', true, true],
+ ]);
$this->mapper->method('hasExpiredTokens')
->with($uid)
diff --git a/tests/lib/Authentication/Token/PublicKeyTokenTest.php b/tests/lib/Authentication/Token/PublicKeyTokenTest.php
index 35c2e0ece39..5f5f29c865f 100644
--- a/tests/lib/Authentication/Token/PublicKeyTokenTest.php
+++ b/tests/lib/Authentication/Token/PublicKeyTokenTest.php
@@ -2,43 +2,27 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * 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
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Authentication\Token;
use OC\Authentication\Token\PublicKeyToken;
+use OCP\Authentication\Token\IToken;
use Test\TestCase;
class PublicKeyTokenTest extends TestCase {
- public function testSetScopeAsArray() {
- $scope = ['filesystem' => false];
+ public function testSetScopeAsArray(): void {
+ $scope = [IToken::SCOPE_FILESYSTEM => false];
$token = new PublicKeyToken();
$token->setScope($scope);
$this->assertEquals(json_encode($scope), $token->getScope());
$this->assertEquals($scope, $token->getScopeAsArray());
}
- public function testDefaultScope() {
- $scope = ['filesystem' => true];
+ public function testDefaultScope(): void {
+ $scope = [IToken::SCOPE_FILESYSTEM => true];
$token = new PublicKeyToken();
$this->assertEquals($scope, $token->getScopeAsArray());
}
diff --git a/tests/lib/Authentication/Token/RemoteWipeTest.php b/tests/lib/Authentication/Token/RemoteWipeTest.php
index e2506562dd4..ca09767c759 100644
--- a/tests/lib/Authentication/Token/RemoteWipeTest.php
+++ b/tests/lib/Authentication/Token/RemoteWipeTest.php
@@ -3,24 +3,8 @@
declare(strict_types=1);
/**
- * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
- *
- * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * 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
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Authentication\Token;
@@ -40,7 +24,6 @@ use Psr\Log\LoggerInterface;
use Test\TestCase;
class RemoteWipeTest extends TestCase {
-
/** @var ITokenProvider|MockObject */
private $tokenProvider;
@@ -123,7 +106,7 @@ class RemoteWipeTest extends TestCase {
$this->assertTrue($result);
}
- public function testStartWipingNotAWipeToken() {
+ public function testStartWipingNotAWipeToken(): void {
$token = $this->createMock(IToken::class);
$this->tokenProvider->expects($this->once())
->method('getToken')
@@ -137,7 +120,7 @@ class RemoteWipeTest extends TestCase {
$this->assertFalse($result);
}
- public function testStartWiping() {
+ public function testStartWiping(): void {
$token = $this->createMock(IToken::class);
$this->tokenProvider->expects($this->once())
->method('getToken')
@@ -154,7 +137,7 @@ class RemoteWipeTest extends TestCase {
$this->assertTrue($result);
}
- public function testFinishWipingNotAWipeToken() {
+ public function testFinishWipingNotAWipeToken(): void {
$token = $this->createMock(IToken::class);
$this->tokenProvider->expects($this->once())
->method('getToken')