From e61d0abb5df6c9d6ed3835917d8f271003ff8069 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Thu, 14 Mar 2024 00:45:26 +0100 Subject: [PATCH] Refactored SubscriberInterface --- src/Broker/Subscriber/SseSubscriber.php | 2 +- src/Broker/Subscriber/SubscriberInterface.php | 23 ++++++++++- src/Broker/Subscriber/WsSubscriber.php | 2 +- src/Broker/Topic.php | 2 +- tests/Broker/TopicTest.php | 40 ++++++++----------- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/Broker/Subscriber/SseSubscriber.php b/src/Broker/Subscriber/SseSubscriber.php index b2967f1..66b99fc 100644 --- a/src/Broker/Subscriber/SseSubscriber.php +++ b/src/Broker/Subscriber/SseSubscriber.php @@ -25,7 +25,7 @@ class SseSubscriber implements SubscriberInterface $this->stream->write($message->toString()); } - public function isAuthorized(): bool + public function isAuthenticated(): bool { return $this->request->getAttribute('authorized'); } diff --git a/src/Broker/Subscriber/SubscriberInterface.php b/src/Broker/Subscriber/SubscriberInterface.php index b25e34d..9530aa1 100644 --- a/src/Broker/Subscriber/SubscriberInterface.php +++ b/src/Broker/Subscriber/SubscriberInterface.php @@ -6,11 +6,32 @@ use NoccyLabs\Mercureact\Broker\Message; interface SubscriberInterface { + /** + * Deliver a message to the subscriber. + * + * @param Message $message + * @return void + */ public function deliver(Message $message): void; - public function isAuthorized(): bool; + /** + * Returns true if the subscriber has athenticated. + * + * @return bool true if the subscriber has provided valid credentials + */ + public function isAuthenticated(): bool; + /** + * Returns the content of the JWT mercure.payload claim if present. + * + * @return array|null + */ public function getPayload(): ?array; + /** + * Get the unique subscriber ID, in the form 'urn:uuid:...' + * + * @return string + */ public function getId(): string; } \ No newline at end of file diff --git a/src/Broker/Subscriber/WsSubscriber.php b/src/Broker/Subscriber/WsSubscriber.php index 0bb1060..ea96ccd 100644 --- a/src/Broker/Subscriber/WsSubscriber.php +++ b/src/Broker/Subscriber/WsSubscriber.php @@ -68,7 +68,7 @@ class WsSubscriber implements SubscriberInterface, EventEmitterInterface ], JSON_UNESCAPED_SLASHES)); } - public function isAuthorized(): bool + public function isAuthenticated(): bool { return $this->token && $this->token->isValid(); } diff --git a/src/Broker/Topic.php b/src/Broker/Topic.php index 048ad80..ce89fc5 100644 --- a/src/Broker/Topic.php +++ b/src/Broker/Topic.php @@ -39,7 +39,7 @@ class Topic foreach ($this->subscribers as $subscriber) { // Skip sending private messages to unauthorized subscribers - if ($message->private && !$subscriber->isAuthorized()) { + if ($message->private && !$subscriber->isAuthenticated()) { continue; } // Deliver to the subscriber diff --git a/tests/Broker/TopicTest.php b/tests/Broker/TopicTest.php index e5bf3ff..7327f86 100644 --- a/tests/Broker/TopicTest.php +++ b/tests/Broker/TopicTest.php @@ -12,19 +12,11 @@ class TopicTest extends \PHPUnit\Framework\TestCase public function testPublicMessagesAreDeliveredToAllSubscribers() { - $authorizedSubscriber = new class implements SubscriberInterface { - public array $messages = []; - public function isAuthorized():bool { return true; } - public function deliver(Message $message):void { $this->messages[] = $message; } - public function getPayload(): ?array { return null; } - public function getId(): string { return ""; } + $authorizedSubscriber = new class extends _Subscriber { + public function isAuthenticated():bool { return true; } }; - $unauthorizedSubscriber = new class implements SubscriberInterface { - public array $messages = []; - public function isAuthorized():bool { return false; } - public function deliver(Message $message):void { $this->messages[] = $message; } - public function getPayload(): ?array { return null; } - public function getId(): string { return ""; } + $unauthorizedSubscriber = new class extends _Subscriber { + public function isAuthenticated():bool { return false; } }; $topic = new Topic("foo"); @@ -40,19 +32,11 @@ class TopicTest extends \PHPUnit\Framework\TestCase public function testPrivateMessagesAreNotDeliveredToUnauthorizedSubscribers() { - $authorizedSubscriber = new class implements SubscriberInterface { - public array $messages = []; - public function isAuthorized():bool { return true; } - public function deliver(Message $message):void { $this->messages[] = $message; } - public function getPayload(): ?array { return null; } - public function getId(): string { return ""; } + $authorizedSubscriber = new class extends _Subscriber { + public function isAuthenticated():bool { return true; } }; - $unauthorizedSubscriber = new class implements SubscriberInterface { - public array $messages = []; - public function isAuthorized():bool { return false; } - public function deliver(Message $message):void { $this->messages[] = $message; } - public function getPayload(): ?array { return null; } - public function getId(): string { return ""; } + $unauthorizedSubscriber = new class extends _Subscriber { + public function isAuthenticated():bool { return false; } }; $topic = new Topic("foo"); @@ -67,3 +51,11 @@ class TopicTest extends \PHPUnit\Framework\TestCase } } + +abstract class _Subscriber implements SubscriberInterface { + public array $messages = []; + public function isAuthenticated():bool { return false; } + public function deliver(Message $message):void { $this->messages[] = $message; } + public function getPayload(): ?array { return null; } + public function getId(): string { return ""; } +}; \ No newline at end of file