
    fg                         d dl Z d dlmZ d dlmZmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZ d d
lmZmZmZ d dlmZmZ ddlmZ ddlmZ  G d de      Zedd       Z ej>                  e      Z eZ! ej>                  e      Z"y)    N)	timedelta)HttpResponseNotAllowedHttpResponseRedirect)reverse)timezone)	urlencode)csrf_exempt)jwtkit)SocialToken)OAuth2AdapterOAuth2CallbackViewOAuth2LoginView)build_absolute_uriget_request_param   )get_apple_session)AppleOAuth2Clientc                   J    e Zd ZeZdZdZdZdZe	d        Z
d Zd Zd Zdd
Zy	)AppleOAuth2Adapterapplez$https://appleid.apple.com/auth/tokenz(https://appleid.apple.com/auth/authorizez#https://appleid.apple.com/auth/keysc                     t        j                  || j                  d|j                         t         j                        }|S )Nzhttps://appleid.apple.com)
credentialkeys_urlissueraudience
lookup_kid)r
   verify_and_decodepublic_key_urlget_audslookup_kid_jwk)clsproviderid_tokendatas       f/var/www/cs2snipe.com/venv/lib/python3.12/site-packages/allauth/socialaccount/providers/apple/views.pyget_verified_identity_dataz-AppleOAuth2Adapter.get_verified_identity_data   s=    ''''.&&(,,
     c                 N   t        |d         }|j                  dd      |_        |j                  | j                        }|r/t	        j
                         t        t        |            z   |_        t        j                  | j                         |d         }i |||_        |S )Naccess_token)tokenrefresh_token )secondsr#   )r   gettoken_secretexpires_in_keyr   nowr   int
expires_atr   r&   get_provider	user_data)selfr$   r*   
expires_inidentity_datas        r%   parse_tokenzAppleOAuth2Adapter.parse_token)   s    ~&
 "XXor:XXd112
'||~	#j/0RRE +EEj!1
 4T3]3r'   c                     |j                   }| j                         j                  ||      }|j                   |j                  d<   t	        |      j                          |S )N)requestresponser#   )r5   r4   sociallogin_from_responsestater   delete)r6   r;   appr*   kwargs
extra_datalogins          r%   complete_loginz!AppleOAuth2Adapter.complete_login<   sZ    __
!!#==j > 
 #(//J 	'"))+r'   c                     |j                   j                  dd      }	 t        j                  |      S # t        j                  $ r i cY S w xY w)Nuserr,   )apple_login_sessionr.   jsonloadsJSONDecodeError)r6   r;   user_scope_datas      r%   get_user_scope_dataz&AppleOAuth2Adapter.get_user_scope_dataH   sJ    !5599&"E	::o..## 	 I	s   3 A
ANc                     t        |      }t        |d      }|j                  ||      }|j                  dd      }||j                  j                  d      }i || j                  |      d|iS )z8We need to gather the info from the apple specific logincode)pkce_code_verifierr#   N)r   r   get_access_tokenr.   storerL   )	r6   r;   r@   clientrO   apple_sessionrN   access_token_datar#   s	            r%   get_access_token_dataz(AppleOAuth2Adapter.get_access_token_dataQ   s    )'2 !&1"33%7 4 
 %((T:$**..z:H

&&w/
 
 	
r'   )N)__name__
__module____qualname__r   client_classprovider_idaccess_token_urlauthorize_urlr   classmethodr&   r9   rD   rL   rU    r'   r%   r   r      sB    $LK=>M:N &

r'   r   c                 v   | j                   dk7  rt        dg      S t        |       }g d}i }|D ]  }t        | |d      }|s|||<    ddg}|D ]  }t        | |d      |j                  |<    t        | t        |            }t        dj                  |t        |                  }	|j                  |	       |	S )a  
    Apple uses a `form_post` response type, which due to
    CORS/Samesite-cookie rules means this request cannot access
    the request since the session cookie is unavailable.

    We work around this by storing the apple response in a
    separate, temporary session and redirecting to a more normal
    oauth flow.

    args:
        finish_endpoint_name (str): The name of a defined URL, which can be
            overridden in your url configuration if you have more than one
            callback endpoint.
    POST)rN   r>   errorr,   rF   r#   z{url}?{query})urlquery)methodr   r   r   rQ   r   r   r   formatr   save)
r;   finish_endpoint_namerS   keys_to_put_in_url
url_paramskeyvaluekeys_to_save_to_sessionrb   r<   s
             r%   apple_post_callbackrm   g   s      ~~%vh//%g.M 4J! $!'33#JsO$  &z2& G#4Wc2#FC G Wg.B&C
DC#3i
.CDH x Or'   )apple_finish_callback)#rH   datetimer   django.httpr   r   django.urlsr   django.utilsr   django.utils.httpr   django.views.decorators.csrfr	   allauth.socialaccount.internalr
   allauth.socialaccount.modelsr   ,allauth.socialaccount.providers.oauth2.viewsr   r   r   allauth.utilsr   r   rS   r   rR   r   r   rm   adapter_viewoauth2_loginoauth2_callbackoauth2_finish_loginr^   r'   r%   <module>r}      s      D  ! ' 4 1 4 
 @ , %M
 M
` % %P ,++,>?%5(556HI r'   