Update app.py
Browse files
app.py
CHANGED
|
@@ -67,18 +67,47 @@ def sha256_hex(value: str) -> str:
|
|
| 67 |
|
| 68 |
def seed_api_keys_from_env():
|
| 69 |
"""
|
| 70 |
-
Seed API keys
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
"""
|
| 74 |
-
seed = os.getenv("API_KEYS_SEED", "")
|
| 75 |
if not seed:
|
| 76 |
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
conn = get_db_connection()
|
| 78 |
try:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
continue
|
| 83 |
owner, key = entry.split(":", 1)
|
| 84 |
owner = owner.strip()
|
|
@@ -86,7 +115,6 @@ def seed_api_keys_from_env():
|
|
| 86 |
if not owner or not key:
|
| 87 |
continue
|
| 88 |
key_hash = sha256_hex(key)
|
| 89 |
-
# Insert if not exists
|
| 90 |
conn.execute(
|
| 91 |
"""
|
| 92 |
INSERT OR IGNORE INTO api_keys (owner, key_hash, active, created_at)
|
|
@@ -94,7 +122,9 @@ def seed_api_keys_from_env():
|
|
| 94 |
""",
|
| 95 |
(owner, key_hash, datetime.utcnow().isoformat(timespec="seconds")),
|
| 96 |
)
|
|
|
|
| 97 |
conn.commit()
|
|
|
|
| 98 |
finally:
|
| 99 |
conn.close()
|
| 100 |
|
|
|
|
| 67 |
|
| 68 |
def seed_api_keys_from_env():
|
| 69 |
"""
|
| 70 |
+
Seed API keys from env var API_KEYS_SEED.
|
| 71 |
+
Accepts entries separated by commas, newlines, semicolons, or spaces.
|
| 72 |
+
Each entry must be 'owner:raw_key'. Example:
|
| 73 |
+
|
| 74 |
+
alice:MY_SUPER_KEY_1
|
| 75 |
+
bob:ANOTHER_KEY_2
|
| 76 |
+
|
| 77 |
+
or single-line:
|
| 78 |
+
alice:MY_SUPER_KEY_1,bob:ANOTHER_KEY_2
|
| 79 |
"""
|
| 80 |
+
seed = os.getenv("API_KEYS_SEED", "")
|
| 81 |
if not seed:
|
| 82 |
return
|
| 83 |
+
|
| 84 |
+
# Normalize separators into commas
|
| 85 |
+
normalized = seed.replace("\r", "\n")
|
| 86 |
+
for sep in ["\n", ";", " "]:
|
| 87 |
+
normalized = normalized.replace(sep, ",")
|
| 88 |
+
|
| 89 |
+
entries = [e.strip() for e in normalized.split(",") if e.strip()]
|
| 90 |
+
if not entries:
|
| 91 |
+
return
|
| 92 |
+
|
| 93 |
conn = get_db_connection()
|
| 94 |
try:
|
| 95 |
+
# Ensure schema exists
|
| 96 |
+
conn.execute(
|
| 97 |
+
"""
|
| 98 |
+
CREATE TABLE IF NOT EXISTS api_keys (
|
| 99 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 100 |
+
owner TEXT NOT NULL,
|
| 101 |
+
key_hash TEXT NOT NULL UNIQUE,
|
| 102 |
+
active INTEGER NOT NULL DEFAULT 1,
|
| 103 |
+
created_at TEXT NOT NULL
|
| 104 |
+
);
|
| 105 |
+
"""
|
| 106 |
+
)
|
| 107 |
+
from datetime import datetime
|
| 108 |
+
count = 0
|
| 109 |
+
for entry in entries:
|
| 110 |
+
if ":" not in entry:
|
| 111 |
continue
|
| 112 |
owner, key = entry.split(":", 1)
|
| 113 |
owner = owner.strip()
|
|
|
|
| 115 |
if not owner or not key:
|
| 116 |
continue
|
| 117 |
key_hash = sha256_hex(key)
|
|
|
|
| 118 |
conn.execute(
|
| 119 |
"""
|
| 120 |
INSERT OR IGNORE INTO api_keys (owner, key_hash, active, created_at)
|
|
|
|
| 122 |
""",
|
| 123 |
(owner, key_hash, datetime.utcnow().isoformat(timespec="seconds")),
|
| 124 |
)
|
| 125 |
+
count += 1
|
| 126 |
conn.commit()
|
| 127 |
+
print(f"Seeded {count} API key entries from API_KEYS_SEED")
|
| 128 |
finally:
|
| 129 |
conn.close()
|
| 130 |
|