o
    |fn$                     @   st  d dl Z d dlZd dlZ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Zd dlmZ d dlmZ d dlZddlmZmZmZ dZd	Zed
ZG dd deZdd Zdd Zdd Zdd Zdd Z dd Z!dd Z"dd Z#dd Z$dd  Z%d!d" Z&d#d$ Zd%d& Z'd9d'd(Z(d:d)d*Z)d+d, Z*d-d. Z+d/d0 Z,d1d2 Z-d3d4 Z.d5d6 Z/G d7d8 d8Z0dS );    N)parse_qs)	urlencodeurlparse
urlunparse)HTTPAdapter)PoolManager   )AuthCanceledAuthForbiddenAuthUnreachableProviderSOCIAL_AUTHpartial_pipeline_tokensocialc                       s6   e Zd ZdZ fddZd	ddZedd Z  ZS )
SSLHttpAdapterz "
    Transport adapter that allows to use any SSL protocol. Based on:
    http://requests.rtfd.org/latest/user/advanced/#example-specific-ssl-version
    c                    s   || _ t   d S N)ssl_protocolsuper__init__)selfr   	__class__ R/var/www/html/humari/django-venv/lib/python3.10/site-packages/social_core/utils.pyr   !   s   zSSLHttpAdapter.__init__Fc                 C   s   t |||| jd| _d S )N)	num_poolsmaxsizeblockssl_version)r   r   poolmanager)r   connectionsr   r   r   r   r   init_poolmanager%   s   zSSLHttpAdapter.init_poolmanagerc                 C   s   t  }|dt| |S )Nhttps://)requestsSessionmountr   )clsr   sessionr   r   r   ssl_adapter_session-   s   z"SSLHttpAdapter.ssl_adapter_session)F)	__name__
__module____qualname____doc__r   r   classmethodr&   __classcell__r   r   r   r   r      s    
r   c                 C   s   t |  tj|  S r   )
__import__sysmodules)namer   r   r   import_module4   s   
r1   c                 C   s"   |  dd\}}t|}t||S )N.r   )rsplitr1   getattr)r0   modmembermoduler   r   r   module_member9   s   
r8   c                   C   s
   dt j S )z5Builds a simple User-Agent string to send in requestszsocial-auth-)social_core__version__r   r   r   r   
user_agent?   s   
r;   c                 C   s>   |rt t| }t|d }|| t||d< t|} | S )zEAdds parameters to URL, parameter will be repeated if already present   )listr   r   updater   r   )urlparams	fragmentsvaluer   r   r   url_add_parametersD   s   
rC   c                  G   s   d dd | D S )N_c                 S   s    g | ]}|r|  d dqS )-rD   )upperreplace).0r0   r   r   r   
<listcomp>P   s     z#to_setting_name.<locals>.<listcomp>)joinnamesr   r   r   to_setting_nameO   s   rM   c                  G   s   t tf|   S r   )rM   SETTING_PREFIXrK   r   r   r   setting_nameS      rO   c              	   C   s^   |rt |dr|drdS zt|d p| d }W n ttfy&   Y dS w || v r-|S dS )z
    Given a list of hostnames and an untrusted URL to redirect to,
    this method tests it to make sure it isn't garbage/harmful
    and returns it, else returns None, similar as how's it done
    on django.contrib.auth.views.
    
