mirror of
https://github.com/nextcloud/server.git
synced 2026-12-05 00:18:12 +01:00
The token passed to rotate() can come from the token cache, which may still hold a token that was deleted or changed in the meantime. Rotate the stored row instead and stop serving the old hash from the cache. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Peter Ringelmann <peter.ringelmann@nextcloud.com>
867 lines
28 KiB
PHP
867 lines
28 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* 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\Exceptions\PasswordlessTokenException;
|
|
use OC\Authentication\Exceptions\WipeTokenException;
|
|
use OC\Authentication\Token\PublicKeyToken;
|
|
use OC\Authentication\Token\PublicKeyTokenMapper;
|
|
use OC\Authentication\Token\PublicKeyTokenProvider;
|
|
use OC\Memcache\ArrayCache;
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
use OCP\Authentication\Token\IToken;
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
|
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 {
|
|
private PublicKeyTokenProvider $tokenProvider;
|
|
|
|
private PublicKeyTokenMapper&MockObject $mapper;
|
|
private IConfig&MockObject $config;
|
|
private IDBConnection&MockObject $db;
|
|
private LoggerInterface&MockObject $logger;
|
|
private ITimeFactory&MockObject $timeFactory;
|
|
private ICacheFactory&MockObject $cacheFactory;
|
|
|
|
private int $time;
|
|
private IHasher $hasher;
|
|
private ICrypto $crypto;
|
|
private IEventDispatcher $eventDispatcher;
|
|
|
|
#[\Override]
|
|
protected function setUp(): void {
|
|
parent::setUp();
|
|
|
|
$this->mapper = $this->createMock(PublicKeyTokenMapper::class);
|
|
$this->config = $this->createMock(IConfig::class);
|
|
$this->config->method('getSystemValue')
|
|
->willReturnMap([
|
|
['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->hasher = Server::get(IHasher::class);
|
|
$this->crypto = Server::get(ICrypto::class);
|
|
$this->eventDispatcher = Server::get(IEventDispatcher::class);
|
|
|
|
$this->tokenProvider = new PublicKeyTokenProvider(
|
|
$this->mapper,
|
|
$this->crypto,
|
|
$this->config,
|
|
$this->db,
|
|
$this->logger,
|
|
$this->timeFactory,
|
|
$this->hasher,
|
|
$this->cacheFactory,
|
|
$this->eventDispatcher,
|
|
);
|
|
}
|
|
|
|
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->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 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';
|
|
$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;
|
|
$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('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(): void {
|
|
$tk = new PublicKeyToken();
|
|
$this->mapper->expects($this->once())
|
|
->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(): 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('updateActivity')
|
|
->with($tk, $this->time);
|
|
|
|
$this->tokenProvider->updateTokenActivity($tk);
|
|
}
|
|
|
|
public function testGetTokenByUser(): void {
|
|
$this->mapper->expects($this->once())
|
|
->method('getTokenByUser')
|
|
->with('uid')
|
|
->willReturn(['token']);
|
|
|
|
$this->assertEquals(['token'], $this->tokenProvider->getTokenByUser('uid'));
|
|
}
|
|
|
|
public function testGetAllTokensByUser(): void {
|
|
$tokens = (fn () => yield new PublicKeyToken())();
|
|
$this->mapper->expects($this->once())
|
|
->method('getAllTokensByUser')
|
|
->with('uid')
|
|
->willReturn($tokens);
|
|
|
|
$this->assertSame($tokens, $this->tokenProvider->getAllTokensByUser('uid'));
|
|
}
|
|
|
|
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';
|
|
$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->assertSame($password, $this->tokenProvider->getPassword($actual, $token));
|
|
}
|
|
|
|
public function testGetPasswordPasswordLessToken(): void {
|
|
$this->expectException(PasswordlessTokenException::class);
|
|
|
|
$token = 'token1234';
|
|
$tk = new PublicKeyToken();
|
|
$tk->setPassword(null);
|
|
|
|
$this->tokenProvider->getPassword($tk, $token);
|
|
}
|
|
|
|
public function testGetPasswordInvalidToken(): void {
|
|
$this->expectException(InvalidTokenException::class);
|
|
|
|
$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->getPassword($actual, 'wrongtoken');
|
|
}
|
|
|
|
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';
|
|
$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->mapper->method('getTokenByUser')
|
|
->with('user')
|
|
->willReturn([$actual]);
|
|
|
|
$newpass = 'newpass';
|
|
$this->mapper->expects($this->once())
|
|
->method('update')
|
|
->with($this->callback(function ($token) use ($newpass) {
|
|
return $newpass === $this->tokenProvider->getPassword($token, 'tokentokentokentokentoken');
|
|
}));
|
|
|
|
$this->tokenProvider->setPassword($actual, $token, $newpass);
|
|
|
|
$this->assertSame($newpass, $this->tokenProvider->getPassword($actual, 'tokentokentokentokentoken'));
|
|
}
|
|
|
|
public function testSetPasswordInvalidToken(): void {
|
|
$this->expectException(InvalidTokenException::class);
|
|
|
|
$token = $this->createMock(IToken::class);
|
|
$tokenId = 'token123';
|
|
$password = '123456';
|
|
|
|
$this->tokenProvider->setPassword($token, $tokenId, $password);
|
|
}
|
|
|
|
public function testInvalidateToken(): void {
|
|
$calls = [
|
|
[hash('sha512', 'token7' . '1f4h9s')],
|
|
[hash('sha512', 'token7')]
|
|
];
|
|
|
|
$this->mapper->expects($this->exactly(2))
|
|
->method('invalidate')
|
|
->willReturnCallback(function () use (&$calls): void {
|
|
$expected = array_shift($calls);
|
|
$this->assertEquals($expected, func_get_args());
|
|
});
|
|
|
|
$this->tokenProvider->invalidateToken('token7');
|
|
}
|
|
|
|
public function testInvalidateTokenById(): void {
|
|
$id = 123;
|
|
|
|
$this->mapper->expects($this->once())
|
|
->method('getTokenById')
|
|
->with($id);
|
|
|
|
$this->tokenProvider->invalidateTokenById('uid', $id);
|
|
}
|
|
|
|
public function testInvalidateOldTokens(): void {
|
|
$defaultSessionLifetime = 60 * 60 * 24;
|
|
$defaultRememberMeLifetime = 60 * 60 * 24 * 15;
|
|
$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],
|
|
]);
|
|
|
|
$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')
|
|
->willReturnCallback(function () use (&$calls): void {
|
|
$expected = array_shift($calls);
|
|
$this->assertEquals($expected, func_get_args());
|
|
});
|
|
|
|
$this->tokenProvider->invalidateOldTokens();
|
|
}
|
|
|
|
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';
|
|
$type = IToken::PERMANENT_TOKEN;
|
|
|
|
$oldToken = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
|
|
$oldToken->setId(1);
|
|
|
|
$this->config->method('getUserKeys')
|
|
->with($uid, 'login_token_2fa')
|
|
->willReturn([]);
|
|
$this->config->expects($this->never())
|
|
->method('setUserValue');
|
|
$this->mapper
|
|
->expects($this->once())
|
|
->method('getToken')
|
|
->with(hash('sha512', 'oldIdtokentokentokentoken' . '1f4h9s'))
|
|
->willReturn($oldToken);
|
|
$this->mapper
|
|
->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->mapper
|
|
->expects($this->once())
|
|
->method('delete')
|
|
->with($this->callback(function ($token) use ($oldToken) {
|
|
return $token === $oldToken;
|
|
}));
|
|
|
|
$this->tokenProvider->renewSessionToken('oldIdtokentokentokentoken', 'newIdtokentokentokentoken');
|
|
}
|
|
|
|
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';
|
|
$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);
|
|
$oldToken->setId(1);
|
|
|
|
$this->config->method('getUserKeys')
|
|
->with($uid, 'login_token_2fa')
|
|
->willReturn([]);
|
|
$this->config->expects($this->never())
|
|
->method('setUserValue');
|
|
$this->mapper
|
|
->expects($this->once())
|
|
->method('getToken')
|
|
->with(hash('sha512', 'oldIdtokentokentokentoken' . '1f4h9s'))
|
|
->willReturn($oldToken);
|
|
$this->mapper
|
|
->expects($this->once())
|
|
->method('insert')
|
|
->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->once())
|
|
->method('delete')
|
|
->with($this->callback(function ($token) use ($oldToken): bool {
|
|
return $token === $oldToken;
|
|
}));
|
|
|
|
$this->tokenProvider->renewSessionToken('oldIdtokentokentokentoken', 'newIdtokentokentokentoken');
|
|
}
|
|
|
|
public function testRenewSessionTokenKeepsPendingSecondFactor(): void {
|
|
$uid = 'user';
|
|
|
|
$oldToken = $this->tokenProvider->generateToken('oldIdtokentokentokentoken', $uid, 'User', null, 'name');
|
|
$oldToken->setId(1);
|
|
|
|
$this->mapper
|
|
->expects($this->once())
|
|
->method('getToken')
|
|
->with(hash('sha512', 'oldIdtokentokentokentoken' . '1f4h9s'))
|
|
->willReturn($oldToken);
|
|
$this->mapper
|
|
->expects($this->once())
|
|
->method('insert')
|
|
->willReturnCallback(function (PublicKeyToken $token): PublicKeyToken {
|
|
$token->setId(2);
|
|
return $token;
|
|
});
|
|
$this->mapper
|
|
->expects($this->once())
|
|
->method('delete')
|
|
->with($oldToken);
|
|
$this->config->method('getUserKeys')
|
|
->with($uid, 'login_token_2fa')
|
|
->willReturn(['1', '7']);
|
|
$this->config->method('getUserValue')
|
|
->with($uid, 'login_token_2fa', '1')
|
|
->willReturn('1234');
|
|
$this->config->expects($this->once())
|
|
->method('setUserValue')
|
|
->with($uid, 'login_token_2fa', '2', '1234');
|
|
$this->config->expects($this->once())
|
|
->method('deleteUserValue')
|
|
->with($uid, 'login_token_2fa', '1');
|
|
|
|
$newToken = $this->tokenProvider->renewSessionToken('oldIdtokentokentokentoken', 'newIdtokentokentokentoken');
|
|
|
|
$this->assertSame(2, $newToken->getId());
|
|
}
|
|
|
|
public function testGetToken(): void {
|
|
$token = new PublicKeyToken();
|
|
$token->setType(IToken::TEMPORARY_TOKEN);
|
|
|
|
$this->config->method('getSystemValue')
|
|
->with('secret')
|
|
->willReturn('mysecret');
|
|
|
|
$this->mapper->method('getToken')
|
|
->with(
|
|
$this->callback(function (string $token) {
|
|
return hash('sha512', 'unhashedTokentokentokentokentoken' . '1f4h9s') === $token;
|
|
})
|
|
)->willReturn($token);
|
|
|
|
$this->assertSame($token, $this->tokenProvider->getToken('unhashedTokentokentokentokentoken'));
|
|
}
|
|
|
|
public function testGetInvalidToken(): void {
|
|
$this->expectException(InvalidTokenException::class);
|
|
|
|
$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('unhashedTokentokentokentokentoken');
|
|
}
|
|
|
|
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';
|
|
$type = IToken::PERMANENT_TOKEN;
|
|
|
|
$actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
|
|
$actual->setExpires(42);
|
|
|
|
$this->mapper->method('getToken')
|
|
->with(
|
|
$this->callback(function (string $token) {
|
|
return hash('sha512', 'tokentokentokentokentoken' . '1f4h9s') === $token;
|
|
})
|
|
)->willReturn($actual);
|
|
|
|
try {
|
|
$this->tokenProvider->getToken('tokentokentokentokentoken');
|
|
$this->fail();
|
|
} catch (ExpiredTokenException $e) {
|
|
$this->assertSame($actual, $e->getToken());
|
|
}
|
|
}
|
|
|
|
public function testGetTokenById(): void {
|
|
$token = $this->createMock(PublicKeyToken::class);
|
|
|
|
$this->mapper->expects($this->once())
|
|
->method('getTokenById')
|
|
->with($this->equalTo(42))
|
|
->willReturn($token);
|
|
|
|
$this->assertSame($token, $this->tokenProvider->getTokenById(42));
|
|
}
|
|
|
|
public function testGetInvalidTokenById(): void {
|
|
$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(): void {
|
|
$token = new PublicKeyToken();
|
|
$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(): 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';
|
|
$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->mapper->method('getToken')
|
|
->with($actual->getToken())
|
|
->willReturn($actual);
|
|
|
|
$new = $this->tokenProvider->rotate($actual, 'oldtokentokentokentokentoken', 'newtokentokentokentokentoken');
|
|
|
|
$this->assertSame('password', $this->tokenProvider->getPassword($new, 'newtokentokentokentokentoken'));
|
|
}
|
|
|
|
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';
|
|
$type = IToken::PERMANENT_TOKEN;
|
|
|
|
$actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
|
|
$this->mapper->method('getToken')
|
|
->with($actual->getToken())
|
|
->willReturn($actual);
|
|
|
|
$oldPrivate = $actual->getPrivateKey();
|
|
|
|
$new = $this->tokenProvider->rotate($actual, 'oldtokentokentokentokentoken', 'newtokentokentokentokentoken');
|
|
|
|
$newPrivate = $new->getPrivateKey();
|
|
|
|
$this->assertNotSame($newPrivate, $oldPrivate);
|
|
$this->assertNull($new->getPassword());
|
|
}
|
|
|
|
public function testRotateInvalidatesOldTokenInCache(): void {
|
|
$oldToken = 'oldtokentokentokentokentoken';
|
|
$newToken = 'newtokentokentokentokentoken';
|
|
|
|
$this->cacheFactory->method('createInMemory')
|
|
->willReturn(new ArrayCache());
|
|
$tokenProvider = new PublicKeyTokenProvider(
|
|
$this->mapper,
|
|
$this->crypto,
|
|
$this->config,
|
|
$this->db,
|
|
$this->logger,
|
|
$this->timeFactory,
|
|
$this->hasher,
|
|
$this->cacheFactory,
|
|
$this->eventDispatcher,
|
|
);
|
|
|
|
$actual = $tokenProvider->generateToken($oldToken, 'user', 'User', null, 'name', IToken::PERMANENT_TOKEN);
|
|
$oldHash = $actual->getToken();
|
|
$this->assertSame($oldHash, $tokenProvider->getToken($oldToken)->getToken());
|
|
|
|
// Only the cache knows the token after the rotation
|
|
$stored = true;
|
|
$this->mapper->method('getToken')
|
|
->willReturnCallback(function (string $hash) use (&$stored, $oldHash, $actual) {
|
|
if ($stored && $hash === $oldHash) {
|
|
return $actual;
|
|
}
|
|
throw new DoesNotExistException('');
|
|
});
|
|
|
|
$tokenProvider->rotate($actual, $oldToken, $newToken);
|
|
$stored = false;
|
|
|
|
$this->assertSame($actual->getToken(), $tokenProvider->getToken($newToken)->getToken());
|
|
|
|
$this->expectException(InvalidTokenException::class);
|
|
$tokenProvider->getToken($oldToken);
|
|
}
|
|
|
|
public function testRotateStoredToken(): void {
|
|
$oldToken = 'oldtokentokentokentokentoken';
|
|
$newToken = 'newtokentokentokentokentoken';
|
|
|
|
// What the caller has, e.g. from the cache, is older than what is stored
|
|
$outdated = $this->tokenProvider->generateToken($oldToken, 'user', 'User', null, 'name', IToken::PERMANENT_TOKEN);
|
|
$stored = clone $outdated;
|
|
$stored->setScope([IToken::SCOPE_FILESYSTEM => false]);
|
|
|
|
$this->mapper->expects($this->once())
|
|
->method('getToken')
|
|
->with($outdated->getToken())
|
|
->willReturn($stored);
|
|
$this->mapper->expects($this->once())
|
|
->method('update')
|
|
->with($stored);
|
|
|
|
$new = $this->tokenProvider->rotate($outdated, $oldToken, $newToken);
|
|
|
|
$this->assertSame($stored, $new);
|
|
$this->assertSame([IToken::SCOPE_FILESYSTEM => false], $new->getScopeAsArray());
|
|
}
|
|
|
|
public function testRotateDeletedToken(): void {
|
|
$oldToken = 'oldtokentokentokentokentoken';
|
|
$newToken = 'newtokentokentokentokentoken';
|
|
|
|
$actual = $this->tokenProvider->generateToken($oldToken, 'user', 'User', null, 'name', IToken::PERMANENT_TOKEN);
|
|
|
|
$this->mapper->expects($this->once())
|
|
->method('getToken')
|
|
->with($actual->getToken())
|
|
->willThrowException(new DoesNotExistException(''));
|
|
$this->mapper->expects($this->never())
|
|
->method('update');
|
|
|
|
$this->expectException(InvalidTokenException::class);
|
|
$this->tokenProvider->rotate($actual, $oldToken, $newToken);
|
|
}
|
|
|
|
public function testRotateTokenMarkedForWipe(): void {
|
|
$oldToken = 'oldtokentokentokentokentoken';
|
|
$newToken = 'newtokentokentokentokentoken';
|
|
|
|
$actual = $this->tokenProvider->generateToken($oldToken, 'user', 'User', null, 'name', IToken::PERMANENT_TOKEN);
|
|
|
|
$stored = clone $actual;
|
|
$stored->setType(IToken::WIPE_TOKEN);
|
|
$this->mapper->expects($this->once())
|
|
->method('getToken')
|
|
->with($actual->getToken())
|
|
->willReturn($stored);
|
|
$this->mapper->expects($this->never())
|
|
->method('update');
|
|
|
|
$this->expectException(WipeTokenException::class);
|
|
$this->tokenProvider->rotate($actual, $oldToken, $newToken);
|
|
}
|
|
|
|
public function testMarkPasswordInvalidInvalidToken(): void {
|
|
$token = $this->createMock(IToken::class);
|
|
|
|
$this->expectException(InvalidTokenException::class);
|
|
|
|
$this->tokenProvider->markPasswordInvalid($token, 'tokenId');
|
|
}
|
|
|
|
public function testMarkPasswordInvalid(): void {
|
|
$token = $this->createMock(PublicKeyToken::class);
|
|
|
|
$token->expects($this->once())
|
|
->method('setPasswordInvalid')
|
|
->with(true);
|
|
$this->mapper->expects($this->once())
|
|
->method('update')
|
|
->with($token);
|
|
|
|
$this->tokenProvider->markPasswordInvalid($token, 'tokenId');
|
|
}
|
|
|
|
public function testUpdatePasswords(): void {
|
|
$uid = 'myUID';
|
|
$token1 = $this->tokenProvider->generateToken(
|
|
'foobetokentokentokentoken',
|
|
$uid,
|
|
$uid,
|
|
'bar',
|
|
'random1',
|
|
IToken::PERMANENT_TOKEN,
|
|
IToken::REMEMBER);
|
|
$token2 = $this->tokenProvider->generateToken(
|
|
'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)
|
|
->willReturn(true);
|
|
$this->mapper->expects($this->once())
|
|
->method('getTokenByUser')
|
|
->with($uid)
|
|
->willReturn([$token1, $token2]);
|
|
$this->mapper->expects($this->exactly(2))
|
|
->method('update')
|
|
->with($this->callback(function (PublicKeyToken $t) use ($token1, $token2) {
|
|
return $t === $token1 || $t === $token2;
|
|
}));
|
|
|
|
$this->tokenProvider->updatePasswords($uid, 'bar2');
|
|
}
|
|
|
|
public function testUpdatePasswordsInvalidatesSessionsWithOutdatedPassword(): void {
|
|
$uid = 'myUID';
|
|
$this->config->method('getSystemValueBool')
|
|
->willReturnMap([
|
|
['auth.storeCryptedPassword', true, true],
|
|
]);
|
|
|
|
$generateToken = fn (string $token, ?string $password, int $type): PublicKeyToken => $this->tokenProvider->generateToken(
|
|
$token . 'tokentokentokentoken',
|
|
$uid,
|
|
$uid,
|
|
$password,
|
|
$token,
|
|
$type,
|
|
);
|
|
$currentSession1 = $generateToken('current1', 'bar2', IToken::TEMPORARY_TOKEN);
|
|
$outdatedAppPassword = $generateToken('app', 'bar', IToken::PERMANENT_TOKEN);
|
|
$outdatedSession1 = $generateToken('outdated1', 'bar', IToken::TEMPORARY_TOKEN);
|
|
$currentSession2 = $generateToken('current2', 'bar2', IToken::TEMPORARY_TOKEN);
|
|
$currentSession2->setPasswordHash($currentSession1->getPasswordHash());
|
|
$outdatedSession2 = $generateToken('outdated2', 'bar', IToken::TEMPORARY_TOKEN);
|
|
$outdatedSession2->setPasswordHash($outdatedSession1->getPasswordHash());
|
|
$passwordlessSession = $generateToken('passwordless', null, IToken::TEMPORARY_TOKEN);
|
|
|
|
$this->mapper->expects($this->once())
|
|
->method('getTokenByUser')
|
|
->with($uid)
|
|
->willReturn([
|
|
$currentSession1,
|
|
$outdatedAppPassword,
|
|
$outdatedSession1,
|
|
$currentSession2,
|
|
$outdatedSession2,
|
|
$passwordlessSession,
|
|
]);
|
|
|
|
$updated = [];
|
|
$this->mapper->method('update')
|
|
->willReturnCallback(function (PublicKeyToken $t) use (&$updated) {
|
|
$updated[] = $t;
|
|
return $t;
|
|
});
|
|
$invalidated = [];
|
|
$this->mapper->method('invalidate')
|
|
->willReturnCallback(function (string $token) use (&$invalidated): void {
|
|
$invalidated[] = $token;
|
|
});
|
|
|
|
$this->tokenProvider->updatePasswords($uid, 'bar2');
|
|
|
|
$this->assertSame([$outdatedAppPassword, $passwordlessSession], $updated);
|
|
$this->assertSame([$outdatedSession1->getToken(), $outdatedSession2->getToken()], $invalidated);
|
|
$this->assertSame('bar2', $this->tokenProvider->getPassword($outdatedAppPassword, 'apptokentokentokentoken'));
|
|
$this->assertSame('bar', $this->tokenProvider->getPassword($outdatedSession1, 'outdated1tokentokentokentoken'));
|
|
}
|
|
}
|