
    2Bf                        d dl mZ d dlZd dlZd dlZd dlmZmZmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ ddlmZmZ dd	lmZmZ d
dgZeeef   ZddZ G d d
e      Z	 	 	 	 d	 	 	 	 	 	 	 	 	 ddZy)    )annotationsN)Any	AwaitableCallableIterableOptionalTupleUnioncast   )Headers)InvalidHeader)build_www_authenticate_basicparse_authorization_basic   )HTTPResponseWebSocketServerProtocol BasicAuthWebSocketServerProtocolbasic_auth_protocol_factoryc                |    	 | \  }}t        |t              xr t        |t              S # t        t        f$ r Y yw xY wNF)
isinstancestr	TypeError
ValueError)valueusernamepasswords      V/var/www/highfloat_scraper/venv/lib/python3.12/site-packages/websockets/legacy/auth.pyis_credentialsr       sF    G"( (C(FZ#-FF z" s   ) ;;c                       e Zd ZU dZdZded<   	 dZded<   	 ddd	 	 	 	 	 	 	 	 	 d fd	Zdd
Z	 	 	 	 	 	 d fdZ	 xZ
S )r   zC
    WebSocket server protocol that enforces HTTP Basic Auth.

     r   realmNOptional[str]r   r#   check_credentialsc               D    ||| _         || _        t        |   |i | y N)r#   _check_credentialssuper__init__)selfr#   r&   argskwargs	__class__s        r   r+   z)BasicAuthWebSocketServerProtocol.__init__-   s,     DJ"3$)&)    c                \   K   | j                   | j                  ||       d{   S y7 w)a  
        Check whether credentials are authorized.

        This coroutine may be overridden in a subclass, for example to
        authenticate against a database or an external service.

        Args:
            username: HTTP Basic Auth username.
            password: HTTP Basic Auth password.

        Returns:
            bool: :obj:`True` if the handshake should continue;
            :obj:`False` if it should fail with an HTTP 401 error.

        NF)r)   )r,   r   r   s      r   r&   z2BasicAuthWebSocketServerProtocol.check_credentials9   s3       "".008DDD Es   ",*,c                  K   	 |d   }	 t        |      \  }}| j                  ||       d{   s3t        j                  j                  dt	        | j
                        fgdfS || _	        t        | -  ||       d{   S # t         $ r6 t        j                  j                  dt	        | j
                        fgdfcY S w xY w# t        $ r6 t        j                  j                  dt	        | j
                        fgdfcY S w xY w7 7 w)zS
        Check HTTP Basic Auth and return an HTTP 401 response if needed.

        AuthorizationzWWW-Authenticates   Missing credentials
s   Unsupported credentials
Ns   Invalid credentials
)KeyErrorhttp
HTTPStatusUNAUTHORIZEDr   r#   r   r   r&   r   r*   process_request)r,   pathrequest_headersauthorizationr   r   r/   s         r   r8   z0BasicAuthWebSocketServerProtocol.process_requestN   s    	+O<M	!:=!IHh ++Hh???,,$&B4::&NOP(  !W,T?CCC3  	,,$&B4::&NOP( 	  	,,$&B4::&NOP, 	 @ Dsa   DB C DD	AD DD<CDCD<DDDDD)
r-   r   r#   r$   r&   /Optional[Callable[[str, str], Awaitable[bool]]]r.   r   returnNoner   r   r   r   r=   bool)r9   r   r:   r   r=   zOptional[HTTPResponse])__name__
__module____qualname____doc__r#   __annotations__r   r+   r&   r8   __classcell__)r/   s   @r   r   r      s    
 E3O #Hm"-
  $MQ	
*
* 
* K	
*
 
* 

**$D$D !$D 
 	$D $Dr0   c                ^   |du |du k(  rt        d      |wt        |      rt        t        |      g}nIt	        |t
              r+t        |      }t        d |D              st        d|       t        d|       t        |      dfd}|t        }t        j                  || |      S )a  
    Protocol factory that enforces HTTP Basic Auth.

    :func:`basic_auth_protocol_factory` is designed to integrate with
    :func:`~websockets.server.serve` like this::

        websockets.serve(
            ...,
            create_protocol=websockets.basic_auth_protocol_factory(
                realm="my dev server",
                credentials=("hello", "iloveyou"),
            )
        )

    Args:
        realm: Scope of protection. It should contain only ASCII characters
            because the encoding of non-ASCII characters is undefined.
            Refer to section 2.2 of :rfc:`7235` for details.
        credentials: Hard coded authorized credentials. It can be a
            ``(username, password)`` pair or a list of such pairs.
        check_credentials: Coroutine that verifies credentials.
            It receives ``username`` and ``password`` arguments
            and returns a :class:`bool`. One of ``credentials`` or
            ``check_credentials`` must be provided but not both.
        create_protocol: Factory that creates the protocol. By default, this
            is :class:`BasicAuthWebSocketServerProtocol`. It can be replaced
            by a subclass.
    Raises:
        TypeError: If the ``credentials`` or ``check_credentials`` argument is
            wrong.

    Nz/provide either credentials or check_credentialsc              3  2   K   | ]  }t        |        y wr(   )r    ).0items     r   	<genexpr>z.basic_auth_protocol_factory.<locals>.<genexpr>   s     I~d+Is   zinvalid credentials argument: c                b   K   	 |    }t        j                  ||      S # t         $ r Y yw xY wwr   )r4   hmaccompare_digest)r   r   expected_passwordcredentials_dicts      r   r&   z6basic_auth_protocol_factory.<locals>.check_credentials   s@     $4X$>! &&'8(CC  s   /  /	,/,/r%   r?   )r   r    r   Credentialsr   r   listalldictr   	functoolspartial)r#   credentialsr&   create_protocolcredentials_listrP   s        @r   r   r   u   s    L 	t!2d!:;IJJ+& $[+ >?X.#K0I8HII"@ NOO<[MJKK 01	D :+ r0   )r   r   r=   r@   )NNNN)
r#   r$   rW   z3Optional[Union[Credentials, Iterable[Credentials]]]r&   r<   rX   z9Optional[Callable[..., BasicAuthWebSocketServerProtocol]]r=   z/Callable[..., BasicAuthWebSocketServerProtocol])
__future__r   rU   rM   r5   typingr   r   r   r   r   r	   r
   r   datastructuresr   
exceptionsr   headersr   r   serverr   r   __all__r   rQ   r    r   r    r0   r   <module>rb      s    "    S S S $ & M 9 ./L
MCHoGVD'> VDt  GKIMQU	CCDC GC O	C
 5Cr0   