o
    *i                     @   s   d Z ddlZddlmZmZmZ ddlmZmZ de	eB eB de	fddZ
d	e	d
e	defddZdee	B dB dedB fddZdS )zKUtilities for OAuth 2.0 Resource Indicators (RFC 8707) and PKCE (RFC 7636).    N)urlparseurlsplit
urlunsplit)AnyUrlHttpUrlurlreturnc                 C   s4   t | }t|}t|j|j |j dd}|S )aD  Convert server URL to canonical resource URL per RFC 8707.

    RFC 8707 section 2 states that resource URIs "MUST NOT include a fragment component".
    Returns absolute URI with lowercase scheme/host for canonical form.

    Args:
        url: Server URL to convert

    Returns:
        Canonical resource URL string
     )schemenetlocfragment)strr   r   _replacer
   lowerr   )r   url_strparsed	canonical r   R/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/mcp/shared/auth_utils.pyresource_url_from_server_url	   s    r   requested_resourceconfigured_resourcec                 C   s   t | }t |}|j |j ks|j |j krdS |j}|j}t|t|k r.dS |ds7|d7 }|ds@|d7 }||S )a$  Check if a requested resource URL matches a configured resource URL.

    A requested resource matches if it has the same scheme, domain, port,
    and its path starts with the configured resource's path. This allows
    hierarchical matching where a token for a parent resource can be used
    for child resources.

    Args:
        requested_resource: The resource URL being requested
        configured_resource: The resource URL that has been configured

    Returns:
        True if the requested resource matches the configured resource
    F/)r   r
   r   r   pathlenendswith
startswith)r   r   	requested
configuredrequested_pathconfigured_pathr   r   r   check_resource_allowed   s   (


r!   
expires_inc                 C   s   | du rdS t   t|  S )zCalculate token expiry timestamp from expires_in seconds.

    Args:
        expires_in: Seconds until token expiration (may be string from some servers)

    Returns:
        Unix timestamp when token expires, or None if no expiry specified
    N)timeint)r"   r   r   r   calculate_token_expiryI   s   	r%   )__doc__r#   urllib.parser   r   r   pydanticr   r   r   r   boolr!   r$   floatr%   r   r   r   r   <module>   s    "*