
    qhm                     t    S r SSKrSSKrSSKJrJrJr  SSKrSSKJ	r	  SSK
Jr  SSKJr  S	 r " S
 S\5      rg)zt
MediaWiki OAuth1 backend, docs at:
    https://python-social-auth.readthedocs.io/en/latest/backends/mediawiki.html
    N)parse_qs	urlencodeurlparse)OAuth1   )AuthException   )
BaseOAuth1c                 H    [        U [        5      (       a  U $ [        U S5      $ )z
Return string in unicode.
zunicode-escape)
isinstancestr)values    P/var/www/html/env/lib/python3.13/site-packages/social_core/backends/mediawiki.pyforce_unicoder      s$     %u&''    c                   F    \ rS rSrSrSrSrSrSrS r	S r
S	 rS
 rS rSrg)	MediaWiki   zA
Handles the handshake with Mediawiki and fetching of user data.
	mediawikiz&https://meta.wikimedia.org/w/index.phpoobg      $@c                    U R                  5       nUR                  U R                  5       5        SUS'   U R                  5       u  p#U R	                  U R                  S5      U[        X#U R                  S5      S9U R                  S9nUR                  R                  5       R                  S5      (       a#  [        XR                  R                  5       5      eUR                  R                  5       $ )z
Return request for unauthorized token (first stage)

Mediawiki request token is requested from e.g.:
 * https://en.wikipedia.org/w/index.php?title=Special:OAuth/initiate
zSpecial:OAuth/initiatetitleMEDIAWIKI_URLCALLBACK)callback_uri)paramsauthmethodError)request_token_extra_argumentsupdateget_scope_argumentget_key_and_secretrequestsettingr   REQUEST_TOKEN_METHODcontentdecode
startswithr   )selfr   keysecretresponses        r   unauthorized_tokenMediaWiki.unauthorized_token$   s     335d--/02w--/<<LL)$,,z2JK,,	   
 ""$//88&6&6&=&=&?@@&&((r   c                 H   [        U[        5      (       d  [        U5      nUR                  U R                  5      S   nU R                  5       nU R                  S5      nSR                  U[        SSU R                  UU R                  U R                  U5      05      5      $ )z.
Generates the URL for the authorization link
r   r   z{}?{}r   zSpecial:Oauth/authenticate)r   dictr   getOAUTH_TOKEN_PARAMETER_NAMEget_or_create_stater%   formatr   REDIRECT_URI_PARAMETER_NAMEget_redirect_uri)r*   tokenoauth_tokenstatebase_urls        r   oauth_authorization_request%MediaWiki.oauth_authorization_request;   s     %&&UOEii ? ?@C((*<<0~~933[44d6K6KE6R	
 		
r   c                    U R                  U5      nU R                  U R                  S5      SSS0US9nUR                  R	                  5       R                  S5      (       a#  [        XR                  R	                  5       5      e[        UR                  5      nUR                  S5      S   nUR                  S	5      S   nUR	                  5       nUR	                  5       nUUS
.$ )z%
Fetches the Mediawiki access token.
r   POSTr   zSpecial:Oauth/tokenr   r   r   r   s   oauth_tokenr   s   oauth_token_secret)r9   oauth_token_secret)	
oauth_authr$   r%   r'   r(   r)   r   r   r2   )r*   r8   
auth_tokenr-   credentialsoauth_token_keyrA   s          r   access_tokenMediaWiki.access_tokenP   s     __U+
<<LL)23	   
 ""$//88&6&6&=&=&?@@x//0%//.9!<(__-BCAF)002/668 +"4
 	
r   c                 \   U R                  5       u  p#US   n[        UUUS   US   S9nU R                  U R                  S5      SSS0US	9n [        R
                  " UR                  UUS
/U R                  S9n[        US   5      R                  n	[        U R                  S5      5      R                  n
X:X  d  [        U SU	 SU
 35      e[        R                  " 5       n[        US   5      nXU R                  -
  :  d  [        U SX-
   S35      e[        UR                  R                  S   5      n[         R"                  " SU5      R%                  S5      nUS   U:w  a  [        U SR'                  US   U5      5      eUS   US   UR)                  S5      UR)                  S5      UR)                  S5      UR)                  S5      UR)                  S 5      UR)                  S!5      UR)                  S"5      S#.	$ ! [        R                   a  n[        U SSU 3-   5      eSnAff = f)$z3
Gets the user details from Special:OAuth/identify
rF   r9   rA   )client_secretresource_owner_keyresource_owner_secretr   r?   r   zSpecial:OAuth/identifyr@   HS256)audience
algorithmsleewayz,An error occurred while trying to read json z	content: NisszUnexpected issuer z, expected iatzIdentity issued z seconds in the futureAuthorizationzoauth_nonce="(.*?)"r	   noncez Replay attack detected: {} != {}usernamesubemailconfirmed_email	editcountrightsgroups
registeredblocked)	rT   userIDrV   rW   rX   rY   rZ   r[   r\   )r#   r   r$   r%   jwtr(   r'   LEEWAYInvalidTokenErrorr   r   netloctimefloatr   headersresearchgroupr5   r2   )r*   r-   r+   r,   rF   r   req_respidentity	exceptionissuerexpected_domainnow	issued_atauthorization_headerrequest_nonces                  r   get_user_detailsMediaWiki.get_user_detailsi   sY    --// +M:"./C"D	
 <<LL)56	   
	zz  #9{{H (5/*11"4<<#@AHH($VHK7HI 
 iik(5/*	4;;./((99OP   -X-=-=-E-Eo-VW		"8:NOUUVWXG-299W%}  !,uo\\'*'||,=>!k2ll8,ll8,",,|4||I.

 
	
E $$ 	>i[)* 	s   -H   H+H&&H+c                     US   $ )z#
Get the unique Mediawiki user ID.
r]    )r*   detailsr-   s      r   get_user_idMediaWiki.get_user_id   s     x  r   rt   N)__name__
__module____qualname____firstlineno____doc__namer   SOCIAL_AUTH_MEDIAWIKI_CALLBACKr_   r.   r<   rF   rq   rv   __static_attributes__rt   r   r   r   r      s9     D<M%*"F).
*
2I
V!r   r   )r|   re   rb   urllib.parser   r   r   r^   requests_oauthlibr   
exceptionsr   oauthr
   r   r   rt   r   r   <module>r      s7   
 
  6 6 
 $ & (^!
 ^!r   