U
    >iy                     @   s   d dl mZ d dlmZmZmZ d dl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 G d	d
 d
Zdd Zedd Zeeeef  dddZdS )    )contextmanager)AnyDictOptional)SimpleLazyObjectempty)app_settings)get_pending_stage)ImmediateHttpResponse)Client)
sessionkitc                   @   s4   e Zd Zdd Zedd Zdd Zedd Zd	S )
AuthenticationStatusc                 C   s
   || _ d S N)request)selfr    r   E/tmp/pip-unpacked-wheel-upujnpc2/allauth/headless/internal/authkit.py__init__   s    zAuthenticationStatus.__init__c                 C   s
   | j jjS r   )r   useris_authenticatedr   r   r   r   r      s    z%AuthenticationStatus.is_authenticatedc                 C   s
   t | jS r   )r	   r   r   r   r   r   r	      s    z&AuthenticationStatus.get_pending_stagec                 C   s(   t js
dS ddlm} t|j| jS )NFr   )flows)allauth_settingsZSOCIALACCOUNT_ENABLEDZallauth.socialaccount.internalr   boolZsignupZget_pending_signupr   )r   r   r   r   r   has_pending_signup   s    z'AuthenticationStatus.has_pending_signupN)__name__
__module____qualname__r   propertyr   r	   r   r   r   r   r   r      s   
r   c                 C   s6   dD ]}t | |rt| | qt| jtr2t| j_d S )N)Z_cached_userZ_acached_user)hasattrdelattr
isinstancer   r   r   Z_wrapped)r   attrr   r   r   purge_request_user_cache"   s
    
r#   c              	   c   s   ddl m} | j}| j}ztt	 | _t
|  tj}|| }|rl||}|s^t|| dd|| _t
|  | j| jj_| jjjj d V  W 5 | jjr| j s| j  || _|| _d| jd< X d S )Nr   )UnauthorizedResponseFZCSRF_COOKIE_NEEDS_UPDATEi  )status)Zallauth.headless.base.responser$   r   sessionmodifiedZis_emptysaveZMETAr   Znew_sessionr#   r   TOKEN_STRATEGYZget_session_tokenZlookup_sessionr
   allauthheadless	_pre_userpk)r   r$   Zold_userZold_sessionstrategyZsession_tokenr&   r   r   r   authentication_context*   s,    




r/   )returnc                 C   sR   | j jjtjkrdS | jjs dS | j jj}|jrB|j| jjkrBdS t	j
}|| S )z?
    Determines if a new access token needs to be exposed.
    N)r*   r+   clientr   ZAPPr   r   r,   r-   r   r)   Zcreate_access_token_payload)r   Zpre_userr.   r   r   r   expose_access_tokenI   s    
r2   N)
contextlibr   typingr   r   r   Zdjango.utils.functionalr   r   r*   r   r   Z!allauth.account.internal.stagekitr	   Zallauth.core.exceptionsr
   Zallauth.headlessZallauth.headless.constantsr   Zallauth.headless.internalr   r   r#   r/   strr2   r   r   r   r   <module>   s   
