U
    nËjÿ  ã                   @  sr   d dl mZ d dlZd dlmZmZmZmZ d dlm	Z	 d dl
mZmZ d dlmZmZmZmZ G dd„ dƒZdS )	é    )ÚannotationsN)ÚAuthCredentialsÚAuthenticationBackendÚAuthenticationErrorÚUnauthenticatedUser)ÚHTTPConnection)ÚPlainTextResponseÚResponse)ÚASGIAppÚReceiveÚScopeÚSendc                   @  sL   e Zd Zddddddœdd„Zd	d
dddœdd„Zeddddœdd„ƒZdS )ÚAuthenticationMiddlewareNr
   r   zGtyping.Callable[[HTTPConnection, AuthenticationError], Response] | NoneÚNone)ÚappÚbackendÚon_errorÚreturnc                 C  s$   || _ || _|d k	r|n| j| _d S )N)r   r   Údefault_on_errorr   )Úselfr   r   r   © r   úG/tmp/pip-unpacked-wheel-pzfrzqh9/starlette/middleware/authentication.pyÚ__init__   s    ÿz!AuthenticationMiddleware.__init__r   r   r   )ÚscopeÚreceiveÚsendr   c              
   Ã  sè   |d dkr$|   |||¡I d H  d S t|ƒ}z| j |¡I d H }W nh tk
rª } zJ|  ||¡}|d dkr‚|dddœƒI d H  n||||ƒI d H  W Y ¢d S d }~X Y nX |d krÀtƒ tƒ f}|\|d< |d< |   |||¡I d H  d S )	NÚtype)ÚhttpÚ	websocketr   zwebsocket.closeiè  )r   ÚcodeÚauthÚuser)r   r   r   Zauthenticater   r   r   r   )r   r   r   r   ÚconnZauth_resultÚexcÚresponser   r   r   Ú__call__   s     z!AuthenticationMiddleware.__call__r   Ú	Exceptionr	   )r"   r#   r   c                 C  s   t t|ƒdd�S )Ni�  )Ústatus_code)r   Ústr)r"   r#   r   r   r   r   2   s    z)AuthenticationMiddleware.default_on_error)N)Ú__name__Ú
__module__Ú__qualname__r   r%   Ústaticmethodr   r   r   r   r   r      s
    ür   )Ú
__future__r   ÚtypingZstarlette.authenticationr   r   r   r   Zstarlette.requestsr   Zstarlette.responsesr   r	   Zstarlette.typesr
   r   r   r   r   r   r   r   r   Ú<module>   s   