
    k7iT7                         S r SSKrSSKrSSKrSSKJr  SSKJr  SSKJr  SSK	J
r
  SSK	Jr  S	rS
rSrSrSrSrSS jrSS jrS rS rS rS rS rS rS rS rS rS rS rg)zauthlib.oauth1.rfc5849.signature.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This module represents a direct implementation of `section 3.4`_ of the spec.

.. _`section 3.4`: https://tools.ietf.org/html/rfc5849#section-3.4
    N)to_bytes)
to_unicode)urlparse   escape)unescapez	HMAC-SHA1zRSA-SHA1	PLAINTEXTHEADERQUERYBODYc                 8   [        X5      n/ nU H@  u  pgUS;   a  M  UR                  S5      (       a  [        U5      nUR                  Xg45        MB     [	        U5      nSR                  [        U R                  5       5      [        U5      [        U5      /5      $ )a  Generate signature base string from request, per `Section 3.4.1`_.

For example, the HTTP request::

    POST /request?b5=%3D%253D&a3=a&c%40=&a2=r%20b HTTP/1.1
    Host: example.com
    Content-Type: application/x-www-form-urlencoded
    Authorization: OAuth realm="Example",
        oauth_consumer_key="9djdj82h48djs9d2",
        oauth_token="kkk9d7dh3k39sjv7",
        oauth_signature_method="HMAC-SHA1",
        oauth_timestamp="137131201",
        oauth_nonce="7d8f3e4a",
        oauth_signature="bYT5CMsGcbgUdFHObYMEfcx6bsw%3D"

    c2&a3=2+q

is represented by the following signature base string (line breaks
are for display purposes only)::

    POST&http%3A%2F%2Fexample.com%2Frequest&a2%3Dr%2520b%26a3%3D2%2520q
    %26a3%3Da%26b5%3D%253D%25253D%26c%2540%3D%26c2%3D%26oauth_consumer_
    key%3D9djdj82h48djs9d2%26oauth_nonce%3D7d8f3e4a%26oauth_signature_m
    ethod%3DHMAC-SHA1%26oauth_timestamp%3D137131201%26oauth_token%3Dkkk
    9d7dh3k39sjv7

.. _`Section 3.4.1`: https://tools.ietf.org/html/rfc5849#section-3.4.1
)oauth_signaturerealmoauth_&)normalize_base_string_uri
startswithr	   appendnormalize_parametersjoinr   upper)	methoduriparamshostbase_string_uriunescaped_paramskvnormalized_paramss	            Z/home/james-whalen/.local/lib/python3.13/site-packages/authlib/oauth1/rfc5849/signature.pyconstruct_base_stringr#      s    < 0:O ,, <<!!A'  --=> 886<<>"?#$%	
     c                 t   [        U 5      n [        R                  " U 5      u  p#pEpgU(       a  U(       d  [        S5      eU(       d  SnUR                  5       nUR                  5       nUb  UR                  5       nSnSU;   a  UR	                  SS5      u  pX)4U;   a  Un[        R
                  " X#XESS45      $ )a  Normalize Base String URI per `Section 3.4.1.2`_.

For example, the HTTP request::

    GET /r%20v/X?id=123 HTTP/1.1
    Host: EXAMPLE.COM:80

is represented by the base string URI: "http://example.com/r%20v/X".

In another example, the HTTPS request::

    GET /?q=1 HTTP/1.1
    Host: www.example.net:8080

is represented by the base string URI: "https://www.example.net:8080/".

.. _`Section 3.4.1.2`: https://tools.ietf.org/html/rfc5849#section-3.4.1.2

The host argument overrides the netloc part of the uri argument.
z$uri must include a scheme and netloc/))http80)https443:r    )r   r   
ValueErrorlowersplit
urlunparse)
r   r   schemenetlocpathr   queryfragmentdefault_portsports
             r"   r   r   V   s    * S/C4<4E4Ec4J1FD% ?@@  \\^F\\^F M f}\\#q)
