TahaFawzyElshrif commited on
Commit
afbde84
·
1 Parent(s): ced802e
Files changed (3) hide show
  1. Consumer.py +1 -1
  2. app.py +47 -1
  3. requirements.txt +2 -1
Consumer.py CHANGED
@@ -76,7 +76,7 @@ def callback(ch, method, properties, body):
76
 
77
  # simulate processing
78
  print(f"TYPE {type(recieved_msg)}, CONTENT {recieved_msg}")
79
- model_call(recieved_msg)
80
 
81
  # (put your logic here)
82
  print(f"CONSUMER {consumer_id}:::: Processing done")
 
76
 
77
  # simulate processing
78
  print(f"TYPE {type(recieved_msg)}, CONTENT {recieved_msg}")
79
+ #model_call(recieved_msg)
80
 
81
  # (put your logic here)
82
  print(f"CONSUMER {consumer_id}:::: Processing done")
app.py CHANGED
@@ -39,7 +39,7 @@ print("Starting API Server...")
39
 
40
  class RequestModel(BaseModel):
41
  prompt: str
42
- ht_token : str
43
  user_email : str
44
  user_name : str
45
  memory: list[str]
@@ -49,6 +49,52 @@ class RequestAnswer(BaseModel):
49
  user_id: str
50
  msg_id:str
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  ##################################################
54
  # ROUTES
 
39
 
40
  class RequestModel(BaseModel):
41
  prompt: str
42
+ ht_token_encrypted_dumped : str
43
  user_email : str
44
  user_name : str
45
  memory: list[str]
 
49
  user_id: str
50
  msg_id:str
51
 
52
+ ##################################################
53
+ import os
54
+ import hashlib
55
+ from cryptography.hazmat.primitives.ciphers.aead import AESGCM
56
+
57
+
58
+ # ===== Key (same idea as Node) =====
59
+ def get_key():
60
+ raw_key = os.environ["TOKEN_KEY_ENCRYPTION"].encode()
61
+ return hashlib.sha256(raw_key).digest() # 32 bytes
62
+
63
+
64
+ # ===== Encrypt =====
65
+ def encrypt_token_to_json(token: str) -> dict:
66
+ key = get_key()
67
+ aesgcm = AESGCM(key)
68
+
69
+ iv = os.urandom(12) # same as crypto.randomBytes(12)
70
+
71
+ encrypted = aesgcm.encrypt(iv, token.encode(), None)
72
+
73
+ # في AESGCM في Python: التاج (tag) بيكون في آخر 16 بايت
74
+ ciphertext = encrypted[:-16]
75
+ tag = encrypted[-16:]
76
+
77
+ return {
78
+ "iv": iv.hex(),
79
+ "data": ciphertext.hex(),
80
+ "tag": tag.hex(),
81
+ }
82
+
83
+
84
+ # ===== Decrypt =====
85
+ def decrypt_token_from_json(enc: dict) -> str:
86
+ key = get_key()
87
+ aesgcm = AESGCM(key)
88
+
89
+ iv = bytes.fromhex(enc["iv"])
90
+ ciphertext = bytes.fromhex(enc["data"])
91
+ tag = bytes.fromhex(enc["tag"])
92
+
93
+ encrypted = ciphertext + tag
94
+
95
+ decrypted = aesgcm.decrypt(iv, encrypted, None)
96
+
97
+ return decrypted.decode()
98
 
99
  ##################################################
100
  # ROUTES
requirements.txt CHANGED
@@ -12,4 +12,5 @@ fastapi
12
  uvicorn
13
  markdown_pdf
14
  pika
15
- redis
 
 
12
  uvicorn
13
  markdown_pdf
14
  pika
15
+ redis
16
+ cryptography