U
    Z!cS/                     @   s  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Zd d	lmZ d d
lmZmZ d dlmZ ddlmZ ddlmZmZ ddlmZ ddlm Z  G dd de	j!Z"G dd de	j#Z$G dd de	j#Z%G dd de	j#Z&G dd de'Z(dS )    )absolute_importauthenticate)get_current_site)PermissionDenied)models)get_random_string)gettext_lazyN)EmailAddress)get_next_redirect_urlsetup_user_email)get_user_model   )get_request_param   )app_settings	providers)get_adapter)	JSONFieldc                   @   s   e Zd ZdddZdS )SocialAppManagerNc                 C   sb   i }|rt |di }||_||}|s^tjjrJt|}| j|j|d}n| j|d}|||< |S )N_socialapp_cache)Z	sites__idprovider)r   )getattrr   getallauthr   SITES_ENABLEDr   id)selfr   requestcacheappsite r"   `/var/www/html/crackeverytest/cet_env/lib/python3.8/site-packages/allauth/socialaccount/models.pyget_current   s    
zSocialAppManager.get_current)N)__name__
__module____qualname__r$   r"   r"   r"   r#   r      s   r   c                   @   s   e Zd Ze Zejeddej	
 dZejedddZejedded	d
ZejedddeddZejedddeddZejjrejdddZdZG dd dZdd ZdS )	SocialAppr      verbose_name
max_lengthchoicesname(   r+   r,   z	client id   zApp ID, or consumer key)r+   r,   	help_textz
secret keyTz-API secret, client secret, or consumer secret)r+   r,   blankr2   keyZKeyz
sites.Site)r3   Nc                   @   s   e Zd ZedZedZdS )zSocialApp.Metazsocial applicationzsocial applicationsN)r%   r&   r'   _r+   verbose_name_pluralr"   r"   r"   r#   MetaK   s   r7   c                 C   s   | j S N)r.   r   r"   r"   r#   __str__O   s    zSocialApp.__str__)r%   r&   r'   r   objectsr   	CharFieldr5   r   registry
as_choicesr   r.   Z	client_idZsecretr4   r   r   r   ZManyToManyFieldZsitesZcertificate_keyr7   r:   r"   r"   r"   r#   r(   &   s:      r(   c                   @   s   e Zd ZejejjejdZ	ej
eddej dZej
edejdZejeddd	Zejed
ddZeededZG dd dZdd Zdd Zdd Zdd Zdd Zdd ZdS )SocialAccountZ	on_deleter   r)   r*   uidr0   z
last loginT)r+   Zauto_nowzdate joined)r+   Zauto_now_addz
extra data)r+   defaultc                   @   s    e Zd ZdZedZedZdS )zSocialAccount.Metar   rA   zsocial accountzsocial accountsNr%   r&   r'   Zunique_togetherr5   r+   r6   r"   r"   r"   r#   r7   q   s   r7   c                 C   s
   t | dS )N)accountr   r9   r"   r"   r#   r   v   s    zSocialAccount.authenticatec                 C   s   ddl m} || S )Nr   )socialaccount_user_display)ZhelpersrF   )r   rF   r"   r"   r#   r:   y   s    zSocialAccount.__str__c                 C   s   |    S r8   )get_provider_accountget_profile_urlr9   r"   r"   r#   rH   ~   s    zSocialAccount.get_profile_urlc                 C   s   |    S r8   )rG   get_avatar_urlr9   r"   r"   r#   rI      s    zSocialAccount.get_avatar_urlc                 C   s   t j| jS r8   )r   r=   Zby_idr   r9   r"   r"   r#   get_provider   s    zSocialAccount.get_providerc                 C   s   |   | S r8   )rJ   Zwrap_accountr9   r"   r"   r#   rG      s    z"SocialAccount.get_provider_accountN)r%   r&   r'   r   
ForeignKeyr   r   Z
USER_MODELCASCADEuserr<   r5   r   r=   r>   r   ZUID_MAX_LENGTHrA   DateTimeFieldZ
last_loginZdate_joinedr   dict
extra_datar7   r   r:   rH   rI   rJ   rG   r"   r"   r"   r#   r?   S   s(    r?   c                   @   s   e Zd ZejeejdZejeejdZ	ej
ededdZej
dededdZejdded	d
ZG dd dZdd ZdS )SocialTokenr@   tokenz/"oauth_token" (OAuth1) or access token (OAuth2))r+   r2   Tztoken secretz7"oauth_token_secret" (OAuth1) or refresh token (OAuth2))r3   r+   r2   z
expires at)r3   nullr+   c                   @   s    e Zd ZdZedZedZdS )zSocialToken.Meta)r    rE   zsocial application tokenzsocial application tokensNrD   r"   r"   r"   r#   r7      s   r7   c                 C   s   | j S r8   )rR   r9   r"   r"   r#   r:      s    zSocialToken.__str__N)r%   r&   r'   r   rK   r(   rL   r    r?   rE   Z	TextFieldr5   rR   token_secretrN   
expires_atr7   r:   r"   r"   r"   r#   rQ      s$     rQ   c                   @   s   e Zd ZdZdddg fddZdd Zdd Zed	d
 ZdddZ	e
dd Zdd Zdd Zedd Zedd Zedd Zedd ZdS )SocialLogina'  
    Represents a social user that is in the process of being logged
    in. This consists of the following information:

    `account` (`SocialAccount` instance): The social account being
    logged in. Providers are not responsible for checking whether or
    not an account already exists or not. Therefore, a provider
    typically creates a new (unsaved) `SocialAccount` instance. The
    `User` instance pointed to by the account (`account.user`) may be
    prefilled by the provider for use as a starting point later on
    during the signup process.

    `token` (`SocialToken` instance): An optional access token token
    that results from performing a successful authentication
    handshake.

    `state` (`dict`): The state to be preserved during the
    authentication handshake. Note that this state may end up in the
    url -- do not put any secrets in here. It currently only contains
    the url to redirect to after login.

    `email_addresses` (list of `EmailAddress`): Optional list of
    e-mail addresses retrieved from the provider.
    Nc                 C   s>   |r|j d ks|j |kst|| _|| _|| _ || _i | _d S r8   )rE   AssertionErrorrR   rM   email_addressesstate)r   rM   rE   rR   rX   r"   r"   r#   __init__   s    zSocialLogin.__init__c                 C   s   || _ | j|dd d S )NT)connect)rM   save)r   r   rM   r"   r"   r#   r[      s    zSocialLogin.connectc                    sN   t  j t | j | j| j fdd| jD d}| jrJ | j|d< |S )Nc                    s   g | ]} |qS r"   r"   ).0easerialize_instancer"   r#   
<listcomp>   s     z)SocialLogin.serialize.<locals>.<listcomp>)rE   rM   rY   rX   rR   )r   r`   rO   rE   rM   rY   rX   rR   )r   retr"   r_   r#   	serialize   s    zSocialLogin.serializec           
      C   s   t  j}|t|d }|t |d }d|kr>|t|d }nd }g }|d D ]}|t|}|| qN|  }	||	_||	_||	_	||	_
