Spaces:
Running
Running
File size: 2,503 Bytes
a2543e1 6c66675 a2543e1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
"""
Generate encrypted request payload for Postman
"""
import base64
import gzip
import json
import sys
import os
from nacl.secret import SecretBox
from nacl.utils import random
# Your 64-character hex key from .env
SECRET_KEY_HEX = "7633eeaf69156124e49025ce8f6a3adbdbf6be87f1e58529397a67168a65bd66"
# Convert hex to bytes (32 bytes)
SECRET_KEY = bytes.fromhex(SECRET_KEY_HEX)
if len(SECRET_KEY) != 32:
print(f"β ERROR: Key must be 32 bytes, got {len(SECRET_KEY)}")
sys.exit(1)
print(f"β Key length: {len(SECRET_KEY)} bytes")
# Get file path
if len(sys.argv) > 1:
file_path = sys.argv[1]
else:
file_path = "tests/samples/sample1.pdf"
if not os.path.exists(file_path):
print(f"β File not found: {file_path}")
sys.exit(1)
print(f"π Reading: {file_path}")
# Read your test file
with open(file_path, 'rb') as f:
file_data = f.read()
print(f"β Read {len(file_data):,} bytes")
# Determine file type
if file_path.lower().endswith('.pdf'):
file_type = "pdf"
elif file_path.lower().endswith(('.jpg', '.jpeg', '.png', '.tiff', '.bmp')):
file_type = "image"
else:
file_type = "image" # default
print(f"π File type: {file_type}")
# Prepare payload
payload = {
"filename": os.path.basename(file_path),
"file_type": file_type,
"file_data": base64.b64encode(file_data).decode('utf-8'),
"patient_id": "TEST_001"
}
print("π Encrypting...")
# Encrypt
json_data = json.dumps(payload).encode('utf-8')
compressed = gzip.compress(json_data, compresslevel=6)
compressed_b64 = base64.b64encode(compressed).decode('utf-8')
box = SecretBox(SECRET_KEY) # Use bytes directly, not .encode()
nonce = random(SecretBox.NONCE_SIZE)
ciphertext = box.encrypt(compressed_b64.encode('utf-8'), nonce)
encrypted_request = {
"ciphertext": base64.b64encode(ciphertext.ciphertext).decode('utf-8'),
"nonce": base64.b64encode(nonce).decode('utf-8')
}
# Save for Postman
with open('postman_request.json', 'w') as f:
json.dump(encrypted_request, f, indent=2)
print("\n" + "="*70)
print("β Generated postman_request.json")
print("="*70)
print("\nπ COPY THIS JSON FOR POSTMAN:")
print("-"*70)
print(json.dumps(encrypted_request, indent=2))
print("-"*70)
print("\nπ POSTMAN INSTRUCTIONS:")
print("1. Method: POST")
print("2. URL: http://localhost:8000/analyze-lab-secure")
print("3. Headers: Content-Type = application/json")
print("4. Body β raw β JSON")
print("5. Paste the JSON above")
print("6. Click Send")
print("\nβ
Done!")
|