
    _ie                        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	rddlmZmZ ddZddZ G 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                >    t          | d          r| j        S | j        S )z7Compat helper to return the response's next_update_utc.next_update_utc)hasattrr   next_updater   s    5D:\Nbitz\venv\Lib\site-packages\pymongo/ocsp_cache.py_next_updater      )     u'(( %$$    c                >    t          | d          r| j        S | j        S )z7Compat helper to return the response's this_update_utc.this_update_utc)r   r   this_updater   s    r   _this_updater   &   r   r   c                  N    e Zd ZdZ edg d          ZddZdd
ZddZddZ	dS )
_OCSPCachezA cache for OCSP responses.OcspResponseCacheKeyhash_algorithmissuer_name_hashissuer_key_hashserial_numberr   Nonec                :    i | _         t                      | _        d S )N)_datar
   _lock)selfs    r   __init__z_OCSPCache.__init__6   s    .0
!^^


r   ocsp_requestr   CACHE_KEY_TYPEc                    |                      |j        j                                        |j        |j        |j                  S )Nr    )r,   r!   namelowerr"   r#   r$   )r)   r+   s     r   _get_cache_keyz_OCSPCache._get_cache_key;   sF    ""'6;AACC):(8&4	 # 
 
 	
r   keyr   r   c                   | j         5  |                     |          }t          |          }|)| j                            |d           	 ddd           dS t          |          }|	 ddd           dS t          j        t          j	                  }|j
        |                    d          }||cxk    r|k     sn 	 ddd           dS | j                            |d          }||| j        |<   	 ddd           dS t          |          }|||k     r
|| j        |<   ddd           dS # 1 swxY w 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(   r0   r   r'   popr   	_datetimenowr   utcr6   replaceget)	r)   r1   r   	cache_keyr   r   r9   cached_valuecached_next_updates	            r   __setitem__z_OCSPCache.__setitem__C   s    Z 	. 	.++C00I
 'u--K"
y$///	. 	. 	. 	. 	. 	. 	. 	. 'u--K"	. 	. 	. 	. 	. 	. 	. 	. -8<000C!)kkk..344444444)	. 	. 	. 	. 	. 	. 	. 	.0  :>>)T::L#(-
9%7	. 	. 	. 	. 	. 	. 	. 	.8 ".l!;!;!-2D{2R2R(-
9%=	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.s+   AD3D37AD3(D3!D33D7:D7itemc                   | j         5  |                     |          }| j        |         }t          |          }t	          |          }|J |J t          j        t          j                  }|j	        |
                    d          }||cxk    r|k     rn n|cddd           S | j                            |d           t          |          # 1 swxY w 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.
        Nr3   r5   )r(   r0   r'   r   r   r8   r9   r   r:   r6   r;   r7   KeyError)r)   rA   r=   r   r   r   r9   s          r   __getitem__z_OCSPCache.__getitem__k   s>    Z 	& 	&++D11IJy)E 'u--K&u--K******-8<000C!)kkk..c////K/////	& 	& 	& 	& 	& 	& 	& 	&  JNN9d+++9%%%#	& 	& 	& 	& 	& 	& 	& 	& 	& 	&s   BC+*CCCN)r   r%   )r+   r   r   r,   )r1   r   r   r   r   r%   )rA   r   r   r   )
__name__
__module____qualname____doc__r   r,   r*   r0   r@   rD    r   r   r   r   .   s        %%ZRRR N
$ $ $ $

 
 
 
&. &. &. &.P& & & & & &r   r   N)r   r   r   r   )rH   
__future__r   collectionsr   r   r8   r   typingr   r   r	   pymongo.lockr
   cryptography.x509.ocspr   r   r   r   r   rI   r   r   <module>rO      s
   , + " " " " " " " " " " " " * * * * * *       / / / / / / / / / / % % % % % % A@@@@@@@@      U& U& U& U& U& U& U& U& U& U&r   