diff options
-rw-r--r-- | lib/Command/Room/TRoomCommand.php | 2 | ||||
-rw-r--r-- | lib/Controller/PageController.php | 8 | ||||
-rw-r--r-- | lib/Controller/RoomController.php | 6 | ||||
-rw-r--r-- | lib/Room.php | 22 | ||||
-rw-r--r-- | lib/Service/ParticipantService.php | 10 | ||||
-rw-r--r-- | lib/Service/RoomService.php | 24 | ||||
-rw-r--r-- | tests/php/Chat/SystemMessage/ListenerTest.php | 2 | ||||
-rw-r--r-- | tests/php/Controller/SignalingControllerTest.php | 8 | ||||
-rw-r--r-- | tests/php/RoomTest.php | 94 | ||||
-rw-r--r-- | tests/php/Service/RoomServiceTest.php | 77 | ||||
-rw-r--r-- | tests/php/Signaling/BackendNotifierTest.php | 35 |
11 files changed, 151 insertions, 137 deletions
diff --git a/lib/Command/Room/TRoomCommand.php b/lib/Command/Room/TRoomCommand.php index b878fbb2e..154d32edf 100644 --- a/lib/Command/Room/TRoomCommand.php +++ b/lib/Command/Room/TRoomCommand.php @@ -175,7 +175,7 @@ trait TRoomCommand { * @throws InvalidArgumentException */ protected function setRoomPassword(Room $room, string $password): void { - if ($room->hasPassword() ? $room->verifyPassword($password)['result'] : ($password === '')) { + if ($room->hasPassword() ? $this->roomService->verifyPassword($room, $password)['result'] : ($password === '')) { return; } diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index 9b3bfa440..2ee61ccc3 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -31,6 +31,7 @@ use OCA\Talk\Config; use OCA\Talk\Manager; use OCA\Talk\Participant; use OCA\Talk\Room; +use OCA\Talk\Service\RoomService; use OCA\Talk\TalkSession; use OCA\Talk\TInitialState; use OCA\Viewer\Event\LoadViewer; @@ -66,6 +67,7 @@ class PageController extends Controller { private IUserSession $userSession; private LoggerInterface $logger; private Manager $manager; + private RoomService $roomService; private IURLGenerator $url; private INotificationManager $notificationManager; private IAppManager $appManager; @@ -80,6 +82,7 @@ class PageController extends Controller { ?string $UserId, LoggerInterface $logger, Manager $manager, + RoomService $roomService, IURLGenerator $url, INotificationManager $notificationManager, IAppManager $appManager, @@ -96,6 +99,7 @@ class PageController extends Controller { $this->userId = $UserId; $this->logger = $logger; $this->manager = $manager; + $this->roomService = $roomService; $this->url = $url; $this->notificationManager = $notificationManager; $this->appManager = $appManager; @@ -214,7 +218,7 @@ class PageController extends Controller { if ($requirePassword) { $password = $password !== '' ? $password : (string) $this->talkSession->getPasswordForRoom($token); - $passwordVerification = $room->verifyPassword($password); + $passwordVerification = $this->roomService->verifyPassword($room, $password); if ($passwordVerification['result']) { $this->talkSession->renewSessionId(); @@ -289,7 +293,7 @@ class PageController extends Controller { if ($room->hasPassword()) { $password = $password !== '' ? $password : (string) $this->talkSession->getPasswordForRoom($token); - $passwordVerification = $room->verifyPassword($password); + $passwordVerification = $this->roomService->verifyPassword($room, $password); if ($passwordVerification['result']) { $this->talkSession->renewSessionId(); $this->talkSession->setPasswordForRoom($token, $password); diff --git a/lib/Controller/RoomController.php b/lib/Controller/RoomController.php index 216f4644d..94e56b157 100644 --- a/lib/Controller/RoomController.php +++ b/lib/Controller/RoomController.php @@ -1372,12 +1372,12 @@ class RoomController extends AEnvironmentAwareController { $user = $this->userManager->get($this->userId); try { - $result = $room->verifyPassword((string) $this->session->getPasswordForRoom($token)); + $result = $this->roomService->verifyPassword($room, (string) $this->session->getPasswordForRoom($token)); if ($user instanceof IUser) { - $participant = $this->participantService->joinRoom($room, $user, $password, $result['result']); + $participant = $this->participantService->joinRoom($this->roomService, $room, $user, $password, $result['result']); $this->participantService->generatePinForParticipant($room, $participant); } else { - $participant = $this->participantService->joinRoomAsNewGuest($room, $password, $result['result'], $previousParticipant); + $participant = $this->participantService->joinRoomAsNewGuest($this->roomService, $room, $password, $result['result'], $previousParticipant); } } catch (InvalidPasswordException $e) { return new DataResponse([], Http::STATUS_FORBIDDEN); diff --git a/lib/Room.php b/lib/Room.php index a8beaf51c..923821324 100644 --- a/lib/Room.php +++ b/lib/Room.php @@ -31,7 +31,6 @@ use OCA\Talk\Events\ModifyLobbyEvent; use OCA\Talk\Events\ModifyRoomEvent; use OCA\Talk\Events\RoomEvent; use OCA\Talk\Events\SignalingRoomPropertiesEvent; -use OCA\Talk\Events\VerifyRoomPasswordEvent; use OCA\Talk\Exceptions\ParticipantNotFoundException; use OCA\Talk\Model\Attendee; use OCA\Talk\Model\SelectHelper; @@ -1018,25 +1017,4 @@ class Room { return true; } - - /** - * @param string $password - * @return array - */ - public function verifyPassword(string $password): array { - $event = new VerifyRoomPasswordEvent($this, $password); - $this->dispatcher->dispatch(self::EVENT_PASSWORD_VERIFY, $event); - - if ($event->isPasswordValid() !== null) { - return [ - 'result' => $event->isPasswordValid(), - 'url' => $event->getRedirectUrl(), - ]; - } - - return [ - 'result' => !$this->hasPassword() || $this->hasher->verify($password, $this->password), - 'url' => '', - ]; - } } diff --git a/lib/Service/ParticipantService.php b/lib/Service/ParticipantService.php index b0a829f5c..045a5f769 100644 --- a/lib/Service/ParticipantService.php +++ b/lib/Service/ParticipantService.php @@ -234,6 +234,7 @@ class ParticipantService { } /** + * @param RoomService $roomService * @param Room $room * @param IUser $user * @param string $password @@ -242,7 +243,7 @@ class ParticipantService { * @throws InvalidPasswordException * @throws UnauthorizedException */ - public function joinRoom(Room $room, IUser $user, string $password, bool $passedPasswordProtection = false): Participant { + public function joinRoom(RoomService $roomService, Room $room, IUser $user, string $password, bool $passedPasswordProtection = false): Participant { $event = new JoinRoomUserEvent($room, $user, $password, $passedPasswordProtection); $this->dispatcher->dispatch(Room::EVENT_BEFORE_ROOM_CONNECT, $event); @@ -258,7 +259,7 @@ class ParticipantService { $manager = \OC::$server->get(Manager::class); $isListableByUser = $manager->isRoomListableByUser($room, $user->getUID()); - if (!$isListableByUser && !$event->getPassedPasswordProtection() && !$room->verifyPassword($password)['result']) { + if (!$isListableByUser && !$event->getPassedPasswordProtection() && !$roomService->verifyPassword($room, $password)['result']) { throw new InvalidPasswordException('Provided password is invalid'); } @@ -295,6 +296,7 @@ class ParticipantService { } /** + * @param RoomService $roomService * @param Room $room * @param string $password * @param bool $passedPasswordProtection @@ -303,7 +305,7 @@ class ParticipantService { * @throws InvalidPasswordException * @throws UnauthorizedException */ - public function joinRoomAsNewGuest(Room $room, string $password, bool $passedPasswordProtection = false, ?Participant $previousParticipant = null): Participant { + public function joinRoomAsNewGuest(RoomService $roomService, Room $room, string $password, bool $passedPasswordProtection = false, ?Participant $previousParticipant = null): Participant { $event = new JoinRoomGuestEvent($room, $password, $passedPasswordProtection); $this->dispatcher->dispatch(Room::EVENT_BEFORE_GUEST_CONNECT, $event); @@ -311,7 +313,7 @@ class ParticipantService { throw new UnauthorizedException('Participant is not allowed to join'); } - if (!$event->getPassedPasswordProtection() && !$room->verifyPassword($password)['result']) { + if (!$event->getPassedPasswordProtection() && !$roomService->verifyPassword($room, $password)['result']) { throw new InvalidPasswordException(); } diff --git a/lib/Service/RoomService.php b/lib/Service/RoomService.php index b8263d76b..4805a33d8 100644 --- a/lib/Service/RoomService.php +++ b/lib/Service/RoomService.php @@ -25,6 +25,7 @@ namespace OCA\Talk\Service; use InvalidArgumentException; use OCA\Talk\Events\ModifyRoomEvent; +use OCA\Talk\Events\VerifyRoomPasswordEvent; use OCA\Talk\Exceptions\RoomNotFoundException; use OCA\Talk\Manager; use OCA\Talk\Model\Attendee; @@ -32,21 +33,25 @@ use OCA\Talk\Participant; use OCA\Talk\Room; use OCP\EventDispatcher\IEventDispatcher; use OCP\IUser; +use OCP\Security\IHasher; use OCP\Share\IManager as IShareManager; class RoomService { protected Manager $manager; protected ParticipantService $participantService; protected IShareManager $shareManager; - private IEventDispatcher $dispatcher; + protected IHasher $hasher; + protected IEventDispatcher $dispatcher; public function __construct(Manager $manager, ParticipantService $participantService, IShareManager $shareManager, + IHasher $hasher, IEventDispatcher $dispatcher) { $this->manager = $manager; $this->participantService = $participantService; $this->shareManager = $shareManager; + $this->hasher = $hasher; $this->dispatcher = $dispatcher; } @@ -195,4 +200,21 @@ class RoomService { return true; } + + public function verifyPassword(Room $room, string $password): array { + $event = new VerifyRoomPasswordEvent($room, $password); + $this->dispatcher->dispatch(Room::EVENT_PASSWORD_VERIFY, $event); + + if ($event->isPasswordValid() !== null) { + return [ + 'result' => $event->isPasswordValid(), + 'url' => $event->getRedirectUrl(), + ]; + } + + return [ + 'result' => !$room->hasPassword() || $this->hasher->verify($password, $room->getPassword()), + 'url' => '', + ]; + } } diff --git a/tests/php/Chat/SystemMessage/ListenerTest.php b/tests/php/Chat/SystemMessage/ListenerTest.php index 448a676fb..8e53679bd 100644 --- a/tests/php/Chat/SystemMessage/ListenerTest.php +++ b/tests/php/Chat/SystemMessage/ListenerTest.php @@ -63,7 +63,7 @@ class ListenerTest extends TestCase { protected ?array $handlers = null; protected ?\DateTime $dummyTime = null; - public function setUp(): void { + protected function setUp(): void { parent::setUp(); $this->request = $this->createMock(IRequest::class); diff --git a/tests/php/Controller/SignalingControllerTest.php b/tests/php/Controller/SignalingControllerTest.php index 53234ce49..e5879629c 100644 --- a/tests/php/Controller/SignalingControllerTest.php +++ b/tests/php/Controller/SignalingControllerTest.php @@ -35,6 +35,7 @@ use OCA\Talk\Model\SessionMapper; use OCA\Talk\Participant; use OCA\Talk\Room; use OCA\Talk\Service\ParticipantService; +use OCA\Talk\Service\RoomService; use OCA\Talk\Service\SessionService; use OCA\Talk\Signaling\Messages; use OCA\Talk\TalkSession; @@ -1059,9 +1060,12 @@ class SignalingControllerTest extends TestCase { ->willReturn($this->userId); $room = $this->manager->createRoom(Room::TYPE_PUBLIC); + $roomService = $this->createMock(RoomService::class); + $roomService->method('verifyPassword') + ->willReturn(['result' => true, 'url' => '']); // The user joined the room. - $oldParticipant = $participantService->joinRoom($room, $testUser, ''); + $oldParticipant = $participantService->joinRoom($roomService, $room, $testUser, ''); $oldSessionId = $oldParticipant->getSession()->getSessionId(); $this->performBackendRequest([ 'type' => 'room', @@ -1077,7 +1081,7 @@ class SignalingControllerTest extends TestCase { // The user is reloading the browser which will join him with another // session id. - $newParticipant = $participantService->joinRoom($room, $testUser, ''); + $newParticipant = $participantService->joinRoom($roomService, $room, $testUser, ''); $newSessionId = $newParticipant->getSession()->getSessionId(); $this->performBackendRequest([ 'type' => 'room', diff --git a/tests/php/RoomTest.php b/tests/php/RoomTest.php deleted file mode 100644 index 07b00ccff..000000000 --- a/tests/php/RoomTest.php +++ /dev/null @@ -1,94 +0,0 @@ -<?php - -declare(strict_types=1); -/** - * @copyright Copyright (c) 2018 Peter Edens <petere@conceiva.com> - * - * @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 OCA\Talk\Tests\php; - -use OC\EventDispatcher\EventDispatcher; -use OCA\Talk\Events\VerifyRoomPasswordEvent; -use OCA\Talk\Manager; -use OCA\Talk\Model\Attendee; -use OCA\Talk\Participant; -use OCA\Talk\Room; -use OCA\Talk\Webinary; -use OCP\AppFramework\Utility\ITimeFactory; -use OCP\IDBConnection; -use OCP\Security\IHasher; -use Psr\Log\LoggerInterface; -use Test\TestCase; - -class RoomTest extends TestCase { - public function testVerifyPassword() { - $dispatcher = new EventDispatcher( - new \Symfony\Component\EventDispatcher\EventDispatcher(), - \OC::$server, - $this->createMock(LoggerInterface::class) - ); - $dispatcher->addListener(Room::EVENT_PASSWORD_VERIFY, static function (VerifyRoomPasswordEvent $event) { - $password = $event->getPassword(); - - if ($password === '1234') { - $event->setIsPasswordValid(true); - $event->setRedirectUrl(''); - } else { - $event->setIsPasswordValid(false); - $event->setRedirectUrl('https://test'); - } - }); - - $room = new Room( - $this->createMock(Manager::class), - $this->createMock(IDBConnection::class), - $dispatcher, - $this->createMock(ITimeFactory::class), - $this->createMock(IHasher::class), - 1, - Room::TYPE_PUBLIC, - Room::READ_WRITE, - Room::LISTABLE_NONE, - Webinary::LOBBY_NONE, - 0, - null, - 'foobar', - 'Test', - 'description', - 'passy', - '', - '', - 0, - Attendee::PERMISSIONS_DEFAULT, - Attendee::PERMISSIONS_DEFAULT, - Participant::FLAG_DISCONNECTED, - null, - null, - 0, - null, - null, - '', - '' - ); - $verificationResult = $room->verifyPassword('1234'); - $this->assertSame($verificationResult, ['result' => true, 'url' => '']); - $verificationResult = $room->verifyPassword('4321'); - $this->assertSame($verificationResult, ['result' => false, 'url' => 'https://test']); - $this->assertSame('passy', $room->getPassword()); - } -} diff --git a/tests/php/Service/RoomServiceTest.php b/tests/php/Service/RoomServiceTest.php index faf324a78..342997543 100644 --- a/tests/php/Service/RoomServiceTest.php +++ b/tests/php/Service/RoomServiceTest.php @@ -24,16 +24,24 @@ declare(strict_types=1); namespace OCA\Talk\Tests\php\Service; use InvalidArgumentException; +use OC\EventDispatcher\EventDispatcher; +use OCA\Talk\Events\VerifyRoomPasswordEvent; use OCA\Talk\Exceptions\RoomNotFoundException; use OCA\Talk\Manager; +use OCA\Talk\Model\Attendee; use OCA\Talk\Participant; use OCA\Talk\Room; use OCA\Talk\Service\ParticipantService; use OCA\Talk\Service\RoomService; +use OCA\Talk\Webinary; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\EventDispatcher\IEventDispatcher; +use OCP\IDBConnection; use OCP\IUser; +use OCP\Security\IHasher; use OCP\Share\IManager as IShareManager; use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; use Test\TestCase; class RoomServiceTest extends TestCase { @@ -44,6 +52,8 @@ class RoomServiceTest extends TestCase { protected $participantService; /** @var IShareManager|MockObject */ protected $shareManager; + /** @var IHasher|MockObject */ + protected $hasher; /** @var IEventDispatcher|MockObject */ protected $dispatcher; private ?RoomService $service = null; @@ -55,11 +65,13 @@ class RoomServiceTest extends TestCase { $this->manager = $this->createMock(Manager::class); $this->participantService = $this->createMock(ParticipantService::class); $this->shareManager = $this->createMock(IShareManager::class); + $this->hasher = $this->createMock(IHasher::class); $this->dispatcher = $this->createMock(IEventDispatcher::class); $this->service = new RoomService( $this->manager, $this->participantService, $this->shareManager, + $this->hasher, $this->dispatcher ); } @@ -295,4 +307,69 @@ class RoomServiceTest extends TestCase { public function testPrepareConversationName(string $input, string $expected): void { $this->assertSame($expected, $this->service->prepareConversationName($input)); } + + public function testVerifyPassword(): void { + $dispatcher = new EventDispatcher( + new \Symfony\Component\EventDispatcher\EventDispatcher(), + \OC::$server, + $this->createMock(LoggerInterface::class) + ); + $dispatcher->addListener(Room::EVENT_PASSWORD_VERIFY, static function (VerifyRoomPasswordEvent $event) { + $password = $event->getPassword(); + + if ($password === '1234') { + $event->setIsPasswordValid(true); + $event->setRedirectUrl(''); + } else { + $event->setIsPasswordValid(false); + $event->setRedirectUrl('https://test'); + } + }); + + $service = new RoomService( + $this->manager, + $this->participantService, + $this->shareManager, + $this->hasher, + $dispatcher + ); + + $room = new Room( + $this->createMock(Manager::class), + $this->createMock(IDBConnection::class), + $dispatcher, + $this->createMock(ITimeFactory::class), + $this->createMock(IHasher::class), + 1, + Room::TYPE_PUBLIC, + Room::READ_WRITE, + Room::LISTABLE_NONE, + Webinary::LOBBY_NONE, + 0, + null, + 'foobar', + 'Test', + 'description', + 'passy', + '', + '', + 0, + Attendee::PERMISSIONS_DEFAULT, + Attendee::PERMISSIONS_DEFAULT, + Participant::FLAG_DISCONNECTED, + null, + null, + 0, + null, + null, + '', + '' + ); + + $verificationResult = $service->verifyPassword($room, '1234'); + $this->assertSame($verificationResult, ['result' => true, 'url' => '']); + $verificationResult = $service->verifyPassword($room, '4321'); + $this->assertSame($verificationResult, ['result' => false, 'url' => 'https://test']); + $this->assertSame('passy', $room->getPassword()); + } } diff --git a/tests/php/Signaling/BackendNotifierTest.php b/tests/php/Signaling/BackendNotifierTest.php index 3bb2fc386..e0c4c1cdb 100644 --- a/tests/php/Signaling/BackendNotifierTest.php +++ b/tests/php/Signaling/BackendNotifierTest.php @@ -33,6 +33,7 @@ use OCA\Talk\Model\SessionMapper; use OCA\Talk\Participant; use OCA\Talk\Room; use OCA\Talk\Service\ParticipantService; +use OCA\Talk\Service\RoomService; use OCA\Talk\Signaling\BackendNotifier; use OCA\Talk\TalkSession; use OCA\Talk\Webinary; @@ -312,12 +313,16 @@ class BackendNotifierTest extends TestCase { ->method('getUID') ->willReturn($this->userId); + $roomService = $this->createMock(RoomService::class); + $roomService->method('verifyPassword') + ->willReturn(['result' => true, 'url' => '']); + $room = $this->manager->createRoom(Room::TYPE_PUBLIC); $this->participantService->addUsers($room, [[ 'actorType' => 'users', 'actorId' => $this->userId, ]]); - $participant = $this->participantService->joinRoom($room, $testUser, ''); + $participant = $this->participantService->joinRoom($roomService, $room, $testUser, ''); $this->controller->clearRequests(); $this->participantService->leaveRoomAsSession($room, $participant); @@ -331,8 +336,12 @@ class BackendNotifierTest extends TestCase { ->method('getUID') ->willReturn($this->userId); + $roomService = $this->createMock(RoomService::class); + $roomService->method('verifyPassword') + ->willReturn(['result' => true, 'url' => '']); + $room = $this->manager->createRoom(Room::TYPE_PUBLIC); - $participant = $this->participantService->joinRoom($room, $testUser, ''); + $participant = $this->participantService->joinRoom($roomService, $room, $testUser, ''); $this->controller->clearRequests(); $this->participantService->leaveRoomAsSession($room, $participant); @@ -360,8 +369,12 @@ class BackendNotifierTest extends TestCase { } public function testRoomDisinviteOnLeaveOfGuest() { + $roomService = $this->createMock(RoomService::class); + $roomService->method('verifyPassword') + ->willReturn(['result' => true, 'url' => '']); + $room = $this->manager->createRoom(Room::TYPE_PUBLIC); - $participant = $this->participantService->joinRoomAsNewGuest($room, ''); + $participant = $this->participantService->joinRoomAsNewGuest($roomService, $room, ''); $this->controller->clearRequests(); $this->participantService->leaveRoomAsSession($room, $participant); @@ -587,7 +600,11 @@ class BackendNotifierTest extends TestCase { ->method('getUID') ->willReturn($this->userId); - $participant = $this->participantService->joinRoom($room, $testUser, ''); + $roomService = $this->createMock(RoomService::class); + $roomService->method('verifyPassword') + ->willReturn(['result' => true, 'url' => '']); + + $participant = $this->participantService->joinRoom($roomService, $room, $testUser, ''); $userSession = $participant->getSession()->getSessionId(); $participant = $room->getParticipantBySession($userSession); @@ -624,7 +641,7 @@ class BackendNotifierTest extends TestCase { $this->controller->clearRequests(); - $guestParticipant = $this->participantService->joinRoomAsNewGuest($room, ''); + $guestParticipant = $this->participantService->joinRoomAsNewGuest($roomService, $room, ''); $guestSession = $guestParticipant->getSession()->getSessionId(); $guestParticipant = $room->getParticipantBySession($guestSession); $this->participantService->changeInCall($room, $guestParticipant, Participant::FLAG_IN_CALL); @@ -745,7 +762,11 @@ class BackendNotifierTest extends TestCase { ->method('getUID') ->willReturn($this->userId); - $participant = $this->participantService->joinRoom($room, $testUser, ''); + $roomService = $this->createMock(RoomService::class); + $roomService->method('verifyPassword') + ->willReturn(['result' => true, 'url' => '']); + + $participant = $this->participantService->joinRoom($roomService, $room, $testUser, ''); $userSession = $participant->getSession()->getSessionId(); $participant = $room->getParticipantBySession($userSession); @@ -780,7 +801,7 @@ class BackendNotifierTest extends TestCase { $this->controller->clearRequests(); - $guestParticipant = $this->participantService->joinRoomAsNewGuest($room, ''); + $guestParticipant = $this->participantService->joinRoomAsNewGuest($roomService, $room, ''); $guestSession = $guestParticipant->getSession()->getSessionId(); $guestParticipant = $room->getParticipantBySession($guestSession); |