
    )Jf4                         d dl Z d dl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 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mZmZmZ ddlm Z   G d de!          Z"ddZdS )    N)ImproperlyConfiguredMultipleObjectsReturnedValidationError)Qreverse)gettext_lazy)context   )get_adapter)EmailVerificationMethod)EmailAddress)
user_email
user_fielduser_username)deserialize_instanceimport_attributeserialize_instancevalid_email_or_none   )app_settingsc                       e Zd Zd ed          iZddZd Z	 	 	 ddZd ZddZ	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd ZddZddZd Zd Zd ZdS )DefaultSocialAccountAdapteremail_takenzvAn account already exists with this email address. Please sign in to that account first, then connect your %s account.Nc                 (    t           j        | _        d S N)r
   request)selfr   s     `/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/allauth/socialaccount/adapter.py__init__z$DefaultSocialAccountAdapter.__init__%   s         c                     dS )a	  
        Invoked just after a user successfully authenticates via a
        social provider, but before the login is actually processed
        (and before the pre_social_login signal is emitted).

        You can use this hook to intervene, e.g. abort the login by
        raising an ImmediateHttpResponse

        Why both an adapter hook and the signal? Intervening in
        e.g. the flow from within a signal handler is bad -- multiple
        handlers may be active and are executed in undetermined order.
        N r   r   sociallogins      r   pre_social_loginz,DefaultSocialAccountAdapter.pre_social_login*   s	     	r!   c                     t          | d          r5t          j        d           |                     ||j        |||           dS dS )a  
        Invoked when there is an error in the authentication cycle. In this
        case, pre_social_login will not be reached.

        You can use this hook to intervene, e.g. redirect to an
        educational flow by raising an ImmediateHttpResponse.
        authentication_errorzSadapter.authentication_error() is deprecated, use adapter.on_authentication_error())error	exceptionextra_contextN)hasattrwarningswarnr(   id)r   r   providerr)   r*   r+   s         r   on_authentication_errorz3DefaultSocialAccountAdapter.on_authentication_error9   sr     4/00 	Me   %%#+ &     	 	r!   c                 D    t                                          |          S )z3
        Instantiates a new User instance.
        )get_account_adapternew_userr$   s      r   r4   z$DefaultSocialAccountAdapter.new_userU   s     #$$--g666r!   c                     |j         }|                                 |r$t                                          |||           n"t                                          ||           |                    |           |S )zy
        Saves a newly signed up social login. In case of auto-signup,
        the signup form is not available.
        )userset_unusable_passwordr3   	save_userpopulate_usernamesave)r   r   r%   formus        r   r8   z%DefaultSocialAccountAdapter.save_user[   s}    
 	!!! 	@!!++GQ====!!33GQ???!!!r!   c                    |                     d          }|                     d          }|                     d          }|                     d          }|                     d          }|j        }	t          |	|pd           t          |	t	          |          pd           |pd                    d          }
t          |	d|p|
d                    t          |	d|p|
d	                    |	S )
a  
        Hook that can be used to further populate the user instance.

        For convenience, we populate several common fields.

        Note that the user instance being populated represents a
        suggested User instance that represents the social user that is
        in the process of being logged in.

        The User instance need not be completely valid and conflict
        free. For example, verifying whether or not the username
        already exists, is not a responsibility.
        username
first_name	last_nameemailname  r   r   )getr6   r   r   r   	partitionr   )r   r   r%   datar>   r?   r@   rA   rB   r6   
name_partss              r   populate_userz)DefaultSocialAccountAdapter.populate_useri   s     88J''XXl++
HH[))	!!xxdHN+++4,U339r:::jb++C00
4z'BZ]CCC4i&@:a=AAAr!   c                 $    t          d          }|S )zp
        Returns the default URL to redirect to after successfully
        connecting a social account.
        socialaccount_connectionsr   )r   r   socialaccounturls       r   get_connect_redirect_urlz4DefaultSocialAccountAdapter.get_connect_redirect_url   s    
 122
r!   c                 z   t          |          dk    r|j                                        st          t	          d                    t
          j        t          j        k    rVt          j
                            |j        d                                          s t          t	          d                    dS dS dS )zg
        Validate whether or not the socialaccount account can be
        safely disconnected.
        r   z$Your account has no password set up.T)r6   verifiedz+Your account has no verified email address.N)lenr6   has_usable_passwordr   _r   EMAIL_VERIFICATIONr   	MANDATORYr   objectsfilterexists)r   accountaccountss      r   validate_disconnectz/DefaultSocialAccountAdapter.validate_disconnect   s    
 x==A<3355 Q%a(N&O&OPPP.2I2SSS#+22  3  &(( *GHH   
 TS r!   c                     t           j        }|S r   )r   AUTO_SIGNUP)r   r   r%   auto_signups       r   is_auto_signup_allowedz2DefaultSocialAccountAdapter.is_auto_signup_allowed   s    ".r!   c                 F    t          |                              |          S )z
        Checks whether or not the site is open for signups.

        Next to simply returning True/False you can also intervene the
        regular flow by raising an ImmediateHttpResponse
        )r3   is_open_for_signupr$   s      r   ra   z.DefaultSocialAccountAdapter.is_open_for_signup   s      #7++>>wGGGr!   c                     |j         }t          |          pdt          |          pdt          |d          pdt          |d          pdd}|S )NrC   r?   r@   )rA   r>   r?   r@   )r6   r   r   r   )r   r%   r6   initials       r   get_signup_form_initial_dataz8DefaultSocialAccountAdapter.get_signup_form_initial_data   sa    %%+%d++1r$T<88>B#D+66<"	
 
 r!   c                 "    t          ||          S r   )r   )r   modelrG   s      r   r   z0DefaultSocialAccountAdapter.deserialize_instance   s    #E4000r!   c                      t          |          S r   )r   )r   instances     r   r   z.DefaultSocialAccountAdapter.serialize_instance   s    !(+++r!   c                 z   ddl m} g }|                                }|                     |          }i }|D ]0}|                    |j        g                               |           1|D ]Q}|                    |j        g           }	|	s|j	        r'd g}	|	D ]$} |||          }
|                    |
           %R|S )Nr   registry)r   app)
allauth.socialaccount.providersrk   get_class_list	list_apps
setdefaultr0   appendrE   r/   	uses_apps)r   r   rk   retprovider_classesappsapps_maprl   provider_classprovider_appsr0   s              r   list_providersz*DefaultSocialAccountAdapter.list_providers   s    <<<<<<#2244~~g&& 	> 	>Cb1188====. 	% 	%N$LL):B??M  '!+ !%$ % %)>'sCCC

8$$$$% 
r!   c                 Z   ddl m} |                    |          }||j        rY|                     ||          }|s|                    |j                  }|st          d|j                    |||          S |r|j        rJ  ||d          S t          d|j                   )z_Looks up a `provider`, supporting subproviders by looking up by
        `provider_id`.
        r   rj   Nr0   zunknown provider: )rl   )rm   rk   	get_classrr   get_appr0   r   )r   r   r0   rk   rw   rl   s         r   get_providerz(DefaultSocialAccountAdapter.get_provider   s     	=<<<<<!++H55!^%=!,,w,::C! B "*!3!3CL!A!A! P*+N+N+NOOO!>'s3333 	L%////!>'t4444&'JCL'J'JKKKr!   c           	         ddl m} i }|r|j                            |          }n|j                                        }|r4|                    t          |          t          |          z            }|r|                    |          }|D ]2}|                    |j        g           }|	                    |           3t          j                                        D ]\  }	}
|
                    d          }||
                    d          }|4|g}|                    |	g           }|D ]} ||	          }d	D ]}||v rt          ||||                    d
|v r$t          j        d           |d
         |j        d
<   |r|j        |k    rd|r|j        |k    r|j        |k    r}|	                    |           g }|                                D ]}|                    |           |S )zSocialApp's can be setup in the database, or, via
        `settings.SOCIALACCOUNT_PROVIDERS`.  This methods returns a uniform list
        of all known apps matching the specified criteria, and blends both
        (db/settings) sources of data.
        r   	SocialAppr{   )provider_id)	client_idAPPSNAPP)rB   r   r   secretkeysettingscertificate_keyz3'certificate_key' should be moved into app.settings)allauth.socialaccount.modelsr   rV   on_siteallrW   r   rp   r0   rq   r   	PROVIDERSitemsrE   setattrr-   r.   r   r   r   valuesextend)r   r   r0   r   r   provider_to_appsdb_appsrl   ru   ppcfgapp_configs
app_configconfigfieldrx   s                   r   ro   z%DefaultSocialAccountAdapter.list_apps   sc    	;:::::   	.'//88GG'++--G 	UnnQ%9%9%9A(<S<S<S%STTG 	:nnyn99G 	 	C#..s|R@@DKK $-3355  	!  	!GAt((6**K"!XXe__
%)l#..q"55D% ! !i+++ 	; 	;E UF5M:::$..M"WXXX6<=N6OCL!23 )!;!;83300C    /!4 -4466 	' 	'MKK&&&&r!   c                     ddl m} |                     |||          }t          |          dk    rt          t          |          dk    r|                                |d         S )Nr   r   )r0   r   r   )r   r   ro   rQ   r   DoesNotExist)r   r   r0   r   r   ru   s         r   r}   z#DefaultSocialAccountAdapter.get_app(  sj    ::::::~~gI~NNt99q==))YY!^^((***Awr!   c                     dd l }|                                }t          j        |j        t
          j                  |_        |S )Nr   )timeout)requestsSession	functoolspartialr   r   REQUESTS_TIMEOUT)r   r   sessions      r   get_requests_sessionz0DefaultSocialAccountAdapter.get_requests_session2  sF    ""$$#+O\%B
 
 
 r!   c                    d}|j         r|j         j                            d          }|*|                                }|                    dd          }t	          |t
                    rnbt	          |t                    r>|                    d          d                                         }d |D             }||v }nt          d          |S )	a  
        Returns ``True`` iff the given email encountered during a social
        login for the given provider is to be assumed verified.

        This can be configured with a ``"verified_email"`` key in the provider
        app settings, or a ``"VERIFIED_EMAIL"`` in the global provider settings
        (``SOCIALACCOUNT_PROVIDERS``).  Both can be set to ``False`` or
        ``True``, or, a list of domains to match email addresses against.
        Nverified_emailVERIFIED_EMAILF@r   c                 6    g | ]}|                                 S r#   )lower).0ds     r   
<listcomp>zADefaultSocialAccountAdapter.is_email_verified.<locals>.<listcomp>O  s     BBBa		BBBr!   z!verified_email wrongly configured)
rl   r   rE   get_settings
isinstanceboollistrF   r   r   )r   r0   rA   r   r   email_domainverified_domainss          r   is_email_verifiedz-DefaultSocialAccountAdapter.is_email_verified;  s     < 	I%\2667GHHN!,,..H%\\*:EBBNnd++ 	L-- 	L ??3//288::LBB>BBB)-==NN&'JKKKr!   c                     d}|j                                         }|j        r|j        j                            d          }|4t
          j        p'|                                                    dd          }|S )a,  
        Returns ``True`` iff  authentication by email is active for this login/email.

        This can be configured with a ``"email_authentication"`` key in the provider
        app settings, or a ``"VERIFIED_EMAIL"`` in the global provider settings
        (``SOCIALACCOUNT_PROVIDERS``).
        Nemail_authenticationEMAIL_AUTHENTICATIONF)rY   r~   rl   r   rE   r   r   r   )r   loginrA   rs   r0   s        r   can_authenticate_by_emailz5DefaultSocialAccountAdapter.can_authenticate_by_emailU  s|     =--//< 	D,'++,BCCC;3 x7L7L7N7N7R7R&8 8C 
r!   r   )NNN)NN)__name__
__module____qualname__rS   error_messagesr    r&   r1   r4   r8   rI   rN   r[   r_   ra   rd   r   r   ry   r~   ro   r}   r   r   r   r#   r!   r   r   r      s       qq 
 
N' ' ' '
  &    87 7 7     6    $  
H H H  1 1 1, , ,  (L L L,@ @ @ @D       4    r!   r   c                 F     t          t          j                  |           S r   )r   r   ADAPTER)r   s    r   r   r   h  s    1L011':::r!   r   )#r   r-   django.core.exceptionsr   r   r   django.db.modelsr   django.urlsr   django.utils.translationr	   rS   allauth.corer
   account.adapterr   r3   account.app_settingsr   account.modelsr   account.utilsr   r   r   utilsr   r   r   r   rC   r   objectr   r#   r!   r   <module>r      s                
             6 6 6 6 6 6             @ @ @ @ @ @ : : : : : : ) ) ) ) ) ) A A A A A A A A A A                 I I I I I& I I IX
; ; ; ; ; ;r!   