o
    uvXjÏ  ã                   @   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   úd/var/www/html/myproject/venv/lib/python3.10/site-packages/django/contrib/auth/password_validation.pyÚget_default_password_validators   s   
r   c              
   C   sb   g }| D ]*}zt |d ƒ}W n ty   d}t||d  ƒ‚w | |di | di ¡¤Ž¡ q|S )NÚNAMEzZThe module in NAME could not be imported: %s. Check your AUTH_PASSWORD_VALIDATORS setting.ÚOPTIONSr   )r   ÚImportErrorr   ÚappendÚget)Úvalidator_configÚ
validatorsÚ	validatorÚklassÚmsgr   r   r   r      s   þr   c                 C   sj   g }|du r	t ƒ }|D ]!}z| | |¡ W q ty, } z| |¡ W Y d}~qd}~ww |r3t|ƒ‚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   s6   |du rt ƒ }|D ]}t|ddd„ ƒ}|| |ƒ q	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 r   r   )Ú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   s.   g }| du r	t ƒ } | D ]	}| | ¡ ¡ q|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 r   r   )Ú.0Ú	help_textr   r   r   Ú	<genexpr>T   s   € z6_password_validators_help_text_html.<locals>.<genexpr>z<ul>{}</ul>)r,   r
   r	   )r"   r+   Ú
help_itemsr   r   r   Ú#_password_validators_help_text_htmlN   s   r2   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 r   )Ú
min_length)Úselfr5   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.Úpassword_too_shortr5   ©ÚcodeÚparams)Úlenr5   r   r   ©r6   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.r5   )r   r5   ©r6   r   r   r   r*   n   s   ýüz$MinimumLengthValidator.get_help_text)r4   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r7   r   r*   r   r   r   r   r3   [   s
    

r3   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 r   )Úuser_attributesÚmax_similarity)r6   rH   rI   r   r   r   r7   ƒ   s   
z)UserAttributeSimilarityValidator.__init__Nc              
   C   s²   |sd S | j D ]O}t||d ƒ}|rt|tƒsqt d|¡|g }|D ]3}t| ¡ | ¡ d� ¡ | j	krUzt|j
 |¡jƒ}W n tyI   |}Y nw ttdƒdd|id�‚q"qd S )Nz\W+)r'   Úbz4The password is too similar to the %(verbose_name)s.Úpassword_too_similarÚverbose_namer9   )rH   r)   Ú
isinstanceÚstrÚreÚsplitr   ÚlowerÚquick_ratiorI   Ú_metaÚ	get_fieldrL   r   r   Ú_)r6   r    r!   Úattribute_nameÚvalueÚvalue_partsÚ
value_partrL   r   r   r   r   ‡   s,   
ÿýûÿûz)UserAttributeSimilarityValidator.validatec                 C   ó   t dƒS )NzFYour password can't be too similar to your other personal information.©rU   r>   r   r   r   r*   œ   ó   z.UserAttributeSimilarityValidator.get_help_textr   )r?   r@   rA   rB   ÚDEFAULT_USER_ATTRIBUTESr7   r   r*   r   r   r   r   rC   v   s    

rC   c                   @   s@   e Zd ZdZeeƒ ¡ 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   sž   z!t  t|ƒ¡�}| ¡  ¡  ¡ }W d   ƒ n1 sw   Y  W n# tyD   tt|ƒƒ�}| ¡ }W d   ƒ n1 s=w   Y  Y nw dd„ |D ƒ| _d S )Nc                 S   s   h | ]}|  ¡ ’qS r   )Ústrip)r.   Úpr   r   r   Ú	<setcomp>´   s    z3CommonPasswordValidator.__init__.<locals>.<setcomp>)	ÚgzipÚopenrN   ÚreadÚdecodeÚ
splitlinesÚIOErrorÚ	readlinesÚ	passwords)r6   Úpassword_list_pathÚfÚcommon_passwords_linesr   r   r   r7   ¬   s   ÿ€
ÿ€ÿz CommonPasswordValidator.__init__Nc                 C   s&   |  ¡  ¡ | jv rttdƒdd�‚d S )NzThis password is too common.Úpassword_too_common©r:   )rQ   r_   ri   r   rU   r=   r   r   r   r   ¶   s   þÿz CommonPasswordValidator.validatec                 C   rZ   )Nz0Your password can't be a commonly used password.r[   r>   r   r   r   r*   ½   r\   z%CommonPasswordValidator.get_help_textr   )r?   r@   rA   rB   r   Ú__file__ÚresolveÚparentÚDEFAULT_PASSWORD_LIST_PATHr7   r   r*   r   r   r   r   r^       s    	

r^   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.Úpassword_entirely_numericrn   )Úisdigitr   rU   r=   r   r   r   r   Å   s   þÿz!NumericPasswordValidator.validatec                 C   rZ   )Nz(Your password can't be entirely numeric.r[   r>   r   r   r   r*   Ì   r\   z&NumericPasswordValidator.get_help_textr   )r?   r@   rA   rB   r   r*   r   r   r   r   rs   Á   s    
rs   )NNr   )%Ú	functoolsrb   rO   Údifflibr   Úpathlibr   Údjango.confr   Údjango.core.exceptionsr   r   r   Údjango.utils.functionalr   Údjango.utils.htmlr	   r
   Údjango.utils.module_loadingr   Údjango.utils.translationr   rU   r   Ú	lru_cacher   r   r%   r&   r,   r2   rN   Ú"password_validators_help_text_htmlr3   rC   r^   rs   r   r   r   r   Ú<module>   s.    







*!