Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also topic check on publish, and pass variable to differentiate between pub and sub in plugin #195

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions hbmqtt/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def client_connected(self, listener_name, reader: ReaderAdapter, writer: WriterA
yield from self.plugins_manager.fire_event(EVENT_BROKER_MESSAGE_RECEIVED,
client_id=client_session.client_id,
message=app_message)
yield from self._broadcast_message(client_session, app_message.topic, app_message.data)
yield from self._broadcast_message_acl(client_session, app_message.topic, app_message.data)
if app_message.publish_packet.retain_flag:
self.retain_message(client_session, app_message.topic, app_message.data, app_message.qos)
wait_deliver = asyncio.Task(handler.mqtt_deliver_next_message(), loop=self._loop)
Expand Down Expand Up @@ -563,7 +563,7 @@ def authenticate(self, session: Session, listener):
return auth_result

@asyncio.coroutine
def topic_filtering(self, session: Session, topic):
def topic_filtering(self, session: Session, topic, command):
"""
This method call the topic_filtering method on registered plugins to check that the subscription is allowed.
User is considered allowed if all plugins called return True.
Expand All @@ -573,7 +573,8 @@ def topic_filtering(self, session: Session, topic):
- None if topic filtering can't be achieved (then plugin result is then ignored)
:param session:
:param listener:
:param topic: Topic in which the client wants to subscribe
:param topic: Topic in which the client wants to publish or subscribe
:param command: Whether it's a publish (1) or subscibe (0) command
:return:
"""
topic_plugins = None
Expand All @@ -584,6 +585,7 @@ def topic_filtering(self, session: Session, topic):
"topic_filtering",
session=session,
topic=topic,
command=command,
filter_plugins=topic_plugins)
topic_result = True
if returns:
Expand Down Expand Up @@ -622,7 +624,7 @@ def add_subscription(self, subscription, session):
# [MQTT-4.7.1-3] + wildcard character must occupy entire level
return 0x80
# Check if the client is authorised to connect to the topic
permitted = yield from self.topic_filtering(session, topic=a_filter)
permitted = yield from self.topic_filtering(session, topic=a_filter, command=0)
if not permitted:
return 0x80
qos = subscription[1]
Expand Down Expand Up @@ -725,6 +727,13 @@ def _broadcast_loop(self):
if running_tasks:
yield from asyncio.wait(running_tasks, loop=self._loop)

@asyncio.coroutine
def _broadcast_message_acl(self, session, topic, data, force_qos=None):
permitted = yield from self.topic_filtering(session, topic=topic, command=1)

if permitted:
yield from self._broadcast_message(session, topic, data, force_qos)

@asyncio.coroutine
def _broadcast_message(self, session, topic, data, force_qos=None):
broadcast = {
Expand Down