o
    ÿÑÐbO7  ã                   @   sÐ   d Z ddlZddlZddlZddlmZ ddlmZmZ ddl	m
Z
mZ dZdZd	Zd
ZdZdZd'dd„Zd'dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ ZdS )(zå
    authlib.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)Úurlparse)Ú
to_unicodeÚto_bytesé   )ÚescapeÚunescapez	HMAC-SHA1zRSA-SHA1Z	PLAINTEXTÚHEADERZQUERYZBODYc           	      C   sn   t ||ƒ}g }|D ]\}}|dv rq	| d¡rt|ƒ}| ||f¡ q	t|ƒ}d t|  ¡ ƒt|ƒt|ƒg¡S )aX  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
    )Zoauth_signatureÚrealmZoauth_ú&)Únormalize_base_string_uriÚ
startswithr   ÚappendÚnormalize_parametersÚjoinr   Úupper)	ÚmethodÚuriÚparamsÚhostZbase_string_uriZunescaped_paramsÚkÚvZnormalized_params© r   ú_/var/www/secure340b-portal/env/lib/python3.10/site-packages/authlib/oauth1/rfc5849/signature.pyÚconstruct_base_string   s   


ýr   c           
      C   s˜   t | ƒ} t | ¡\}}}}}}|r|stdƒ‚|sd}| ¡ }| ¡ }|dur+| ¡ }d}d|v rA| dd¡\}}	||	f|v rA|}t ||||ddf¡S )a7  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ú/N))ÚhttpÚ80)ÚhttpsÚ443ú:r   Ú )r   r   Ú
ValueErrorÚlowerÚsplitÚ
urlunparse)
r   r   ÚschemeÚnetlocÚpathr   ÚqueryÚfragmentZdefault_portsÚportr   r   r   r   R   s    
r   c                 C   s.   dd„ | D ƒ}|  ¡  dd„ |D ƒ}d |¡S )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
    c                 S   s    g | ]\}}t |ƒt |ƒf‘qS r   ©r   ©Ú.0r   r   r   r   r   Ú
<listcomp>ã   s     z(normalize_parameters.<locals>.<listcomp>c                 S   s   g | ]
\}}d   ||¡‘qS )z{0}={1})Úformatr,   r   r   r   r.   í   s    r
   )Úsortr   )r   Ú
key_valuesZparameter_partsr   r   r   r   ™   s   J
r   c                 C   s"   | j  dd¡}t| j| j| j|ƒS )z,Generate signature base string from request.ÚHostN)ÚheadersÚgetr   r   r   r   )Úrequestr   r   r   r   Úgenerate_signature_base_stringõ   s   ÿr6   c                 C   s^   | }t |pdƒ}|d7 }|t |pdƒ7 }t t|ƒt|ƒtj¡}t | ¡ ¡dd… }t	|ƒS )aZ  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Úsigr   r   r   Úhmac_sha1_signatureü   s   rF   c                 C   s<   ddl m} t| ƒ} |t| ƒ|ƒ}t |¡dd… }t|ƒS )ar  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_sha1Nr7   )ÚrsarG   r   r<   r=   r   )r?   Zrsa_private_keyrG   ÚsrE   r   r   r   Úrsa_sha1_signature*  s
   rJ   c                 C   s(   t | pdƒ}|d7 }|t |pdƒ7 }|S )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+   )r@   rA   rD   r   r   r   Úplaintext_signature>  s   rK   c                 C   s   t |ƒ}t|| j| jƒS )zSign a HMAC-SHA1 signature.)r6   rF   r@   rA   ©Úclientr5   r?   r   r   r   Úsign_hmac_sha1^  s   
ÿrN   c                 C   s   t |ƒ}t|| jƒS )z4Sign a RSASSA-PKCS #1 v1.5 base64 encoded signature.)r6   rJ   Zrsa_keyrL   r   r   r   Úsign_rsa_sha1e  s   rO   c                 C   s   t | j| jƒS )zSign a PLAINTEXT signature.)rK   r@   rA   )rM   r5   r   r   r   Úsign_plaintextk  s   rP   c                 C   s&   t | ƒ}t|| j| jƒ}t || j¡S )zVerify a HMAC-SHA1 signature.)r6   rF   r@   rA   r8   Úcompare_digestrD   )r5   r?   rE   r   r   r   Úverify_hmac_sha1p  s
   
ÿrR   c                 C   s6   ddl m} t| ƒ}t t| jƒ¡}||t|ƒ| jƒS )z6Verify a RSASSA-PKCS #1 v1.5 base64 encoded signature.r   )Úverify_sha1)rH   rS   r6   r<   Ú
a2b_base64r   rD   Zrsa_public_key)r5   rS   r?   rE   r   r   r   Úverify_rsa_sha1x  s   rU   c                 C   s   t | j| jƒ}t || j¡S )zVerify a PLAINTEXT signature.)rK   r@   rA   r8   rQ   rD   )r5   rE   r   r   r   Úverify_plaintext€  s   rV   )N)Ú__doc__r<   r:   r8   Zauthlib.common.urlsr   Zauthlib.common.encodingr   r   Úutilr   r   ZSIGNATURE_HMAC_SHA1ZSIGNATURE_RSA_SHA1ZSIGNATURE_PLAINTEXTZSIGNATURE_TYPE_HEADERZSIGNATURE_TYPE_QUERYZSIGNATURE_TYPE_BODYr   r   r   r6   rF   rJ   rK   rN   rO   rP   rR   rU   rV   r   r   r   r   Ú<module>   s4   

8G\. 