o
    ưi
                     @   sj   d Z ddlZddlmZ ddlmZmZmZmZ ddl	Z	ddl
mZ ddlmZ ejjZG dd dZdS )	z7
Prometheus Auth Middleware - Pure ASGI implementation
    N)Request)ASGIAppReceiveScopeSend)SpecialHeaders)user_api_key_authc                   @   s<   e Zd ZdZdeddfddZdeded	eddfd
dZ	dS )PrometheusAuthMiddlewarea  
    Middleware to authenticate requests to the metrics endpoint.

    By default, auth is not run on the metrics endpoint.

    Enabled by setting the following in proxy_config.yaml:

    ```yaml
    litellm_settings:
        require_auth_for_metrics_endpoint: true
    ```
    appreturnNc                 C   s
   || _ d S )N)r
   )selfr
    r   j/home/app/Keep/.python/lib/python3.10/site-packages/litellm/proxy/middleware/prometheus_auth_middleware.py__init__   s   
z!PrometheusAuthMiddleware.__init__scopereceivesendc           	         s  |d dksd| ddvr| |||I d H  d S tjdu rt||}|j tp,d}zt||dI d H  W nH ty } z<t	|dt
|}td	| d
}|ddddgdt
t|dggdI d H  |d|dI d H  W Y d }~d S d }~ww | |||I d H  d S )Ntypehttpz/metricspath T)requestapi_keymessagez)Unauthorized access to metrics endpoint: zutf-8zhttp.response.starti  s   content-types   application/jsons   content-lengthascii)r   statusheaderszhttp.response.body)r   body)getr
   litellmZ!require_auth_for_metrics_endpointr   r   _AUTHORIZATION_HEADERr   	Exceptiongetattrstrjsondumpsencodelen)	r   r   r   r   r   r   eerror_messager   r   r   r   __call__"   sB   




z!PrometheusAuthMiddleware.__call__)
__name__
__module____qualname____doc__r   r   r   r   r   r*   r   r   r   r   r	      s    r	   )r.   r$   Zfastapir   Zstarlette.typesr   r   r   r   r   Zlitellm.proxy._typesr   Z$litellm.proxy.auth.user_api_key_authr   Zopenai_authorizationvaluer    r	   r   r   r   r   <module>   s    