a
    ñ»jA  ã                   @  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œdd„ZG dd„ deƒZddddd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_factoryr   Úbool)ÚvalueÚreturnc              	   C  s>   z| \}}W n t tfy$   Y dS 0 t|tƒo8t|tƒS d S ©NF)Ú	TypeErrorÚ
ValueErrorÚ
isinstanceÚstr)r   ÚusernameÚpassword© r   ú<E:\soccer\ml_env\Lib\site-packages\websockets/legacy/auth.pyÚis_credentials   s
    r   c                      sx   e Zd ZU dZdZded< dZded< dddœd	dd
d	ddœ‡ fdd„Zddddœdd„Zddddœ‡ fdd„Z	‡  Z
S )r   zC
    WebSocket server protocol that enforces HTTP Basic Auth.

    Ú r   ÚrealmNú
str | Noner   ©r    Úcheck_credentialsr   ú,Callable[[str, str], Awaitable[bool]] | NoneÚNone)Úargsr    r#   Úkwargsr   c                  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   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_credentialsr	   zHTTPResponse | None)ÚpathÚrequest_headersr   c                 ƒ  s¾   z|d }W n, t y8   tjjdt| jƒfgdf Y S 0 zt|ƒ\}}W n, tyv   tjjdt| jƒfgdf Y S 0 |  ||¡I dH s¤t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   r7   O   s*    	ý
ý
ýz0BasicAuthWebSocketServerProtocol.process_request)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    Ú__annotations__r   r+   r#   r7   Ú__classcell__r   r   r-   r   r      s   
ür!   z*Credentials | Iterable[Credentials] | Noner$   z6Callable[..., BasicAuthWebSocketServerProtocol] | Nonez/Callable[..., BasicAuthWebSocketServerProtocol])r    Úcredentialsr#   Úcreate_protocolr   c                   sÎ   |du |du krt dƒ‚|dur t|ƒr6tt|ƒg}nLt|tƒrttttt |ƒƒ}tdd„ |D ƒƒs‚t d|› �ƒ‚nt d|› �ƒ‚t|ƒ‰ ddddœ‡ fd	d
„}|du r¬t	}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>¤   ó    z.basic_auth_protocol_factory.<locals>.<genexpr>zinvalid credentials argument: r   r   r/   c                 “  s.   zˆ |  }W n t y    Y dS 0 t ||¡S r   )r3   ÚhmacÚcompare_digest)r   r   Zexpected_password©Zcredentials_dictr   r   r#   «   s
    z6basic_auth_protocol_factory.<locals>.check_credentials.r"   )r   r   r   ÚCredentialsr   r   ÚlistÚallÚdictr   r   Ú	functoolsÚpartial)r    r?   r#   r@   Zcredentials_listr   rG   r   r   v   s,    &
ÿý)NNNN)Ú
__future__r   rL   rE   r4   Úcollections.abcr   r   Útypingr   r   r   Zdatastructuresr	   Ú
exceptionsr
   Úheadersr   r   Úserverr   r   Ú__all__Útupler   rH   r   r   r   r   r   r   r   Ú<module>   s$   	Z    ü