File size: 1,028 Bytes
a5784e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import os
from typing import Set

API_KEYS: Set[str] = set()
KEY_FILE_PATH = os.path.join(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
    "auth_profiles",
    "key.txt",
)


def load_api_keys():
    """Loads API keys from the key file into the API_KEYS set."""
    global API_KEYS
    API_KEYS.clear()
    if os.path.exists(KEY_FILE_PATH):
        with open(KEY_FILE_PATH, "r") as f:
            for line in f:
                key = line.strip()
                if key:
                    API_KEYS.add(key)


def initialize_keys():
    """Initializes API keys. Ensures key.txt exists and loads keys."""
    if not os.path.exists(KEY_FILE_PATH):
        with open(KEY_FILE_PATH, "w"):
            pass  # Create an empty file
    load_api_keys()


def verify_api_key(api_key_from_header: str) -> bool:
    """
    Verifies the API key.
    Returns True if API_KEYS is empty (no validation) or if the key is valid.
    """
    if not API_KEYS:
        return True
    return api_key_from_header in API_KEYS