diff options
author | Joachim Bauch <bauch@struktur.de> | 2022-07-07 14:53:41 +0200 |
---|---|---|
committer | Joachim Bauch <bauch@struktur.de> | 2022-07-07 14:53:41 +0200 |
commit | 39715df9b0fd0128a71f7b1fd21dfc186fe40857 (patch) | |
tree | 1fb46e25cb9e9ce4083e625fe110200daf2104a1 /tests | |
parent | fdd9a0df7fab7e290d13e5257e61c2855e48ec20 (diff) |
Add support for algorithm "EdDSA" and add tests.
Signed-off-by: Joachim Bauch <bauch@struktur.de>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/php/CapabilitiesTest.php | 17 | ||||
-rw-r--r-- | tests/php/ConfigTest.php | 126 |
2 files changed, 143 insertions, 0 deletions
diff --git a/tests/php/CapabilitiesTest.php b/tests/php/CapabilitiesTest.php index 65fbe67d2..9445a48e9 100644 --- a/tests/php/CapabilitiesTest.php +++ b/tests/php/CapabilitiesTest.php @@ -315,4 +315,21 @@ class CapabilitiesTest extends TestCase { $this->assertInstanceOf(IPublicCapability::class, $capabilities); $this->assertSame([], $capabilities->getCapabilities()); } + + public function testCapabilitiesHelloV2Key(): void { + $capabilities = new Capabilities( + $this->serverConfig, + $this->talkConfig, + $this->commentsManager, + $this->userSession, + $this->appManager + ); + + $this->talkConfig->expects($this->once()) + ->method('getSignalingTokenPublicKey') + ->willReturn('this-is-the-key'); + + $data = $capabilities->getCapabilities(); + $this->assertEquals('this-is-the-key', $data['spreed']['config']['signaling']['hello-v2-token-key']); + } } diff --git a/tests/php/ConfigTest.php b/tests/php/ConfigTest.php index c98d1c7ae..569d89177 100644 --- a/tests/php/ConfigTest.php +++ b/tests/php/ConfigTest.php @@ -20,6 +20,9 @@ */ namespace OCA\Talk\Tests\php; +use Firebase\JWT\JWT; +use Firebase\JWT\Key; + use OCA\Talk\Config; use OCA\Talk\Events\GetTurnServersEvent; use OCA\Talk\Tests\php\Mocks\GetTurnServerListener; @@ -28,6 +31,7 @@ use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IGroupManager; use OCP\IURLGenerator; +use OCP\IUser; use OCP\IUserManager; use OCP\Security\ISecureRandom; use PHPUnit\Framework\MockObject\MockObject; @@ -330,4 +334,126 @@ class ConfigTest extends TestCase { self::invokePrivate($helper, 'getWebSocketDomainForSignalingServer', [$url]) ); } + + public function dataTicketV2Algorithm() { + return [ + ['ES384'], + ['ES256'], + ['RS256'], + ['RS384'], + ['RS512'], + ['EdDSA'], + ]; + } + + /** + * @dataProvider dataTicketV2Algorithm + * @param string $algo + */ + public function testSignalingTicketV2User(string $algo): void { + /** @var IConfig $config */ + $config = \OC::$server->getConfig(); + /** @var MockObject|ITimeFactory $timeFactory */ + $timeFactory = $this->createMock(ITimeFactory::class); + /** @var MockObject|ISecureRandom $secureRandom */ + $secureRandom = $this->createMock(ISecureRandom::class); + /** @var MockObject|IGroupManager $groupManager */ + $groupManager = $this->createMock(IGroupManager::class); + /** @var MockObject|IUserManager $userManager */ + $userManager = $this->createMock(IUserManager::class); + /** @var MockObject|IURLGenerator $urlGenerator */ + $urlGenerator = $this->createMock(IURLGenerator::class); + /** @var MockObject|IEventDispatcher $dispatcher */ + $dispatcher = $this->createMock(IEventDispatcher::class); + /** @var MockObject|IUser $user */ + $user = $this->createMock(IUser::class); + + $now = time(); + $timeFactory + ->expects($this->once()) + ->method('getTime') + ->willReturn($now); + $urlGenerator + ->expects($this->once()) + ->method('getAbsoluteURL') + ->with('') + ->willReturn('https://domain.invalid/nextcloud'); + $userManager + ->expects($this->once()) + ->method('get') + ->with('user1') + ->willReturn($user); + $user + ->expects($this->once()) + ->method('getUID') + ->willReturn('user1'); + $user + ->expects($this->once()) + ->method('getDisplayName') + ->willReturn('Jane Doe'); + + $helper = new Config($config, $secureRandom, $groupManager, $userManager, $urlGenerator, $timeFactory, $dispatcher); + + $config->setAppValue('spreed', 'signaling_token_alg', $algo); + // Make sure new keys are generated. + $config->deleteAppValue('spreed', 'signaling_token_privkey_' . strtolower($algo)); + $config->deleteAppValue('spreed', 'signaling_token_pubkey_' . strtolower($algo)); + $ticket = $helper->getSignalingTicket(Config::SIGNALING_TICKET_V2, 'user1'); + $this->assertNotNull($ticket); + + $key = new Key($config->getAppValue('spreed', 'signaling_token_pubkey_' . strtolower($algo)), $algo); + $decoded = JWT::decode($ticket, $key); + + $this->assertEquals($now, $decoded->iat); + $this->assertEquals('https://domain.invalid/nextcloud', $decoded->iss); + $this->assertEquals('user1', $decoded->sub); + $this->assertSame(['displayname' => 'Jane Doe'], (array) $decoded->userdata); + } + + /** + * @dataProvider dataTicketV2Algorithm + * @param string $algo + */ + public function testSignalingTicketV2Anonymous(string $algo): void { + /** @var IConfig $config */ + $config = \OC::$server->getConfig(); + /** @var MockObject|ITimeFactory $timeFactory */ + $timeFactory = $this->createMock(ITimeFactory::class); + /** @var MockObject|ISecureRandom $secureRandom */ + $secureRandom = $this->createMock(ISecureRandom::class); + /** @var MockObject|IGroupManager $groupManager */ + $groupManager = $this->createMock(IGroupManager::class); + /** @var MockObject|IUserManager $userManager */ + $userManager = $this->createMock(IUserManager::class); + /** @var MockObject|IURLGenerator $urlGenerator */ + $urlGenerator = $this->createMock(IURLGenerator::class); + /** @var MockObject|IEventDispatcher $dispatcher */ + $dispatcher = $this->createMock(IEventDispatcher::class); + + $now = time(); + $timeFactory + ->expects($this->once()) + ->method('getTime') + ->willReturn($now); + $urlGenerator + ->expects($this->once()) + ->method('getAbsoluteURL') + ->with('') + ->willReturn('https://domain.invalid/nextcloud'); + + $helper = new Config($config, $secureRandom, $groupManager, $userManager, $urlGenerator, $timeFactory, $dispatcher); + + $config->setAppValue('spreed', 'signaling_token_alg', $algo); + // Make sure new keys are generated. + $config->deleteAppValue('spreed', 'signaling_token_privkey_' . strtolower($algo)); + $config->deleteAppValue('spreed', 'signaling_token_pubkey_' . strtolower($algo)); + $ticket = $helper->getSignalingTicket(Config::SIGNALING_TICKET_V2, null); + $this->assertNotNull($ticket); + + $key = new Key($config->getAppValue('spreed', 'signaling_token_pubkey_' . strtolower($algo)), $algo); + $decoded = JWT::decode($ticket, $key); + + $this->assertEquals($now, $decoded->iat); + $this->assertEquals('https://domain.invalid/nextcloud', $decoded->iss); + } } |