U
    c                     @   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   sv   t | j}|  z| }W n. tjk
rN } ztd|W 5 d }~X Y nX |d D ]}|d |krX|  S qXd 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#   o/var/www/html/crackeverytest/cet_env/lib/python3.8/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>;   s     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   sl   |   }| |}z&| |}tj||dg|dd}|W S  tjk
rf } ztd|W 5 d }~X Y nX d S )NZRS256zhttps://appleid.apple.com)r'   ZaudienceZissuerzInvalid id_token)get_providerr5   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   s`   t |d d}|dd|_|| j}|rDt tt|d |_| 	|d }|||_
|S )NZaccess_token)tokenZrefresh_token )secondsr)   )r
   r   Ztoken_secretZexpires_in_keyr   nowr   intZ
expires_atr9   	user_data)r   r    r:   Z
expires_inr8   r#   r#   r$   parse_tokenO   s    
zAppleOAuth2Adapter.parse_tokenc                 K   s:   |j }|  j||d}|j |jd< t| |j  |S )N)r,   r   r)   )r?   r6   Zsociallogin_from_responsestater   apple_login_sessiondelete)r   r,   r4   r:   kwargs
extra_dataZloginr#   r#   r$   complete_login`   s     
z!AppleOAuth2Adapter.complete_loginc                 C   s:   |j dd}zt|W S  tjk
r4   i  Y S X d S )Nuserr;   )rB   r   r   loadsr   )r   r,   Zuser_scope_datar#   r#   r$   get_user_scope_datan   s
    z&AppleOAuth2Adapter.get_user_scope_datac                 C   sJ   t | t|d}|jdd}|j||d}|| |d|jdiS )z8We need to gather the info from the apple specific logincodepkce_code_verifierN)rK   r)   )r   r   ZsessionpopZget_access_tokenrI   rB   r   )r   r,   r4   clientrJ   rK   Zaccess_token_datar#   r#   r$   get_access_token_dataw   s    
  
z(AppleOAuth2Adapter.get_access_token_dataN)__name__
__module____qualname__r   Zclient_classr   idr2   Zaccess_token_urlZauthorize_urlr   r%   r+   r5   r9   r@   rF   rI   rN   r#   r#   r#   r$   r      s   
	r   apple_finish_callbackc           	      C   s   | j dkrtdgS t|  dddg}i }|D ]}t| |d}|r.|||< q.ddg}|D ]}t| |d| j|< qXt| 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POSTrJ   rA   errorr;   rG   r)   z{url}?{query})urlquery)methodr   r   r   rB   r   r   r   formatr   r   )	r,   Zfinish_endpoint_nameZkeys_to_put_in_urlZ
url_paramskeyvalueZkeys_to_save_to_sessionrU   r   r#   r#   r$   apple_post_callback   s$    




r[   )rS   )*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   rM   r   r-   r   r   r[   Zadapter_viewZoauth2_loginZoauth2_callbackZoauth2_finish_loginr#   r#   r#   r$   <module>   s,   m(
