o
    r¨Êh~  ã                   @   s¾   d 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 ddlm	Z	 ddlm
Z
 ddlm  mZ ddlm  mZ ejZddlm  mZ G dd„ dƒZG d	d
„ d
ƒZe dej¡ZdS )z>$Id: auth_handler.py,v 1.6 2002/11/25 19:40:23 akuchling Exp $é    N)Ú	as_stringÚas_bytes)ÚencodestringÚdecodestring)Úlong)Úmd5c                   @   sF   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dS )Úauth_handlerÚdefaultc                 C   s.   t |ƒ| _|| _|| _t ¡ | _t ¡ | _d S ©N)Údictionary_authorizerÚ
authorizerÚhandlerÚrealmÚcounterÚ
pass_countÚ
fail_count)ÚselfÚdictr   r   © r   úP/var/www/html/env/lib/python3.10/site-packages/supervisor/medusa/auth_handler.pyÚ__init__#   s
   

zauth_handler.__init__c                 C   s   | j  |¡S r
   )r   Úmatch©r   Úrequestr   r   r   r   *   s   zauth_handler.matchc                 C   sÜ   t t|jƒ}|rg| ¡ }|dkrXt t|jdƒ}z
ttt|ƒƒƒ}W n   tj 	d| ¡ | 
d¡ Y d S | dd¡}| j |¡rQ| j ¡  ||_| j |¡ d S |  |¡ d S tj 	d| ¡ |  |¡ d S |  |¡ d S )NÚbasicé   z"malformed authorization info <%s>
i�  ú:é   z$unknown/unsupported auth method: %s
)Ú
get_headerÚAUTHORIZATIONÚheaderÚlowerr   r   r   ÚsysÚstderrÚwriteÚerrorÚsplitr   Ú	authorizer   Ú	incrementÚ	auth_infor   Úhandle_requestÚhandle_unauthorized)r   r   ÚschemeÚcookieÚdecodedr)   r   r   r   r*   .   s(   

	zauth_handler.handle_requestc                 C   s:   | j  ¡  |j d ¡ d|d< d| j |d< | d¡ d S )NÚcloseÚ
ConnectionzBasic realm="%s"zWWW-Authenticatei‘  )r   r(   ÚchannelÚset_terminatorr   r%   r   r   r   r   r+   R   s
   
z auth_handler.handle_unauthorizedc                 C   s\   |j jj}ttt ¡ ƒƒ}|dd… dkr|dd… }tt| ƒƒ}d |||g¡}|  |¡S )zEA digest-authentication <nonce>, constructed as suggested in RFC 2069éÿÿÿÿNÚLr   )	r1   ÚserverÚipÚstrr   ÚtimeÚidÚjoinÚ
apply_hash)r   r   r6   ÚnowÚprivate_keyÚnoncer   r   r   Ú
make_nonce[   s   

zauth_handler.make_noncec                 C   s(   t ƒ }| |¡ | ¡ }t|ƒdd… S )z;Apply MD5 to a string <s>, then wrap it in base64 encoding.Nr3   )r   ÚupdateÚdigestr   )r   ÚsÚmÚdr   r   r   r;   e   s   
zauth_handler.apply_hashc                 C   sH   t  d| j ¡g}t| jdƒr| | j ¡ ¡ | t  d¡¡ t  |¡S )NzB<li>Authorization Extension : <b>Unauthorized requests:</b> %s<ul>Ústatusz</ul>)Ú	producersÚsimple_producerr   Úhasattrr   ÚappendrE   Úcomposite_producer)r   Úrr   r   r   rE   m   s   ÿÿÿÿ
zauth_handler.statusN)r	   )
Ú__name__Ú
__module__Ú__qualname__r   r   r*   r+   r?   r;   rE   r   r   r   r   r   "   s    
$	
r   c                   @   s   e Zd Zdd„ Zdd„ ZdS )r   c                 C   s
   || _ d S r
   ©r   )r   r   r   r   r   r   }   s   
zdictionary_authorizer.__init__c                 C   s(   |\}}|| j v r| j | |krdS dS )Nr   r   rO   )r   r)   ÚusernameÚpasswordr   r   r   r'   €   s   zdictionary_authorizer.authorizeN)rL   rM   rN   r   r'   r   r   r   r   r   |   s    r   zAuthorization: ([^ ]+) (.*))ÚRCS_IDÚrer"   r8   Úsupervisor.compatr   r   r   r   r   r   Úsupervisor.medusa.counterÚmedusar   Ú!supervisor.medusa.default_handlerÚdefault_handlerr   Úsupervisor.medusa.producersrF   r   r   ÚcompileÚ
IGNORECASEr   r   r   r   r   Ú<module>   s$   Zý