
    Vvg"                         d dl Z d dlmZmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZmZmZ  G d	 d
e          Z G d d          Z G d dee          ZdS )    N)ABCabstractmethod)Optional)warn)reverse)NoReverseMatch)settings)get_failure_limit is_client_ip_address_blacklisted is_client_ip_address_whitelistedis_client_method_whitelistedis_user_attempt_whitelistedc                       e Zd ZdZed	defd            Zed             Zed             Zed	de	e         de
fd            ZdS )
AbstractAxesHandlerz3
    Contract that all handlers need to follow
    Ncredentialsc                      t          d          )zm
        Handles the Django ``django.contrib.auth.signals.user_login_failed`` authentication signal.
        z'user_login_failed should be implementedNotImplementedErrorselfsenderr   requestkwargss        O/var/www/pixelcanvas.ch/venv/lib/python3.11/site-packages/axes/handlers/base.pyuser_login_failedz%AbstractAxesHandler.user_login_failed   s    
 ""KLLL    c                      t          d          )zj
        Handles the Django ``django.contrib.auth.signals.user_logged_in`` authentication signal.
        z$user_logged_in should be implementedr   r   r   r   userr   s        r   user_logged_inz"AbstractAxesHandler.user_logged_in   s    
 ""HIIIr   c                      t          d          )zk
        Handles the Django ``django.contrib.auth.signals.user_logged_out`` authentication signal.
        z%user_logged_out should be implementedr   r   s        r   user_logged_outz#AbstractAxesHandler.user_logged_out&   s    
 ""IJJJr   returnc                      t          d          )a  
        Checks the number of failures associated to the given request and credentials.

        This is a virtual method that needs an implementation in the handler subclass
        if the ``settings.AXES_LOCK_OUT_AT_FAILURE`` flag is set to ``True``.
        z"get_failures should be implementedr   r   r   r   s      r   get_failuresz AbstractAxesHandler.get_failures-   s     ""FGGGr   N)__name__
__module____qualname____doc__r   dictr   r    r"   r   intr&    r   r   r   r      s          M MT M M M ^M J J ^J K K ^K H H$ H3 H H H ^H H Hr   r   c            	       ^   e Zd ZdZddee         defdZddee         defdZddee         defdZ	ddee         defdZ
dee         fd	Zdefd
ZdefdZdddddee         dee         dedefdZdddee         defdZdddee         defdZdddedee         defdZdS )AxesBaseHandlera  
    Handler API definition for implementations that are used by the ``AxesProxyHandler``.

    If you wish to specialize your own handler class, override the necessary methods
    and configure the class for use by setting ``settings.AXES_HANDLER = 'module.path.to.YourClass'``.
    Make sure that new the handler is compliant with AbstractAxesHandler and make sure it extends from this mixin.
    Refer to `AxesHandler` for an example.

    The default implementation that is actually used by Axes is ``axes.handlers.database.AxesDatabaseHandler``.

    .. note:: This is a virtual class and **can not be used without specialization**.
    Nr   r#   c                     t           j        r|                     |          sdS |                     ||          rdS |                     ||          rdS |                     ||          rdS dS )aF  
        Checks if the user is allowed to access or use given functionality such as a login view or authentication.

        This method is abstract and other backends can specialize it as needed, but the default implementation
        checks if the user has attempted to authenticate into the site too many times through the
        Django authentication backends and returns ``False`` if user exceeds the configured Axes thresholds.

        This checker can implement arbitrary checks such as IP whitelisting or blacklisting,
        request frequency checking, failed attempt monitoring or similar functions.

        Please refer to the ``axes.handlers.database.AxesDatabaseHandler`` for the default implementation
        and inspiration on some common checks and access restrictions before writing your own implementation.
        TF)r	   AXES_ONLY_ADMIN_SITEis_admin_requestis_blacklistedis_whitelisted	is_lockedr%   s      r   
is_allowedzAxesBaseHandler.is_allowedF   s     ( 	1F1Fw1O1O 	4w44 	5w44 	4>>';// 	5tr   c                 (    t          |          rdS dS )zY
        Checks if the request or given credentials are blacklisted from access.
        TF)r   r%   s      r   r4   zAxesBaseHandler.is_blacklistedc   s    
 ,G44 	4ur   c                 n    t          ||          rdS t          |          rdS t          |          rdS dS )zX
        Checks if the request or given credentials are whitelisted for access.
        TF)r   r   r   r%   s      r   r5   zAxesBaseHandler.is_whitelistedm   sJ    
 'w<< 	4+G44 	4'00 	4ur   c                 n    t           j        r(|                     ||          t          ||          k    S dS )zH
        Checks if the request or given credentials are locked.
        F)r	   AXES_LOCK_OUT_AT_FAILUREr&   r
   r%   s      r   r6   zAxesBaseHandler.is_locked}   sE    
 , 	9$$ "7K889 9 ur   c                 D    	 t          d          S # t          $ r Y dS w xY w)zE
        Returns admin url if exists, otherwise returns None
        admin:indexN)r   r   )r   s    r   get_admin_urlzAxesBaseHandler.get_admin_url   s7    	=))) 	 	 	44	s    
