fix auth, decryption, api interaction

This commit is contained in:
Alex Bezdieniezhnykh
2025-01-20 10:17:35 +02:00
parent e21dd7e70f
commit ce25ef38b0
12 changed files with 146 additions and 103 deletions
+37 -23
View File
@@ -1,11 +1,13 @@
import io
import json
import os
from http import HTTPStatus
import requests
cimport constants
from hardware_service cimport HardwareService, HardwareInfo
from security import Security
from security cimport Security
from io import BytesIO
cdef class ApiClient:
"""Handles API authentication and downloading of the AI model."""
@@ -20,45 +22,57 @@ cdef class ApiClient:
self.token = None
cdef get_encryption_key(self, str hardware_hash):
return f'{self.email}-{self.password}-{hardware_hash}-#%@AzaionKey@%#---'
cdef str key = f'{self.email}-{self.password}-{hardware_hash}-#%@AzaionKey@%#---'
return Security.calc_hash(key)
cdef login(self, str email, str password, bint persist_token=False):
cdef login(self, str email, str password):
response = requests.post(f"{constants.API_URL}/login", json={"email": email, "password": password})
response.raise_for_status()
self.token = response.json()["token"]
print(f'')
if persist_token:
with open(<str>constants.TOKEN_FILE, 'w') as file:
file.write(self.token)
with open(<str>constants.TOKEN_FILE, 'w') as file:
file.write(self.token)
cdef bytes load_file(self, str filename, bint persist_token=False):
cdef load_bytes(self, str filename):
hardware_service = HardwareService()
cdef HardwareInfo hardware = hardware_service.get_hardware_info()
if self.token is None:
self.login(self.email, self.password, persist_token)
self.login(self.email, self.password)
url = f"{constants.API_URL}/resources/get/{self.folder}"
headers = {"Authorization": f"Bearer {self.token}"}
payload = {
"password": self.password,
"hardware": hardware,
"fileName": filename
headers = {
"Authorization": f"Bearer {self.token}",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers, stream=True)
payload = json.dumps(
{
"password": self.password,
"hardware": hardware.to_json_object(),
"fileName": filename
}, indent=4)
response = requests.post(url, data=payload, headers=headers, stream=True)
if response.status_code == HTTPStatus.UNAUTHORIZED or response.status_code == HTTPStatus.FORBIDDEN:
self.login(self.email, self.password, persist_token)
response = requests.post(url, json=payload, headers=headers, stream=True)
self.login(self.email, self.password)
headers = {
"Authorization": f"Bearer {self.token}",
"Content-Type": "application/json"
}
response = requests.post(url, data=payload, headers=headers, stream=True)
if response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR:
print('500!')
key = self.get_encryption_key(hardware.hash)
encrypted_stream = io.BytesIO(response.content)
decrypted_stream = io.BytesIO()
Security.decrypt_to(encrypted_stream, decrypted_stream, key)
return decrypted_stream
cdef bytes load_ai_model(self):
return self.load_file(constants.AI_MODEL_FILE, <bint>True)
stream = BytesIO(response.raw.read())
return Security.decrypt_to(stream, key)
cdef load_ai_model(self):
return self.load_bytes(constants.AI_MODEL_FILE)
cdef load_queue_config(self):
return self.load_bytes(constants.QUEUE_CONFIG_FILENAME).decode(encoding='utf-8')