
    )Jf                     F   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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 ddl m!Z!  G d de          Z"edd            Z# ej$        e"          Z%e#Z& ej$        e"          Z'dS )    N)	timedelta)HttpResponseNotAllowedHttpResponseRedirect)reverse)timezone)	urlencode)csrf_exempt)get_adapter)SocialToken)OAuth2Error)OAuth2AdapterOAuth2CallbackViewOAuth2LoginView)build_absolute_uriget_request_param   )get_apple_session)AppleOAuth2Client)AppleProviderc                   \    e Zd ZeZej        ZdZdZ	dZ
d Zd Zd Zd Zd Zd	 Zd
 Zd ZdS )AppleOAuth2Adapterz$https://appleid.apple.com/auth/tokenz(https://appleid.apple.com/auth/authorizez#https://appleid.apple.com/auth/keysc                 N   t                                                                          | j                  }|                                 	 |                                }n'# t
          j        $ r}t          d          |d }~ww xY w|d         D ]}|d         |k    r|c S d S )Nz"Error retrieving apple public key.keyskid)r
   get_requests_sessiongetpublic_key_urlraise_for_statusjsonJSONDecodeErrorr   )selfr   responsedataeds         n/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/allauth/socialaccount/providers/apple/views.py_get_apple_public_keyz(AppleOAuth2Adapter._get_apple_public_key"   s    ==5577;;D<OPP!!###	K==??DD# 	K 	K 	KBCCJ	K f 	 	Ax3 	 	s   A# #B2BBc                     t          j        |          d         }|                     |          }t           j        j                            t          j        |                    }|S )zT
        Get the public key which matches the `kid` in the id_token header.
        r   )r   )jwtget_unverified_headerr'   
algorithmsRSAAlgorithmfrom_jwkr   dumps)r!   id_tokenr   apple_public_key
public_keys        r&   get_public_keyz!AppleOAuth2Adapter.get_public_key.   sY     '11%855#5>>^099$*EU:V:VWW
    c                     t                                          d | j                  }d |j                            d          D             S )N)requestproviderc                 6    g | ]}|                                 S  )strip).0auds     r&   
<listcomp>z4AppleOAuth2Adapter.get_client_id.<locals>.<listcomp>:   s     @@@		@@@r3   ,)r
   get_appprovider_id	client_idsplit)r!   r6   apps      r&   get_client_idz AppleOAuth2Adapter.get_client_id8   sE    mm##D4;K#LL@@s}':':3'?'?@@@@r3   c                    |                                  }|                     |          }	 |                     |          }t          j        ||dg|d          }|S # t          j        $ r}t          d          |d }~ww xY w)NRS256zhttps://appleid.apple.com)r+   audienceissuerzInvalid id_token)get_providerrC   r2   r)   decode
PyJWTErrorr   )r!   r/   r6   allowed_audsr1   identity_datar$   s          r&   get_verified_identity_dataz-AppleOAuth2Adapter.get_verified_identity_data<   s    $$&&))(33	9,,X66JJ#9%2  M ! ~ 	9 	9 	9011q8	9s   0A B +A;;B c                 X   t          |d                   }|                    dd          |_        |                    | j                  }|r6t	          j                    t          t          |                    z   |_        | 	                    |d                   }i |||_
        |S )Naccess_token)tokenrefresh_token )secondsr/   )r   r   token_secretexpires_in_keyr   nowr   int
expires_atrM   	user_data)r!   r#   rP   
expires_inrL   s        r&   parse_tokenzAppleOAuth2Adapter.parse_tokenN   s    ~&
 
 
 "XXor::XXd122
 	S'|~~	#j//0R0R0RRE 77Z8HII3T3]3r3   c                     |j         }|                                                     ||          }|j         |j        d<   t	          |                                           |S )N)r5   r"   r/   )rY   rH   sociallogin_from_responsestater   delete)r!   r5   rB   rP   kwargs
extra_datalogins          r&   complete_loginz!AppleOAuth2Adapter.complete_login_   se    _
!!##==j > 
 
 #(/J 	'""))+++r3   c                     |j                             dd          }	 t          j        |          S # t          j        $ r i cY S w xY w)NuserrR   )apple_login_sessionr   r   loadsr    )r!   r5   user_scope_datas      r&   get_user_scope_dataz&AppleOAuth2Adapter.get_user_scope_datak   sX    !599&"EE	:o...# 	 	 	 III	s   1 AAc                 B   t          |          }t          |d          }|j                            dd          }|                    ||          }|                    dd          }||j                            d          }i ||                     |          d|iS )z8We need to gather the info from the apple specific logincodepkce_code_verifierN)rl   r/   )r   r   sessionpopget_access_tokenr   storeri   )	r!   r5   rB   clientapple_sessionrk   rl   access_token_datar/   s	            r&   get_access_token_dataz(AppleOAuth2Adapter.get_access_token_datat   s    )'22 !&11$_001EtLL"33%7 4 
 
 %((T::$*..z::H

&&w//
 
 
 	
r3   N)__name__
__module____qualname__r   client_classr   idr?   access_token_urlauthorize_urlr   r'   r2   rC   rM   r[   rc   ri   rt   r8   r3   r&   r   r      s        $L"K=>M:N
 
 
  A A A9 9 9$  "
 
 
  
 
 
 
 
r3   r   apple_finish_callbackc                    | j         dk    rt          dg          S t          |           }g d}i }|D ]}t          | |d          }|r|||<   ddg}|D ]}t          | |d          |j        |<   t          | t          |                    }t          d                    |t          |                              }	|
                    |	           |	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)rk   r^   errorrR   re   r/   z{url}?{query})urlquery)methodr   r   r   rp   r   r   r   formatr   save)
r5   finish_endpoint_namerr   keys_to_put_in_url
url_paramskeyvaluekeys_to_save_to_sessionr   r"   s
             r&   apple_post_callbackr      s     ~%vh///%g..M 433J! $ $!'333 	$#JsO  &z2& G G#4Wc2#F#FC  
Wg.B&C&C
D
DC#3i
.C.CDD H x   Or3   )r|   )(r   datetimer   django.httpr   r   django.urlsr   django.utilsr   django.utils.httpr   django.views.decorators.csrfr	   r)   allauth.socialaccount.adapterr
   allauth.socialaccount.modelsr   -allauth.socialaccount.providers.oauth2.clientr   ,allauth.socialaccount.providers.oauth2.viewsr   r   r   allauth.utilsr   r   rr   r   rq   r   r6   r   r   r   adapter_viewoauth2_loginoauth2_callbackoauth2_finish_loginr8   r3   r&   <module>r      s          D D D D D D D D       ! ! ! ! ! ! ' ' ' ' ' ' 4 4 4 4 4 4 



 5 5 5 5 5 5 4 4 4 4 4 4 E E E E E E         
 @ ? ? ? ? ? ? ? , , , , , , % % % % % % # # # # # #m
 m
 m
 m
 m
 m
 m
 m
` % % % %P ,+,>??%5(56HII   r3   