U
    h}'                     @   s   d Z ddlZddlZeeZddlmZ ddlm	Z	m
Z
 ddlmZmZmZ ddlm  mZ dddgZG d	d dejjejZed
ZdZG dd dejjejZG dd dejZdS )z.passlib.handlers.misc - misc generic handlers
    N)warn)to_native_strstr_consteq)unicodeuunicode_or_bytes_typesunix_disabledunix_fallback	plaintextc                       sL   e Zd ZdZd ZdZedd Zd fdd	Zdd	 Z	edd
dZ
  ZS )r	   a  This class provides the fallback behavior for unix shadow files, and follows the :ref:`password-hash-api`.

    This class does not implement a hash, but instead provides fallback
    behavior as found in /etc/shadow on most unix variants.
    If used, should be the last scheme in the context.

    * this class will positively identify all hash strings.
    * for security, passwords will always hash to ``!``.
    * it rejects all passwords if the hash is NOT an empty string (``!`` or ``*`` are frequently used).
    * by default it rejects all passwords if the hash is an empty string,
      but if ``enable_wildcard=True`` is passed to verify(),
      all passwords will be allowed through if the hash is an empty string.

    .. deprecated:: 1.6
        This has been deprecated due to its "wildcard" feature,
        and will be removed in Passlib 1.8. Use :class:`unix_disabled` instead.
    )enable_wildcardc                 C   s    t |trdS tj|dd S NThash
isinstancer   uhexcExpectedStringErrorclsr    r   ;./venv/lib/python3.8/site-packages/passlib/handlers/misc.pyidentify.   s    
zunix_fallback.identifyFc                    s&   t dt tt| jf | || _d S )Nzf'unix_fallback' is deprecated, and will be removed in Passlib 1.8; please use 'unix_disabled' instead.)r   DeprecationWarningsuperr	   __init__r   )selfr   kwds	__class__r   r   r   5   s
    zunix_fallback.__init__c                 C   s   | j r| j S tdS d S )N!)Zchecksumr   )r   secretr   r   r   _calc_checksum=   s    zunix_fallback._calc_checksumc                 C   s4   t | t|ts$t j|dn|r,dS |S d S )Nr   F)r   validate_secretr   r   r   r   )r   r    r   r   r   r   r   verifyE   s    

zunix_fallback.verify)F)F)__name__
__module____qualname____doc__namecontext_kwdsclassmethodr   r   r!   r#   __classcell__r   r   r   r   r	      s   
z*!s   *!c                       s   e Zd ZdZd ZdZdZeee	Z
dejkr8edZnedZed fdd		Zed
d Zedd Zedd ZejdddedddZedddZedd Z  ZS )r   a  This class provides disabled password behavior for unix shadow files,
    and follows the :ref:`password-hash-api`.

    This class does not implement a hash, but instead matches the "disabled account"
    strings found in ``/etc/shadow`` on most Unix variants. "encrypting" a password
    will simply return the disabled account marker. It will reject all passwords,
    no matter the hash string. The :meth:`~passlib.ifc.PasswordHash.hash`
    method supports one optional keyword:

    :type marker: str
    :param marker:
        Optional marker string which overrides the platform default
        used to indicate a disabled account.

        If not specified, this will default to ``"*"`` on BSD systems,
        and use the Linux default ``"!"`` for all other platforms.
        (:attr:`!unix_disabled.default_marker` will contain the default value)

    .. versionadded:: 1.6
        This class was added as a replacement for the now-deprecated
        :class:`unix_fallback` class, which had some undesirable features.
    markerr   Zbsd*r   Nc                    s:   t t| jf |}|d k	r6| |s0td| ||_|S )Nzinvalid marker: %r)r   r   usingr   
ValueErrordefault_marker)r   r-   r   subclsr   r   r   r/   x   s    
zunix_disabled.usingc                 C   s@   t |trt}nt |tr t}ntj|d| p>|d |kS )Nr   r   )r   r   _MARKER_CHARSbytes_MARKER_BYTESr   r   r   )r   r   startr   r   r   r      s    

