a
    $`x                     @   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   d/var/www/html/Ranjet/env/lib/python3.9/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__Z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s4td|jd|  ||t	dd d}| j
dd	}t||jd|d
}|S )z6Create a JWT signed with an apple provided private keyZapplezApple 'key' missingzApple 'certificate_key' missingzhttps://appleid.apple.com   )hours)ZissZaudsubZiatexpZES256)kidZalg)payloadkey	algorithmheaders)r   utcnowr   Zget_apprequestr$   r   Zcertificate_keyget_client_idr   Zconsumer_secretr   )selfnowZappZ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   )Zconsumer_keysplit)r*   r   r   r   r)   9   s    zAppleOAuth2Client.get_client_idc                 C   s   | j }|  }|  |d| j|d}| | tj| j||| jd}d }|j	dv rz|
 }W n  ty~   tt|j}Y n0 |rd|vrtd|j |S )NZauthorization_code)	client_idcodeZ
grant_typeredirect_urir,   )datar&   )      access_tokenz!Error retrieving access token: %s)Zaccess_token_urlr-   r)   callback_urlZ_strip_empty_keysrequestsr(   Zaccess_token_methodr&   Zstatus_codejson
ValueErrordictr   textr
   content)r*   r1   urlr,   r3   r   r6   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 )NZ	form_postzcode id_token)r0   r2   Zresponse_modescopeZresponse_typestatez%s?%s)	quote_via)r)   r7   r@   rA   updater   r   )r*   Zauthorization_urlZextra_paramsparamsr   r   r   get_redirect_urlU   s    

z"AppleOAuth2Client.get_redirect_urlN)r   r   r   __doc__r-   r)   r?   rE   r   r   r   r   r      s
   r   )r8   r   r   urllib.parser   r   r   Zdjango.core.exceptionsr   r   Zallauth.socialaccount.adapterr   Z-allauth.socialaccount.providers.oauth2.clientr	   r
   r   objectr   r   r   r   r   r   <module>   s   