
    kKj                         d dl Z d dlm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mZ dd	lmZmZmZmZmZmZ dd
lmZ g dZ G d d      Z e       Zddee   dz  defdZy)    N)Any)
Collection)Enum   )JWSAlgModel   )	JoseErrorUnsupportedAlgorithmErrorSecurityWarningExceededSizeError)JWS_HEADER_REGISTRYHeaderHeaderRegistryDictcheck_registry_headercheck_crit_headercheck_supported_header)KeySet)JWSRegistryconstruct_registrydefault_registryc            	          e Zd ZU dZ G d de      ZeZee	d<   i Z
eeef   e	d<   g Zee   e	d<   dZee	d<   d	Zee	d
<   dZee	d<   	 	 	 d#dedz  dee   dz  defdZededdfd       ZdedefdZdeddfdZdeddfdZdeddfdZdeddfdZede dededz  fd       Z!ede dededz  fd        Z"ed$de d!ee   dz  dee   fd"       Z#y)%r   aq  A registry for JSON Web Signature to keep all the supported algorithms.
    An instance of ``JWSRegistry`` is usually used together with methods in
    ``joserfc.jws``.

    :param header_registry: extra header parameters registry
    :param algorithms: allowed algorithms to be used
    :param strict_check_header: only allow header key in the registry to be used
    c                       e Zd ZdZdZy)JWSRegistry.Strategyr   r   N)__name__
__module____qualname__RECOMMENDEDSECURITY     j/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/joserfc/_rfc7515/registry.pyStrategyr   '   s    r    r"   default_header_registry
algorithmsrecommendedi   max_header_lengthi  max_payload_lengthi   max_signature_lengthNheader_registrystrict_check_headerc                     i | _         | j                   j                  | j                         || j                   j                  |       || _        || _        y N)r)   updater#   allowedr*   )selfr)   r$   r*   s       r!   __init__zJWSRegistry.__init__8   sO     46##D$@$@A&  ''8!#6 r    algreturnc                     || j                   |j                  <   |j                  r&| j                  j                  |j                         yy)z8Register a given JWS algorithm instance to the registry.N)r$   namer%   append)clsr1   s     r!   registerzJWSRegistry.registerE   s8     $'sxx ??OO""388, r    r4   c                 J   || j                   vrt        d| d      | j                  r|| j                  vr,t        d| d      || j                  vrt        d| d      | j                   |   }|j                  r$t        j                  |j                  t               |S )zGet the allowed algorithm instance of the given name.

        :param name: value of the ``alg``, e.g. ``HS256``, ``RS256``
        zAlgorithm of 'z' is not supportedz' is not allowedz' is not recommended)r$   r
   r.   r%   security_warningwarningswarnr   )r/   r4   r1   s      r!   get_algzJWSRegistry.get_algL   s    
 t&+nTFBT,UVV<<4<<'/.FV0WXX4+++/.FZ0[\\ood#MM#..@
r    headerc                     t        | j                  |       t        | j                  |       | j                  rt	        | j                  |       yy)z=Check and validate the fields in header part of a JWS object.N)r   r)   r   r*   r   r/   r=   s     r!   check_headerzJWSRegistry.check_header`   s@    $..7d22F;##"4#7#7@ $r    c                 l    |r2t        |      | j                  kD  rt        d| j                   d      y y )NzHeader size exceeds  bytes.)lenr&   r   r?   s     r!   validate_header_sizez JWSRegistry.validate_header_sizeg   s:    c&kD$:$::#&:4;Q;Q:RRY$Z[[ ;6r    payloadc                 l    |r2t        |      | j                  kD  rt        d| j                   d      y y )NzPayload size exceeds rB   )rC   r'   r   )r/   rE   s     r!   validate_payload_sizez!JWSRegistry.validate_payload_sizek   s:    s7|d&=&==#&;D<S<S;TT[$\]] >7r    	signaturec                 f    t        |      | j                  kD  rt        d| j                   d      y )NzSignature of exceeds rB   )rC   r(   r   )r/   rH   s     r!   validate_signature_sizez#JWSRegistry.validate_signature_sizeo   s6    y>D555#&;D<U<U;VV]$^__ 6r    keystrategyc                 b   || j                   j                  k(  r| j                  || j                        }nr|| j                   j                  k(  rJt        | j                  j                               }| j                  ||      }|j                  d d       nt        d| d      |r|d   S y)zGuess the JWS algorithm for a given key.

        :param key: key instance or a KeySet
        :param strategy: the strategy for guessing the JWS algorithm
        c                     | j                   S r,   )algorithm_security)r1   s    r!   <lambda>z-JWSRegistry.guess_algorithm.<locals>.<lambda>   s
    C,B,Br    T)rK   reversezUnknown algorithm strategy ''r   N)
r"   r   filter_algorithmsr%   r   listr$   keyssortNotImplementedError)r6   rK   rL   r$   namess        r!   guess_algorithmzJWSRegistry.guess_algorithms   s     s||///..sCOODJ...,,./E..sE:JOO BDOQ%(DXJa&PQQa= r    c                 x    t        j                  dt               | j                  ||      }|r|j                  S y )Nz)Please use guess_algorithm(key, strategy))r:   r;   DeprecationWarningrY   r4   )r6   rK   rL   r1   s       r!   	guess_algzJWSRegistry.guess_alg   s3    ACUV!!#x088Or    rX   c                    |#t        | j                  j                               }g }t        |t              r@|j                  D ]/  }| j                  ||      D ]  }||vs|j                  |        1 |S |D ]4  }| j                  |   }	 |j                  |       |j                  |       6 |S # t        $ r Y Dw xY w)zFilter JWS algorithms based on the given algorithm names.

        :param key: a key instance or a KeySet
        :param names: list of algorithm names
        )	rT   r$   rU   
isinstancer   rS   r5   	check_keyr	   )r6   rK   rX   rvkr1   r4   s          r!   rS   zJWSRegistry.filter_algorithms   s     =,,./E "c6"XX00E:C"}		# ;  ID..&Cc"		#	  	  s   "B33	B?>B?)NNTr,   )$r   r   r   __doc__r   r"   r   r#   r   __annotations__r$   dictstrr   r%   rT   r&   intr'   r(   r   boolr0   classmethodr7   r<   r   r@   bytesrD   rG   rJ   r   rY   r\   rS   r   r    r!   r   r      s   4  3F/E)+JS+%&+Kc !s $$ $#$ 6:-1$(	7+d27 sOd*7 "	7 -; -4 - -C K (A6 Ad A\5 \T \^U ^t ^` `4 ` #  kD>P  * C 8 d
   C S	D0@ DQ\L]  r    r   r$   r2   c                 2    | rt        |       }|S t        }|S )N)r$   )r   r   )r$   registrys     r!   r   r      s"    *5 O $Or    r,   )r:   typingr   collections.abcr   enumr   modelr   errorsr	   r
   r   r   rk   r   r   r   r   r   r   _keysr   __all__r   r   re   r   r   r    r!   <module>rs      sg      &     L L` = :c?T#9 [ r    