o
    LfQ                     @   s   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 )    )datetime	timedelta)	parse_qslquote	urlencode)ImproperlyConfiguredN)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   m/var/www/html/humari/django-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| j| jd}|jstd|jd}|s!td|jd| 	 ||t
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.com   )hours)issaudsubiatexpES256)kidalg)payloadkey	algorithmheaders)r   utcnowr   requestget_appr,   r   settingsgetget_client_idr   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)r6   r   r   r   r4   9   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_urir:   code_verifier)datar.   )      access_tokenz!Error retrieving access token: %s)access_token_urlr;   r4   callback_url_strip_empty_keysr   get_requests_sessionr0   access_token_methodr.   status_codejson
ValueErrordictr   textr
   content)r6   rA   pkce_code_verifierurlr:   rE   r   rH   r   r   r   get_access_token=   s4   

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@   rC   response_modescoperesponse_typestatez%s?%s)	quote_via)r4   rJ   rY   r[   updater   r   )r6   authorization_urlextra_paramsparamsr   r   r   get_redirect_urlY   s   

z"AppleOAuth2Client.get_redirect_url)N)r   r   r   __doc__r;   r4   rV   ra   r   r   r   r   r      s    
r   )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    