U
    d                     @  sz   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	rhddlmZmZ G d	d
 d
ZdS )z%Utilities for caching OCSP responses.    )annotations)
namedtuple)datetime)timezone)TYPE_CHECKINGAnyDict)_create_lock)OCSPRequestOCSPResponsec                   @  sb   e Zd ZdZedddddgZddd	d
ZdddddZddddddZdddddZ	dS )
_OCSPCachezA cache for OCSP responses.ZOcspResponseCacheKeyhash_algorithmissuer_name_hashissuer_key_hashserial_numberNone)returnc                 C  s   i | _ t | _d S )N)_datar	   _lock)self r   6/tmp/pip-unpacked-wheel-oblwsawz/pymongo/ocsp_cache.py__init__&   s    z_OCSPCache.__init__r
   CACHE_KEY_TYPE)ocsp_requestr   c                 C  s    | j |jj |j|j|jdS )N)r   r   r   r   )r   r   namelowerr   r   r   )r   r   r   r   r   _get_cache_key+   s    
z_OCSPCache._get_cache_keyr   )keyvaluer   c              	   C  s   | j  | |}|jdkr8| j|d W 5 Q R  dS |jtjtj	dj
dd  krb|jk srn W 5 Q R  dS | j|d}|dks|jdk	r|j|jk r|| j|< W 5 Q R X 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   next_updater   popthis_update	_datetimenowr   utcreplaceget)r   r   r   	cache_keyZcached_valuer   r   r   __setitem__3   s$    


z_OCSPCache.__setitem__)itemr   c              
   C  s   | j  | |}| j| }|jdk	s*t|jdk	s8t|jtjtj	dj
dd  krb|jk rvn n|W  5 Q R  S | j|d t|W 5 Q R X 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&   AssertionErrorr$   r'   r(   r   r)   r*   r%   KeyError)r   r.   r,   r   r   r   r   __getitem__V   s    


z_OCSPCache.__getitem__N)
__name__
__module____qualname____doc__r   r   r   r   r-   r1   r   r   r   r   r      s   
#r   N)r5   
__future__r   collectionsr   r   r'   r   typingr   r   r   Zpymongo.lockr	   Zcryptography.x509.ocspr
   r   r   r   r   r   r   <module>   s   