def create_user(self, username: str, passwd: str, session_id: str):
     """ Creates a user in the authentication server.
     ---
     Parameters:
         - username: The username string.
         - password: The password string.
         - session_id: The session id string.
     Throws:
         - BadRequestError: if the request is malformed.
         - UnauthorizedError: if the requestor does not meet the security
           requirements.
         - ConflictError: if a user with the given username already exists.
         - HTTPException: On an unhandled 500 error.
     """
     form: str = urlencode({
         'username': username,
         'password': passwd,
         'session_id': session_id
     })
     headers: dict = {'Content-type': 'application/x-www-form-urlencoded'}
     connection: HTTPConnection = self.__get_connection()
     connection.request('POST', '/users', form, headers)
     response: HTTPResponse = connection.getresponse()
     if response.status == 200:
         return
     if response.status == 400:
         raise BadRequestError()
     if response.status == 401:
         raise UnauthorizedError()
     if response.status == 409:
         raise ConflictError()
     if response.status == 500:
         raise HTTPException('Server error')
 def revoke(self, username: str, right: str, session_id: str):
     """ Revoke a right to a user from the authentication server.
     ---
     Parameters:
         - username: The username string.
         - right: The right string.
         - session_id: The session id string.
     Throws:
         - UnauthorizedError: if the requestor does not meet the security
           requirements or no session was provided.
         - NotFoundError: if the user does not exist or the right does not exist.
         - HTTPException: On an unhandled 500 error.
     """
     form: str = urlencode({
         'username': username,
         'right': right,
         'session_id': session_id
     })
     headers: dict = {'Content-type': 'application/x-www-form-urlencoded'}
     connection: HTTPConnection = self.__get_connection()
     connection.request('DELETE',
                        '/users/' + str(username) + '/rights/' + str(right),
                        form, headers)
     response: HTTPResponse = connection.getresponse()
     if response.status == 200:
         return
     if response.status == 401:
         raise UnauthorizedError()
     if response.status == 404:
         raise NotFoundError()
     if response.status == 500:
         raise HTTPException('Server error')
Exemplo n.º 3
0
 def get_rule(self, rulename: str, user: str) -> dict:
     """ Gets the specified rule.
     ---
     Parameters:
         - rulename: The rule name string.
         - user: The username string.
     Returns:
         A dictionary with a rule, where it has rule_name, type,
         data and frequency
     Throws:
         - BadRequestError: If the request is malformed.
         - NotFoundError: If the rule does not exist.
         - HTTPException: On an unhandled 500 error.
     """
     form: str = urlencode({'username': user})
     headers: dict = {'Content-type': 'application/x-www-form-urlencoded'}
     connection: HTTPConnection = self.__get_connection()
     connection.request('GET', '/rule/' + quote(str(rulename)), form,
                        headers)
     response: HTTPResponse = connection.getresponse()
     if response.status == 200:
         response_data_json = response.read()
         return json.loads(response_data_json)
     if response.status == 400:
         raise BadRequestError()
     if response.status == 401:
         raise UnauthorizedError()
     if response.status == 404:
         raise NotFoundError()
     if response.status == 500:
         raise HTTPException('Server error')
     return {}
Exemplo n.º 4
0
    def cambiarreglas(self, regla, tipo_sensor, username) -> dict:
        """ Método que cambia las reglas de los sensores.
        ---
        Parameters:
            - regla: regla a cambiar
            - tipo_sensor: tipo del sensor
        Returns:
            Diccionario con los valores de las reglas del sensor
        """
        if self.__auth_service.is_running() == True:
            status = self.__auth_service.has_right(username, 'AdminRules')
            if status == 200:
                if self.__sensor1_service.is_running() == True:
                    dict_respuesta = self.__sensor1_service.actualizarlasreglas(
                        tipo_sensor, regla)
                else:
                    dict_respuesta = {}

                return dict_respuesta
            elif status == 404:
                raise UnauthorizedError()
            else:
                print("ERROR: ....", status)
                raise Exception
        return {}
Exemplo n.º 5
0
 def get_all_rules(self, user: str) -> List[dict]:
     """ Gets the list of rules.
     ---
     Parameters:
         - user: The username string.
     Returns:
         A dictionary with the list of rules, where each has rule_name, type,
         data and frequency
     Throws:
         - HTTPException: On an unhandled 500 error.
     """
     form: str = urlencode({'username': user})
     headers: dict = {'Content-type': 'application/x-www-form-urlencoded'}
     connection: HTTPConnection = self.__get_connection()
     connection.request('GET', '/rules/', form, headers)
     response: HTTPResponse = connection.getresponse()
     if response.status == 200:
         response_data_json = response.read()
         lista = json.loads(response_data_json)
         retorno: List[dict] = []
         for rule in lista:
             retorno.append(json.loads(rule))
         return retorno
     if response.status == 401:
         raise UnauthorizedError()
     if response.status == 500:
         raise HTTPException('Server error')
     return []
Exemplo n.º 6
0
 def delete_rule(self, rulename: str, user: str):
     """ Removes a specified rule.
     ---
     Parameters:
         - rulename: The rule name string.
         - user: The username string.
     Throws:
         - BadRequestError: If the request is malformed.
         - NotFoundError: If the rule does not exist.
         - HTTPException: On an unhandled 500 error.
     """
     form: str = urlencode({'username': user})
     headers: dict = {'Content-type': 'application/x-www-form-urlencoded'}
     connection: HTTPConnection = self.__get_connection()
     connection.request('DELETE', '/rule/' + quote(str(rulename)), form,
                        headers)
     response: HTTPResponse = connection.getresponse()
     if response.status == 200:
         return
     if response.status == 400:
         raise BadRequestError()
     if response.status == 401:
         raise UnauthorizedError()
     if response.status == 404:
         raise NotFoundError()
     if response.status == 500:
         raise HTTPException('Server error')
