Update app.py
Browse files
app.py
CHANGED
@@ -19,7 +19,8 @@ from cryptography.hazmat.primitives.asymmetric import utils
|
|
19 |
import base64
|
20 |
import json
|
21 |
import datetime
|
22 |
-
|
|
|
23 |
def load_public_key_from_file(file_path):
|
24 |
with open(file_path, "rb") as key_file:
|
25 |
public_key = serialization.load_pem_public_key(
|
@@ -61,11 +62,17 @@ def swap_face(source_file, target_file, doFaceEnhancer, skey, key):
|
|
61 |
signature = base64.b64decode(skey["s"])
|
62 |
if not skey or not key or not verify_signature(public_key, skey["t"] + "_" + key, signature):
|
63 |
raise Exception("verify authkey failed.")
|
64 |
-
timestamp_requested =
|
65 |
timestamp_now = int(datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).timestamp())
|
66 |
if timestamp_now - timestamp_requested > 600:
|
67 |
raise Exception(f"authkey timeout, {timestamp_now - timestamp_requested}")
|
68 |
print(f"authkey pass, {timestamp_now - timestamp_requested}")
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
source_path = "input.jpg"
|
70 |
target_path = "target.jpg"
|
71 |
|
|
|
19 |
import base64
|
20 |
import json
|
21 |
import datetime
|
22 |
+
import pylru
|
23 |
+
token_queue = pylru.lrucache(1000)
|
24 |
def load_public_key_from_file(file_path):
|
25 |
with open(file_path, "rb") as key_file:
|
26 |
public_key = serialization.load_pem_public_key(
|
|
|
62 |
signature = base64.b64decode(skey["s"])
|
63 |
if not skey or not key or not verify_signature(public_key, skey["t"] + "_" + key, signature):
|
64 |
raise Exception("verify authkey failed.")
|
65 |
+
timestamp_requested = float(skey["t"])
|
66 |
timestamp_now = int(datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).timestamp())
|
67 |
if timestamp_now - timestamp_requested > 600:
|
68 |
raise Exception(f"authkey timeout, {timestamp_now - timestamp_requested}")
|
69 |
print(f"authkey pass, {timestamp_now - timestamp_requested}")
|
70 |
+
|
71 |
+
token = skey["t"]
|
72 |
+
token_cache = token_queue.get(token, None)
|
73 |
+
if token_cache:
|
74 |
+
raise Exception(f"duplicated token, {token_cache}")
|
75 |
+
token_queue[token] = True
|
76 |
source_path = "input.jpg"
|
77 |
target_path = "target.jpg"
|
78 |
|