|
| 1 | +"""API Key Store — server-side key management for MCP client authentication. |
| 2 | +
|
| 3 | +Implements admin-managed API keys with SHA-256 hashing, thread-safe |
| 4 | +file-backed persistence, and prefix-based identification/revocation. |
| 5 | +
|
| 6 | +Keys are stored as SHA-256 hashes in a JSON file (/data/keys.json by |
| 7 | +default). Raw keys are never persisted — only returned once at |
| 8 | +generation time. |
| 9 | +
|
| 10 | +Design mirrors the GDP MCP Server's Model B: Admin-Managed Key Store. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import hashlib |
| 16 | +import json |
| 17 | +import logging |
| 18 | +import os |
| 19 | +import secrets |
| 20 | +import threading |
| 21 | +from dataclasses import asdict, dataclass, field |
| 22 | +from datetime import datetime, timezone |
| 23 | +from pathlib import Path |
| 24 | +from typing import Any |
| 25 | + |
| 26 | +logger = logging.getLogger(__name__) |
| 27 | + |
| 28 | +# Default location — mounted as a Docker volume for persistence |
| 29 | +DEFAULT_KEYS_FILE = os.getenv("KEYS_FILE", "/data/keys.json") |
| 30 | + |
| 31 | + |
| 32 | +@dataclass |
| 33 | +class KeyRecord: |
| 34 | + """Metadata for a single API key (raw key is never stored).""" |
| 35 | + |
| 36 | + hash: str # SHA-256 hex digest of the raw key |
| 37 | + prefix: str # First 8 characters of the raw key (for identification) |
| 38 | + user: str # Email / label of the authorised user |
| 39 | + created_at: str = field( # ISO-8601 timestamp |
| 40 | + default_factory=lambda: datetime.now(timezone.utc).isoformat() |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +class KeyStore: |
| 45 | + """Thread-safe, file-backed API key store with SHA-256 hashing. |
| 46 | +
|
| 47 | + Public API: |
| 48 | + generate(user) → raw key (shown once) |
| 49 | + validate(raw_key) → bool |
| 50 | + list_keys() → list of {prefix, user, created_at} |
| 51 | + revoke(prefix) → bool |
| 52 | + has_any_keys() → bool |
| 53 | + """ |
| 54 | + |
| 55 | + def __init__(self, keys_file: str = DEFAULT_KEYS_FILE) -> None: |
| 56 | + self._path = Path(keys_file) |
| 57 | + self._lock = threading.Lock() |
| 58 | + self._keys: list[KeyRecord] = [] |
| 59 | + self._load() |
| 60 | + |
| 61 | + # ── Public API ────────────────────────────────────────────────── |
| 62 | + |
| 63 | + def generate(self, user: str) -> str: |
| 64 | + """Generate a new 64-char hex API key for *user*. |
| 65 | +
|
| 66 | + Returns the raw key (displayed once — never stored in plain text). |
| 67 | + """ |
| 68 | + raw_key = secrets.token_hex(32) # 64 hex chars |
| 69 | + record = KeyRecord( |
| 70 | + hash=self._hash(raw_key), |
| 71 | + prefix=raw_key[:8], |
| 72 | + user=user, |
| 73 | + ) |
| 74 | + with self._lock: |
| 75 | + self._keys.append(record) |
| 76 | + self._save() |
| 77 | + logger.info("Generated API key for user=%s prefix=%s", user, record.prefix) |
| 78 | + return raw_key |
| 79 | + |
| 80 | + def validate(self, raw_key: str) -> bool: |
| 81 | + """Return True if *raw_key* matches any stored hash.""" |
| 82 | + key_hash = self._hash(raw_key) |
| 83 | + with self._lock: |
| 84 | + return any(k.hash == key_hash for k in self._keys) |
| 85 | + |
| 86 | + def list_keys(self) -> list[dict[str, str]]: |
| 87 | + """Return a list of key metadata (prefix, user, created_at) — no hashes.""" |
| 88 | + with self._lock: |
| 89 | + return [ |
| 90 | + {"prefix": k.prefix, "user": k.user, "created_at": k.created_at} |
| 91 | + for k in self._keys |
| 92 | + ] |
| 93 | + |
| 94 | + def revoke(self, prefix: str) -> bool: |
| 95 | + """Revoke (delete) the key identified by *prefix*. Returns True if found.""" |
| 96 | + with self._lock: |
| 97 | + before = len(self._keys) |
| 98 | + self._keys = [k for k in self._keys if k.prefix != prefix] |
| 99 | + if len(self._keys) < before: |
| 100 | + self._save() |
| 101 | + logger.info("Revoked API key with prefix=%s", prefix) |
| 102 | + return True |
| 103 | + logger.warning("Revoke failed — no key with prefix=%s", prefix) |
| 104 | + return False |
| 105 | + |
| 106 | + def has_any_keys(self) -> bool: |
| 107 | + """Return True if at least one key exists.""" |
| 108 | + with self._lock: |
| 109 | + return len(self._keys) > 0 |
| 110 | + |
| 111 | + # ── Internal helpers ──────────────────────────────────────────── |
| 112 | + |
| 113 | + @staticmethod |
| 114 | + def _hash(raw_key: str) -> str: |
| 115 | + return hashlib.sha256(raw_key.encode()).hexdigest() |
| 116 | + |
| 117 | + def _load(self) -> None: |
| 118 | + """Load keys from disk (if the file exists).""" |
| 119 | + if not self._path.exists(): |
| 120 | + logger.info("No keys file at %s — starting with empty store", self._path) |
| 121 | + return |
| 122 | + try: |
| 123 | + data: list[dict[str, Any]] = json.loads(self._path.read_text()) |
| 124 | + self._keys = [KeyRecord(**rec) for rec in data] |
| 125 | + logger.info("Loaded %d API key(s) from %s", len(self._keys), self._path) |
| 126 | + except Exception: |
| 127 | + logger.exception("Failed to load keys from %s — starting empty", self._path) |
| 128 | + self._keys = [] |
| 129 | + |
| 130 | + def _save(self) -> None: |
| 131 | + """Persist keys to disk with owner-only permissions (0o600).""" |
| 132 | + self._path.parent.mkdir(parents=True, exist_ok=True) |
| 133 | + self._path.write_text(json.dumps([asdict(k) for k in self._keys], indent=2)) |
| 134 | + try: |
| 135 | + os.chmod(self._path, 0o600) |
| 136 | + except OSError: |
| 137 | + pass # Windows or permission issues — best-effort |
0 commit comments