Example #1
0
    def make_authorization(self, username, auth_text):
        """
        Expects an authorization json object as follows:
            {
                "publish": ["example/weather-stations/F83A5D/#", ...],
                "subscribe": ["example/weather-server/status", ...]
            }

        Strings in publish and subscribe arrays can and should follow MQTT's
        topic specification. Wildcards are allowed.

        The value of `publish` and `subscribe` fields can also be `"*"`,
        meaning full authorization. If an empty list is passed, no
        authorization is granted.

        :param string auth_text:
        :return: an Authorization object
        :rtype: Authorization
        """
        try:
            if isinstance(auth_text, bytes):
                auth_text = auth_text.decode('utf-8')
            assert isinstance(auth_text, str)
            return Authorization.from_dict(json.loads(auth_text))
        except:
            self.logger.error('error parsing authorizations for user:%s' %
                              username,
                              exc_info=True)
            return None
Example #2
0
    def make_authorization(self, username, auth_text):
        """
        Expects an authorization json object as follows:
            {
                "publish": ["example/weather-stations/F83A5D/#", ...],
                "subscribe": ["example/weather-server/status", ...]
            }

        Strings in publish and subscribe arrays can and should follow MQTT's
        topic specification. Wildcards are allowed.

        The value of `publish` and `subscribe` fields can also be `"*"`,
        meaning full authorization. If an empty list is passed, no
        authorization is granted.

        :param string auth_text:
        :return: an Authorization object
        :rtype: Authorization
        """
        try:
            if isinstance(auth_text, bytes):
                auth_text = auth_text.decode('utf-8')
            assert isinstance(auth_text, str)
            return Authorization.from_dict(json.loads(auth_text))
        except:
            self.logger.error('error parsing authorizations for user:%s' %
                              username,
                              exc_info=True)
            return None
Example #3
0
    def __init__(self, server, connection, authorization=None,
                 uid=None, clean_session=False,
                 keep_alive=60, persistence=None, receive_subscriptions=None):

        self.uid = uid
        self.logger = getLogger('activity.clients')
        self.persistence = persistence or InMemoryClientPersistence(uid)

        self.subscriptions = ClientSubscriptions(persistence.subscriptions)

        self._connected = Event()

        self.last_will = None
        self.connection = None
        self.clean_session = None
        self.keep_alive = None
        self.receive_subscriptions = False

        self.server = server
        self.authorization = authorization or Authorization.no_restrictions()

        # Queue of the packets ready to be delivered
        self.outgoing_queue = OutgoingQueue(self.persistence.outgoing_publishes)

        self.update_configuration(clean_session, keep_alive, receive_subscriptions)
        self.update_connection(connection)
Example #4
0
    def __init__(self,
                 server,
                 connection,
                 authorization=None,
                 uid=None,
                 clean_session=False,
                 keep_alive=60,
                 persistence=None,
                 receive_subscriptions=None):

        self.uid = uid
        self.logger = getLogger('activity.clients')
        self.persistence = persistence or InMemoryClientPersistence(uid)

        self.subscriptions = ClientSubscriptions(persistence.subscriptions)

        self._connected = Event()

        self.last_will = None
        self.connection = None
        self.clean_session = None
        self.keep_alive = None
        self.receive_subscriptions = False

        self.server = server
        self.authorization = authorization or Authorization.no_restrictions()

        # Queue of the packets ready to be delivered
        self.outgoing_queue = OutgoingQueue(
            self.persistence.outgoing_publishes)

        self.update_configuration(clean_session, keep_alive,
                                  receive_subscriptions)
        self.update_connection(connection)
Example #5
0
 def authenticate(self, client_id, username, password):
     self.logger.debug('authenticating client:%s user:%s' %
                       (client_id, username))
     if username in self.auth and self.auth[username].check_pw(password):
         return self.auth[username].authorization
     else:
         return Authorization.denied()
Example #6
0
 def authenticate(self, client_id, username, password):
     self.logger.debug('authenticating client:%s user:%s' %
                       (client_id, username))
     result = yield self._request_authorization(client_id, username, password)
     if result:
         return self.make_authorization(username, result)
     else:
         return Authorization.denied()
Example #7
0
 def authenticate(self, client_id, username, password):
     self.logger.debug('authenticating client:%s user:%s' %
                       (client_id, username))
     result = yield self._request_authorization(client_id, username,
                                                password)
     if result:
         return self.make_authorization(username, result)
     else:
         return Authorization.denied()
Example #8
0
 def from_dict(cls, item, pwcheck=None):
     assert 'username' in item
     assert 'password' in item
     authorization = Authorization.from_dict(item)
     return cls(item['username'], item['password'], authorization, pwcheck)