Refactored SubscriberInterface
This commit is contained in:
parent
17c683d9e8
commit
e61d0abb5d
@ -25,7 +25,7 @@ class SseSubscriber implements SubscriberInterface
|
|||||||
$this->stream->write($message->toString());
|
$this->stream->write($message->toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isAuthorized(): bool
|
public function isAuthenticated(): bool
|
||||||
{
|
{
|
||||||
return $this->request->getAttribute('authorized');
|
return $this->request->getAttribute('authorized');
|
||||||
}
|
}
|
||||||
|
@ -6,11 +6,32 @@ use NoccyLabs\Mercureact\Broker\Message;
|
|||||||
|
|
||||||
interface SubscriberInterface
|
interface SubscriberInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Deliver a message to the subscriber.
|
||||||
|
*
|
||||||
|
* @param Message $message
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
public function deliver(Message $message): 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;
|
public function getPayload(): ?array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the unique subscriber ID, in the form 'urn:uuid:...'
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function getId(): string;
|
public function getId(): string;
|
||||||
}
|
}
|
@ -68,7 +68,7 @@ class WsSubscriber implements SubscriberInterface, EventEmitterInterface
|
|||||||
], JSON_UNESCAPED_SLASHES));
|
], JSON_UNESCAPED_SLASHES));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isAuthorized(): bool
|
public function isAuthenticated(): bool
|
||||||
{
|
{
|
||||||
return $this->token && $this->token->isValid();
|
return $this->token && $this->token->isValid();
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ class Topic
|
|||||||
|
|
||||||
foreach ($this->subscribers as $subscriber) {
|
foreach ($this->subscribers as $subscriber) {
|
||||||
// Skip sending private messages to unauthorized subscribers
|
// Skip sending private messages to unauthorized subscribers
|
||||||
if ($message->private && !$subscriber->isAuthorized()) {
|
if ($message->private && !$subscriber->isAuthenticated()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// Deliver to the subscriber
|
// Deliver to the subscriber
|
||||||
|
@ -12,19 +12,11 @@ class TopicTest extends \PHPUnit\Framework\TestCase
|
|||||||
|
|
||||||
public function testPublicMessagesAreDeliveredToAllSubscribers()
|
public function testPublicMessagesAreDeliveredToAllSubscribers()
|
||||||
{
|
{
|
||||||
$authorizedSubscriber = new class implements SubscriberInterface {
|
$authorizedSubscriber = new class extends _Subscriber {
|
||||||
public array $messages = [];
|
public function isAuthenticated():bool { return true; }
|
||||||
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 ""; }
|
|
||||||
};
|
};
|
||||||
$unauthorizedSubscriber = new class implements SubscriberInterface {
|
$unauthorizedSubscriber = new class extends _Subscriber {
|
||||||
public array $messages = [];
|
public function isAuthenticated():bool { return false; }
|
||||||
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 ""; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
$topic = new Topic("foo");
|
$topic = new Topic("foo");
|
||||||
@ -40,19 +32,11 @@ class TopicTest extends \PHPUnit\Framework\TestCase
|
|||||||
|
|
||||||
public function testPrivateMessagesAreNotDeliveredToUnauthorizedSubscribers()
|
public function testPrivateMessagesAreNotDeliveredToUnauthorizedSubscribers()
|
||||||
{
|
{
|
||||||
$authorizedSubscriber = new class implements SubscriberInterface {
|
$authorizedSubscriber = new class extends _Subscriber {
|
||||||
public array $messages = [];
|
public function isAuthenticated():bool { return true; }
|
||||||
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 ""; }
|
|
||||||
};
|
};
|
||||||
$unauthorizedSubscriber = new class implements SubscriberInterface {
|
$unauthorizedSubscriber = new class extends _Subscriber {
|
||||||
public array $messages = [];
|
public function isAuthenticated():bool { return false; }
|
||||||
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 ""; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
$topic = new Topic("foo");
|
$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 ""; }
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user