o
    i                     @   s   d Z ddlZddlZddlZddlZz
ddlmZmZ W n ey-   ddl	mZmZ Y nw ddl
mZmZ eeZG dd dejZdS )zXGeneric functions and types for working with a TokenCache that is not platform specific.    N   )CrossPlatLock	LockError)_mkdir_pPersistenceNotFoundc                       sD   e Zd ZdZd fdd	Zdd Zd fdd	Z fd	d
Z  ZS )PersistedTokenCachea  A token cache backed by a persistence layer, coordinated by a file lock,
    to sustain a certain level of multi-process concurrency for a desktop app.

    The scenario is that multiple instances of same desktop app
    (or even multiple different apps)
    create their own ``PersistedTokenCache`` instances,
    which are all backed by the same token cache file on disk
    (known as a persistence). The goal is to have Single Sign On (SSO).

    Each instance of ``PersistedTokenCache`` holds a snapshot of the token cache
    in memory.
    Each :func:`~find` call will
    automatically reload token cache from the persistence when necessary,
    so that it will have fresh data.
    Each :func:`~modify` call will
    automatically reload token cache from the persistence when necessary,
    so that new writes will be appended on top of latest token cache data,
    and then the new data will be immediately flushed back to the persistence.

    Note: :func:`~deserialize` and :func:`~serialize` remain the same
    as their counterparts in the parent class ``msal.SerializableTokenCache``.
    In other words, they do not have the "reload from persistence if necessary"
    nor the "flush back to persistence" behavior.
    Nc                    sV   t t|   |rtj|n| d | _ttj	| j || _
d| _|j| _d S )Nz	.lockfiler   )superr   __init__ospath
expanduserZget_location_lock_locationr   dirname_persistence
_last_syncis_encrypted)selfpersistenceZlock_location	__class__ R/home/app/Keep/.python/lib/python3.10/site-packages/msal_extensions/token_cache.pyr	   .   s   
zPersistedTokenCache.__init__c                 C   sL   z| j | j k r| | j  t | _ W dS W dS  ty%   Y dS w )z1Reload cache from persistence layer, if necessaryN)r   r   Ztime_last_modifiedZdeserializeloadtimer   )r   r   r   r   _reload_if_necessary8   s   z(PersistedTokenCache._reload_if_necessaryc                    sh   t | j% |   tt| j|||d | j|   t		 | _
W d    d S 1 s-w   Y  d S )N)new_key_value_pairs)r   r   r   r   r   modifyr   save	serializer   r   )r   credential_typeZ	old_entryr   r   r   r   r   D   s   
"zPersistedTokenCache.modifyc              	      sx   d}t d|d D ]0}z|   W n ty+   ||k r(td| td n Y q	w tt| j	|fi |  S g S )N   r   z1Unable to load token cache file in No. %d attemptg      ?)
ranger   	Exceptionloggerdebugr   sleepr   r   search)r   r   kwargsretryattemptr   r   r   r&   N   s   zPersistedTokenCache.search)N)	__name__
__module____qualname____doc__r	   r   r   r&   __classcell__r   r   r   r   r      s    

r   )r-   r
   r   loggingZmsalZ
cache_lockr   r   ImportErrorZfilelockr   r   r   	getLoggerr*   r#   ZSerializableTokenCacher   r   r   r   r   <module>   s    
