
    )JfQ                         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 Z G d d	e          Z G d
 de          ZdS )    )datetime	timedelta)	parse_qslquote	urlencode)ImproperlyConfiguredN)get_adapter)OAuth2ClientOAuth2Errorc                  |    t          j        | i |}t          |t                    r|                    d          }|S )Nzutf-8)jwtencode
isinstancebytesdecode)argskwargsresps      o/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/allauth/socialaccount/providers/apple/client.py
jwt_encoder      s>    :t&v&&D$ ${{7##K    c                       e Zd ZdZdZdS )ScopeemailnameN)__name__
__module____qualname__EMAILNAME r   r   r   r      s        EDDDr   r   c                   ,    e Zd ZdZd Zd ZddZ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                    t          j                    }t          | j                                      | j        d          }|j        st          d          |j                            d          }|st          d          |j        d| 	                                ||t          d          z   d}| j        d	d
}t          ||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_appr2   r   settingsgetget_client_idr   consumer_secretr   )selfnowappr&   claimsr4   client_secrets          r   generate_client_secretz(AppleOAuth2Client.generate_client_secret#   s    o$,''//gFFw 	>&'<===,**+<== 	J&'HIII7.%%''++++
 
 .w??"7G
 
 
 r   c                 B    | j                             d          d         S )zCWe support multiple client_ids, but use the first one for api calls,r   )consumer_keysplit)r<   s    r   r:   zAppleOAuth2Client.get_client_id9   s     &&s++A..r   Nc                    | j         }|                                 }|                                 |d| j        |d}|r||d<   |                     |           t                                                                          | j        ||| j	                  }d }|j
        dv rG	 |                                }n1# t          $ r$ t          t          |j                            }Y nw xY w|rd|vrt!          d|j        z            |S )Nauthorization_code)	client_idcode
grant_typeredirect_urir@   code_verifier)datar4   )      access_tokenz!Error retrieving access token: %s)access_token_urlrA   r:   callback_url_strip_empty_keysr	   get_requests_sessionr6   access_token_methodr4   status_codejson
ValueErrordictr   textr   content)r<   rI   pkce_code_verifierurlr@   rM   r   rP   s           r   get_access_tokenz"AppleOAuth2Client.get_access_token=   s-   #3355++--. -*
 
  	7$6D!t$$$MM!!##WT-st|WTT 	
 z)):#yy{{ : : :#Idi$8$899: 	R~\AAADLPQQQs   $B9 9+C'&C'c                     |                                  | j        d| j        dd}| j        r
| j        |d<   |                    |           |dt          |t                    S )N	form_postzcode id_token)rH   rK   response_modescoperesponse_typestate?)	quote_via)r:   rR   rb   rd   updater   r   )r<   authorization_urlextra_paramsparamss       r   get_redirect_urlz"AppleOAuth2Client.get_redirect_urlY   sx    ++-- -(Z,
 
 : 	)"jF7Ol###+++Yv-O-O-O-OPPr   )N)r   r   r   __doc__rA   r:   r^   rk   r!   r   r   r#   r#      sd           ,/ / /   8Q Q Q Q Qr   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   <module>rr      s   ( ( ( ( ( ( ( ( 4 4 4 4 4 4 4 4 4 4 7 7 7 7 7 7 



 5 5 5 5 5 5             F   
HQ HQ HQ HQ HQ HQ HQ HQ HQ HQr   