3
v^                 @   s  d dl Z d dlZd dl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 d dlmZ d dlmZmZ d dlmZ d d	lmZmZ e jdd
dd Zdd ZdddZd ddZd!ddZd"ddZeeeZ G dd dZ!G dd dZ"G dd dZ#G dd dZ$dS )#    N)SequenceMatcher)Path)settings)FieldDoesNotExistImproperlyConfiguredValidationError)lazy)format_htmlformat_html_join)import_string)gettextngettext)maxsizec               C   s
   t tjS )N)get_password_validatorsr   AUTH_PASSWORD_VALIDATORS r   r   K/usr/lib/python3.6/site-packages/django/contrib/auth/password_validation.pyget_default_password_validators   s    r   c             C   sh   g }x^| D ]V}yt |d }W n( tk
rF   d}t||d  Y nX |j|f |jdi  q
W |S )NNAMEzZThe module in NAME could not be imported: %s. Check your AUTH_PASSWORD_VALIDATORS setting.OPTIONS)r   ImportErrorr   appendget)Zvalidator_config
validators	validatorklassmsgr   r   r   r      s    
r   c             C   sn   g }|dkrt  }xJ|D ]B}y|j| | W q tk
rX } z|j| W Y dd}~X qX qW |rjt|dS )z
    Validate whether the password meets all validator requirements.

    If the password is valid, return ``None``.
    If the password is invalid, raise ValidationError with all error messages.
    N)r   validater   r   )passworduserpassword_validatorserrorsr   errorr   r   r   validate_password#   s    
 r#   c             C   s:   |dkrt  }x&|D ]}t|ddd }|| | qW dS )zy
    Inform all validators that have implemented a password_changed() method
    that the password has been changed.
    Npassword_changedc              W   s   d S )Nr   )ar   r   r   <lambda>>   s    z"password_changed.<locals>.<lambda>)r   getattr)r   r   r    r   r$   r   r   r   r$   6   s
    
r$   c             C   s2   g }| dkrt  } x| D ]}|j|j  qW |S )zG
    Return a list of all help texts of all configured validators.
    N)r   r   get_help_text)r    
