o
    ãË·e  ã                   @   sD   d Z ddlmZ ddlZddlZG dd„ deƒZG dd„ deƒZdS )z,A simple store using only in-process memory.é    )ÚnonceNc                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚServerAssocsc                 C   s
   i | _ d S ©N)Úassocs©Úself© r   úL/var/www/ideatree/venv/lib/python3.10/site-packages/openid/store/memstore.pyÚ__init__
   ó   
zServerAssocs.__init__c                 C   s   || j |j< d S r   )r   Úhandle)r   Úassocr   r   r	   Úset   s   zServerAssocs.setc                 C   s   | j  |¡S r   )r   Úget©r   r   r   r   r	   r      s   zServerAssocs.getc                 C   s$   z| j |= W dS  ty   Y dS w ©NFT)r   ÚKeyErrorr   r   r   r	   Úremove   s   
ýÿzServerAssocs.removec                 C   s4   d}t | j ¡ ƒD ]}|du s|j|jk r|}q	|S )zhReturns association with the oldest issued date.

        or None if there are no associations.
        N)Úlistr   ÚvaluesÚissued)r   Úbestr   r   r   r	   r      s   €zServerAssocs.bestc                 C   sP   g }| j  ¡ D ]\}}|jdkr| |¡ q|D ]}| j |= qt|ƒt| j ƒfS )zoRemove expired associations.

        @return: tuple of (removed associations, remaining associations)
        r   )r   ÚitemsÚ	expiresInÚappendÚlen)r   r   r   r   r   r   r	   Úcleanup&   s   

€
zServerAssocs.cleanupN)	Ú__name__Ú
__module__Ú__qualname__r
   r   r   r   r   r   r   r   r   r	   r   	   s    r   c                   @   sb   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	d
„Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ ZdS )ÚMemoryStorezcIn-process memory store.

    Use for single long-running processes.  No persistence supplied.
    c                 C   s   i | _ i | _d S r   ©Úserver_assocsÚnoncesr   r   r   r	   r
   :   s   
zMemoryStore.__init__c                 C   s4   z| j | W S  ty   tƒ  }| j |< | Y S w r   )r"   r   r   )r   Ú
server_urlr   r   r   r	   Ú_getServerAssocs>   s   þzMemoryStore._getServerAssocsc                 C   s   |   |¡}| t |¡¡ d S r   )r%   r   ÚcopyÚdeepcopy)r   r$   r   r   r   r   r	   ÚstoreAssociationE   s   
zMemoryStore.storeAssociationNc                 C   s$   |   |¡}|d u r| ¡ S | |¡S r   )r%   r   r   ©r   r$   r   r   r   r   r	   ÚgetAssociationI   s   

zMemoryStore.getAssociationc                 C   s   |   |¡}| |¡S r   )r%   r   r)   r   r   r	   ÚremoveAssociationP   s   

zMemoryStore.removeAssociationc                 C   sL   t |t ¡  ƒtjkrdS t|ƒt|ƒt|ƒf}|| jv rdS d | j|< dS r   )ÚabsÚtimer   ÚSKEWÚstrÚintr#   )r   r$   Ú	timestampÚsaltÚanoncer   r   r	   ÚuseNonceT   s   

zMemoryStore.useNoncec                 C   sV   t   ¡ }g }| j ¡ D ]}t|d | ƒtjkr| |¡ q|D ]}| j|= q t|ƒS )Né   )r-   r#   Úkeysr,   r   r.   r   r   )r   ÚnowÚexpiredr3   r   r   r	   ÚcleanupNonces_   s   
€
zMemoryStore.cleanupNoncesc                 C   sT   g }d}| j  ¡ D ]\}}| ¡ \}}||7 }|s| |¡ q	|D ]}| j |= q!|S )Nr   )r"   r   r   r   )r   Úremove_urlsÚremoved_assocsr$   r   ÚremovedÚ	remainingr   r   r	   ÚcleanupAssociationsk   s   
€
zMemoryStore.cleanupAssociationsc                 C   s   | j |j ko| j|jkS r   r!   ©r   Úotherr   r   r	   Ú__eq__y   s   
ÿzMemoryStore.__eq__c                 C   s
   | |k S r   r   r?   r   r   r	   Ú__ne__}   r   zMemoryStore.__ne__r   )r   r   r   Ú__doc__r
   r%   r(   r*   r+   r4   r9   r>   rA   rB   r   r   r   r	   r    4   s    
r    )rC   Úopenid.storer   r&   r-   Úobjectr   r    r   r   r   r	   Ú<module>   s    +