o
    h                     @   s  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 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 d dlmZmZmZ eedi di ddZeedi di ddZeedi di ddZeedi di ddZ eedi di ddZ!eedi di ddZ"d"ddZ#G dd deZ$e%e$Z&e%e$Z'G d d! d!eZ(e	e() Z*dS )#    N)settings)PermissionDeniedValidationError)method_decorator)csrf_exempt)View)login_not_required)get_adapter)complete_social_loginrender_authentication_error)jwtkit)OAuth2Error)OAuth2AdapterOAuth2CallbackViewOAuth2LoginViewSOCIALACCOUNT_PROVIDERSgoogle	CERTS_URLz*https://www.googleapis.com/oauth2/v1/certsIDENTITY_URLz-https://www.googleapis.com/oauth2/v2/userinfoACCESS_TOKEN_URLz#https://oauth2.googleapis.com/tokenAUTHORIZE_URLz,https://accounts.google.com/o/oauth2/v2/authID_TOKEN_ISSUERzhttps://accounts.google.comFETCH_USERINFOFTc                 C   s   t j|tt| jt j|dS )N)
credentialkeys_urlissueraudience
lookup_kidverify_signature)r   verify_and_decoder   r   	client_idlookup_kid_pem_x509_certificate)appr   r    r#   c/var/www/html/pos/venv/lib/python3.10/site-packages/allauth/socialaccount/providers/google/views.py_verify_and_decode=   s   r%   c                   @   s<   e Zd ZdZeZeZeZ	e
ZeZdd Zdd Zdd ZdS )	GoogleOAuth2Adapterr   c                 K   sp   d }| d}|r(| ||}| jr'd|vr'| |j}| d}	|	r'|	|d< n| |j}|  ||}
|
S )Nid_tokenpicture)get_decode_id_tokenfetch_userinfo_fetch_user_infotokenget_providersociallogin_from_response)selfrequestr"   r-   responsekwargsdatar'   infor(   loginr#   r#   r$   complete_loginP   s   

z"GoogleOAuth2Adapter.complete_loginc                 C   s   | j  }t|||dS )aB  
        If the token was received by direct communication protected by
        TLS between this library and Google, we are allowed to skip checking the
        token signature according to the OpenID Connect Core 1.0 specification.

        https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation
        )r   )did_fetch_access_tokenr%   )r0   r"   r'   r   r#   r#   r$   r*   _   s   z$GoogleOAuth2Adapter._decode_id_tokenc                 C   s6   t   j| jdd|id}|jstd| S )NAuthorizationz	Bearer {})headerszRequest to user info failed)r	   get_requests_sessionr)   identity_urlformatokr   json)r0   access_tokenrespr#   r#   r$   r,   j   s   z$GoogleOAuth2Adapter._fetch_user_infoN)__name__
__module____qualname__provider_idr   access_token_urlr   authorize_urlr   id_token_issuerr   r<   r   r+   r7   r*   r,   r#   r#   r#   r$   r&   H   s    r&   c                       s<   e Zd Zee fddZdd Zdd Zdd Z  Z	S )	LoginByTokenViewc                    sh   t  | _| j|tj| _zt |W S  tt	j
ttfy3 } zt|| j|dW  Y d }~S d }~ww )N)	exception)r	   adapterr.   r&   rE   providersuperdispatchr   requestsRequestExceptionr   r   r   )r0   r1   exc	__class__r#   r$   rN   }   s   zLoginByTokenView.dispatchc                 C   s   t d)N405)r   )r0   r1   r#   r#   r$   r)      s   zLoginByTokenView.getc                 O   s2   |  | |jd}| j|d|i}t||S )Nr   r'   )
check_csrfPOSTr)   rL   verify_tokenr
   )r0   r1   argsr3   r   r6   r#   r#   r$   post   s   

zLoginByTokenView.postc                 C   sD   |j d}|std|jd}|std||kr tdd S )Ng_csrf_tokenzNo CSRF token in Cookie.zNo CSRF token in post body.z&Failed to verify double submit cookie.)COOKIESr)   r   rV   )r0   r1   csrf_token_cookiecsrf_token_bodyr#   r#   r$   rU      s   zLoginByTokenView.check_csrf)
rB   rC   rD   r   r   rN   r)   rY   rU   __classcell__r#   r#   rR   r$   rI   |   s    rI   )T)+rO   django.confr   django.core.exceptionsr   r   django.utils.decoratorsr   django.views.decorators.csrfr   django.views.genericr   #allauth.account.internal.decoratorsr   allauth.socialaccount.adapterr	   allauth.socialaccount.helpersr
   r   allauth.socialaccount.internalr   -allauth.socialaccount.providers.oauth2.clientr   ,allauth.socialaccount.providers.oauth2.viewsr   r   r   getattrr)   r   r   r   r   r   r   r%   r&   adapter_viewoauth2_loginoauth2_callbackrI   as_viewlogin_by_tokenr#   r#   r#   r$   <module>   sT    







0
(