o
    "4g?#                     @   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mZm	Z	 d dl
mZ d dlmZ d dlmZ d dl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 Zejdd Zejdd Z ejdd Z!ejdd Z"ejdd Z#ejdd Z$ejdd Z%ejdd Z&ejdd Z'ejd d! Z(ejd"d#d$d% Z)ejd&d' Z*ejd(d) Z+ejd*d+ Z,ejd,d- Z-ejd.d/ Z.ejd0d1 Z/ejd2d3 Z0ejd4d5 Z1ejd6d7 Z2ejd8d9 Z3ejd:d; Z4dS )<    Ncontextmanager)MockPropertyMockpatch)get_user_model)MessageMiddleware)SessionMiddleware)EmailAddress)
user_emailuser_pk_to_url_struser_username)context)statekit)AuthProcessc                 C   sR   |  ddkr%g }|D ]}|jd ds|| q|D ]	}|| qd S d S )Nz--dsztests.headless_only.settingsr   zallauth/headless)	getoptionlocation
startswithappendremove)configitemsremoved_itemsitem r   Y/var/www/html/authentication-server/venv/lib/python3.10/site-packages/allauth/conftest.pypytest_collection_modifyitems   s   
r   c                 C      |  S Nr   )user_factoryr   r   r   user      r    c                 C   s   |  | | S r   )force_login)clientr    r   r   r   auth_client$   s   
r$   c                  C      dd } | S )Nc                   S   s   t t S r   )struuiduuid4r   r   r   r   f,   s   zpassword_factory.<locals>.fr   r)   r   r   r   password_factory*   s   r+   c                 C   r   r   r   )r+   r   r   r   user_password2   r!   r,   c                   C      dS )NTr   r   r   r   r   email_verified7      r.   c                    s$   d d dd|d ddf fdd	}|S )NTFc                    s   |st  j}| s|r |d} t }| }	|dkr||	_n|	|d u r&n| t|	| t|	| p3d |rK|	  | rK|rKt	j
j|	|  |dd |r\ddlm}
 |
j|	|
  |	S )N)username! T)r    emailverifiedprimaryr   auth)r'   r(   hexr   passwordset_passwordr   r   saver
   objectscreatelowerallauth.mfa.totp.internalr7   TOTPactivategenerate_totp_secret)r3   r0   commit
with_emailr.   r9   with_emailaddress	with_totpUserr    r7   email_factoryr,   r   r   factory>   s0   



zuser_factory.<locals>.factoryr   )rI   dbr,   r.   rJ   r   rH   r   r   <   s   'r   c                  C   s   ddd} | S )NFc                 S   sT   |d u r| st  j} |  dt  j d}|r$ddd |D }|S | }|S )N@z.orgr2   c                 S   s"   g | ]}t | | gqS r   )randomchoiceupperr>   ).0cr   r   r   
<listcomp>q   s   " z2email_factory.<locals>.factory.<locals>.<listcomp>)r'   r(   r8   joinr>   )r0   r3   
mixed_caser   r   r   rJ   j   s   
zemail_factory.<locals>.factory)NNFr   )rJ   r   r   r   rI   h   s   
rI   c                  C      t dd } | S )Nc                  s   <    t d} d| _d V  W d    d S 1 sw   Y  d S )NzIallauth.account.internal.flows.reauthentication.did_recently_authenticateTr   return_valuemr   r   r   r)   |   s   "z"reauthentication_bypass.<locals>.fr   r*   r   r   r   reauthentication_bypassz   s   
r[   c                  C   rU   )Nc           
      s   s:   ddl m} ddlm} tdtd}tdc}i ddif|_td	H}td
