a
    $`                     @   s  d dl Z 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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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)SocialToken)OAuth2Error)OAuth2AdapterOAuth2CallbackViewOAuth2LoginView)build_absolute_uriget_request_param   )add_apple_sessionpersist_apple_session)AppleOAuth2Client)AppleProviderc                   @   sb   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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   sx   t | j}|  z| }W n0 tjyP } ztd|W Y d }~n
d }~0 0 |d D ]}|d |krZ|  S qZd S )Nz"Error retrieving apple public key.keyskid)requestsgetpublic_key_urlZraise_for_statusjsonJSONDecodeErrorr   )selfr   responsedataed r#   c/var/www/html/Ranjet/env/lib/python3.9/site-packages/allauth/socialaccount/providers/apple/views.py_get_apple_public_key#   s     z(AppleOAuth2Adapter._get_apple_public_keyc                 C   s2   t |d }| j|d}t jjt|}|S )zT
        Get the public key which matches the `kid` in the id_token header.
        r   )r   )jwtZget_unverified_headerr%   
algorithmsZRSAAlgorithmZfrom_jwkr   dumps)r   id_tokenr   Zapple_public_key
public_keyr#   r#   r$   get_public_key/   s    z!AppleOAuth2Adapter.get_public_keyc                 C   s(   t  jd | jd}dd |jdD S )N)requestproviderc                 S   s   g | ]}|  qS r#   )strip).0Zaudr#   r#   r$   
<listcomp>;       z4AppleOAuth2Adapter.get_client_id.<locals>.<listcomp>,)r	   Zget_appprovider_idZ	client_idsplit)r   r-   appr#   r#   r$   get_client_id9   s    z AppleOAuth2Adapter.get_client_idc              
   C   sp   |   }| |}z(| |}tj||dgd|dd}|W S  tjyj } ztd|W Y d }~n
d }~0 0 d S )NZRS256Tzhttps://appleid.apple.com)r'   ZverifyZaudienceZissuerzInvalid id_token)get_providerr6   r+   r&   decodeZ
PyJWTErrorr   )r   r)   r-   Zallowed_audsr*   identity_datar!   r#   r#   r$   get_verified_identity_data=   s    

z-AppleOAuth2Adapter.get_verified_identity_datac                 C   sd   t |d d}|dd|_|| j}|rDt tt|d |_| 	|d }i |||_
|S )NZaccess_token)tokenZrefresh_token )secondsr)   )r
   r   Ztoken_secretZexpires_in_keyr   nowr   intZ
expires_atr:   	user_data)r   r    r;   Z
expires_inr9   r#   r#   r$   parse_tokenP   s    zAppleOAuth2Adapter.parse_tokenc                 K   s:   |j }|  j||d}|j |jd< t| |j  |S )N)r,   r   r)   )r@   r7   Zsociallogin_from_responsestater   apple_login_sessiondelete)r   r,   r5   r;   kwargs
extra_dataZloginr#   r#   r$   complete_logina   s    
z!AppleOAuth2Adapter.complete_loginc                 C   s8   |j dd}zt|W S  tjy2   i  Y S 0 d S )Nuserr<   )rC   r   r   loadsr   )r   r,   Zuser_scope_datar#   r#   r$   get_user_scope_datao   s
    z&AppleOAuth2Adapter.get_user_scope_datac                 C   s>   t | t|d}||}i || |d|jdiS )z8We need to gather the info from the apple specific logincoder)   )r   r   Zget_access_tokenrJ   rC   r   )r   r,   r5   clientrK   Zaccess_token_datar#   r#   r$   get_access_token_datax   s    

z(AppleOAuth2Adapter.get_access_token_dataN)__name__
__module____qualname__r   Zclient_classr   idr3   Zaccess_token_urlZauthorize_urlr   r%   r+   r6   r:   rA   rG   rJ   rM   r#   r#   r#   r$   r      s   
	r   apple_finish_callbackc           	      C   s   | j dkrtdgt|  g d}i }|D ]}t| |d}|r,|||< q,ddg}|D ]}t| |d| j|< qVt| t|}tdj|t	|d}t
| | |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.
    ZPOST)rK   rB   errorr<   rH   r)   z{url}?{query})urlquery)methodr   r   r   rC   r   r   r   formatr   r   )	r,   Zfinish_endpoint_nameZkeys_to_put_in_urlZ
url_paramskeyvalueZkeys_to_save_to_sessionrT   r   r#   r#   r$   apple_post_callback   s$    



rZ   )rR   )*r   r   datetimer   Zdjango.httpr   r   Zdjango.urlsr   Zdjango.utilsr   Zdjango.utils.httpr   Zdjango.views.decorators.csrfr   r&   Zallauth.socialaccount.adapterr	   Zallauth.socialaccount.modelsr
   Z-allauth.socialaccount.providers.oauth2.clientr   Z,allauth.socialaccount.providers.oauth2.viewsr   r   r   Zallauth.utilsr   r   Zapple_sessionr   r   rL   r   r-   r   r   rZ   Zadapter_viewZoauth2_loginZoauth2_callbackZoauth2_finish_loginr#   r#   r#   r$   <module>   s,   k(
