Exemplo n.º 1
0
 def check_version(self):
     """Check the version of the client and the server.
     :return:
     """
     response = requests.get(self.client.api_url)
     parse_error(response, json=False)
     version = int(response.content.decode("utf-8").split("\n")[1])
     if version != CLIENT_VERSION:
         raise VersionDiscrepancy(
             f"The Version of the server is {version} and the version of the client {CLIENT_VERSION}: Please "
             f"update the client or the server! ")
Exemplo n.º 2
0
 def get(self,
         group: GroupTarget,
         key: str = None,
         private_key: str = None):
     """Fetch a group.
     :param group: The target to the group.
     :param key: The key of the group (if you have set the key in the group target, you must not set this parameter).
     :param private_key: The private_key of the group (if you have set the private_key in the group target,
             you must not set this parameter).
     :return: :class:`Group`.
     """
     key, private_key = check_group_values(group, key, private_key)
     path = f"{self.client.api_url}/group/{group.name}"
     response = requests.put(path,
                             data={
                                 "key": key,
                                 "private_key": private_key
                             })
     j = parse_error(response)
     return Group(
         group.name,
         key,
         j["hashed_key"],
         private_key,
         [FileTarget(key, file, group=group) for file in j["files"]],
     )
Exemplo n.º 3
0
 def delete_authentication_token(self, authentication_token):
     """Delete a authentication token on the server.
     :param authentication_token: The authentication token to authenticate.
     :return:
     """
     response = requests.delete(
         f"{self.client.api_url}/authentication",
         data={"authentication_token": authentication_token},
     )
     j = parse_error(response)
     if not ("status" in j and j["status"] == "success"):
         raise BasicError()
Exemplo n.º 4
0
 def test_authentication(self, authentication_token):
     """Test the authentication token (Attention: don't use this function in production, because the action needs some time).
     :param authentication_token: The authentication token to authenticate.
     :return:
     """
     response = requests.post(
         f"{self.client.api_url}/authentication/test",
         data={"authentication_token": authentication_token},
     )
     j = parse_error(response)
     if not ("status" in j and j["status"] == "success"):
         raise BasicError()
Exemplo n.º 5
0
 def get(
     self,
     key,
     filename,
     group: GroupTarget = None,
     read_from_cache=True,
     write_in_cache=True,
 ):
     """Download/Get any content of the file.
     :param key: The file is encrypted with this key.
     :param filename: The name of the file.
     :param group: If you would like to download a file which belongs to any group, you must hand over the group target.
     :param read_from_cache: If the file is cached, the function would return the cached file.
     :param write_in_cache: If the file has been successfully loaded, the file would be cached.
     :return: :class:`File`
     """
     if read_from_cache and group is None:
         for file in self.cached_files:
             if file.key == key and file.filename == filename:
                 return file
     if group is None:
         response = requests.get(f"{self.client.api_url}/{key}/{filename}")
         parse_error(response, check_json=True)
         file = File(key, None, filename, None, response.content)
         if write_in_cache:
             self.cached_files.append(file)
         return file
     else:
         key = check_group_values(group, key)
         response = requests.get(
             f"{self.client.api_url}/{group.name}/{key}/{filename}")
         parse_error(response, check_json=True)
         return File(key,
                     None,
                     filename,
                     None,
                     response.content,
                     group=group)
Exemplo n.º 6
0
 def create_authentication_token(self, key_identifier, private_key: RSA.RsaKey):
     """Create new authentication token
     :param key_identifier: The identifier of the key (the variable key on the server).
     :param private_key: The :class:`RSA.RsaKey` Key for authentication.
     :return: string authentication_token
     """
     response = requests.post(
         f"{self.client.api_url}/authentication",
         data={"key_identifier": key_identifier},
     )
     j = parse_error(response)
     encrypted_authentication_token = j["encrypted_authentication_token"]
     return (
         PKCS1_OAEP.new(private_key)
         .decrypt(base64.b64decode(encrypted_authentication_token.encode("utf-8")))
         .decode("utf-8")
     )
