
    kKj(+                        d dl mZmZ d dlmZ ddlmZmZmZm	Z	m
Z
 ddlmZmZmZ ddlmZmZmZ ddlmZmZmZmZmZmZ ddlmZmZmZ dd	lmZ dd
l m!Z!m"Z# ddl$m%Z%m&Z' ddl(m)Z)m*Z* ddl+m,Z,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4 g dZ5 e0        	 	 d*de4de6e7z  de-dz  dee7   dz  dedz  de7fdZ8	 	 d*dede-dz  dee7   dz  dedz  de9f
dZ:	 	 	 d+de6e7z  de-dz  dee7   dz  dedz  de6e7z  dz  defdZ;e	 	 d*de<e   de6e7z  de-dee7   dz  dedz  defd        Z=e	 	 d*dede6e7z  de-dee7   dz  dedz  defd!       Z=	 	 d*dee<e   z  de6e7z  de-dee7   dz  dedz  deez  fd"Z=e	 	 d*dede-dee7   dz  dedz  de	f
d#       Z>e	 	 d*dede-dee7   dz  dedz  de
f
d$       Z>	 	 d*deez  de-dee7   dz  dedz  de	e
z  f
d%Z>ede7de7fd&       Z?edede@e7ef   fd'       Z?edede@e7ef   fd(       Z?dedefd)Z?y),    )overloadAny)
Collection   )JWSAlgModelHeaderMemberCompactSignatureGeneralJSONSignatureFlattenedJSONSignature)JWSRegistryconstruct_registrydefault_registry)sign_compactverify_compactdetach_compact_content)sign_general_jsonsign_flattened_jsonverify_general_jsonverify_flattened_jsonextract_general_jsondetach_json_content)
HeaderDictGeneralJSONSerializationFlattenedJSONSerialization)is_rfc7797_enabled)sign_rfc7515_compactextract_rfc7515_compact)sign_rfc7797_jsonextract_rfc7797_json)BadSignatureErrorMissingKeyError)KeyKeyFlexible	guess_key)setup_jws_algorithms)to_bytes)Header)r   r   r   r   r   r	   r
   r   serialize_compactdeserialize_compactextract_compactvalidate_compactserialize_jsondeserialize_jsondetach_contentr   N	protectedpayloadprivate_key
algorithmsregistryreturnc                 |   |t        |      }|j                  |        t        |       }t        | t	        |            }|j                  | d         }d}|j                  dk7  r,|
t               t        ||dd      }|j                  |       |rt        |||      }	nt        |||      }	|	j                  d      S )a  Generate a JWS Compact Serialization. The JWS Compact Serialization
    represents digitally signed or MACed content as a compact, URL-safe
    string, per Section 7.1.

    .. code-block:: text

        BASE64URL(UTF8(JWS Protected Header)) || '.' ||
        BASE64URL(JWS Payload) || '.' ||
        BASE64URL(JWS Signature)

    :param protected: protected header part of the JWS, in dict
    :param payload: payload data of the JWS, in bytes
    :param private_key: a flexible private key to sign the signature
    :param algorithms: a collection (list, tuple, or set) of allowed algorithms
    :param registry: a JWSRegistry to use
    :return: JWS in str
    NalgnoneTsigusezutf-8)r   check_headerr   r	   r&   get_algnamer!   r$   	check_keyr   r   decode)
r/   r0   r1   r2   r3   
is_rfc7797objr6   keyouts
             \/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/joserfc/jws.pyr(   r(   J   s    0 %j1)$#I.J
9hw&7
8C''	%(89C C
xx6!##S$E:c"3S13S)::g    rA   
public_keyc                     |t        |      }| j                         }|j                  |       |j                  |d         }|d   dk(  rt	        | |d      S |
t               t        || d      }|j                  |       t	        | ||      S )az  Validate the JWS Compact Serialization with the given key.
    This method is usually used together with ``extract_compact``.

    :param obj: object of the JWS Compact Serialization
    :param public_key: a flexible public key to verify the signature
    :param algorithms: a collection (list, tuple, or set) of allowed algorithms
    :param registry: a JWSRegistry to use
    Nr6   r7   r8   r9   )r   headersr;   r<   r   r!   r$   r>   )rA   rF   r2   r3   rH   r6   rB   s          rD   r+   r+   {   s     %j1kkmG'"''7C u~c3--Se4CMM##sC((rE   valuec                 b    t        t        |       ||      }t        ||||      s
t               |S )a  Extract and validate the JWS Compact Serialization (in string, or bytes)
    with the given key. An JWE Compact Serialization looks like:

    .. code-block:: text
        :caption: line breaks for display purposes only

        eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9
        .
        eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt
        cGxlLmNvbS9pc19yb290Ijp0cnVlfQ
        .
        dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk

    :param value: a string (or bytes) of the JWS Compact Serialization
    :param public_key: a flexible public key to verify the signature
    :param algorithms: a collection (list, tuple, or set) of allowed algorithms
    :param registry: a JWSRegistry to use
    :param payload: optional payload, required with detached content
    :raises BadSignatureError: when signature verification fails
    :return: object of the CompactSignature
    )r*   r&   r+   r    )rI   rF   r2   r3   r0   rA   s         rD   r)   r)      s2    8 (5/7H
=CCZB!!JrE   membersc                      y N rK   r0   r1   r2   r3   s        rD   r,   r,      s      #rE   c                      y rM   rN   rO   s        rD   r,   r,      s     "%rE   c                 N   |t        |      }dt        dt        ffd}t        |      }t	        | t
              r&| D cg c]  }t        di | }}t        ||||      S t        di | }t        |j                               rt        ||||      S t        ||||      S c c}w )a|  Generate a JWS JSON Serialization (in dict). The JWS JSON Serialization
    represents digitally signed or MACed content as a JSON object. This representation
    is neither optimized for compactness nor URL-safe.

    A general JWS JSON Serialization contains:

    payload
        The "payload" member MUST be present and contain the value
        BASE64URL(JWS Payload).

    signatures
        The "signatures" member value MUST be an array of JSON objects.
        Each object represents a signature or MAC over the JWS Payload and
        the JWS Protected Header.

    A flatten JWS JSON Serialization looks like:

    .. code-block:: text

        {
            "payload":"<payload contents>",
            "protected":"<integrity-protected header contents>",
            "header":<non-integrity-protected header contents>,
            "signature":"<signature contents>"
        }
    rA   r4   c                 "    t        | dd      S )NTr8   r9   r$   )rA   r1   s    rD   find_keyz serialize_json.<locals>.find_key   s    c4U;;rE   rN   )r   r   r"   r&   