startswithz///Nr   r   )hasattrrQ   r   	TypeErrorAttributeError)hostsredirect_tonetlocr   r   r   sanitize_redirectW   s   	rX   c                 C   B   | rt | drt| jr|  }|S | j}|S | rd}|S d}|S )Nis_authenticatedTF)rR   callablerZ   )userauthenticatedr   r   r   user_is_authenticatedp   s   
r^   c                 C   rY   )N	is_activeTF)rR   r[   r_   )r\   r_   r   r   r   user_is_active}   s   r`   c                 C   sB   t dt| ddd} tdd|   } tdd| S )zConverts to lowercase, removes non-word characters (alphanumerics
    and underscores) and converts spaces to hyphens. Also strips leading
    and trailing whitespace.NFKDasciiignorez[^\w\s-] z[-\s]+rE   )	unicodedata	normalizestrencodedecoderesubstriplowerrB   r   r   r   slugify   s   ro   c                 C   s   |D ]
}| |r|  S qdS )z<Return the first item in the list for what func returns TrueNr   )funcitemsitemr   r   r   first   s
   rs   c                 C   s   t t| S )z@Like urlparse.parse_qs but transform list values to single items)
drop_listsbattery_parse_qsrn   r   r   r   r      s   r   c                 C   sR   i }|   D ] \}}|d }t|trt|d}t|tr"t|d}|||< q|S )Nr   utf-8)rq   
isinstancebytesrg   )rB   outkeyvalr   r   r   rt      s   




rt   c                 O   s   | j  }| dd}|p||p| j td }|rc| j |}d}|rD|j| jkrDd}| j	|v rD|j
d}	|| j	}
|	|
krDd}|r[|rN|d| |d| || |S | j | d S d S )NPARTIAL_PIPELINE_TOKEN_NAMEpartial_tokenFTuidr\   request)strategyrequest_datasettinggetsession_getPARTIAL_TOKEN_SESSION_NAMEpartial_loadbackendr0   ID_KEYkwargs
setdefaultextend_kwargsclean_partial_pipeline)r   r\   r}   argsr   r   partial_argument_namepartialpartial_matches_requestid_from_partialid_from_requestr   r   r   partial_pipeline_data   s6   


r   c                 C   sH   |pd}| ds| dr|S | dr | dr |dd }| | S )z-Build absolute URI with given (optional) pathrd   http://r    /r   N)rQ   endswith)host_urlpathr   r   r   build_absolute_uri   s   r   c                 C   s4   t | tr
| d} t |tr|d}t| |S )zDCompare two values and prevent timing attacks for cryptographic use.rv   )rw   rg   rh   hmaccompare_digest)val1val2r   r   r   constant_time_compare   s
   



r   c                 C   s"   | o|  dp|  dp|  dS )Nr   r    r   )rQ   rn   r   r   r   is_url   s   
r   c                 G   s8   |D ]}t |r|  S | |}t |r|  S qd S r   )r   r   )r   rL   r0   rB   r   r   r   setting_url   s   
r   c                    s   t   fdd}|S )Nc               
      s|   z | i |W S  t jy= } z(|jjdkr t| d |jd|jjdkr,t| d |jjdkr8t| d  d }~ww )Ni  r   )responsei  i  )r!   	HTTPErrorr   status_coder	   r
   r   )r   r   errrp   r   r   wrapper   s   z#handle_http_errors.<locals>.wrapper)	functoolswraps)rp   r   r   r   r   handle_http_errors   s   r   c                 C   s   | r|  ds|  d} | S )zMake sure we append a slash at the end of the URL otherwise we
    have issues with urljoin Example:
    >>> urlparse.urljoin('http://www.example.com/api/v3', 'user/1/')
    'http://www.example.com/api/user/1/'
    r   )r   )r?   r   r   r   append_slash  s   
r   c                 O   s&   t | }t |}||g|R i |S r   )r8   )r   storager   r   StrategyStorager   r   r   get_strategy  s   r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	cachez
    Cache decorator that caches the return value of a method for a
    specified time.

    It maintains a cache per class, so subclasses have a different cache entry
    for the same cached method.

    Does not work for methods with arguments.
    c                 C   s   || _ i | _d S r   )ttlr   )r   r   r   r   r   r   '  s   
zcache.__init__c                    s    fdd}j |_|S )Nc                    s|   t   }d }d }| jjv rj| j \}}|r|| jkr<z | }||fj| j< W |S  ty;   |s8 Y |S w |S r   )timer   r   r   	Exception)thisnowlast_updatedcached_valuefnr   r   r   wrapped,  s"   zcache.__call__.<locals>.wrapped)_invalidate
invalidate)r   r   r   r   r   r   __call__+  s   zcache.__call__c                 C   s   | j   d S r   )r   clear)r   r   r   r   r   ?  rP   zcache._invalidateN)r'   r(   r)   r*   r   r   r   r   r   r   r   r     s
    
r   )NNr   )1r   r   loggingrj   r.   r   re   urllib.parser   ru   r   r   r   r!   requests.adaptersr   %requests.packages.urllib3.poolmanagerr   r9   
exceptionsr	   r
   r   rN   r   	getLoggersocial_loggerr   r1   r8   r;   rC   rM   rO   rX   r^   r`   ro   rs   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sP    


'
	
