
    l h                        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y)z%Utilities for caching OCSP responses.    )annotations)
namedtuple)datetime)timezone)TYPE_CHECKINGAnyOptional)_create_lock)OCSPRequestOCSPResponsec                J    t        | d      r| j                  S | j                  S )z7Compat helper to return the response's next_update_utc.next_update_utc)hasattrr   next_updatevalues    x/var/www/html/SchoolMeal/SchoolMeal/pds_admin_SchoolMeal/Backend/venv/lib/python3.12/site-packages/pymongo/ocsp_cache.py_next_updater      '     u'($$$    c                J    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                  F    e Zd ZdZ edg d      Zd	dZd
dZddZddZ	y)
_OCSPCachezA cache for OCSP responses.OcspResponseCacheKeyhash_algorithmissuer_name_hashissuer_key_hashserial_numberc                0    i | _         t               | _        y )N)_datar
   _lock)selfs    r   __init__z_OCSPCache.__init__6   s    .0
!^
r   c                    | j                  |j                  j                  j                         |j                  |j
                  |j                        S )Nr   )CACHE_KEY_TYPEr   namelowerr    r!   r"   )r&   ocsp_requests     r   _get_cache_keyz_OCSPCache._get_cache_key;   sM    ""'66;;AAC)::(88&44	 # 
 	
r   c                ^   | j                   5  | j                  |      }t        |      }|&| j                  j	                  |d       	 ddd       yt        |      }|
	 ddd       yt        j                  t        j                        }|j                  |j                  d      }||cxk  r|k  sn 	 ddd       y| j                  j                  |d      }||| j                  |<   	 ddd       yt        |      }|||k  r|| j                  |<   ddd       y# 1 sw Y   yxY w)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   utcr2   replaceget)	r&   keyr   	cache_keyr   r   r5   cached_valuecached_next_updates	            r   __setitem__z_OCSPCache.__setitem__C   s*    ZZ 	.++C0I
 'u-K"

y$/	. 	. 'u-K"	. 	. --8<<0C!!)kkk.344)	. 	.0  ::>>)T:L#(-

9%7	. 	.8 ".l!;!-2D{2R(-

9%=	. 	. 	.s$   <D#D#)AD#.D#9!D##D,c                   | j                   5  | j                  |      }| j                  |   }t        |      }t	        |      }|J |J t        j                  t        j                        }|j                  |j                  d      }||cxk  r|k  rn n|cddd       S | j                  j                  |d       t        |      # 1 sw Y   yxY w)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/   r1   )r%   r-   r$   r   r   r4   r5   r   r6   r2   r7   r3   KeyError)r&   itemr:   r   r   r   r5   s          r   __getitem__z_OCSPCache.__getitem__k   s     ZZ 	&++D1IJJy)E 'u-K&u-K******--8<<0C!!)kkk.c/K/	& 	&  JJNN9d+9%%#	& 	&s   BC''CCN)returnNone)r,   r   rB   r)   )r9   r   r   r   rB   rC   )r@   r   rB   r   )
__name__
__module____qualname____doc__r   r)   r'   r-   r=   rA    r   r   r   r   .   s*    %RN
$

&.P&r   r   N)r   r   rB   zOptional[_datetime])rG   
__future__r   collectionsr   r   r4   r   typingr   r   r	   pymongo.lockr
   cryptography.x509.ocspr   r   r   r   r   rH   r   r   <module>rN      s;    , " " *  / / %@U& U&r   