
    g                        d dl mZ d dlZd dlZd dlZd dlmZmZ d dl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dS )     )annotationsN)	AwaitableIterable)AnyCallablecast   )Headers)InvalidHeader)build_www_authenticate_basicparse_authorization_basic   )HTTPResponseWebSocketServerProtocol BasicAuthWebSocketServerProtocolbasic_auth_protocol_factoryvaluer   returnboolc                    	 | \  }}t          |t                    ot          |t                    S # t          t          f$ r Y dS w xY wNF)
isinstancestr	TypeError
ValueError)r   usernamepasswords      b/var/www/html/nodeJS/PythonScripts/venv3.11/lib/python3.11/site-packages/websockets/legacy/auth.pyis_credentialsr      s\    G"( (C((FZ#-F-FF z"   uus   1 AAc                  b     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
str | Noner   r"   check_credentialsargsr   r%   ,Callable[[str, str], Awaitable[bool]] | Nonekwargsr   Nonec               Z    ||| _         || _         t                      j        |i | d S N)r"   _check_credentialssuper__init__)selfr"   r%   r&   r(   	__class__s        r   r.   z)BasicAuthWebSocketServerProtocol.__init__.   s<     DJ"3$)&)))))    r   r   c                P   K   | j         |                      ||           d{V S dS )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:
            :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_credentials:   s>        ".008DDDDDDDDDur1   pathrequest_headersr
   HTTPResponse | Nonec                  K   	 |d         }n9# t           $ r, t          j        j        dt	          | j                  fgdfcY S w xY w	 t          |          \  }}n9# t          $ r, t          j        j        dt	          | j                  fgdfcY S w xY w|                     ||           d{V s)t          j        j        dt	          | j                  fgdfS || _	        t                                          ||           d{V S )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/   r3   r4   authorizationr   r   r0   s         r   r<   z0BasicAuthWebSocketServerProtocol.process_requestO   s]     	+O<MM 	 	 	,$&B4:&N&NOP(   		!:=!I!IHhh 	 	 	,$&B4:&N&NOP,   	 ++Hh???????? 	,$&B4:&N&NOP(  !WW,,T?CCCCCCCCCs!    3AAA 3BB)
r&   r   r"   r#   r%   r'   r(   r   r   r)   r   r   r   r   r   r   )r3   r   r4   r
   r   r5   )__name__
__module____qualname____doc__r"   __annotations__r   r.   r%   r<   __classcell__)r0   s   @r   r   r      s          
 EOOOO  H-
 !JN	
* 
* 
* 
* 
* 
* 
* 
*   *$D $D $D $D $D $D $D $D $D $Dr1   r"   r#   credentials*Credentials | Iterable[Credentials] | Noner%   r'   create_protocol6Callable[..., BasicAuthWebSocketServerProtocol] | None/Callable[..., BasicAuthWebSocketServerProtocol]c                4   |du |du k    rt          d          |t          |          rt          t          |          g}nt	          |t
                    rYt          t          t
          t                   |                    }t          d |D                       st          d|           nt          d|           t          |          dfd
}|t          }t          t          dt          f         |          }t          j        || |          S )a  
    Protocol factory that enforces HTTP Basic Auth.

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

        serve(
            ...,
            create_protocol=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  4   K   | ]}t          |          V  d S r+   )r   ).0items     r   	<genexpr>z.basic_auth_protocol_factory.<locals>.<genexpr>   s*      II~d++IIIIIIr1   zinvalid credentials argument: r   r   r   r   r   c                h   K   	 |          }n# t           $ r Y dS w xY wt          j        ||          S r   )r8   hmaccompare_digest)r   r   expected_passwordcredentials_dicts      r   r%   z6basic_auth_protocol_factory.<locals>.check_credentials   sP      $4X$>!!   uu&'8(CCCs    
.r$   r>   )r   r   r   Credentialsr   r   listalldictr   r   	functoolspartial)r"   rE   r%   rG   credentials_listrS   s        @r   r   r   v   se   L 	t!2d!:;;IJJJ+&& 	L $[+ > >?X.. 	L#D+)>$L$LMMII8HIIIII P N N NOOOP J[JJKKK 011	D 	D 	D 	D 	D 	D : 667 O +   r1   )r   r   r   r   )NNNN)
r"   r#   rE   rF   r%   r'   rG   rH   r   rI   )
__future__r   rX   rP   r9   collections.abcr   r   typingr   r   r   datastructuresr
   
exceptionsr   headersr   r   serverr   r   __all__tupler   rT   r   r   r    r1   r   <module>re      se   " " " " " "       / / / / / / / / & & & & & & & & & & $ $ $ $ $ $ & & & & & & M M M M M M M M 9 9 9 9 9 9 9 9 ./L
MCHoG G G GVD VD VD VD VD'> VD VD VDt >BFJNR	H H H H H H Hr1   