Coverage for .venv/lib/python3.13/site-packages/litellm/proxy/custom_auth_auto.py: 0%

11 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 12:01 +0000

1""" 

2Example custom auth function. 

3 

4This will allow all keys starting with "my-custom-key" to pass through. 

5""" 

6 

7from fastapi import Request 

8 

9from litellm.proxy._types import ProxyException, UserAPIKeyAuth 

10 

11 

12async def user_api_key_auth(request: Request, api_key: str) -> UserAPIKeyAuth | str: 

13 try: 

14 if api_key.startswith("my-custom-key"): 

15 return "sk-P1zJMdsqCPNN54alZd_ETw" 

16 if api_key == "invalid-api-key": 

17 # raise a custom exception back to the client 

18 raise ProxyException( 

19 message="Invalid API key", 

20 type="invalid_request_error", 

21 param="api_key", 

22 code=401, 

23 ) 

24 else: 

25 raise Exception("Invalid API key") 

26 except Exception: 

27 raise Exception("Invalid API key")