|d |	_|	S )NrE   rM   rR   rX   rY   )r   deserialize_instancer?   r   rQ   r
   appendrR   rE   rM   rX   rY   )
clsdatard   rE   rM   rR   rX   r^   Zemail_addressrb   r"   r"   r#   deserialize   s"    

zSocialLogin.deserializeFc                 C   sj   | j r
t| j}|  || j_| j  tjrR| jrR| jjrR| j| j_| j  |rXnt	||| j
 dS )z
        Saves a new account. Note that while the account is new,
        the user may be an existing one (when connecting accounts)
        N)is_existingrW   rM   r\   rE   r   STORE_TOKENSrR   Zapp_idr   rX   )r   r   r[   rM   r"   r"   r#   r\      s    



zSocialLogin.savec                 C   s   | j jdk	S )zL
        Account is temporary, not yet backed by a database record.
        N)rE   pkr9   r"   r"   r#   ri      s    zSocialLogin.is_existingc                 C   s   | j r
tztjj| jj| jjd}| jj|_|| _| jj	| _	|
  tjr| jr| jjjr| jjrhtzNtjj| j| jjd}| jj|_| jjr| jj|_| jj|_|
  || _W n( tjk
r   || j_| j
  Y nX W n tjk
r   Y nX dS )z2
        Lookup existing account, if any.
        rC   )rE   r    N)ri   rW   r?   r;   r   rE   r   rA   rP   rM   r\   r   rj   rR   r    rk   rQ   rT   rU   ZDoesNotExist)r   atr"   r"   r#   lookup  s8    
 

 



zSocialLogin.lookupc                 C   s   | j d}|S )Nnext)rY   r   )r   r   urlr"   r"   r#   get_redirect_url)  s    zSocialLogin.get_redirect_urlc                 C   sL   i }t |}|r||d< t|dd|d< t|dd|d< t|dd|d< |S )Nro   processZloginZscope Zauth_params)r   r   )rf   r   rY   Znext_urlr"   r"   r#   state_from_request-  s    zSocialLogin.state_from_requestc                 C   s$   |  |}td}||f|jd< |S )N   socialaccount_state)rt   r   sessionrf   r   rY   verifierr"   r"   r#   stash_state8  s    
zSocialLogin.stash_statec                 C   s$   d|j krt |j d\}}|S Nrv   rw   r   poprx   r"   r"   r#   unstash_state?  s    
zSocialLogin.unstash_statec                 C   s2   d|j krt |j d\}}||kr.t |S r{   r|   )rf   r   ry   rY   Z	verifier2r"   r"   r#   verify_and_unstash_stateF  s    
z$SocialLogin.verify_and_unstash_state)F)r%   r&   r'   __doc__rZ   r[   rc   classmethodrh   r\   propertyri   rn   rq   rt   rz   r~   r   r"   r"   r"   r#   rV      s&   	


#



rV   ))
__future__r   Zdjango.contrib.authr   Zdjango.contrib.sites.shortcutsr   Zdjango.core.exceptionsr   Z	django.dbr   Zdjango.utils.cryptor   Zdjango.utils.translationr	   r5   Zallauth.app_settingsr   Zallauth.account.modelsr
   Zallauth.account.utilsr   r   Zallauth.utilsr   utilsr   rs   r   r   Zadapterr   fieldsr   Managerr   ZModelr(   r?   rQ   objectrV   r"   r"   r"   r#   <module>   s&   -8