o
    i                     @  sX   d Z ddlmZ ddlZddlmZ ddlmZ ddl	m
Z
 e
eZG dd deZdS )	zEnhanced authentication middleware with better error messages.

This module provides enhanced versions of MCP SDK authentication middleware
that return more helpful error messages for developers troubleshooting
authentication issues.
    )annotationsN)RequireAuthMiddleware)Send)
get_loggerc                   @  s   e Zd ZdZdddZdS )r   a  Enhanced authentication middleware with detailed error messages.

    Extends the SDK's RequireAuthMiddleware to provide more actionable
    error messages when authentication fails. This helps developers
    understand what went wrong and how to fix it.
    sendr   status_codeinterrorstrdescriptionreturnNonec           
        s   |}|dkr|dkrd}n|dkr	 d| dd| dg}| j r,|d| j  d d	d
| }||d}t| }	|d|ddtt|	 fd| fgdI dH  |d|	dI dH  t	d|| dS )a  Send an authentication error response with enhanced error messages.

        Overrides the SDK's _send_auth_error to provide more detailed
        error descriptions that help developers troubleshoot authentication
        issues.

        Args:
            send: ASGI send callable
            status_code: HTTP status code (401 or 403)
            error: OAuth error code
            description: Base error description
        invalid_tokeni  zAuthentication failed. The provided bearer token is invalid, expired, or no longer recognized by the server. To resolve: clear authentication tokens in your MCP client and reconnect. Your client should automatically re-register and obtain new tokens.insufficient_scopezerror=""zerror_description="zresource_metadata="zBearer z, )r	   error_descriptionzhttp.response.start)s   content-types   application/jsons   content-lengths   www-authenticate)typestatusheadersNzhttp.response.body)r   bodyz#Auth error returned: %s (status=%d))
resource_metadata_urlappendjoinjsondumpsencoder
   lenloggerinfo)
selfr   r   r	   r   enhanced_descriptionwww_auth_partswww_authenticater   
body_bytes r$   d/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/fastmcp/server/auth/middleware.py_send_auth_error   sD   





z&RequireAuthMiddleware._send_auth_errorN)
r   r   r   r   r	   r
   r   r
   r   r   )__name__
__module____qualname____doc__r&   r$   r$   r$   r%   r      s    r   )r*   
__future__r   r   &mcp.server.auth.middleware.bearer_authr   SDKRequireAuthMiddlewarestarlette.typesr   fastmcp.utilities.loggingr   r'   r   r$   r$   r$   r%   <module>   s    