o
    h                     @  s   d 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 ddlmZ e	r4ddlmZmZ dddZdddZG dd dZdS )z%Utilities for caching OCSP responses.    )annotations)
namedtuple)datetime)timezone)TYPE_CHECKINGAnyOptional)_create_lock)OCSPRequestOCSPResponsevaluer   returnOptional[_datetime]c                 C     t | dr| jS | jS )z7Compat helper to return the response's next_update_utc.next_update_utc)hasattrr   next_updater    r   E/var/www/html/venv/lib/python3.10/site-packages/pymongo/ocsp_cache.py_next_update      
r   c                 C  r   )z7Compat helper to return the response's this_update_utc.this_update_utc)r   r   this_updater   r   r   r   _this_update&   r   r   c                   @  sF   e Zd ZdZedg dZdddZdddZdddZdddZ	dS )
_OCSPCachezA cache for OCSP responses.ZOcspResponseCacheKeyhash_algorithmissuer_name_hashissuer_key_hashserial_numberr   Nonec                 C  s   i | _ t | _d S )N)_datar	   _lock)selfr   r   r   __init__6   s   z_OCSPCache.__init__ocsp_requestr
   CACHE_KEY_TYPEc                 C  s    | j |jj |j|j|jdS )Nr   )r'   r   namelowerr   r   r    )r$   r&   r   r   r   _get_cache_key;   s   
z_OCSPCache._get_cache_keykeyr   r   c           	      C  sH  | j  | |}t|}|du r!| j|d 	 W d   dS t|}|du r2	 W d   dS tjtj	d}|j
du rD|jdd}||  krN|k sXn 	 W d   dS | j|d}|du rq|| j|< 	 W d   dS t|}|dur||k r|| j|< W d   dS W d   dS W d   dS 1 sw   Y  dS )zAdd/update a cache entry.

        'key' is of type cryptography.x509.ocsp.OCSPRequest
        'value' is of type cryptography.x509.ocsp.OCSPResponse

        Validity of the OCSP response must be checked by caller.
        Ntztzinfo)r#   r*   r   r"   popr   	_datetimenowr   utcr/   replaceget)	r$   r+   r   	cache_keyr   r   r2   Zcached_valueZcached_next_updater   r   r   __setitem__C   s<   


"z_OCSPCache.__setitem__itemc                 C  s   | j Q | |}| j| }t|}t|}|dusJ |dus"J tjtjd}|j	du r4|j
dd}||  kr>|k rIn n	|W  d   S | j|d t|1 sWw   Y  dS )zGet a cache entry if it exists.

        'item' is of type cryptography.x509.ocsp.OCSPRequest

        Raises KeyError if the item is not in the cache.
        Nr,   r.   )r#   r*   r"   r   r   r1   r2   r   r3   r/   r4   r0   KeyError)r$   r8   r6   r   r   r   r2   r   r   r   __getitem__k   s    


z_OCSPCache.__getitem__N)r   r!   )r&   r
   r   r'   )r+   r
   r   r   r   r!   )r8   r
   r   r   )
__name__
__module____qualname____doc__r   r'   r%   r*   r7   r:   r   r   r   r   r   .   s    


(r   N)r   r   r   r   )r>   
__future__r   collectionsr   r   r1   r   typingr   r   r   Zpymongo.lockr	   Zcryptography.x509.ocspr
   r   r   r   r   r   r   r   r   <module>   s   