isinstancelistr   r   rH   r   r   )	rK   r0   r1   r2   r3   rT   _payloadmember_memberss	     `      rD   r,   r,      s    B %j1<l <s <  H'4 9@AvL*6*A 8XxHH((fnn./$VXxJJ"68XxHH Bs    B"c                      y rM   rN   rI   rF   r2   r3   s       rD   r-   r-     s     rE   c                      y rM   rN   r[   s       rD   r-   r-     s     !rE   c                     |t        |      }dt        dt        ffd}d| v r%t        | |      }t	        |||      s
t               |S t        | |      }t        |||      s
t               |S )a  Extract and validate the JWS (in string) with the given key.

    :param value: a dict of the JSON signature
    :param public_key: a flexible public key to verify the signature
    :param algorithms: a collection (list, tuple, or set) of allowed algorithms
    :param registry: a JWSRegistry to use
    :return: object of GeneralJSONSignature or FlattenedJSONSignature
    :raises BadSignatureError: when signature verification fails
    rA   r4   c                      t        | d      S )Nr8   r9   rS   )rA   rF   s    rD   rT   z"deserialize_json.<locals>.find_key(  s    Se44rE   
signatures)r   r   r"   r   r   r    extract_flattened_jsonr   )rI   rF   r2   r3   rT   general_objflattened_objs    `     rD   r-   r-     s     %j15l 5s 5 u*5(;";(C#%%.uh?$]HhG#%%rE   c                      y rM   rN   rI   s    rD   r.   r.   7  s    '*rE   c                      y rM   rN   rd   s    rD   r.   r.   ;  s    GJrE   c                      y rM   rN   rd   s    rD   r.   r.   ?  s    ILrE   c                 N    t        | t              rt        |       S t        |       S )a  In some contexts, it is useful to integrity-protect content that is
    not itself contained in a JWS. This method is an implementation of
    https://www.rfc-editor.org/rfc/rfc7515#appendix-F

    It is used to detach the content of the compact and JSON serialization.

    .. code-block:: python

        >>> from joserfc import jws
        >>> from joserfc.jwk import OctKey
        >>> key = OctKey.import_key("secret")
        >>> encoded_text = jws.serialize_compact({"alg": "HS256"}, b"hello", key)
        >>> jws.detach_content(encoded_text)
        'eyJhbGciOiJIUzI1NiJ9..UYmO_lPAY5V0Wf4KZsfhiYs1SxqXPhxvjuYqellDV5A'

    You can also detach the JSON serialization:

    .. code-block:: python

        >>> obj = jws.serialize_json({"protected": {"alg": "HS256"}}, b"hello", key)
        >>> jws.detach_content(obj)
        {
            'payload': '',
            'signature': 'UYmO_lPAY5V0Wf4KZsfhiYs1SxqXPhxvjuYqellDV5A',
            'protected': 'eyJhbGciOiJIUzI1NiJ9'
        }
    )rU   strr   r   rd   s    rD   r.   r.   C  s$    8 %%e,,u%%rE   )NN)NNN)Atypingr   r   collections.abcr   _rfc7515.modelr   r   r	   r
   r   _rfc7515.registryr   r   r   _rfc7515.compactr   r   r   _rfc7515.jsonr   r   r   r   r   r   _rfc7515.typesr   r   r   _rfc7797.utilr   _rfc7797.compactr   r   r*   _rfc7797.jsonr   r   r`   errorsr    r!   jwkr"   r#   r$   jwar%   utilr&   r3   r'   __all__bytesrh   r(   boolr+   r)   rV   r,   r-   r.   dictrN   rE   rD   <module>r{      s=     &  
 
  
 . 7 , , %  .   *.#'..S[. t#. 3$&	.
 D . 	.h *.#'	)	)d") 3$&) D 	)
 
)H *.#'"&3;d" 3$& D 	
 S[4 D 

 *.#'#*#S[# # 3$&	#
 D # # 
# 

 *.#'%%S[% % 3$&	%
 D %  % 
% *.#'/I$z**/IS[/I /I 3$&	/I
 D /I  ::/Id 
 *.#'	# 3$& D 	
  
 
 *.#'	!%!! 3$&! D 	!
 ! 
! *.#'	#&@@ 3$& D 	
 22B 
 *# *# * 
 * 
 J2 JtCH~ J 
 J 
 L4 Lc3h L 
 L&# &# &rE   