
    Ki)                    v    d Z ddlmZ ddlZddlmZmZ ddlmZ ddl	m
Z
 ddlmZ  ee      Z G d d	e      Zy)
aU  Debug token verifier for testing and special cases.

This module provides a flexible token verifier that delegates validation
to a custom callable. Useful for testing, development, or scenarios where
standard verification isn't possible (like opaque tokens without introspection).

Example:
    ```python
    from fastmcp import FastMCP
    from fastmcp.server.auth.providers.debug import DebugTokenVerifier

    # Accept all tokens (default - useful for testing)
    auth = DebugTokenVerifier()

    # Custom sync validation logic
    auth = DebugTokenVerifier(validate=lambda token: token.startswith("valid-"))

    # Custom async validation logic
    async def check_cache(token: str) -> bool:
        return await redis.exists(f"token:{token}")

    auth = DebugTokenVerifier(validate=check_cache)

    mcp = FastMCP("My Server", auth=auth)
    ```
    )annotationsN)	AwaitableCallable)TokenVerifier)AccessToken)
get_loggerc                  F     e Zd ZdZd dddf	 	 	 	 	 	 	 d fdZddZ xZS )	DebugTokenVerifiera   Token verifier with custom validation logic.

    This verifier delegates token validation to a user-provided callable.
    By default, it accepts all non-empty tokens (useful for testing).

    Use cases:
    - Testing: Accept any token without real verification
    - Development: Custom validation logic for prototyping
    - Opaque tokens: When you have tokens with no introspection endpoint

    WARNING: This bypasses standard security checks. Only use in controlled
    environments or when you understand the security implications.
    c                     y)NT )tokens    o/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/fastmcp/server/auth/providers/debug.py<lambda>zDebugTokenVerifier.<lambda>:   s        zdebug-clientNc                X    t         |   |       || _        || _        |xs g | _        y)a  Initialize the debug token verifier.

        Args:
            validate: Callable that takes a token string and returns True if valid.
                Can be sync or async. Default accepts all tokens.
            client_id: Client ID to assign to validated tokens
            scopes: Scopes to assign to validated tokens
            required_scopes: Required scopes (inherited from TokenVerifier base class)
        )required_scopesN)super__init__validate	client_idscopes)selfr   r   r   r   	__class__s        r   r   zDebugTokenVerifier.__init__7   s.    " 	9 "lr   c                  K   |r|j                         st        j                  d       y	 | j                  |      }t	        j
                  |      r| d{   }n|}|st        j                  d       yt        || j                  | j                  dd|i      S 7 E# t        $ r"}t        j                  d|d       Y d}~yd}~ww xY ww)	zVerify token using custom validation logic.

        Args:
            token: The token string to validate

        Returns:
            AccessToken if validation succeeds, None otherwise
        zRejecting empty tokenNz0Token validation failed: callable returned Falser   )r   r   r   
expires_atclaimszToken validation error: %sT)exc_info)
striploggerdebugr   inspectisawaitabler   r   r   	Exception)r   r   resultis_valides        r   verify_tokenzDebugTokenVerifier.verify_tokenM   s      EKKMLL01	]]5)F""6*!'<!OP ..{{'  ("  	LL5q4LH	sL   )C+B BB 6C7%B CB 	C
(C CC

C)r   z8Callable[[str], bool] | Callable[[str], Awaitable[bool]]r   strr   list[str] | Noner   r)   )r   r(   returnzAccessToken | None)__name__
__module____qualname____doc__r   r'   __classcell__)r   s   @r   r
   r
   (   sI    " .@'#',0#+# 	#
 !# *#,%r   r
   )r.   
__future__r   r!   collections.abcr   r   fastmcp.server.authr   fastmcp.server.auth.authr   fastmcp.utilities.loggingr   r+   r   r
   r   r   r   <module>r5      s6   6 #  / - 0 0	H	J Jr   