forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_api_key.py
More file actions
376 lines (340 loc) · 13.2 KB
/
Copy pathmcp_api_key.py
File metadata and controls
376 lines (340 loc) · 13.2 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import uuid
from datetime import datetime, timezone
import logging
from typing import Any, Dict, Optional, Tuple, cast
from google.cloud import firestore
import database.redis_db as redis_db
from database._client import get_firestore_client
from database.api_key_metadata import (
MCP_API_KEY_AUTH_CONTEXT_VERSION,
ApiKeyAuthLookupResult,
ApiKeyAuthRepair,
ApiKeyCacheReadMode,
ApiKeyMetadataRepair,
ApiKeyRevocationUnavailableError,
ApiKeyValidationError,
api_key_scopes_need_repair,
api_key_auth_user_id,
contains_raw_api_key,
is_valid_api_key_hash,
normalize_api_key_app_id,
project_api_key_metadata,
valid_api_key_app_id,
)
from models.mcp_api_key import McpApiKey
from utils.mcp_api_keys import generate_api_key, hash_api_key
from utils.mcp_scopes import (
MCP_APP_KEY_MEMORY_GRANTS_DOC_ID,
MCP_DEFAULT_APP_ID,
MCP_FULL_ACCESS_SCOPES,
MCP_MEMORY_CONTROL_COLLECTION,
MCP_MEMORY_GRANT_SCOPES,
normalize_mcp_scopes,
)
logger = logging.getLogger(__name__)
def _db() -> Any:
return get_firestore_client()
def _seed_mcp_memory_grant(
user_id: str,
key_id: str,
app_id: str = MCP_DEFAULT_APP_ID,
firestore_client: Any = None,
) -> None:
firestore_client = firestore_client or _db()
grant_ref = (
firestore_client.collection("users")
.document(user_id)
.collection(MCP_MEMORY_CONTROL_COLLECTION)
.document(MCP_APP_KEY_MEMORY_GRANTS_DOC_ID)
)
grant_ref.set(
{
"grants": {
"mcp": {
"apps": {
app_id: {
"keys": {
key_id: {
"enabled": True,
"scopes": MCP_MEMORY_GRANT_SCOPES,
"default_read": True,
"archive_read": False,
"write": True,
}
}
}
}
}
},
"updated_at": datetime.now(timezone.utc),
},
merge=True,
)
def _ensure_mcp_memory_grant(
user_id: str,
key_id: str,
app_id: str,
firestore_client: Any,
) -> bool:
grant_ref = (
firestore_client.collection("users")
.document(user_id)
.collection(MCP_MEMORY_CONTROL_COLLECTION)
.document(MCP_APP_KEY_MEMORY_GRANTS_DOC_ID)
)
snapshot = grant_ref.get()
raw = snapshot.to_dict() if getattr(snapshot, "exists", False) else {}
grant: object = raw
for field in ("grants", "mcp", "apps", app_id, "keys", key_id):
grant = grant.get(field, {}) if isinstance(grant, dict) else {}
scopes = grant.get("scopes") if isinstance(grant, dict) else None
is_current = (
isinstance(grant, dict)
and grant.get("enabled") is True
and grant.get("default_read") is True
and grant.get("archive_read") is False
and grant.get("write") is True
and isinstance(scopes, list)
and all(isinstance(scope, str) for scope in scopes)
and set(scopes) == set(MCP_MEMORY_GRANT_SCOPES)
)
if is_current:
return False
_seed_mcp_memory_grant(user_id, key_id, app_id, firestore_client=firestore_client)
return True
def _delete_mcp_memory_grant(
user_id: str,
key_id: str,
app_id: str = MCP_DEFAULT_APP_ID,
firestore_client: Any = None,
) -> None:
firestore_client = firestore_client or _db()
grant_ref = (
firestore_client.collection("users")
.document(user_id)
.collection(MCP_MEMORY_CONTROL_COLLECTION)
.document(MCP_APP_KEY_MEMORY_GRANTS_DOC_ID)
)
try:
grant_ref.update(
{
f"grants.mcp.apps.{app_id}.keys.{key_id}": firestore.DELETE_FIELD,
"updated_at": datetime.now(timezone.utc),
}
)
except Exception as e:
logger.warning("Failed to delete MCP memory grant for uid=%s key_id=%s: %s", user_id, key_id, e)
def _valid_cached_auth_context(cached_data: Dict[str, Any]) -> bool:
scopes = normalize_mcp_scopes(cached_data.get("scopes"))
return (
cached_data.get("auth_context_version") == MCP_API_KEY_AUTH_CONTEXT_VERSION
and api_key_auth_user_id(cached_data) is not None
and isinstance(cached_data.get("key_id"), str)
and bool(cached_data["key_id"])
and isinstance(cached_data.get("app_id"), str)
and valid_api_key_app_id(cached_data["app_id"]) == cached_data["app_id"]
and not api_key_scopes_need_repair(
cached_data.get("scopes"),
scopes,
allowed_scopes=MCP_FULL_ACCESS_SCOPES,
missing_is_valid=False,
)
)
def create_mcp_key(
user_id: str,
name: str,
scopes: Optional[list[str]] = None,
app_id: Optional[str] = MCP_DEFAULT_APP_ID,
) -> Tuple[str, McpApiKey]:
"""
Creates a new MCP API key for a user.
Returns the raw key and the key's metadata.
"""
if contains_raw_api_key(name):
raise ApiKeyValidationError("API key name must not contain a raw API key")
if app_id is None:
resolved_app_id = MCP_DEFAULT_APP_ID
else:
resolved_app_id = valid_api_key_app_id(app_id)
if resolved_app_id is None:
raise ApiKeyValidationError("Invalid MCP API key app_id")
raw_key, hashed_key, key_prefix = generate_api_key()
key_id = str(uuid.uuid4())
now = datetime.now(timezone.utc)
resolved_scopes = normalize_mcp_scopes(scopes)
firestore_client = _db()
api_key_doc = {
"id": key_id,
"user_id": user_id,
"name": name,
"hashed_key": hashed_key,
"key_prefix": key_prefix,
"created_at": now,
"last_used_at": None,
"app_id": resolved_app_id,
"scopes": resolved_scopes,
}
firestore_client.collection("mcp_api_keys").document(key_id).set(api_key_doc)
_seed_mcp_memory_grant(user_id, key_id, resolved_app_id, firestore_client=firestore_client)
api_key_data = McpApiKey(
id=key_id,
name=name,
key_prefix=key_prefix,
created_at=now,
last_used_at=None,
app_id=resolved_app_id,
scopes=resolved_scopes,
)
return raw_key, api_key_data
def get_mcp_keys_for_user_with_repair_info(
user_id: str,
) -> tuple[list[McpApiKey], frozenset[ApiKeyMetadataRepair]]:
"""
Retrieves all MCP API keys and bounded metadata-repair reasons for a user.
"""
keys_ref = _db().collection("mcp_api_keys").where("user_id", "==", user_id)
docs = keys_ref.stream()
keys: list[McpApiKey] = []
repairs: set[ApiKeyMetadataRepair] = set()
for doc in docs:
raw: object = doc.to_dict()
data: Dict[str, Any] = cast(Dict[str, Any], raw) if isinstance(raw, dict) else {}
projection = project_api_key_metadata(
document_id=doc.id,
raw=data,
snapshot_create_time=getattr(doc, "create_time", None),
key_kind="mcp",
)
projected = projection.metadata
repairs.update(projection.repairs)
projected["app_id"] = normalize_api_key_app_id(data.get("app_id"), default=MCP_DEFAULT_APP_ID)
if data.get("app_id") != projected["app_id"]:
repairs.add(ApiKeyMetadataRepair.APP_ID)
projected["scopes"] = normalize_mcp_scopes(data.get("scopes"))
if api_key_scopes_need_repair(
data.get("scopes"),
projected["scopes"],
allowed_scopes=MCP_FULL_ACCESS_SCOPES,
missing_is_valid=False,
):
repairs.add(ApiKeyMetadataRepair.SCOPES)
keys.append(McpApiKey.model_validate(projected))
keys.sort(key=lambda key: key.id)
keys.sort(key=lambda key: key.created_at, reverse=True)
return keys, frozenset(repairs)
def get_mcp_keys_for_user(user_id: str) -> list[McpApiKey]:
"""Retrieves all MCP API keys for a user."""
keys, _repairs = get_mcp_keys_for_user_with_repair_info(user_id)
return keys
def delete_mcp_key(user_id: str, key_id: str) -> None:
"""
Deletes an MCP API key.
"""
firestore_client = _db()
key_ref = firestore_client.collection("mcp_api_keys").document(key_id)
key_doc = key_ref.get()
if key_doc.exists:
raw: object = key_doc.to_dict()
key_data: Dict[str, Any] = cast(Dict[str, Any], raw) if isinstance(raw, dict) else {}
if key_data.get("user_id") == user_id:
hashed_key = key_data.get("hashed_key")
if not is_valid_api_key_hash(hashed_key):
raise ApiKeyRevocationUnavailableError("MCP API key credential metadata is invalid")
try:
cache_deleted = redis_db.delete_cached_mcp_api_key_strict(hashed_key)
except Exception as exc:
raise ApiKeyRevocationUnavailableError("MCP API key cache invalidation failed") from exc
if cache_deleted is not True:
raise ApiKeyRevocationUnavailableError("MCP API key cache invalidation was not confirmed")
_delete_mcp_memory_grant(
user_id,
key_id,
normalize_api_key_app_id(key_data.get("app_id"), default=MCP_DEFAULT_APP_ID),
firestore_client=firestore_client,
)
key_ref.delete()
def get_user_id_by_api_key(api_key: str) -> Optional[str]:
"""
Verifies an API key and returns the associated user ID.
Uses a cache to avoid frequent database lookups.
Also updates the last_used_at timestamp on cache miss.
"""
auth_context = get_user_and_scopes_by_api_key(api_key)
return auth_context.get("user_id") if auth_context else None
def get_api_key_auth_result(api_key: str) -> ApiKeyAuthLookupResult:
"""
Verifies an MCP API key and returns uid plus server-owned app/key/scopes.
MCP keys are full-access agent keys. Older key documents may be missing the
app identity, scopes, and memory grant state introduced by the app/key grant
layer; repair them lazily on successful authentication so existing agents
keep working without regenerating keys.
"""
if not api_key.startswith("omi_mcp_"):
return ApiKeyAuthLookupResult(context=None)
secret_part = api_key.replace("omi_mcp_", "", 1)
hashed_key = hash_api_key(secret_part)
cache_read = redis_db.read_cached_mcp_api_key_auth_context(hashed_key)
cached_data = cache_read.data if cache_read.mode == ApiKeyCacheReadMode.HIT else None
if cached_data and _valid_cached_auth_context(cached_data):
return ApiKeyAuthLookupResult(
context={
"user_id": cached_data["user_id"],
"scopes": normalize_mcp_scopes(cached_data.get("scopes")),
"key_id": cached_data["key_id"],
"app_id": cached_data["app_id"],
}
)
firestore_client = _db()
keys_ref = firestore_client.collection("mcp_api_keys").where("hashed_key", "==", hashed_key).limit(1)
docs = list(keys_ref.stream())
if not docs:
return ApiKeyAuthLookupResult(context=None)
key_doc = docs[0]
raw: object = key_doc.to_dict()
key_data: Dict[str, Any] = cast(Dict[str, Any], raw) if isinstance(raw, dict) else {}
user_id = api_key_auth_user_id(key_data)
key_id = key_doc.id if isinstance(key_doc.id, str) and key_doc.id else None
if user_id is None or key_id is None:
return ApiKeyAuthLookupResult(context=None)
repairs: set[ApiKeyAuthRepair] = set()
if cache_read.mode == ApiKeyCacheReadMode.ERROR:
repairs.add(ApiKeyAuthRepair.CACHE_READ)
if key_data.get("id") != key_id:
repairs.add(ApiKeyAuthRepair.DOCUMENT_ID)
# Missing app identity predates the app/key grant contract and is repairable.
# A present malformed identity is ambiguous credential state and must fail auth.
if "app_id" not in key_data:
app_id = MCP_DEFAULT_APP_ID
repairs.add(ApiKeyAuthRepair.APP_ID)
else:
app_id = valid_api_key_app_id(key_data.get("app_id"))
if app_id is None:
return ApiKeyAuthLookupResult(context=None)
scopes = normalize_mcp_scopes(key_data.get("scopes"))
if api_key_scopes_need_repair(
key_data.get("scopes"),
scopes,
allowed_scopes=MCP_FULL_ACCESS_SCOPES,
missing_is_valid=False,
):
repairs.add(ApiKeyAuthRepair.SCOPES)
key_ref = key_doc.reference
key_ref.update({"id": key_id, "last_used_at": datetime.now(timezone.utc), "app_id": app_id, "scopes": scopes})
if _ensure_mcp_memory_grant(user_id, key_id, app_id, firestore_client):
repairs.add(ApiKeyAuthRepair.MEMORY_GRANT)
cache_written = redis_db.cache_mcp_api_key_auth_context(
hashed_key,
user_id,
scopes,
key_id=key_id,
app_id=app_id,
auth_context_version=MCP_API_KEY_AUTH_CONTEXT_VERSION,
)
if cache_written is not True:
repairs.add(ApiKeyAuthRepair.CACHE_WRITE)
return ApiKeyAuthLookupResult(
context={"user_id": user_id, "scopes": scopes, "key_id": key_id, "app_id": app_id},
repairs=frozenset(repairs),
)
def get_user_and_scopes_by_api_key(api_key: str) -> Optional[Dict[str, Any]]:
return get_api_key_auth_result(api_key).context