o
    ˷ex                     @   s   d dl Z d dlmZmZ d dlmZmZmZ d dlmZ d dl	Z	d dl
mZ d dlmZmZ dd ZG d	d
 d
eZG dd deZdS )    N)datetime	timedelta)	parse_qslquote	urlencode)ImproperlyConfigured)get_adapter)OAuth2ClientOAuth2Errorc                  O   s(   t j| i |}t|tr|d}|S )Nzutf-8)jwtencode
isinstancebytesdecode)argskwargsresp r   c/var/www/ideatree/venv/lib/python3.10/site-packages/allauth/socialaccount/providers/apple/client.py
jwt_encode   s   

r   c                   @   s   e Zd ZdZdZdS )ScopeemailnameN)__name__
__module____qualname__EMAILNAMEr   r   r   r   r      s    r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )AppleOAuth2Clientz
    Custom client because `Sign In With Apple`:
        * requires `response_mode` field in redirect_url
        * requires special `client_secret` as JWT
    c                 C   sv   t  }t | jd}|jstd|jstd|jd|  ||t	dd d}| j
dd	}t||jd|d
}|S )z6Create a JWT signed with an apple provided private keyapplezApple 'key' missingzApple 'certificate_key' missingzhttps://appleid.apple.com   )hours)issaudsubiatexpES256)kidalg)payloadkey	algorithmheaders)r   utcnowr   get_apprequestr+   r   certificate_keyget_client_idr   consumer_secretr   )selfnowappclaimsr-   client_secretr   r   r   generate_client_secret$   s"   
z(AppleOAuth2Client.generate_client_secretc                 C   s   | j dd S )zCWe support multiple client_ids, but use the first one for api calls,r   )consumer_keysplit)r4   r   r   r   r2   9   s   zAppleOAuth2Client.get_client_idc                 C   s   | j }|  }|  |d| j|d}| | tj| j||| jd}d }|j	dv r@z|
 }W n ty?   tt|j}Y nw |rFd|vrMtd|j |S )Nauthorization_code)	client_idcode
grant_typeredirect_urir8   )datar-   )      access_tokenz!Error retrieving access token: %s)access_token_urlr9   r2   callback_url_strip_empty_keysrequestsr0   access_token_methodr-   status_codejson
ValueErrordictr   textr
   content)r4   r?   urlr8   rB   r   rE   r   r   r   get_access_token=   s,   

z"AppleOAuth2Client.get_access_tokenc                 C   sF   |   | jd| jdd}| jr| j|d< || d|t|tdf S )N	form_postzcode id_token)r>   rA   response_modescoperesponse_typestatez%s?%s)	quote_via)r2   rG   rU   rW   updater   r   )r4   authorization_urlextra_paramsparamsr   r   r   get_redirect_urlU   s   

z"AppleOAuth2Client.get_redirect_urlN)r   r   r   __doc__r9   r2   rR   r]   r   r   r   r   r      s    r   )rI   r   r   urllib.parser   r   r   django.core.exceptionsr   r   allauth.socialaccount.adapterr   -allauth.socialaccount.providers.oauth2.clientr	   r
   r   objectr   r   r   r   r   r   <module>   s    