ProyectoGrupal/VerifactuMidAPI/test/test_personal.py

41 lines
1.3 KiB
Python

import base64
import json
from urllib.request import urlopen, Request
from urllib.error import URLError
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.backends import default_backend
API_URL = "http://localhost:6789"
# Get public key
req = Request(f"{API_URL}/api/v1/auth/public-key", method="GET")
with urlopen(req, timeout=10) as response:
result = json.loads(response.read().decode())
pub_key_pem = base64.b64decode(result["public_key"])
# Encrypt password
public_key = serialization.load_pem_public_key(pub_key_pem, default_backend())
password = "Mecedora12"
encrypted = public_key.encrypt(password.encode(), padding.PKCS1v15())
encrypted_b64 = base64.b64encode(encrypted).decode()
# Register certificate
data = {
"cert_name": "personal",
"cert_path": "C:/Users/jmest/GolandProjects/VerifactuMidAPI/data/certs/personal.p12",
"password_encrypted": encrypted_b64
}
req = Request(
f"{API_URL}/api/v1/auth/register",
data=json.dumps(data).encode(),
method="POST"
)
req.add_header("Content-Type", "application/json")
try:
with urlopen(req, timeout=30) as response:
result = json.loads(response.read().decode())
print(json.dumps(result, indent=2))
except URLError as e:
print(f"Error: {e}")