a
    ׉_.                     @   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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)Site)get_current_site)PermissionDenied)models)get_random_string)	force_str)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   sL   i }|rt |di }||_||}|sHt|}| j|j|d}|||< |S )N_socialapp_cache)Z	sites__idprovider)getattrr   getr   id)selfr   requestcacheappsite r"   T/var/www/html/Ranjet/env/lib/python3.9/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e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Key)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#   MetaI   s   r7   c                 C   s   | j S N)r.   r   r"   r"   r#   __str__M   s    zSocialApp.__str__)r%   r&   r'   r   objectsr   	CharFieldr5   r   registry
as_choicesr   r.   Z	client_idZsecretr4   ZManyToManyFieldr   ZsitesZcertificate_keyr7   r:   r"   r"   r"   r#   r(   %   s2   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   o   s   r7   c                 C   s
   t | dS )N)accountr   r9   r"   r"   r#   r   t   s    zSocialAccount.authenticatec                 C   s
   t | jS r8   )r
   userr9   r"   r"   r#   r:   w   s    zSocialAccount.__str__c                 C   s   |    S r8   )get_provider_accountget_profile_urlr9   r"   r"   r#   rH   z   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   
ForeignKeyallauthr   Z
USER_MODELCASCADErF   r<   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?   Q   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(   rM   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 u s|j |ksJ || _|| _|| _ || _i | _d S r8   )rE   rR   rF   email_addressesstate)r   rF   rE   rR   rW   r"   r"   r#   __init__   s    zSocialLogin.__init__c                 C   s   || _ | j|dd d S )NT)connect)rF   save)r   r   rF   r"   r"   r#   rZ      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>       z)SocialLogin.serialize.<locals>.<listcomp>)rE   rF   rX   rW   rR   )r   r_   rO   rE   rF   rX   rW   rR   )r   retr"   r^   r#   	serialize   s    zSocialLogin.serializec           
      C   s   t  j}|t|d }|t |d }d|v r>|t|d }nd }g }|d D ]}|t|}|| qN|  }	||	_||	_||	_	||	_
|d |	_|	S )NrE   rF   rR   rW   rX   )r   deserialize_instancer?   r   rQ   r   appendrR   rE   rF   rW   rX   )
clsdatard   rE   rF   rR   rW   r]   Zemail_addressrb   r"   r"   r#   deserialize   s"    

zSocialLogin.deserializeFc                 C   sj   | j r
J | 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_existingrF   r[   rE   r   STORE_TOKENSrR   Zapp_idr   rW   )r   r   rZ   rF   r"   r"   r#   r[      s    



zSocialLogin.savec                 C   s   | j jdu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
J ztjj| jj| jjd}| jj|_|| _| jj| _|	  t
jr| jr| jjjr| jjrhJ zNtjj| j| jjd}| jj|_| jjr| jj|_| jj|_|	  || _W n& tjy   || j_| j	  Y n0 W n tjy   Y n0 dS )z2
        Lookup existing account, if any.
        rC   )rE   r    N)ri   r?   r;   r   rE   r   rA   rP   rF   r[   r   rj   rR   r    rk   rQ   rT   rU   ZDoesNotExist)r   atr"   r"   r#   lookup  s4    







zSocialLogin.lookupc                 C   s   | j d}|S )Nnext)rX   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   rX   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   rX   verifierr"   r"   r#   stash_state4  s    
zSocialLogin.stash_statec                 C   s$   d|j vrt |j d\}}|S Nrv   rw   r   poprx   r"   r"   r#   unstash_state;  s    
zSocialLogin.unstash_statec                 C   s2   d|j vrt |j d\}}||kr.t |S r{   r|   )rf   r   ry   rX   Z	verifier2r"   r"   r#   verify_and_unstash_stateB  s    
z$SocialLogin.verify_and_unstash_state)F)r%   r&   r'   __doc__rY   rZ   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.modelsr   Zdjango.contrib.sites.shortcutsr   Zdjango.core.exceptionsr   Z	django.dbr   Zdjango.utils.cryptor	   Zdjango.utils.encodingr
   Zdjango.utils.translationr   r5   Zallauth.app_settingsrL   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*   ,6