o
    Ð­jƒ  ã                   @  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 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d„ZG dd„ deƒZ				d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              	   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   úS/var/www/html/CropPilot/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,   r/   r0   Úauthorizationr   r   r-   r   r   r7   O   s0   €	ýÿýÿýz0BasicAuthWebSocketServerProtocol.process_request)
r$   r   r    r!   r#   r%   r&   r   r   r'   ©r   r   r   r   r   r   )r/   r   r0   r	   r   r1   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    Ú__annotations__r   r+   r#   r7   Ú__classcell__r   r   r-   r   r      s   
 ü
r    r!   Úcredentialsú*Credentials | Iterable[Credentials] | Noner#   r%   Úcreate_protocolú6Callable[..., 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   )r3   ÚhmacÚcompare_digest)r   r   Úexpected_password©Úcredentials_dictr   r   r#   «   s   €ÿz6basic_auth_protocol_factory.<locals>.check_credentials.r"   r9   )r   r   r   ÚCredentialsr   r   ÚlistÚallÚdictr   r   Ú	functoolsÚpartial)r    r@   r#   rB   Úcredentials_listr   rK   r   r   v   s.   &
ÿÿý)r   r   r   r   )NNNN)
r    r!   r@   rA   r#   r%   rB   rC   r   rD   )Ú
__future__r   rQ   rH   r4   Úcollections.abcr   r   Útypingr   r   r   Údatastructuresr	   Ú
exceptionsr
   Úheadersr   r   Úserverr   r   Ú__all__Útupler   rM   r   r   r   r   r   r   r   Ú<module>   s&    
	Zü