Exemplo n.º 7
0
 def delete(self, group: GroupTarget, private_key: str = None):
     """Delete a group and all files in the group.
     :param group: The target to the group.
     :param private_key: The private_key of the group (if you have set the private_key in the group target,
             you must not set this parameter).
     :return:
     """
     if group.private_key is None and private_key is None:
         raise ValueError(
             "The GroupTarget does not provide any private_key, so you put the private_key in the function!"
         )
     if private_key is None:
         private_key = group.private_key
     path = f"{self.client.api_url}/group/{group.name}"
     response = requests.delete(path, data={"private_key": private_key})
     j = parse_error(response)
     if "status" not in j or j["status"] != "success":
         raise BasicError()
Exemplo n.º 8
0
    def delete(self, file: FileTarget, key=None, private_key=None):
        """Delete a file.
        :param file: The target of the deleting file.
        :param private_key: If the target isn't loaded, you should set a private_key yourself.

        Group File Deletion
        ==========
        If the file belongs to any group (the file.group is not None),
        the function will delete the group file automatically.
        If you don't hand over the key or private_key in the group target you need following parameters:
        :param key: The key of the group.
        :param private_key: The private_key of the group.
        """
        if ((not file.loaded and private_key is None) or
            (private_key is None and file.file.private_key is None)) and (
                file.group is None or file.group.private_key is None):
            raise ValueError(
                "The file is not loaded and no private_key was set or the file doesn't have any private_key"
            )
        if private_key is None:
            private_key = file.file.private_key
        if private_key is None:
            private_key = file.group.private_key
        if file.group is None:
            response = requests.delete(
                f"{self.client.api_url}/{file.key}/{file.filename}",
                data={"private_key": private_key},
            )
        else:
            key = check_group_values(file.group, key)
            response = requests.delete(
                f"{self.client.api_url}/{file.group.name}/{key}/{file.filename}",
                data={"private_key": private_key},
            )
        j = parse_error(response)
        if not ("status" in j and j["status"] == "success"):
            raise BasicError()

        for f in self.cached_files:
            if f.key == file.key and f.filename == file.filename:
                self.cached_files.remove(f)
Exemplo n.º 9
0
 def post(self,
          group_name: str,
          private_key: str = None,
          authentication_token: str = None) -> Group:
     """Create a new group.
     :param group_name: The name of the new group.
     :param private_key: You can set a private_key. If you don't set any private_key the server will generate
             a random private_key.
     :param authentication_token: If upload_authentication is required, you must set the authentication_token.
     :return: The new :class:`Group`.
     """
     path = f"{self.client.api_url}/group/{group_name}"
     response = requests.post(
         path,
         data={
             "authentication_token": authentication_token,
             "private_key": private_key,
         },
     )
     j = parse_error(response)
     return Group(j["name"], j["key"], j["hashed_key"], j["private_key"],
                  [])
Exemplo n.º 10
0
    def post(
        self,
        raw_file,
        filename,
        group: GroupTarget = None,
        key: str = None,
        private_key: str = None,
        authentication_token=None,
    ):
        """Create/Upload a file.
        :param raw_file: The file (open it with open()).
        :param filename: The name of the file.
        :param authentication_token: If the server requires authentication for posting data,
        you should set the authentication_token.

        Group Upload
        ==========
        :param group: The target to the group of the file.
        :param key: The key of the group (
        you don't need the key, if you have hand over the key in the group target).
        :param private_key: The
        private_key of the group (you don't need the private_key, if you have hand over the private_key in the group
        target).

        :return: :class:`File`

        Usage::
            filename = 'test.test'
            file = file_manager.post(open(filename, 'r'), filename)
        """
        if group is None:
            response = requests.post(
                f"{self.client.api_url}/upload",
                files={"file": (filename, raw_file)},
                data={
                    "authentication_token": authentication_token,
                    "private_key": private_key,
                },
            )
            j = parse_error(response)
            file = File(j["key"], j["hashed_key"], j["filename"],
                        j["private_key"], None)
        else:
            key, private_key = check_group_values(group, key, private_key)
            response = requests.post(
                f"{self.client.api_url}/upload",
                files={"file": (filename, raw_file)},
                data={
                    "authentication_token": authentication_token,
                    "group": group.name,
                    "key": key,
                    "private_key": private_key,
                },
            )
            j = parse_error(response)
            file = File(
                j["key"],
                j["hashed_key"],
                j["filename"],
                j["private_key"],
                None,
                group=group,
            )
        return file