B
    £0H[(  ã               @   s
  d dl Z 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	m
Z
mZ d dlmZ d dl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)Úsettings)ÚFieldDoesNotExistÚImproperlyConfiguredÚValidationError)Úlazy)Úformat_html)Úimport_string)ÚgettextÚngettext)Úmaxsizec               C   s
   t tjƒS )N)Úget_password_validatorsr   ZAUTH_PASSWORD_VALIDATORS© r   r   úkC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\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 | |f | 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_configZ
validatorsÚ	validatorÚklassÚmsgr   r   r   r      s    
r   c             C   sn   g }|dkrt ƒ }xJ|D ]B}y| | |¡ W q tk
rX } z| |¡ W dd}~X Y 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 ]}| | ¡ ¡ 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   s,   t | ƒ}dd„ |D ƒ}|r(dd |¡ S dS )z`
    Return an HTML string with all help texts of all configured validators
    in an <ul>.
    c             S   s   g | ]}t d |ƒ‘qS )z<li>{}</li>)r   )Ú.0Ú	help_textr   r   r   ú
<listcomp>T   s    z7_password_validators_help_text_html.<locals>.<listcomp>z<ul>%s</ul>Ú )r&   Újoin)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)Úselfr/   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_shortr/   )ÚcodeÚparams)Úlenr/   r   r   )r0   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.r/   )r   r/   )r0   r   r   r   r$   n   s
    z$MinimumLengthValidator.get_help_text)r.   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r1   r   r$   r   r   r   r   r-   [   s   

r-   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.
    )ÚusernameZ
first_nameÚ	last_nameÚemailgffffffæ?c             C   s   || _ || _d S )N)Úuser_attributesÚmax_similarity)r0   r=   r>   r   r   r   r1   ƒ   s    z)UserAttributeSimilarityValidator.__init__Nc          
   C   s¼   |sd S x®| j D ]¤}t||d ƒ}|rt|tƒs0qt d|¡|g }xp|D ]h}t| ¡ | ¡ d� ¡ | j	krHyt|j
 |¡jƒ}W n tk
r˜   |}Y nX ttdƒdd|id�‚qHW qW d S )Nz\W+)r!   Úbz4The password is too similar to the %(verbose_name)s.Zpassword_too_similarÚverbose_name)r2   r3   )r=   r#   Ú
isinstanceÚstrÚreÚsplitr   ÚlowerZquick_ratior>   Z_metaÚ	get_fieldr@   r   r   Ú_)r0   r   r   Zattribute_nameÚvalueZvalue_partsZ
value_partr@   r   r   r   r   ‡   s"    

z)UserAttributeSimilarityValidator.validatec             C   s   t dƒS )NzFYour password can't be too similar to your other personal information.)rG   )r0   r   r   r   r$   œ   s    z.UserAttributeSimilarityValidator.get_help_text)N)r5   r6   r7   r8   ZDEFAULT_USER_ATTRIBUTESr1   r   r$   r   r   r   r   r9   v   s
   

r9   c               @   sL   e Zd ZdZej ej ej e	¡¡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, which may be gzipped.
    The list Django ships with contains 1000 common passwords, created by Mark Burnett:
    https://xato.net/passwords/more-top-worst-passwords/
    zcommon-passwords.txt.gzc             C   sp   y*t  |¡�}| ¡  ¡  ¡ }W d Q R X W n0 tk
rZ   t|ƒ�}| ¡ }W d Q R X Y nX dd„ |D ƒ| _d S )Nc             S   s   h | ]}|  ¡ ’qS r   )Ústrip)r'   Úpr   r   r   ú	<setcomp>´   s    z3CommonPasswordValidator.__init__.<locals>.<setcomp>)ÚgzipÚopenÚreadÚdecodeÚ
splitlinesÚIOErrorÚ	readlinesÚ	passwords)r0   Zpassword_list_pathÚfZcommon_passwords_linesr   r   r   r1   ¬   s    
z CommonPasswordValidator.__init__Nc             C   s&   |  ¡  ¡ | jkr"ttdƒdd�‚d S )NzThis password is too common.Zpassword_too_common)r2   )rE   rJ   rT   r   rG   )r0   r   r   r   r   r   r   ¶   s    z CommonPasswordValidator.validatec             C   s   t dƒS )Nz0Your password can't be a commonly used password.)rG   )r0   r   r   r   r$   ½   s    z%CommonPasswordValidator.get_help_text)N)r5   r6   r7   r8   ÚosÚpathr+   ÚdirnameÚrealpathÚ__file__ZDEFAULT_PASSWORD_LIST_PATHr1   r   r$   r   r   r   r   rI       s   

rI   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   |  ¡ rttdƒdd�‚d S )Nz"This password is entirely numeric.Zpassword_entirely_numeric)r2   )Úisdigitr   rG   )r0   r   r   r   r   r   r   Å   s    z!NumericPasswordValidator.validatec             C   s   t dƒS )Nz(Your password can't be entirely numeric.)rG   )r0   r   r   r   r$   Ì   s    z&NumericPasswordValidator.get_help_text)N)r5   r6   r7   r8   r   r$   r   r   r   r   r[   Á   s   
r[   )NN)NN)N)N)#Ú	functoolsrM   rV   rC   Údifflibr   Zdjango.confr   Zdjango.core.exceptionsr   r   r   Zdjango.utils.functionalr   Zdjango.utils.htmlr   Zdjango.utils.module_loadingr	   Zdjango.utils.translationr
   rG   r   Ú	lru_cacher   r   r   r    r&   r,   rB   Z"password_validators_help_text_htmlr-   r9   rI   r[   r   r   r   r   Ú<module>   s*   





*!