
    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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Tuple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      K/var/www/openai/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
str | Noner   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:
            :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:   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   r6   z0BasicAuthWebSocketServerProtocol.process_requestO   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$   ,Callable[[str, str], Awaitable[bool]] | Noner,   r   returnNoner   r   r   r   r;   bool)r7   r   r8   r   r;   zHTTPResponse | None)__name__
__module____qualname____doc__r!   __annotations__r   r)   r$   r6   __classcell__)r-   s   @r   r   r      s    
 E3O  Hj-
 !JN	
*
* 
* H	
*
 
* 

**$D$D !$D 
	$D $Dr.   c                   |du |du k(  rt        d      |t        |      rt        t        |      g}n^t	        |t
              r@t        t        t
        t           |            }t        d |D              st        d|       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  2   K   | ]  }t        |        y wr&   )r   ).0items     r   	<genexpr>z.basic_auth_protocol_factory.<locals>.<genexpr>   s     I8H~d+8Hs   zinvalid credentials argument: c                b   K   	 |    }t        j                  ||      S # t         $ r Y yw xY wwr   )r2   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   r   	functoolspartial)r!   credentialsr$   create_protocolcredentials_listrN   s        @r   r   r   v   s    L 	t!2d!:;IJJ+& $[+ >?X.#D+)>$LMI8HII"@ NOO<[MJKK 01	D : 667O + r.   )r   r   r;   r>   )NNNN)
r!   r"   rU   z*Credentials | Iterable[Credentials] | Noner$   r:   rV   z6Callable[..., BasicAuthWebSocketServerProtocol] | Noner;   z/Callable[..., BasicAuthWebSocketServerProtocol])
__future__r   rS   rK   r3   typingr   r   r   r   r   r	   datastructuresr   
exceptionsr   headersr   r   serverr   r   __all__r   rO   r   r   r    r.   r   <module>r`      s    "    B B $ & M 9 ./L
M CHoGVD'> VDt >BFJNR	HH;H DH L	H
 5Hr.   