>]*Fb"EFFr$   c                     U  VVs/ s H  u  p[        U5      [        U5      4PM     nnnUR                  5         U VVs/ s H  u  pU SU 3PM     nnnSR                  U5      $ s  snnf s  snnf )a	  Normalize parameters per `Section 3.4.1.3.2`_.

For example, the list of parameters from the previous section would
be normalized as follows:

Encoded::

+------------------------+------------------+
|          Name          |       Value      |
+------------------------+------------------+
|           b5           |     %3D%253D     |
|           a3           |         a        |
|          c%40          |                  |
|           a2           |       r%20b      |
|   oauth_consumer_key   | 9djdj82h48djs9d2 |
|       oauth_token      | kkk9d7dh3k39sjv7 |
| oauth_signature_method |     HMAC-SHA1    |
|     oauth_timestamp    |     137131201    |
|       oauth_nonce      |     7d8f3e4a     |
|           c2           |                  |
|           a3           |       2%20q      |
+------------------------+------------------+

Sorted::

+------------------------+------------------+
|          Name          |       Value      |
+------------------------+------------------+
|           a2           |       r%20b      |
|           a3           |       2%20q      |
|           a3           |         a        |
|           b5           |     %3D%253D     |
|          c%40          |                  |
|           c2           |                  |
|   oauth_consumer_key   | 9djdj82h48djs9d2 |
|       oauth_nonce      |     7d8f3e4a     |
| oauth_signature_method |     HMAC-SHA1    |
|     oauth_timestamp    |     137131201    |
|       oauth_token      | kkk9d7dh3k39sjv7 |
+------------------------+------------------+

Concatenated Pairs::

+-------------------------------------+
|              Name=Value             |
+-------------------------------------+
|               a2=r%20b              |
|               a3=2%20q              |
|                 a3=a                |
|             b5=%3D%253D             |
|                c%40=                |
|                 c2=                 |
| oauth_consumer_key=9djdj82h48djs9d2 |
|         oauth_nonce=7d8f3e4a        |
|   oauth_signature_method=HMAC-SHA1  |
|      oauth_timestamp=137131201      |
|     oauth_token=kkk9d7dh3k39sjv7    |
+-------------------------------------+

and concatenated together into a single string (line breaks are for
display purposes only)::

    a2=r%20b&a3=2%20q&a3=a&b5=%3D%253D&c%40=&c2=&oauth_consumer_key=9dj
    dj82h48djs9d2&oauth_nonce=7d8f3e4a&oauth_signature_method=HMAC-SHA1
    &oauth_timestamp=137131201&oauth_token=kkk9d7dh3k39sjv7

.. _`Section 3.4.1.3.2`: https://tools.ietf.org/html/rfc5849#section-3.4.1.3.2
=r   )r   sortr   )r   r   r    
key_valuesparameter_partss        r"   r   r      sq    R 6<<VTQ6!9fQi(VJ<
 OO
 /99jda!AaSzjO9
 88O$$ = :s   "A' A-c                     U R                   R                  SS5      n[        U R                  U R                  U R
                  U5      $ )z,Generate signature base string from request.HostN)headersgetr#   r   r   r   )requestr   s     r"   generate_signature_base_stringrB      s5    ??vt,D gnndSSr$   c                 :   U n[        U=(       d    S5      nUS-  nU[        U=(       d    S5      -  n[        R                  " [        U5      [        U5      [        R
                  5      n[        R                  " UR                  5       5      SS n[        U5      $ )aC  Generate signature via HMAC-SHA1 method, per `Section 3.4.2`_.

The "HMAC-SHA1" signature method uses the HMAC-SHA1 signature
algorithm as defined in `RFC2104`_::

    digest = HMAC - SHA1(key, text)

.. _`RFC2104`: https://tools.ietf.org/html/rfc2104
.. _`Section 3.4.2`: https://tools.ietf.org/html/rfc5849#section-3.4.2
r,   r   N)
r   hmacnewr   hashlibsha1binascii
b2a_base64digestr   )base_stringclient_secrettoken_secrettextkey	signaturesigs          r"   hmac_sha1_signaturerS      s    " D $"
%C 3JC
 6,$"%%C#EI 

