fix(proxy_server.py): cache master key check

This commit is contained in:
Krrish Dholakia 2024-03-12 15:15:33 -07:00
parent a18c941621
commit 39f9bfad80
2 changed files with 26 additions and 1 deletions

View file

@ -353,17 +353,32 @@ async def user_api_key_auth(
### CHECK IF ADMIN ###
# note: never string compare api keys, this is vulenerable to a time attack. Use secrets.compare_digest instead
### CHECK IF ADMIN ###
# note: never string compare api keys, this is vulenerable to a time attack. Use secrets.compare_digest instead
## Check CACHE
valid_token = user_api_key_cache.get_cache(key=hash_token(api_key))
if (
valid_token is not None
and isinstance(valid_token, UserAPIKeyAuth)
and valid_token.user_role == "proxy_admin"
):
return valid_token
try:
is_master_key_valid = ph.verify(litellm_master_key_hash, api_key)
except Exception as e:
is_master_key_valid = False
if is_master_key_valid:
return UserAPIKeyAuth(
_user_api_key_obj = UserAPIKeyAuth(
api_key=master_key,
user_role="proxy_admin",
user_id=litellm_proxy_admin_name,
)
user_api_key_cache.set_cache(
key=hash_token(master_key), value=_user_api_key_obj
)
return _user_api_key_obj
if isinstance(
api_key, str