o
    &zhK                     @   sv   d dl 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	ZG d
d deZdS )    N)	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   g/var/www/html/kangema/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                   @   s2   e Zd ZdZdd Zdd Zd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   s   t t }t| j| jd}|jstd|jd}|s#td|jd| 	 ||d d}| j
dd	}t||d|d
}|S )z6Create a JWT signed with an apple provided private keyapplezApple 'key' missingcertificate_keyzApple 'certificate_key' missingzhttps://appleid.apple.comi  )issaudsubiatexpES256)kidalg)payloadkey	algorithmheaders)inttimer   requestget_appr(   r   settingsgetget_client_idconsumer_secretr   )selfnowappr   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)r3   r   r   r   r1   6   s   zAppleOAuth2Client.get_client_idNc                 C   s   | j }|  }|  |d| j|d}|r||d< | | t  j| j||| j	d}d }|j
dv rIz| }W n tyH   tt|j}Y nw |rOd|vrVtd|j |S )Nauthorization_code)	client_idcode
grant_typeredirect_urir7   code_verifier)datar*   )      access_tokenz!Error retrieving access token: %s)access_token_urlr8   r1   callback_url_strip_empty_keysr   get_requests_sessionr-   access_token_methodr*   status_codejson
ValueErrordictr   textr   content)r3   r>   pkce_code_verifierurlr7   rB   r   rE   r   r   r   get_access_token:   s4   

z"AppleOAuth2Client.get_access_tokenc                 C   sT   | j t|}|  | jd|dd}| jr| j|d< || d|t|tdf S )N	form_postzcode id_token)r=   r@   response_modescoperesponse_typestatez%s?%s)	quote_via)	scope_delimiterjoinsetr1   rG   rX   updater   r   )r3   authorization_urlrV   extra_paramsparamsr   r   r   get_redirect_urlV   s   

z"AppleOAuth2Client.get_redirect_url)N)r   r   r   __doc__r8   r1   rS   ra   r   r   r   r   r      s    
r   )r,   urllib.parser   r   r   django.core.exceptionsr   r	   allauth.socialaccount.adapterr   -allauth.socialaccount.providers.oauth2.clientr   r   r   r   r   r   r   r   r   <module>   s    