Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix: resolve field mismatches and direct DB query in jwt key mapping

Harshit28j941129c9e0bc
diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py
index 500a39d9455..a7867fa08c7 100644
--- a/litellm/proxy/auth/auth_checks.py
+++ b/litellm/proxy/auth/auth_checks.py
@@ -2028,6 +2028,28 @@ async def _fetch_key_object_from_db_with_reconnect(
raise
+async def get_jwt_key_mapping_object(
+ jwt_claim_name: str,
+ jwt_claim_value: str,
+ prisma_client: PrismaClient,
+) -> Optional[str]:
+ """
+ Lookup a JWT-to-virtual-key mapping from the database.
+
+ Returns the hashed token (str) if a matching active mapping is found, else None.
+ """
+ mapping = await prisma_client.db.litellm_jwtkeymapping.find_first(
+ where={
+ "jwt_claim_name": jwt_claim_name,
+ "jwt_claim_value": jwt_claim_value,
+ "is_active": True,
+ }
+ )
+ if mapping is not None:
+ return mapping.token
+ return None
+
+
@log_db_metrics
async def get_key_object(
hashed_token: str,
diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.py
index 5c529bc69d6..7098b5e18d6 100644
--- a/litellm/proxy/auth/user_api_key_auth.py
+++ b/litellm/proxy/auth/user_api_key_auth.py
@@ -36,6 +36,7 @@ from litellm.proxy.auth.auth_checks import (
can_key_call_model,
common_checks,
get_end_user_object,
+ get_jwt_key_mapping_object,
get_key_object,
get_project_object,
get_team_object,
@@ -480,16 +481,13 @@ async def _resolve_jwt_to_virtual_key(
if prisma_client is None:
return None
- mapping = await prisma_client.db.litellm_jwtkeymapping.find_first(
- where={
- "jwt_claim_name": virtual_key_claim_field,
- "jwt_claim_value": str(claim_value),
- "is_active": True,
- }
+ token_hash = await get_jwt_key_mapping_object(
+ jwt_claim_name=virtual_key_claim_field,
+ jwt_claim_value=str(claim_value),
+ prisma_client=prisma_client,
)
- if mapping:
- token_hash = mapping.token
+ if token_hash is not None:
await user_api_key_cache.async_set_cache(
key=cache_key,
value=token_hash,
diff --git a/litellm/proxy/management_endpoints/jwt_key_mapping_endpoints.py b/litellm/proxy/management_endpoints/jwt_key_mapping_endpoints.py
index 06a526e13a9..08ca00e66d4 100644
--- a/litellm/proxy/management_endpoints/jwt_key_mapping_endpoints.py
+++ b/litellm/proxy/management_endpoints/jwt_key_mapping_endpoints.py
@@ -1,5 +1,6 @@
from fastapi import APIRouter, Depends, HTTPException
from litellm.proxy._types import *
+from litellm.proxy._types import hash_token
from litellm.proxy.auth.user_api_key_auth import user_api_key_auth
router = APIRouter()
@@ -21,12 +22,12 @@ async def create_jwt_key_mapping(
raise HTTPException(status_code=500, detail="Database not connected")
try:
+ hashed_key = hash_token(data.key)
new_mapping = await prisma_client.db.litellm_jwtkeymapping.create(
data={
"jwt_claim_name": data.jwt_claim_name,
"jwt_claim_value": data.jwt_claim_value,
- "token": data.token,
- "is_active": data.is_active,
+ "token": hashed_key,
}
)
@@ -54,12 +55,12 @@ async def update_jwt_key_mapping(
if prisma_client is None:
raise HTTPException(status_code=500, detail="Database not connected")
- update_data = data.model_dump(exclude_unset=True, exclude={"mapping_id"})
+ update_data = data.model_dump(exclude_unset=True, exclude={"id"})
try:
# Get old mapping for cache invalidation
old_mapping = await prisma_client.db.litellm_jwtkeymapping.find_unique(
- where={"mapping_id": data.mapping_id}
+ where={"id": data.id}
)
if old_mapping:
@@ -67,7 +68,7 @@ async def update_jwt_key_mapping(
await user_api_key_cache.async_delete_cache(cache_key)
updated_mapping = await prisma_client.db.litellm_jwtkeymapping.update(
- where={"mapping_id": data.mapping_id}, data=update_data
+ where={"id": data.id}, data=update_data
)
# Invalidate new cache key if claim fields changed
@@ -97,7 +98,7 @@ async def delete_jwt_key_mapping(
try:
# Get old mapping for cache invalidation
old_mapping = await prisma_client.db.litellm_jwtkeymapping.find_unique(
- where={"mapping_id": data.mapping_id}
+ where={"id": data.id}
)
if old_mapping:
@@ -105,7 +106,7 @@ async def delete_jwt_key_mapping(
await user_api_key_cache.async_delete_cache(cache_key)
await prisma_client.db.litellm_jwtkeymapping.delete(
- where={"mapping_id": data.mapping_id}
+ where={"id": data.id}
)
return {"status": "success"}
except Exception as e:
@@ -135,7 +136,7 @@ async def list_jwt_key_mappings(
@router.get("/jwt/key/mapping/info", tags=["JWT Key Mapping"])
async def info_jwt_key_mapping(
- mapping_id: str,
+ id: str,
user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
):
from litellm.proxy.proxy_server import prisma_client
@@ -150,7 +151,7 @@ async def info_jwt_key_mapping(
try:
mapping = await prisma_client.db.litellm_jwtkeymapping.find_unique(
- where={"mapping_id": mapping_id}
+ where={"id": id}
)
if mapping is None:
raise HTTPException(status_code=404, detail="Mapping not found")