4}| | jd }t	 }d|j
_||_t	 |_t	 }	d|	_|	|_tdd||iiV  W d    n1 s`w   Y  W d    n1 sow   Y  W d    n1 s~w   Y  W d    d S W d    d S 1 sw   Y  d S )Nr   )websafe_encode)get_adapterz>allauth.mfa.webauthn.internal.auth.WebAuthn.authenticator_data)new_callablez+fido2.server.Fido2Server.authenticate_beginstatedummyz.fido2.server.Fido2Server.authenticate_completez@allauth.mfa.webauthn.internal.auth.parse_authentication_responseidcredential_idresponse
userHandle)fido2.utilsr\   allauth.mfa.adapterr]   r   r   rX   %get_public_key_credential_user_entityr    r   credential_datarb   jsondumps)
authenticatorr\   r]   ad_mab_mac_mrZ   user_handleauthenticator_databindingr   r   r   r)      sP   

"z)webauthn_authentication_bypass.<locals>.fr   r*   r   r   r   webauthn_authentication_bypass   s   
!rr   c                  C   rU   )Nc                 3   s    t dP}t d3}t |_G  fdddt}|d}||_tddd iid	d
dddgdddV  W d    n1 s@w   Y  W d    d S W d    d S 1 sXw   Y  d S )Nz*fido2.server.Fido2Server.register_completez>allauth.mfa.webauthn.internal.auth.parse_registration_responsec                          e Zd Z fddZdS )zFwebauthn_registration_bypass.<locals>.f.<locals>.FakeAuthenticatorDatac                    s    S r   r   )selfpasswordlessr   r   is_user_verified   s   zWwebauthn_registration_bypass.<locals>.f.<locals>.FakeAuthenticatorData.is_user_verifiedN)__name__
__module____qualname__rw   r   ru   r   r   FakeAuthenticatorData       r{   s   bindingzcross-platform	credPropsrk123456aocdjusb)attestationObjectclientDataJSON
transportsz
public-key)authenticatorAttachmentclientExtensionResultsra   rawIdrc   type)r   r   rX   bytesri   rj   )r    rv   rc_mrZ   r{   rq   r   ru   r   r)      s4   

"z'webauthn_registration_bypass.<locals>.fr   r*   r   r   r   webauthn_registration_bypass   s   
r   T)autousec                   C   s   t jd  d S r   )r   _request_varsetr   r   r   r   clear_context_request   s   r   c                 c   s.    ddl m} dddii| _|  d V  d S )Nr   )cachedefaultBACKENDz-django.core.cache.backends.locmem.LocMemCache)django.core.cacher   CACHESclear)settingsr   r   r   r   enable_cache   s   
r   c                  C   rU   )Nc                  s   rV   )Nz1allauth.mfa.totp.internal.auth.validate_totp_codeTrW   rY   r   r   r   r)      s
   
"z!totp_validation_bypass.<locals>.fr   r*   r   r   r   totp_validation_bypass   s   
r   c                   C   r-   )Nzunittest-serverr   r   r   r   r   provider_id   r/   r   c                  C   r%   )Nc                 S   s8   ddl m} | }t| }|| }| d| }|S )Nr   )app_settings-)allauth.accountr   PASSWORD_RESET_TOKEN_GENERATORr   
make_token)r    r   token_generatoruidtemp_keykeyr   r   r   r)      s   
z'password_reset_key_generator.<locals>.fr   r*   r   r   r   password_reset_key_generator   s   	r   c                 C   s   ddddgi}d|i| _ |S )NAPPS	client_idsecret)r   r   google)SOCIALACCOUNT_PROVIDERS)r   	gsettingsr   r   r   google_provider_settings   s   
r   c                 C   s"   ddl m} |j| |  | S Nr   r6   )r?   r7   r@   rA   rB   )r    r7   r   r   r   user_with_totp  s   r   c                 C   s   ddl m} |j|  | S r   )#allauth.mfa.recovery_codes.internalr7   RecoveryCodesrA   )r   r7   r   r   r   user_with_recovery_codes  s   r   c                 C   s.   ddl m} |jj| |jjddi dd}|S )Nr   )AuthenticatorzTest passkeyT)namerv   
credential)r    r   data)allauth.mfa.modelsr   r<   r=   TypeWEBAUTHN)r    r   rk   r   r   r   passkey  s   	r   c                 C   s   | S r   r   )r    r   r   r   r   user_with_passkey'  r/   r   c                  C   s   ddd} | S )Nc                 [   sR   d}| j }d|p
tji|}|r||d< i }|t g||< ||tj< |  |S )Nr   processnext)sessionr   LOGINtimer   STATES_SESSION_KEYr;   )r#   r   next_urlkwargsstate_idr   r_   statesr   r   r   setup.  s   
z&sociallogin_setup_state.<locals>.setup)NNr   )r   r   r   r   sociallogin_setup_state,  s   
r   c                    s   G  fddd}| S )Nc                       rs   )z'request_factory.<locals>.RequestFactoryc                    s2     |}tdd | tdd | |S )Nc                 S      d S r   r   requestr   r   r   <lambda>B      z=request_factory.<locals>.RequestFactory.get.<locals>.<lambda>c                 S   r   r   r   r   r   r   r   r   C  r   )getr	   process_requestr   )rt   pathr   rfr   r   r   @  s   
z+request_factory.<locals>.RequestFactory.getN)rx   ry   rz   r   r   r   r   r   RequestFactory?  r|   r   r   )r   r   r   r   r   request_factory=  s   r   )5ri   rM   r   r'   
contextlibr   unittest.mockr   r   r   django.contrib.authr   "django.contrib.messages.middlewarer   "django.contrib.sessions.middlewarer	   pytestallauth.account.modelsr
   allauth.account.utilsr   r   r   allauth.corer   allauth.socialaccount.internalr   .allauth.socialaccount.providers.base.constantsr   r   fixturer    r$   r+   r,   r.   r   rI   r[   rr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sx    






+


&

!











