From 5a486c86bbee5eee567d77cb1b5cf912a031a948 Mon Sep 17 00:00:00 2001 From: tuddman Date: Mon, 1 Apr 2024 22:57:59 +0200 Subject: [PATCH] fix: make upload-client.py work with HMAC correctl --- .gitignore | 1 + README.md | 24 +++++++++++++++++++++++- requirements.txt | 1 + upload-client.py | 35 ++++++++++++++++++++++++----------- 4 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index 4e67f91..8b0b2a8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target uploads/ +myenv diff --git a/README.md b/README.md index 8f6677f..f25de88 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,13 @@ A simple, asynchronous file server to support XMTP Message History transfers. Ru Ensure you have the following installed: -- Rust and Cargo. You can install them from [https://rustup.rs/](https://rustup.rs/) +- Rust and Cargo. You can install them both from [https://rustup.rs](https://rustup.rs) +- To run the example uploader, ensure python is installed on your system. + +Set the `SECRET_KEY` environment variable. + + export SECRET_KEY=super-long-super-secret-unique-key-goes-here + ### Installing @@ -45,6 +51,22 @@ Example using curl: curl http://0.0.0.0:5558/files/{id} --output retrieved_file.aes +### Example Client Uploader + +Set up a virtual environment + + python3 -m venv myenv + source myenv/bin/activate + +Install the dependencies + + pip3 install -r requirements.txt + +Run the uploader script + + python3 upload-client.py + + ## Contributing Contributions are welcome! Please feel free to submit a pull request or open an issue. diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2c24336 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests==2.31.0 diff --git a/upload-client.py b/upload-client.py index c220620..6bf369f 100644 --- a/upload-client.py +++ b/upload-client.py @@ -1,18 +1,31 @@ - import hmac import hashlib import requests +import os + +file_path = "test_bundle.txt" + +secret_key = os.environ.get("SECRET_KEY", "").encode() + +# Ensure the secret key is not empty +if not secret_key: + print("SECRET_KEY environment variable is not set.") + exit(1) -# The shared secret key -secret_key = b"your-secret-key" +# The request payload consisting of a message history bundle +with open(file_path, 'rb') as file: + file_content = file.read() + print(file_content) -# The request payload -payload = b"your request payload" + # Compute the HMAC + hmac_instance = hmac.new(secret_key, file_content, hashlib.sha256) + hmac_hex = hmac_instance.hexdigest() + print(hmac_hex) -# Compute the HMAC -hmac_instance = hmac.new(secret_key, payload, hashlib.sha256) -hmac_hex = hmac_instance.hexdigest() + # Send the request with the HMAC header + headers = {'X-HMAC': hmac_hex} + response = requests.post('http://0.0.0.0:5558/upload', headers=headers, data=file_content) -# Send the request with the HMAC header -headers = {'X-HMAC': hmac_hex} -response = requests.post('http://0.0.0.0:5558/upload', headers=headers, data=payload) +# Log the response +print(f"Response Status Code: {response.status_code}") +print(f"Response Body: {response.text}")