i..0
1#2
6Cc?r$   c                     SSK Jn  [        U 5      n U" [        U 5      U5      n[        R                  " U5      SS n[        U5      $ )aN  Generate signature via RSA-SHA1 method, per `Section 3.4.3`_.

The "RSA-SHA1" signature method uses the RSASSA-PKCS1-v1_5 signature
algorithm as defined in `RFC3447, Section 8.2`_ (also known as
PKCS#1), using SHA-1 as the hash function for EMSA-PKCS1-v1_5.  To
use this method, the client MUST have established client credentials
with the server that included its RSA public key (in a manner that is
beyond the scope of this specification).

.. _`Section 3.4.3`: https://tools.ietf.org/html/rfc5849#section-3.4.3
.. _`RFC3447, Section 8.2`: https://tools.ietf.org/html/rfc3447#section-8.2
r   )	sign_sha1NrD   )rsarU   r   rI   rJ   r   )rL   rsa_private_keyrU   srR   s        r"   rsa_sha1_signaturerY   +  sC     ;'K(;'9A


a
 "
%Cc?r$   c                 f    [        U =(       d    S5      nUS-  nU[        U=(       d    S5      -  nU$ )a  Generate signature via PLAINTEXT method, per `Section 3.4.4`_.

The "PLAINTEXT" method does not employ a signature algorithm.  It
MUST be used with a transport-layer mechanism such as TLS or SSL (or
sent over a secure channel with equivalent protections).  It does not
utilize the signature base string or the "oauth_timestamp" and
"oauth_nonce" parameters.

.. _`Section 3.4.4`: https://tools.ietf.org/html/rfc5849#section-3.4.4
r,   r   r   )rM   rN   rQ   s      r"   plaintext_signaturer[   @  s;    " }*+I I
 *++Ir$   c                 X    [        U5      n[        X R                  U R                  5      $ )zSign a HMAC-SHA1 signature.)rB   rS   rM   rN   clientrA   rL   s      r"   sign_hmac_sha1r_   _  s%    09K{,@,@&BUBUVVr$   c                 B    [        U5      n[        X R                  5      $ )z4Sign a RSASSA-PKCS #1 v1.5 base64 encoded signature.)rB   rY   rsa_keyr]   s      r"   sign_rsa_sha1rb   e  s    09Kk>>::r$   c                 B    [        U R                  U R                  5      $ )zSign a PLAINTEXT signature.)r[   rM   rN   )r^   rA   s     r"   sign_plaintextrd   k  s    v33V5H5HIIr$   c                     [        U 5      n[        XR                  U R                  5      n[        R
                  " X R                  5      $ )zVerify a HMAC-SHA1 signature.)rB   rS   rM   rN   rE   compare_digestrQ   )rA   rL   rR   s      r"   verify_hmac_sha1rg   p  s;    09K
k+@+@'BVBV
WCs$5$566r$   c                     SSK Jn  [        U 5      n[        R                  " [        U R                  5      5      nU" U[        U5      U R                  5      $ )z6Verify a RSASSA-PKCS #1 v1.5 base64 encoded signature.r   )verify_sha1)rV   ri   rB   rI   
a2b_base64r   rQ   rsa_public_key)rA   ri   rL   rR   s       r"   verify_rsa_sha1rl   w  sD     09K


hw'8'89
:CsH[173I3IJJr$   c                     [        U R                  U R                  5      n[        R                  " XR
                  5      $ )zVerify a PLAINTEXT signature.)r[   rM   rN   rE   rf   rQ   )rA   rR   s     r"   verify_plaintextrn     s1    
g33W5I5I
JCs$5$566r$   )N)__doc__rI   rG   rE   authlib.common.encodingr   r   authlib.common.urlsr   utilr   r	   SIGNATURE_HMAC_SHA1SIGNATURE_RSA_SHA1SIGNATURE_PLAINTEXTSIGNATURE_TYPE_HEADERSIGNATURE_TYPE_QUERYSIGNATURE_TYPE_BODYr#   r   r   rB   rS   rY   r[   r_   rb   rd   rg   rl   rn    r$   r"   <module>rz      s       , . (  !  !     6rDGNX%vT*Z*>W;J
7K7r$   