Example #1
0
    def get_context(self, user=None, password=None):
        "Creates or retrieves existing context object for this client"
        s = constants.db_session()
        user_obj = None
        if user or password:
            log.d("Client provided credentials, authenticating...")
            if user == constants.super_user_name and not config.disable_default_user.value:
                log.d("Authenticating with default user")
                user_obj = s.query(db.User).filter(
                    db.User.role == db.User.Role.default).one()
            else:
                user_obj = s.query(
                    db.User).filter(db.User.name == user).one_or_none()
                if not user_obj or (not user_obj.role == db.User.Role.guest
                                    and not user_obj.password == password):
                    raise exceptions.AuthWrongCredentialsError(
                        utils.this_function(), "Wrong credentials")
        else:
            log.d("Client did not provide credentials")
            if not config.allow_guests.value:
                log.d("Guests are disallowed on this server")
                raise exceptions.AuthRequiredError(
                    utils.this_function(), "Authentication is required")
            log.d("Authencticating as guest")
            user_obj = db.User(role=db.User.Role.guest)

        self.context['user'] = user_obj

        self.context['adresss'] = self._address
        if not self.context['user'].context_id:
            self.context['user'].context_id = uuid.uuid4().hex

        self.context['config'] = {}
        log.d("Client accepted")
        self._accepted = True
Example #2
0
    def communicate(self, msg, auth=False):
        """Send and receive data with server

        params:
            msg -- dict
        returns:
            dict from server
        """
        if self._alive and not self._accepted and not auth:
            raise exceptions.AuthRequiredError(
                utils.this_function(),
                "Client '{}' is connected but not authenticated".format(
                    self.name))
        self._send(bytes(utils.json_dumps(msg), 'utf-8'))
        return self._recv()
Example #3
0
    def get_context(self, user=None, password=None):
        "Creates or retrieves existing context object for this client"
        s = constants.db_session()
        user_obj = None
        if user or password:
            log.d("Client provided credentials, authenticating...")
            user_obj = s.query(
                db.User).filter(db.User.name == user).one_or_none()
            if user_obj:
                if not user_obj.password == password:
                    raise exceptions.AuthError(utils.this_function(),
                                               "Wrong credentials")
            else:
                raise exceptions.AuthError(utils.this_function(),
                                           "Wrong credentials")
        else:
            log.d("Client did not provide credentials")

            if not constants.disable_default_user:
                log.d("Authenticating with default user")
                user_obj = s.query(db.User).filter(
                    db.User.role == db.User.Role.default).one()
            else:
                if not constants.allow_guests:
                    log.d("Guests are disallowed on this server")
                    raise exceptions.AuthRequiredError(utils.this_function())
                log.d("Authencticating as guest")
                user_obj = s.query(db.User).filter(
                    db.and_op(
                        db.User.address == self._ip,
                        db.User.role == db.User.Role.guest)).one_or_none()
                if not user_obj:
                    user_obj = db.User(role=db.User.Role.guest)
                    s.add(user_obj)

        self.context = user_obj

        self.context.address = self._ip
        if not self.context.context_id:
            self.context.context_id = uuid.uuid4().hex

        self.context.config = None
        log.d("Client accepted")
        self._accepted = True

        s.commit()
Example #4
0
 def handshake(self, data={}, user=None, password=None, ignore_err=False):
     "Shake hands with server"
     if self.alive():
         if user:
             self._last_user = user
             self._last_pass = password
         if not ignore_err and data:
             serv_error = data.get('error')
             if serv_error:
                 if serv_error[
                         'code'] == exceptions.AuthWrongCredentialsError.code:
                     raise exceptions.AuthWrongCredentialsError(
                         utils.this_function(), serv_error['msg'])
                 elif serv_error[
                         'code'] == exceptions.AuthRequiredError.code:
                     raise exceptions.AuthRequiredError(
                         utils.this_function(), serv_error['msg'])
                 elif serv_error[
                         'code'] == exceptions.AuthMissingCredentials.code:
                     raise exceptions.AuthMissingCredentials(
                         utils.this_function(), serv_error['msg'])
                 else:
                     raise exceptions.AuthError(
                         utils.this_function(),
                         "{}: {}".format(serv_error['code'],
                                         serv_error['msg']))
         if not data:
             d = {}
             if user:
                 d['user'] = user
                 d['password'] = password
             self._send(message.finalize(d, name=self.name))
             self.handshake(self._recv(), ignore_err=ignore_err)
         elif data:
             serv_data = data.get('data')
             if serv_data == "Authenticated":
                 self.session = data.get('session')
                 self._accepted = True