o
    \h                     @  s   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dZG dd
 d
eZ				ddddZdS )     )annotationsN)Any	AwaitableCallableIterableTuplecast   )Headers)InvalidHeader)build_www_authenticate_basicparse_authorization_basic   )HTTPResponseWebSocketServerProtocol BasicAuthWebSocketServerProtocolbasic_auth_protocol_factoryvaluer   returnboolc              	   C  s:   z| \}}W n t tfy   Y dS w t|tot|tS NF)	TypeError
ValueError
isinstancestr)r   usernamepassword r   Y/home/air/segue/gemini/backup/venv/lib/python3.10/site-packages/websockets/legacy/auth.pyis_credentials   s   r   c                      s`   e Zd ZU dZdZded< 	 dZded< 	 dddd fddZdddZd fddZ	  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                  s*   |d ur|| _ || _t j|i | d S N)r!   _check_credentialssuper__init__)selfr!   r$   r%   r'   	__class__r   r   r,   .   s   z)BasicAuthWebSocketServerProtocol.__init__r   r   c                   s"   | j dur|  ||I dH 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   r   r   r   r$   :   s   
z2BasicAuthWebSocketServerProtocol.check_credentialspathrequest_headersr
   HTTPResponse | Nonec                   s   z|d }W n t y   tjjdt| jfgdf Y S w zt|\}}W n ty<   tjjdt| jfgdf Y S w | ||I dH sStjjdt| jfgdfS || _	t
 ||I dH 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-   r0   r1   authorizationr   r   r.   r   r   r8   O   s0   	z0BasicAuthWebSocketServerProtocol.process_request)
r%   r   r!   r"   r$   r&   r'   r   r   r(   r   r   r   r   r   r   )r0   r   r1   r
   r   r2   )__name__
__module____qualname____doc__r!   __annotations__r   r,   r$   r8   __classcell__r   r   r.   r   r      s   
 
r!   r"   credentials*Credentials | Iterable[Credentials] | Noner$   r&   create_protocol6Callable[..., BasicAuthWebSocketServerProtocol] | None/Callable[..., BasicAuthWebSocketServerProtocol]c                   s   |du |du krt d|durLt|rtt|g}n&t|tr:tttt |}tdd |D s9t d| nt d| t| d fdd}|du rRt	}tt
dt	f |}tj|| |d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                 s  s    | ]}t |V  qd S r)   )r   ).0itemr   r   r   	<genexpr>   s    z.basic_auth_protocol_factory.<locals>.<genexpr>zinvalid credentials argument: r   r   r   r   r   c                   s0   z |  }W n
 t y   Y dS w t||S r   )r4   hmaccompare_digest)r   r   expected_passwordcredentials_dictr   r   r$      s   z6basic_auth_protocol_factory.<locals>.check_credentials.r#   r:   )r   r   r   Credentialsr   r   listalldictr   r   	functoolspartial)r!   rA   r$   rC   credentials_listr   rL   r   r   v   s.   &
)r   r   r   r   )NNNN)
r!   r"   rA   rB   r$   r&   rC   rD   r   rE   )
__future__r   rR   rI   r5   typingr   r   r   r   r   r   datastructuresr
   
exceptionsr   headersr   r   serverr   r   __all__r   rN   r   r   r   r   r   r   r   <module>   s$     
	Z