
    Vvg                         d dl 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mZmZmZmZmZmZ d dlmZ d dlmZ  ee          Z G d d	ee          Zd
S )    )	getLogger)Optional)settings)AxesBaseHandlerAbstractAxesHandler)	get_cacheget_cache_timeoutget_client_cache_keysget_client_strget_client_usernameget_credentialsget_failure_limitget_lockout_parameters)AccessAttempt)user_locked_outc            	           e Zd ZdZd Zdddddee         dee         ded	efd
Z	ddee
         d	efdZdde
fdZd Zd ZdS )AxesCacheHandlerzs
    Signal handler implementation that records user login attempts to cache and locks users out if necessary.
    c                 ,    t                      | _        d S N)r   cache)selfs    P/var/www/pixelcanvas.ch/venv/lib/python3.11/site-packages/axes/handlers/cache.py__init__zAxesCacheHandler.__init__   s    [[


    NF)
ip_addressusernameip_or_usernamer   r   r   returnc                X   g }d}||t          d          |rt          d          |                    t          t          ||                               |D ]2}| j                            |          }||t          |          ndz  }3t                              d|           |S )Nr   z#Cannot clear all entries from cachez9Due to the cache key ip_or_username=True is not supported)r   r      z-AXES: Reset %d access attempts from database.)	NotImplementedErrorextendr
   r   r   deleteintloginfo)r   r   r   r   
cache_keyscount	cache_keydeleteds           r   reset_attemptszAxesCacheHandler.reset_attempts   s     
("2%&KLLL 	%K   	!xJGGG 	
 	
 	
 $ 	@ 	@Ij''	22GW%8S\\\a?EE@%HHHr   credentialsc                 ^     t          ||          }t           fd|D                       }|S )Nc              3   P   K   | ] }j                             |d           V  !dS )r   defaultN)r   get).0r)   r   s     r   	<genexpr>z0AxesCacheHandler.get_failures.<locals>.<genexpr>?   sF       
 
5>DJNN9aN00
 
 
 
 
 
r   )r
   max)r   requestr,   r'   failure_counts   `    r   get_failureszAxesCacheHandler.get_failures=   sO    *7K@@
 
 
 
 
BL
 
 
 
 
 r   c           	         |t                               d           dS t          ||          }t          ||          }|dgk    r|t                               d           dS t
          j        s-|j        r&||_        t          j
        d|||j                   dS t          ||j        |j        |j        |          }|                     ||          rt                               d|           dS t#          ||          }t%          |          }	g }
|D ]}| j                            |d|		          }|r|
                    d           7|
                    | j                            |d
                     | j                            ||	           t1          |
          }||_        |dk    r,t                               d||t5          ||                     nt                               d|           t
          j        r\|t5          ||          k    rJt                               d|           d|_        ||_        t          j
        d|||j                   dS dS dS )z
        When user login fails, save attempt record in cache and lock user out if necessary.

        :raises AxesSignalPermissionDenied: if user should be locked out.
        NzMAXES: AxesCacheHandler.user_login_failed does not function without a request.r   zfAXES: Username is None and username is the only one lockout parameter, new record will NOT be created.axes)r5   r   r   z.AXES: Login failed from whitelisted client %s.r    )keyvaluetimeout)r:   delta)r:   r<   z\AXES: Repeated login failure by %s. Count = %d of %d. Updating existing record in the cache.z@AXES: New login failure by %s. Creating new record in the cache.z3AXES: Locking out %s after repeated login failures.T)r%   errorr   r   warningr   -AXES_RESET_COOL_OFF_ON_FAILURE_DURING_LOCKOUTaxes_locked_outaxes_credentialsr   sendaxes_ip_addressr   axes_user_agentaxes_path_infois_whitelistedr&   r
   r	   r   addappendincrtouchr4   axes_failures_since_startr   AXES_LOCK_OUT_AT_FAILURE)r   senderr,   r5   kwargsr   lockout_parameters
client_strr'   cache_timeoutfailuresr)   addedfailures_since_starts                 r   user_login_failedz"AxesCacheHandler.user_login_failedD   s    ?II_   F&w<<3G[II*--(2BKKx   F F	'	 (3G$ !"2	    F###"
 

 w44 	HHEzRRRF*7K@@
)'22# 	G 	GIJNNy=NQQE G""""
IQ G GHHH
  Y FFFF"8}},@)!##KKn$!';77	    KKR   -	$(9';(O(OOOKKEz   '+G#'2G$ !"2	     	 	OOr   c                    |                                 }t          |          }t          ||j        |j        |j        |          }t                              d|           t          j	        rgt          ||          }|D ]V}	| j                            |	d          }
| j                            |	           t                              d|
|           UdS dS )zN
        When user logs in, update the AccessLog related to the user.
        zAXES: Successful login by %s.r   r/   z8AXES: Deleted %d failed login attempts by %s from cache.N)get_usernamer   r   rD   rE   rF   r%   r&   r   AXES_RESET_ON_SUCCESSr
   r   r1   r#   )r   rN   r5   userrO   r   r,   rQ   r'   r)   rU   s              r   user_logged_inzAxesCacheHandler.user_logged_in   s    
 $$&&%h//###"
 

 	0*===) 		.wDDJ'  	'+z~~i~'K'K$
!!),,,N(   		 		 r   c                     |r|                                 nd }t          ||j        |j        |j        |          }t
                              d|           d S )NzAXES: Successful logout by %s.)rX   r   rD   rE   rF   r%   r&   )r   rN   r5   rZ   rO   r   rQ   s          r   user_logged_outz AxesCacheHandler.user_logged_out   sa    *.84$$&&&D###"
 

 	1:>>>>>r   r   )__name__
__module____qualname____doc__r   r   strboolr$   r+   dictr7   rV   r[   r]    r   r   r   r      s         ! ! ! %)"&$   SM 3-	
  
   > $ 3    Y YT Y Y Y Yv  8
? 
? 
? 
? 
?r   r   N)loggingr   typingr   	axes.confr   axes.handlers.baser   r   axes.helpersr   r	   r
   r   r   r   r   r   axes.modelsr   axes.signalsr   r^   r%   r   re   r   r   <module>rm      s-                     C C C C C C C C	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 & % % % % % ( ( ( ( ( (io? o? o? o? o?*O o? o? o? o? o?r   