Spaces:
Runtime error
Runtime error
Threatthriver
commited on
Commit
•
880d4f5
1
Parent(s):
0358da9
Rename api.key to api.py
Browse files
api.key
DELETED
File without changes
|
api.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import secrets
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
|
5 |
+
API_KEYS_FILE = 'api_keys.json'
|
6 |
+
|
7 |
+
def generate_api_key():
|
8 |
+
"""Generate a secure random API key."""
|
9 |
+
return secrets.token_hex(32)
|
10 |
+
|
11 |
+
def save_api_key(user_id, api_key):
|
12 |
+
"""Save the API key associated with a user."""
|
13 |
+
if os.path.exists(API_KEYS_FILE):
|
14 |
+
with open(API_KEYS_FILE, 'r') as f:
|
15 |
+
api_keys = json.load(f)
|
16 |
+
else:
|
17 |
+
api_keys = {}
|
18 |
+
|
19 |
+
api_keys[user_id] = api_key
|
20 |
+
|
21 |
+
with open(API_KEYS_FILE, 'w') as f:
|
22 |
+
json.dump(api_keys, f)
|
23 |
+
|
24 |
+
def generate_and_save_api_key(user_id):
|
25 |
+
"""Generate an API key and save it."""
|
26 |
+
api_key = generate_api_key()
|
27 |
+
save_api_key(user_id, api_key)
|
28 |
+
print(f"API Key for {user_id}: {api_key}")
|
29 |
+
|
30 |
+
if __name__ == "__main__":
|
31 |
+
user_id = input("Enter user ID: ")
|
32 |
+
generate_and_save_api_key(user_id)
|