
    lKj>                        d Z ddlmZ ddlZddlZddlmZ ddlmZ ddl	m
Z
 ddlmZ  eh d      Z G d	 d
e      Z G d de      ZddZy)zEBearer token and OAuth authentication middleware for HTTP transports.    )annotationsN)Callable)BaseHTTPMiddleware)JSONResponse)ASGIApp>   /health/oauth/token/oauth/register/oauth/authorize'/.well-known/oauth-authorization-serverc                  0     e Zd ZdZddd fdZd Z xZS )AuthKeyMiddlewarezRequire Bearer token on all non-public paths.

    Accepts either the static auth_key or, when oauth_validator is provided,
    a valid OAuth access token as a fallback.
    N)oauth_validatorc               \    t         |   |       |j                         | _        || _        y N)super__init__encode_keyr   )selfappauth_keyr   	__class__s       n/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/macos_mcp/infrastructure/auth.pyr   zAuthKeyMiddleware.__init__   s&    OO%	.    c                  K   |j                   j                  t        v r ||       d {   S |j                  j	                  dd      }|j                  d      st        ddidddi	      S |d
d  }t        j                  |j                         | j                        r ||       d {   S | j                  r!| j                  |      r ||       d {   S t        ddidddi	      S 7 7 E7 w)NAuthorization Bearer errorzAMissing or invalid Authorization header. Expected: Bearer <token>  zWWW-AuthenticateBearer)status_codeheaders   zInvalid authentication token)urlpath_PUBLIC_PATHSr$   get
startswithr   secretscompare_digestr   r   r   r   request	call_nextauth_headertokens        r   dispatchzAuthKeyMiddleware.dispatch#   s     ;;},"7+++oo))/2>%%i0]^+X6  AB!!%,,.$))<"7+++D$8$8$?"7+++45'2
 	
% , , ,s4   (C6C0BC6,C2-,C6C4C62C64C6)r   r   r   strr   zCallable[[str], bool] | NonereturnNone__name__
__module____qualname____doc__r   r2   __classcell__r   s   @r   r   r      s     hl /

r   r   c                  *     e Zd ZdZd fdZd Z xZS )OAuthOnlyMiddlewarezIAuthenticate via OAuth Bearer tokens only (no static API key configured).c               2    t         |   |       || _        y r   )r   r   r   )r   r   r   r   s      r   r   zOAuthOnlyMiddleware.__init__A   s    .r   c                .  K   |j                   j                  t        v r ||       d {   S |j                  j	                  dd      }|j                  d      r&|dd  }| j                  |      r ||       d {   S t        ddid      S 7 f7 w)	Nr   r   r   r%   r    Unauthorizedr!   )r#   )r&   r'   r(   r$   r)   r*   r   r   r-   s        r   r2   zOAuthOnlyMiddleware.dispatchE   s     ;;},"7+++oo))/2>!!),OE##E*&w///Wn53GG , 0s"   (BBAB=B>BB)r   r   r   zCallable[[str], bool]r4   r5   r6   r<   s   @r   r>   r>   >   s    S/
Hr   r>   c                    | j                         dk(  ry	 t        j                  |       j                  S # t        $ r Y yw xY w)z8Return True if host refers only to a loopback interface.	localhostTF)lower	ipaddress
ip_addressis_loopback
ValueError)hosts    r   is_loopback_hostrJ   R   sA    zz|{"##D)555 s   5 	A A)rI   r3   r4   bool)r:   
__future__r   rE   r+   typingr   starlette.middleware.baser   starlette.responsesr   starlette.typesr   	frozensetr(   r   r>   rJ    r   r   <module>rS      sP    K "    8 , #   $
* $
NH, H(r   