zunix_disabled.identifyc                 C   s$   t | | |s t j| dS )NF)r   r"   r   r   InvalidHashError)r   r    r   r   r   r   r#      s    

zunix_disabled.verifyc                 K   sP   |r"t | | | jf ||S t | | j}|r@| |sDtt|ddS )Nr-   Zparam)	r   Zwarn_hash_settings_deprecationr/   r   r"   r1   r   AssertionErrorr   )r   r    r   r-   r   r   r   r      s    
zunix_disabled.hash1.72.0Z
deprecatedZremovedc                 C   sT   |  |stj| n8|r2t| t|ddS |d k	rF| j|d} | |S d S )Nconfigr8   r,   )r   r   r   r7   r"   r   r/   r   )r   r    r=   r-   r   r   r   genhash   s    

zunix_disabled.genhashc                 C   sB   |  d}|d k	r>t|dd}| |r2| |}|r>||7 }|S )N r   r8   )r   r   r   enable)r   r   outr   r   r   disable   s    


zunix_disabled.disablec                 C   sV   t |dd}| jD ]2}||r|t|d  }|r<|  S tdqtj| d S )Nr   r8   zcannot restore original hash)r   _disable_prefixes
startswithlenr0   r   r   r7   )r   r   prefixZorigr   r   r   r@      s    


zunix_disabled.enable)N)N)N)r$   r%   r&   r'   r(   setting_kwdsr)   tuplestrr3   rC   sysplatformr   r1   r*   r/   r   r#   r   r   deprecated_methodr>   rB   r@   r+   r   r   r   r   r   R   s.   




	c                   @   s~   e Zd ZdZd ZdZdZdZedd Z	eddd	Z
edd
dZejdddedd ZejdddedddZdS )r
   a~  This class stores passwords in plaintext, and follows the :ref:`password-hash-api`.

    The :meth:`~passlib.ifc.PasswordHash.hash`, :meth:`~passlib.ifc.PasswordHash.genhash`, and :meth:`~passlib.ifc.PasswordHash.verify` methods all require the
    following additional contextual keyword:

    :type encoding: str
    :param encoding:
        This controls the character encoding to use (defaults to ``utf-8``).

        This encoding will be used to encode :class:`!unicode` passwords
        under Python 2, and decode :class:`!bytes` hashes under Python 3.

    .. versionchanged:: 1.6
        The ``encoding`` keyword was added.
    r   encodingzutf-8c                 C   s    t |trdS tj|dd S r   r   r   r   r   r   r      s    
zplaintext.identifyNc                 C   s    t | |s| j}t||dS )Nr    )r   r"   default_encodingr   )r   r    rN   r   r   r   r      s    
zplaintext.hashc                 C   s>   |s
| j }t||d}| |s,tj| t| |||S )Nr   )rO   r   r   r   r   r7   r   r   )r   r    r   rN   r   r   r   r#      s    
zplaintext.verifyr:   r;   r<   c                 C   s
   |  dS )Nr?   )r   )r   r   r   r   	genconfig   s    zplaintext.genconfigc                 C   s$   |  |stj| | j||dS )NrM   )r   r   r   r7   r   )r   r    r=   rN   r   r   r   r>     s    
zplaintext.genhash)N)N)N)r$   r%   r&   r'   r(   rG   r)   rO   r*   r   r   r#   r   rL   rP   r>   r   r   r   r   r
      s"   
)r'   rJ   ZloggingZ	getLoggerr$   logwarningsr   Zpasslib.utilsr   r   Zpasslib.utils.compatr   r   r   Zpasslib.utils.handlersZutilsZhandlersr   __all__ZifcZDisabledHashZStaticHandlerr	   r3   r5   ZMinimalHandlerr   r
   r   r   r   r   <module>   s     
	6~