o
    f~                     @   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   Q/var/www/html/venv/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 2069N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