help_textsr   r   r   r   password_validators_help_textsB   s    
r*   c             C   s0   t | }tdddd |D }|r,td|S dS )z`
    Return an HTML string with all help texts of all configured validators
    in an <ul>.
     z<li>{}</li>c             s   s   | ]}|fV  qd S )Nr   ).0	help_textr   r   r   	<genexpr>T   s    z6_password_validators_help_text_html.<locals>.<genexpr>z<ul>{}</ul>)r*   r
   r	   )r    r)   Z
help_itemsr   r   r   #_password_validators_help_text_htmlN   s    r/   c               @   s,   e Zd ZdZd
ddZdddZdd	 ZdS )MinimumLengthValidatorz?
    Validate whether the password is of a minimum length.
       c             C   s
   || _ d S )N)
min_length)selfr2   r   r   r   __init___   s    zMinimumLengthValidator.__init__Nc             C   s0   t || jk r,ttdd| jdd| jidd S )NzNThis password is too short. It must contain at least %(min_length)d character.zOThis password is too short. It must contain at least %(min_length)d characters.Zpassword_too_shortr2   )codeparams)lenr2   r   r   )r3   r   r   r   r   r   r   b   s    zMinimumLengthValidator.validatec             C   s   t dd| jd| ji S )Nz=Your password must contain at least %(min_length)d character.z>Your password must contain at least %(min_length)d characters.r2   )r   r2   )r3   r   r   r   r(   n   s
    z$MinimumLengthValidator.get_help_text)r1   )N)__name__
__module____qualname____doc__r4   r   r(   r   r   r   r   r0   [   s   

r0   c               @   s4   e Zd ZdZdZedfddZdd
dZdd Zd	S ) UserAttributeSimilarityValidatora  
    Validate whether the password is sufficiently different from the user's
    attributes.

    If no specific attributes are provided, look at a sensible list of
    defaults. Attributes that don't exist are ignored. Comparison is made to
    not only the full attribute value, but also its components, so that, for
    example, a password is validated against either part of an email address,
    as well as the full address.
    username
first_name	last_nameemailgffffff?c             C   s   || _ || _d S )N)user_attributesmax_similarity)r3   rA   rB   r   r   r   r4      s    z)UserAttributeSimilarityValidator.__init__Nc             C   s   |sd S x| j D ]}t||d }| st|t r4qtjd||g }xp|D ]h}t|j |j dj | j	krLyt|j
j|j}W n tk
r   |}Y nX ttddd|idqLW qW d S )Nz\W+)r%   bz4The password is too similar to the %(verbose_name)s.Zpassword_too_similarverbose_name)r5   r6   )rA   r'   
isinstancestrresplitr   lowerquick_ratiorB   _meta	get_fieldrD   r   r   _)r3   r   r   Zattribute_namevaluevalue_partsZ
value_partrD   r   r   r   r      s"    

z)UserAttributeSimilarityValidator.validatec             C   s   t dS )NuH   Your password can’t be too similar to your other personal information.)rM   )r3   r   r   r   r(      s    z.UserAttributeSimilarityValidator.get_help_text)r=   r>   r?   r@   )N)r8   r9   r:   r;   ZDEFAULT_USER_ATTRIBUTESr4   r   r(   r   r   r   r   r<   v   s
   

r<   c               @   s@   e Zd ZdZeej jd ZefddZ	d
ddZ
dd	 ZdS )CommonPasswordValidatora  
    Validate whether the password is a common password.

    The password is rejected if it occurs in a provided list of passwords,
    which may be gzipped. The list Django ships with contains 20000 common
    passwords (lowercased and deduplicated), created by Royce Williams:
    https://gist.github.com/roycewilliams/281ce539915a947a23db17137d91aeb7
    The password list must be lowercased to match the comparison in validate().
    zcommon-passwords.txt.gzc             C   sn   y0t j|ddd}dd |D | _W d Q R X W n8 tk
rh   t|}dd |D | _W d Q R X Y nX d S )NZrtzutf-8)encodingc             S   s   h | ]}|j  qS r   )strip)r,   xr   r   r   	<setcomp>   s    z3CommonPasswordValidator.__init__.<locals>.<setcomp>c             S   s   h | ]}|j  qS r   )rR   )r,   rS   r   r   r   rT      s    )gzipopen	passwordsOSError)r3   Zpassword_list_pathfr   r   r   r4      s    
z CommonPasswordValidator.__init__Nc             C   s&   |j  j | jkr"ttdddd S )NzThis password is too common.Zpassword_too_common)r5   )rI   rR   rW   r   rM   )r3   r   r   r   r   r   r      s    z CommonPasswordValidator.validatec             C   s   t dS )Nu2   Your password can’t be a commonly used password.)rM   )r3   r   r   r   r(      s    z%CommonPasswordValidator.get_help_text)N)r8   r9   r:   r;   r   __file__resolveparentZDEFAULT_PASSWORD_LIST_PATHr4   r   r(   r   r   r   r   rP      s
   	
rP   c               @   s"   e Zd ZdZdddZdd ZdS )NumericPasswordValidatorz8
    Validate whether the password is alphanumeric.
    Nc             C   s   |j  rttdddd S )Nz"This password is entirely numeric.Zpassword_entirely_numeric)r5   )isdigitr   rM   )r3   r   r   r   r   r   r      s    z!NumericPasswordValidator.validatec             C   s   t dS )Nu*   Your password can’t be entirely numeric.)rM   )r3   r   r   r   r(      s    z&NumericPasswordValidator.get_help_text)N)r8   r9   r:   r;   r   r(   r   r   r   r   r]      s   
r]   )NN)NN)N)N)%	functoolsrU   rG   difflibr   pathlibr   django.confr   django.core.exceptionsr   r   r   django.utils.functionalr   django.utils.htmlr	   r
   django.utils.module_loadingr   django.utils.translationr   rM   r   	lru_cacher   r   r#   r$   r*   r/   rF   Z"password_validators_help_text_htmlr0   r<   rP   r]   r   r   r   r   <module>   s*   





*