
    Ch                         S SK r S SKJrJrJrJr  S SKJr  S SKJ	r	  \ R                  \S-     " SSS9rS\	S-  4S jr " S	 S
5      rg)    N)ASGIAppReceiveScopeSend)AuthenticatedUser)AccessTokenauth_context)defaultreturnc                  T    [         R                  5       n U (       a  U R                  $ S$ )z
Get the access token from the current context.

Returns:
    The access token if an authenticated user is available, None otherwise.
N)auth_context_vargetaccess_token)	auth_users    a/home/james-whalen/.local/lib/python3.13/site-packages/mcp/server/auth/middleware/auth_context.pyget_access_tokenr      s%     !$$&I%.9!!8D8    c                   <    \ rS rSrSrS\4S jrS\S\S\	4S jr
S	rg
)AuthContextMiddleware   aB  
Middleware that extracts the authenticated user from the request
and sets it in a contextvar for easy access throughout the request lifecycle.

This middleware should be added after the AuthenticationMiddleware in the
middleware stack to ensure that the user is properly authenticated before
being stored in the context.
appc                     Xl         g )Nr   )selfr   s     r   __init__AuthContextMiddleware.__init__"   s    r   scopereceivesendc                 T  #    UR                  S5      n[        U[        5      (       aF  [        R	                  U5      n U R                  XU5      I S h  vN   [        R                  U5        g U R                  XU5      I S h  vN   g  N5! [        R                  U5        f = f N!7f)Nuser)r   
isinstancer   r   setr   reset)r   r   r   r   r!   tokens         r   __call__AuthContextMiddleware.__call__%   s     yy d-..$((.E.hhut444 &&u- ((54000 5 &&u- 1s:   <B(B B
B +B(B&B(
B B##B(r   N)__name__
__module____qualname____firstlineno____doc__r   r   r   r   r   r&   __static_attributes__ r   r   r   r      s-    G 1E 1G 14 1r   r   )contextvarsstarlette.typesr   r   r   r   &mcp.server.auth.middleware.bearer_authr   mcp.server.auth.providerr   
ContextVarr   r   r   r.   r   r   <module>r4      sP     9 9 D 0 ))*;d*BCN\`a 9+, 91 1r   