o
    shu(                     @  sJ  d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	m
Z
 ddlmZ 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mZmZmZmZmZmZ dd
lZdd
lZg dZ dZ!d,ddZ"d-ddZ#d.ddZ$d/d0ddZ%d1ddZ&d2d!d"Z'G d#d$ d$eZ(ed%ed&Z)ed'd(d&Z*G d)d( d(ee)ef Z+G d*d+ d+e+e, Z-d
S )3zlVarious utility functions.

This module contains various functions used throughout the rest of the project.
    )annotations)urlsafe_b64decodeurlsafe_b64encode)default_backend)hmachashes)BytesIO)fieldsField)abstractmethod)UnionOptionalSequenceMappingDictAnyTypeVarHashableget_type_hintsoverloadTypeN)websafe_encodewebsafe_decodesha256hmac_sha256	bytes2int	int2bytes   databytesreturnc                 C  s$   t t  t }||  | S )ztProduces a SHA256 hash of the input.

    :param data: The input data to hash.
    :return: The resulting hash.
    )r   HashSHA256r   updatefinalize)r   h r&   F/var/www/html/env_mimamsha/lib/python3.10/site-packages/fido2/utils.pyr   F   s   
r   keyc                 C  s&   t | t t }|| | S )zPerforms an HMAC-SHA256 operation on the given data, using the given key.

    :param key: The key to use.
    :param data: The input data to hash.
    :return: The resulting hash.
    )r   HMACr   r"   r   r#   r$   )r(   r   r%   r&   r&   r'   r   Q   s   
r   valueintc                 C  s   t | dS )zParses an arbitrarily sized integer from a byte string.

    :param value: A byte string encoding a big endian unsigned integer.
    :return: The parsed int.
    big)r+   
from_bytes)r*   r&   r&   r'   r   ]   s   r   minlenc                 C  sX   g }| dkr| d| @  | dL } | dks| |  |dg|t|   tt|S )zEncodes an int as a byte string.

    :param value: The integer value to encode.
    :param minlen: An optional minimum length for the resulting byte string.
    :return: The value encoded as a big endian byte string.
          r   )appendextendlenr   reversed)r*   r/   bar&   r&   r'   r   f   s   
r   Union[str, bytes]c                 C  s@   t | tr| d} ntdt | dt|  d  7 } t| S )zDecodes a websafe-base64 encoded string.
    See: "Base 64 Encoding with URL and Filename Safe Alphabet" from Section 5
    in RFC4648 without padding.

    :param data: The input to decode.
    :return: The decoded bytes.
    asciizpCalling websafe_decode on a byte value is deprecated, and will no longer be allowed starting in python-fido2 2.0   =   )
isinstancestrencodewarningswarnDeprecationWarningr4   r   r   r&   r&   r'   r   v   s   
r   r<   c                 C  s   t | dddS )zEncodes a byte string into websafe-base64 encoding.

    :param data: The input to encode.
    :return: The encoded string.
    r9       r8   )r   replacedecoderA   r&   r&   r'   r      s   r   c                      s.   e Zd ZdZdddZdd fddZ  ZS )
ByteBufferz6BytesIO-like object with the ability to unpack values.fmtr<   c                 C  s    t |}|| |jd S )zReads and unpacks a value from the buffer.

        :param fmt: A struct format string yielding a single value.
        :return: The unpacked value.
        r   )structStructunpackreadsize)selfrF   sr&   r&   r'   rI      s   
zByteBuffer.unpackr.   rK   Optional[int]r    r   c                   s@   t  |}|dur|dkrt||krtd|t|f |S )zLike BytesIO.read(), but checks the number of bytes read and raises an error
        if fewer bytes were read than expected.
        Nr   z,Not enough data to read (need: %d, had: %d).)superrJ   r4   
ValueError)rL   rK   r   	__class__r&   r'   rJ      s   zByteBuffer.read)rF   r<   r.   )rK   rN   r    r   )__name__
__module____qualname____doc__rI   rJ   __classcell__r&   r&   rQ   r'   rE      s    
	rE   _T)bound_S_DataClassMappingc                   @  s~   e Zd ZdZdd Zeeddd	Zd
d Zdd Z	dd Z
edd ZeedddZeedddZedd ZdS ) r\   z7A data class with members also accessible as a Mapping.c                 C  s   t t| }|  t| di  t| D ]?}|| j| |< t| |j}|d urRz| 	||j |}t| |j| W q t
ttfyQ   td|j d| jj w qd S )N_field_keyszError parsing field  for )r   typeobject__setattr__r	   r]   _get_field_keygetattrname_parse_value	TypeErrorKeyErrorrP   rR   rT   )rL   hintsfr*   r&   r&   r'   __post_init__   s"   z_DataClassMapping.__post_init__fieldr
   r    rY   c                 C  s   t  N)NotImplementedError)clsrk   r&   r&   r'   rb      s   z _DataClassMapping._get_field_keyc                   s    fdd j  D S )Nc                 3  s(    | ]\}}t  |jd ur|V  qd S rl   )rc   rd   ).0kri   rL   r&   r'   	<genexpr>   s    z-_DataClassMapping.__iter__.<locals>.<genexpr>)r]   itemsrq   r&   rq   r'   __iter__   s   
