o
    Lf                     @   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 ddlm Z  G dd deZ!edddZ"e#e!Z$e"Z%e#e!Z&dS )    N)	timedelta)HttpResponseNotAllowedHttpResponseRedirect)reverse)timezone)	urlencode)csrf_exempt)get_adapter)jwtkit)SocialToken)OAuth2AdapterOAuth2CallbackViewOAuth2LoginView)build_absolute_uriget_request_param   )get_apple_session)AppleOAuth2Client)AppleProviderc                   @   sR   e Zd ZeZejZdZdZ	dZ
dd Zdd Zdd	 Zd
d Zdd Zdd ZdS )AppleOAuth2Adapterz$https://appleid.apple.com/auth/tokenz(https://appleid.apple.com/auth/authorizez#https://appleid.apple.com/auth/keysc                 C   s(   t  jd | jd}dd |jdD S )N)requestproviderc                 S   s   g | ]}|  qS  )strip).0audr   r   l/var/www/html/humari/django-venv/lib/python3.10/site-packages/allauth/socialaccount/providers/apple/views.py
<listcomp>"   s    z4AppleOAuth2Adapter.get_client_id.<locals>.<listcomp>,)r	   get_appprovider_id	client_idsplit)selfr   appr   r   r   get_client_id    s   z AppleOAuth2Adapter.get_client_idc                 C   s.   |   }| |}tj|| jd|tjd}|S )Nzhttps://appleid.apple.com)
credentialkeys_urlissueraudience
lookup_kid)get_providerr%   r
   verify_and_decodepublic_key_urllookup_kid_jwk)r#   id_tokenr   allowed_audsdatar   r   r   get_verified_identity_data$   s   
z-AppleOAuth2Adapter.get_verified_identity_datac                 C   sd   t |d d}|dd|_|| j}|r"t tt|d |_| 	|d }i |||_
|S )Naccess_token)tokenrefresh_token )secondsr/   )r   gettoken_secretexpires_in_keyr   nowr   int
expires_atr2   	user_data)r#   r1   r4   
expires_inidentity_datar   r   r   parse_token0   s   zAppleOAuth2Adapter.parse_tokenc                 K   s4   |j }|  j||d}|j |jd< t|  |S )N)r   responser/   )r>   r+   sociallogin_from_responsestater   delete)r#   r   r$   r4   kwargs
extra_dataloginr   r   r   complete_loginA   s   z!AppleOAuth2Adapter.complete_loginc                 C   s4   |j dd}zt|W S  tjy   i  Y S w )Nuserr6   )apple_login_sessionr8   jsonloadsJSONDecodeError)r#   r   user_scope_datar   r   r   get_user_scope_dataM   s   z&AppleOAuth2Adapter.get_user_scope_datac           	      C   sh   t |}t|d}|jd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_verifierN)rR   r/   )r   r   sessionpopget_access_tokenr8   storerP   )	r#   r   r$   clientapple_sessionrQ   rR   access_token_datar/   r   r   r   get_access_token_dataV   s    
z(AppleOAuth2Adapter.get_access_token_dataN)__name__
__module____qualname__r   client_classr   idr    access_token_urlauthorize_urlr-   r%   r2   rA   rI   rP   rZ   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)rQ   rD   errorr6   rJ   r/   z{url}?{query})urlquery)methodr   r   r   rV   r   r   r   formatr   save)
r   finish_endpoint_namerX   keys_to_put_in_url
url_paramskeyvaluekeys_to_save_to_sessionre   rB   r   r   r   apple_post_callbackm   s&   


rp   )rb   )'rL   datetimer   django.httpr   r   django.urlsr   django.utilsr   django.utils.httpr   django.views.decorators.csrfr   allauth.socialaccount.adapterr	   allauth.socialaccount.internalr
   allauth.socialaccount.modelsr   ,allauth.socialaccount.providers.oauth2.viewsr   r   r   allauth.utilsr   r   rX   r   rW   r   r   r   r   rp   adapter_viewoauth2_loginoauth2_callbackoauth2_finish_loginr   r   r   r   <module>   s*    T
(