o
    C©Ì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   úa/var/www/html/Moveonwheels/move_on_wheels/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.Ú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 s�w   Y  dS )zàAdd/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   Úutcr0   ÚreplaceÚget)	r%   r,   r   Ú	cache_keyr   r   r3   Úcached_valueÚ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 )z Get 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   r2   r3   r   r4   r0   r5   r1   ÚKeyError)r%   r;   r7   r   r   r   r3   r   r   r   Ú__getitem__k   s    


òïz_OCSPCache.__getitem__N)r   r"   )r'   r
   r   r(   )r,   r
   r   r   r   r"   )r;   r
   r   r   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r(   r&   r+   r:   r=   r   r   r   r   r   .   s    þ


(r   N)r   r   r   r   )rA   Ú
__future__r   Úcollectionsr   r   r2   r   Útypingr   r   r   Úpymongo.lockr	   Úcryptography.x509.ocspr
   r   r   r   r   r   r   r   r   Ú<module>   s   