z_DataClassMapping.__iter__c                 C  s   t tt| S rl   )r4   listiterrq   r&   r&   r'   __len__   s   z_DataClassMapping.__len__c                 C  s   | j | }t| |j}|d u rt||jd}|r||S t|tr-t|ts-t|S t|t	rBt
dd |D rBdd |D S |S )N	serializec                 s  s    | ]}t |tV  qd S rl   )r;   r   ro   vr&   r&   r'   rr      s    z0_DataClassMapping.__getitem__.<locals>.<genexpr>c                 S  s"   g | ]}t |tr|nt|qS r&   )r;   dictry   r&   r&   r'   
<listcomp>   s   " z1_DataClassMapping.__getitem__.<locals>.<listcomp>)r]   rc   rd   rg   metadatagetr;   r   r{   r   all)rL   r(   ri   r*   rx   r&   r&   r'   __getitem__   s   
z_DataClassMapping.__getitem__c                   s  t  krjd ztu st|r|W S W n	 ty"   Y nw ttdttr=tdd  fdd|D S ttdtt	r_t|t	r_td\ fdd|
 D S tdd }|rk||S td	d }|rw||S td
t| d )Nr   
__origin____args__c                   s   g | ]}  |qS r&   re   ry   )rn   tr&   r'   r|      s    z2_DataClassMapping._parse_value.<locals>.<listcomp>c                   s&   i | ]\}}  |  |qS r&   r   )ro   rp   rz   )rn   t_kt_vr&   r'   
<dictcomp>   s    z2_DataClassMapping._parse_value.<locals>.<dictcomp>	from_dict__call__zUnparseable value of type r^   )r   r   r   r;   rf   
issubclassrc   r`   r   r   rs   rP   r_   )rn   r   r*   r   wrapr&   )rn   r   r   r   r'   re      s4   
z_DataClassMapping._parse_valuern   Type[_S]r   Nonec                 C     d S rl   r&   rn   r   r&   r&   r'   r   	     z_DataClassMapping.from_dictMapping[_T, Any]r[   c                 C  r   rl   r&   r   r&   r&   r'   r     r   c           	      C  s   |d u rd S t || r|S t |tst| j dt| i }t| }t| D ].}| |}||}|d u r9q(|j	d}|rF||}n||j
 }| ||}|||j
< q(| di |S )Nz/.from_dict called with non-Mapping data of typedeserializer&   )r;   r   rf   rT   r_   r   r	   rb   r~   r}   rd   re   )	rn   r   kwargsrh   ri   r(   r*   r   r   r&   r&   r'   r     s0   





N)rk   r
   r    rY   )rn   r   r   r   r    r   )rn   r   r   r   r    r[   )rT   rU   rV   rW   rj   classmethodr   rb   rt   rw   r   re   r   r   r&   r&   r&   r'   r\      s&    
&c                      s>   e Zd ZdZedddZ fdd	Ze fd
dZ  ZS )_JsonDataObjectzIA data class with members also accessible as a JSON-serializable Mapping.rk   r
   r    r<   c                 C  sD   |j d}|r
|S |jd}|d ddd |dd  D  S )Nrd   _r    c                 s  s    | ]}|  V  qd S rl   )title)ro   pr&   r&   r'   rr   8  s    z1_JsonDataObject._get_field_key.<locals>.<genexpr>   )r}   r~   rd   splitjoin)rn   rk   rd   partsr&   r&   r'   rb   2  s
   $z_JsonDataObject._get_field_keyc                   s"   t  |}t|trt|S |S rl   )rO   r   r;   r   r   )rL   r(   r*   rQ   r&   r'   r   :  s   
z_JsonDataObject.__getitem__c                   sP   t | |kr|jd }n|}t|tr!t|tr!t|tr!t|S t 	||S )Nr   )
r   r   r;   r_   r   r   r<   r   rO   re   )rn   r   r*   t2rQ   r&   r'   re   @  s   z_JsonDataObject._parse_value)rk   r
   r    r<   )	rT   rU   rV   rW   r   rb   r   re   rX   r&   r&   rQ   r'   r   /  s    r   )r   r   r    r   )r(   r   r   r   r    r   )r*   r   r    r+   rS   )r*   r+   r/   r+   r    r   )r   r7   r    r   )r   r   r    r<   ).rW   
__future__r   base64r   r   cryptography.hazmat.backendsr   cryptography.hazmat.primitivesr   r   ior   dataclassesr	   r
   abcr   typingr   r   r   r   r   r   r   r   r   r   r   rG   r>   __all__LOG_LEVEL_TRAFFICr   r   r   r   r   r   rE   rY   r[   r\   r<   r   r&   r&   r&   r'   <module>   s0   4



	

	