Exemplo n.º 7
0
    def dar_quitar_permisos(self, usernameAdmin: str, usernameChanges: str,
                            rightChanges: int, session_id: str,
                            dar_quitar: str):
        """ modifica los permisos de un usurio en el sistema.
        ---
        Parameters:
            - usernameAdmin: Usuario admin que realiza la operacion.
            - username: nombre del usuario a modificar permisos
            - password: contrasena del usuario a modificar permisos
            - session_id: id de la session actual
        Returns:
            Status of the action
        Throws:
            - UnauthorizedError: If the provided session is incorrect or closed.
            - HTTPException: On an unhandled 500 error.
        """

        form: str = urlencode({'session_id': session_id})
        headers: dict = {'Content-type': 'application/x-www-form-urlencoded'}
        right_change: str = ''
        if rightChanges == 1:
            right_change = 'AdminUsers'
        elif rightChanges == 2:
            right_change = 'AdminRights'
        elif rightChanges == 3:
            right_change = 'AdminSensors'
        elif rightChanges == 4:
            right_change = 'AdminRules'
        elif rightChanges == 5:
            right_change = 'ViewReports'

        connection: HTTPConnection = self.__get_connection()
        connection.request('GET',
                           '/users/' + usernameAdmin + '/rights/AdminRights')
        response: HTTPResponse = connection.getresponse()
        if response.status == 200:
            connection.request(
                dar_quitar,
                '/users/' + usernameChanges + '/rights/' + right_change, form,
                headers)
            response = connection.getresponse()
            if response.status == 200:
                return response.status
            elif response.status == 500:
                raise HTTPException('Server error')
            else:
                print(
                    "ERROR, no se de el permiso correctamente por el error --> ",
                    response.status)
                return response.status
        elif response.status == 401:
            raise UnauthorizedError()
        else:
            print("Error....", response.status)
            return response.status
Exemplo n.º 8
0
    def logout(self, session_id: str):
        """ Logs out a user from the authentication server.
        ---
        Parameters:
            - session_id: The session id string.
        Throws:
            - UnauthorizedError: If the provided session is incorrect or closed.
            - HTTPException: On an unhandled 500 error.
        """
        form: str = urlencode({'session_id': session_id})
        headers: dict = {'Content-type': 'application/x-www-form-urlencoded'}
        connection: HTTPConnection = self.__get_connection()
        connection.request('DELETE', '/sessions', form, headers)
        response: HTTPResponse = connection.getresponse()
        if response.status == 200:
            return

        if response.status == 401:
            raise UnauthorizedError()
        if response.status == 500:
            raise HTTPException('Server error')
Exemplo n.º 9
0
    def newUser(self, usernameAdmin: str, username: str, password: str,
                session_id: str):
        """ Crea un nuevo usurio en el sistema.
        ---
        Parameters:
            - usernameAdmin: Usuario admin que realiza la operacion.
            - username: nombre del nuevo usuario
            - password: contrasena del nuevo usuario
            - session_id: id de la session actual
        Returns:
            Status of the action
        Throws:
            - UnauthorizedError: If the provided session is incorrect or closed.
        """

        form: str = urlencode({
            'username': username,
            'password': password,
            'session_id': session_id
        })
        headers: dict = {'Content-type': 'application/x-www-form-urlencoded'}
        connection: HTTPConnection = self.__get_connection()
        connection.request('GET',
                           '/users/' + usernameAdmin + '/rights/AdminUsers')
        response: HTTPResponse = connection.getresponse()
        if response.status == 200:
            connection = self.__get_connection()
            connection.request('POST', '/users', form, headers)
            response = connection.getresponse()
            if response.status == 200:
                return response.status
            else:
                print("Usuario no creado con éxito --> ", response.status)
        elif response.status == 401:
            raise UnauthorizedError()
        else:
            print("ERROR: ....", response.status)
            return response.status

        return response.status
Exemplo n.º 10
0
 def create_rule(self, rulename: str, ruletype: str, ruleargs: str,
                 frequency: int, user: str):
     """ Creates a new rule.
     ---
     Parameters:
         - rulename: The rule name string.
         - ruletype: The type of the rule string. (text: command, file)
         - ruleargs: A command or a file path.
         - frequency (seconds): 0 if it does not execute automatically.
         - user: The username string.
     Throws:
         - BadRequestError: If the request is malformed.
         - ConflictError: If the rule already exists.
         - HTTPException: On an unhandled 500 error.
     """
     form: str = urlencode({
         'rule_name': rulename,
         'type': ruletype,
         'data': ruleargs,
         'frequency': frequency,
         'username': user
     })
     headers: dict = {'Content-type': 'application/x-www-form-urlencoded'}
     connection: HTTPConnection = self.__get_connection()
     connection.request('POST', '/rule/', form, headers)
     response: HTTPResponse = connection.getresponse()
     if response.status == 200:
         return
     if response.status == 400:
         raise BadRequestError()
     if response.status == 401:
         raise UnauthorizedError()
     if response.status == 409:
         raise ConflictError()
     if response.status == 500:
         raise HTTPException('Server error')