o
    ưi                     @   s   d Z ddlmZmZmZmZ ddlmZmZm	Z	 ddl
Z
ddlmZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZ erMdd
lmZ eeef ZneZG dd dZdS )z
Handles Authentication Errors
    )TYPE_CHECKINGAnyOptionalUnion)HTTPExceptionRequeststatusN)verbose_proxy_logger)ProxyErrorTypesProxyExceptionUserAPIKeyAuth)_get_request_ip_address)PrismaDBExceptionHandler)ServiceTypes)Spanc                   @   s:   e Zd Zedededededee	 dede
fdd	Zd
S )UserAPIKeyAuthExceptionHandlererequestrequest_datarouteparent_otel_spanapi_keyreturnc                    sJ  ddl m}m}m} t r't| r'|jjt	j
d| dd tdd||dS t||dd	d
}	tjdt| |	d|	id t|||d}
|j|| |
tj|dI dH }|dur[|} t| tjrkt| jtjdddt| trtt| ddt|  dtjt| ddt| dtjdt| tr| tdt|  tjt| ddtjd)a  
        Handles Connection Errors when reading a Virtual Key from LiteLLM DB
        Use this if you don't want failed DB queries to block LLM API reqiests

        Reliability scenarios this covers:
        - DB is down and having an outage
        - Unable to read / recover a key from the DB

        Returns:
            - UserAPIKeyAuth: If general_settings.allow_requests_on_db_unavailable is True

        Raises:
            - Original Exception in all other cases
        r   )general_settingslitellm_proxy_admin_nameproxy_logging_objZget_key_objectg        )ZserviceZ	call_typeerrordurationzfailed-to-connect-to-db)Zkey_nametokenZuser_idrequest_routeuse_x_forwarded_forF)r   r    z^litellm.proxy.proxy_server.user_api_key_auth(): Exception occured - {}
Requester IP Address:{}requester_ip)extra)r   r   r   )r   Zoriginal_exceptionuser_api_key_dictZ
error_typer   Ni  )messagetypeparamcodedetailzAuthentication Error()r&   Nonestatus_codezAuthentication Error, )Zlitellm.proxy.proxy_serverr   r   r   r   Z&should_allow_request_on_db_unavailableZis_database_connection_errorZservice_logging_objZservice_failure_hookr   ZDBr   r   getr	   	exceptionformatstrZpost_call_failure_hookr
   Z
auth_error
isinstancelitellmZBudgetExceededErrorr   r$   Zbudget_exceededr   getattrr   ZHTTP_401_UNAUTHORIZED)r   r   r   r   r   r   r   r   r   r!   r#   Ztransformed_exception r3   `/home/app/Keep/.python/lib/python3.10/site-packages/litellm/proxy/auth/auth_exception_handler.py_handle_authentication_error   s   
	




z;UserAPIKeyAuthExceptionHandler._handle_authentication_errorN)__name__
__module____qualname__staticmethod	Exceptionr   dictr/   r   r   r   r5   r3   r3   r3   r4   r      s"    r   )__doc__typingr   r   r   r   Zfastapir   r   r   r1   Zlitellm._loggingr	   Zlitellm.proxy._typesr
   r   r   Zlitellm.proxy.auth.auth_utilsr   Z"litellm.proxy.db.exception_handlerr   Zlitellm.types.servicesr   Zopentelemetry.tracer   _Spanr   r3   r3   r3   r4   <module>   s    