o
    &zh                     @   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 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 G dd deZeedddZ e!eZ"e Z#e!eZ$dS )    N)	timedelta)HttpResponseNotAllowedHttpResponseRedirect)reverse)timezone)	urlencode)csrf_exempt)login_not_required)jwtkit)SocialToken)OAuth2AdapterOAuth2CallbackViewOAuth2LoginView)build_absolute_uriget_request_param   )get_apple_session)AppleOAuth2Clientc                   @   sN   e Zd ZeZdZdZdZdZe	dd Z
dd Zd	d
 Zdd ZdddZdS )AppleOAuth2Adapterapplez$https://appleid.apple.com/auth/tokenz(https://appleid.apple.com/auth/authorizez#https://appleid.apple.com/auth/keysc                 C   s    t j|| jd| t jd}|S )Nzhttps://appleid.apple.com)
credentialkeys_urlissueraudience
lookup_kid)r
   verify_and_decodepublic_key_urlget_audslookup_kid_jwk)clsproviderid_tokendata r#   f/var/www/html/kangema/venv/lib/python3.10/site-packages/allauth/socialaccount/providers/apple/views.pyget_verified_identity_data   s   z-AppleOAuth2Adapter.get_verified_identity_datac                 C   sj   t |d d}|dd|_|| j}|r"t tt|d |_t	
|  |d }i |||_|S )Naccess_token)tokenrefresh_token )secondsr!   )r   gettoken_secretexpires_in_keyr   nowr   int
expires_atr   r%   get_provider	user_data)selfr"   r'   
expires_inidentity_datar#   r#   r$   parse_token*   s   zAppleOAuth2Adapter.parse_tokenc                 K   s4   |j }|  j||d}|j |jd< t|  |S )N)requestresponser!   )r2   r1   sociallogin_from_responsestater   delete)r3   r7   appr'   kwargs
extra_dataloginr#   r#   r$   complete_login=   s   z!AppleOAuth2Adapter.complete_loginc                 C   s4   |j dd}zt|W S  tjy   i  Y S w )Nuserr)   )apple_login_sessionr+   jsonloadsJSONDecodeError)r3   r7   user_scope_datar#   r#   r$   get_user_scope_dataI   s   z&AppleOAuth2Adapter.get_user_scope_dataNc           	      C   sZ   t |}t|d}|j||d}|dd}|du r |jd}i || |d|iS )z8We need to gather the info from the apple specific logincode)pkce_code_verifierr!   N)r   r   get_access_tokenr+   storerG   )	r3   r7   r<   clientrI   apple_sessionrH   access_token_datar!   r#   r#   r$   get_access_token_dataR   s   
z(AppleOAuth2Adapter.get_access_token_data)N)__name__
__module____qualname__r   client_classprovider_idaccess_token_urlauthorize_urlr   classmethodr%   r6   r@   rG   rO   r#   r#   r#   r$   r      s    

	r   apple_finish_callbackc           
      C   s   | j dkr
tdgS t| }g d}i }|D ]}t| |d}|r$|||< qddg}|D ]}t| |d|j|< q+t| t|}tdj|t	|d}	|
|	 |	S )a  
    Apple uses a `form_post` response type, which due to
    CORS/Samesite-cookie rules means this request cannot access
    the request since the session cookie is unavailable.

    We work around this by storing the apple response in a
    separate, temporary session and redirecting to a more normal
    oauth flow.

    args:
        finish_endpoint_name (str): The name of a defined URL, which can be
            overridden in your url configuration if you have more than one
            callback endpoint.
    POST)rH   r:   errorr)   rA   r!   z{url}?{query})urlquery)methodr   r   r   rK   r   r   r   formatr   save)
r7   finish_endpoint_namerM   keys_to_put_in_url
url_paramskeyvaluekeys_to_save_to_sessionr[   r8   r#   r#   r$   apple_post_callbackh   s&   


rf   )rX   )%rC   datetimer   django.httpr   r   django.urlsr   django.utilsr   django.utils.httpr   django.views.decorators.csrfr   #allauth.account.internal.decoratorsr	   allauth.socialaccount.internalr
   allauth.socialaccount.modelsr   ,allauth.socialaccount.providers.oauth2.viewsr   r   r   allauth.utilsr   r   rM   r   rL   r   r   rf   adapter_viewoauth2_loginoauth2_callbackoauth2_finish_loginr#   r#   r#   r$   <module>   s*    P
(