c                     t          |d          r7|                                 }|duot          j        d| |j                  duS dS )z>
        Checks that request located under admin site
        pathN^F)hasattrr>   rematchr@   r   r   	admin_urls      r   r3   z AxesBaseHandler.is_admin_request   s]     7F## 	**,,I% HH___gl;;4G
 ur   c                     t          dt                     t          j        rPt	          |d          r@	 t          d          }n# t          $ r Y dS w xY wt          j        d| |j	                   S dS )zq
        Checks if the request is NOT for admin site
        if `settings.AXES_ONLY_ADMIN_SITE` is True.
        zThis method is deprecated and will be removed in future versions. If you looking for method that checks if `request.path` located under admin site, use `is_admin_request` instead.r@   r=   TrA   F)
r   DeprecationWarningr	   r2   rB   r   r   rC   rD   r@   rE   s      r   is_admin_sitezAxesBaseHandler.is_admin_site   s    
 	> 	
 	
 	
 ( 	?WWf-E-E 	?#M22		!   ttxI>>>>us   A 
AAF)
ip_addressusernameip_or_usernamerJ   rK   rL   c                    dS )aZ  
        Resets access attempts that match the given IP address or username.

        This method makes more sense for the DB backend, but as it is used by the ProxyHandler
        (via inherent), it needs to be defined here, so we get compliant with all proxy methods.

        Please overwrite it on each specialized handler as needed.
        r   r.   )r   rJ   rK   rL   s       r   reset_attemptszAxesBaseHandler.reset_attempts   s	     qr   )age_daysrO   c                    dS )aS  
        Resets access logs that are older than given number of days.

        This method makes more sense for the DB backend, but as it is used by the ProxyHandler
        (via inherent), it needs to be defined here, so we get compliant with all proxy methods.

        Please overwrite it on each specialized handler as needed.
        r   r.   r   rO   s     r   
reset_logszAxesBaseHandler.reset_logs   	     qr   c                    dS )a[  
        Resets access failure logs that are older than given number of days.

        This method makes more sense for the DB backend, but as it is used by the ProxyHandler
        (via inherent), it needs to be defined here, so we get compliant with all proxy methods.

        Please overwrite it on each specialized handler as needed.
        r   r.   rQ   s     r   reset_failure_logsz"AxesBaseHandler.reset_failure_logs   rS   r   )limitrV   c                    dS )ay  Remove access failure logs that are over
        AXES_ACCESS_FAILURE_LOG_PER_USER_LIMIT for user username.

        This method makes more sense for the DB backend, but as it is used by the ProxyHandler
        (via inherent), it needs to be defined here, so we get compliant with all proxy methods.

        Please overwrite it on each specialized handler as needed.

        r   r.   )r   rK   rV   s      r    remove_out_of_limit_failure_logsz0AxesBaseHandler.remove_out_of_limit_failure_logs   s	     qr   r'   )r(   r)   r*   r+   r   r,   boolr7   r4   r5   r6   strr>   r3   rI   r-   rN   rR   rU   rX   r.   r   r   r0   r0   8   s"         x~     : 8D> T     8D> T      htn     x}    4        2 %)"&$   SM 3-	
  
   " 7; 	 	 	hsm 	s 	 	 	 	 ?C 	 	 	hsm 	s 	 	 	 	 8<  '/}	     r   r0   c                   J    e Zd ZdZd	defdZd Zd Zd	dee         de	fdZ
dS )
AxesHandlerzI
    Signal bare handler implementation without any storage backend.
    Nr   c                     d S r'   r.   r   s        r   r   zAxesHandler.user_login_failed       r   c                     d S r'   r.   r   s        r   r    zAxesHandler.user_logged_in   r^   r   c                     d S r'   r.   r   s        r   r"   zAxesHandler.user_logged_out   r^   r   r#   c                     dS )Nr   r.   r%   s      r   r&   zAxesHandler.get_failures   s    qr   r'   )r(   r)   r*   r+   r,   r   r    r"   r   r-   r&   r.   r   r   r\   r\      s          T         $ 3      r   r\   )rC   abcr   r   typingr   warningsr   django.urlsr   django.urls.exceptionsr   	axes.confr	   axes.helpersr
   r   r   r   r   r   r0   r\   r.   r   r   <module>ri      sa   				 # # # # # # # #                   1 1 1 1 1 1                   "H "H "H "H "H# "H "H "HJq q q q q q q qh    %     r   