o
    †�zWþ  ã                   @   s€   d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	m
Z
mZmZ ddlmZmZmZ ddlmZ ed ZG d	d„ deƒZdS )
aQ  SecretStorage item contains a *secret*, some *attributes* and a
*label* visible to user. Editing all these properties and reading the
secret is possible only when the :doc:`collection <collection>` storing
the item is unlocked. The collection can be unlocked using collection's
:meth:`~secretstorage.collection.Collection.unlock` method.é    N)Ú	SS_PREFIX)ÚLockedException)ÚInterfaceWrapperÚbus_get_objectÚopen_sessionÚformat_secretÚ
to_unicodeÚunlock_objects)ÚCipherÚ
algorithmsÚmodes)Údefault_backendÚItemc                   @   s–   e Zd ZdZd$d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d„ Zd%dd„Zdd„ Zd d!„ Zd"d#„ ZdS )&r   zRepresents a secret item.Nc                 C   sL   || _ t||ƒ}|| _|| _t|tƒ| _t|tjƒ| _	| j	j
tddd� d S )NÚLabelÚss©Ú	signature)Ú	item_pathr   ÚsessionÚbusr   Ú
ITEM_IFACEÚ
item_ifaceÚdbusÚPROPERTIES_IFACEÚitem_props_ifaceÚGet)Úselfr   r   r   Úitem_obj© r   úD/var/www/html/env/lib/python3.10/site-packages/secretstorage/item.pyÚ__init__   s   
ÿzItem.__init__c                 C   s   | j |j kS ©N)r   )r   Úotherr   r   r   Ú__eq__#   s   zItem.__eq__c                 C   ó   t | jjtddd�ƒS )zDReturns :const:`True` if item is locked, otherwise
		:const:`False`.ÚLockedr   r   )Úboolr   r   r   ©r   r   r   r   Ú	is_locked&   ó   ÿzItem.is_lockedc                 C   s   |   ¡ rtdƒ‚dS )zSIf collection is locked, raises
		:exc:`~secretstorage.exceptions.LockedException`.zItem is locked!N)r(   r   r'   r   r   r   Úensure_not_locked,   s   ÿzItem.ensure_not_lockedc                 C   s   t | j| jg|ƒS )a‘  Requests unlocking the item. Usually, this will mean that the
		whole collection containing this item will be unlocked.

		If `callback` is specified, calls it when unlocking is complete
		(see :func:`~secretstorage.util.exec_prompt` description for
		details). Otherwise, uses the loop from GLib API and returns a
		boolean representing whether the operation was dismissed.

		.. versionadded:: 2.1.2)r	   r   r   )r   Úcallbackr   r   r   Úunlock2   s   
zItem.unlockc                 C   s$   | j jtddd�}dd„ | ¡ D ƒS )z%Returns item attributes (dictionary).Ú
Attributesr   r   c                 S   s   i | ]\}}t |ƒt |ƒ“qS r   )r   )Ú.0ÚkeyÚvaluer   r   r   Ú
<dictcomp>B   s    ÿz'Item.get_attributes.<locals>.<dictcomp>)r   r   r   Úitems)r   Úattrsr   r   r   Úget_attributes>   s   
ÿÿzItem.get_attributesc                 C   s   | j jtd|dd� dS )z2Sets item attributes to `attributes` (dictionary).r-   Ússvr   N)r   ÚSetr   )r   Ú
attributesr   r   r   Úset_attributesE   s   
ÿzItem.set_attributesc                 C   s   | j jtddd�}t|ƒS )z$Returns item label (unicode string).r   r   r   )r   r   r   r   ©r   Úlabelr   r   r   Ú	get_labelJ   s   
ÿzItem.get_labelc                 C   s    |   ¡  | jjtd|dd� dS )zSets item label to `label`.r   r5   r   N)r*   r   r6   r   r9   r   r   r   Ú	set_labelP   s   
ÿzItem.set_labelc                 C   s   |   ¡  | jjdd�S )zDeletes the item.Ú r   )r*   r   ÚDeleter'   r   r   r   ÚdeleteV   s   zItem.deletec                 C   s¼   |   ¡  | jst| jƒ| _| jj| jjdd�}| jjs#tt	|d ƒƒS t
 | jj¡}tt	|d ƒƒ}t|t |¡tƒ ƒ ¡ }tt	|d ƒƒ}| |¡| ¡  }t	|ƒ}t|d|d  … ƒS )z!Returns item secret (bytestring).Úor   é   é   Néÿÿÿÿ)r*   r   r   r   r   Ú	GetSecretÚobject_pathÚ	encryptedÚbytesÚ	bytearrayr   ÚAESÚaes_keyr
   r   ÚCBCr   Ú	decryptorÚupdateÚfinalize)r   ÚsecretÚaesÚaes_ivrL   Úencrypted_secretÚpadded_secretr   r   r   Ú
get_secret[   s   ÿzItem.get_secretc                 C   s:   |   ¡  | jst| jƒ| _| jj| jjdd�}t|d ƒS )z-Returns content type of item secret (string).r@   r   é   )r*   r   r   r   r   rD   rE   Ústr)r   rO   r   r   r   Úget_secret_content_typel   s   ÿzItem.get_secret_content_typeú
text/plainc                 C   s<   |   ¡  | jst| jƒ| _t| j||ƒ}| jj|dd� dS )zƒSets item secret to `secret`. If `content_type` is given,
		also sets the content type of the secret (``text/plain`` by
		default).z(oayays)r   N)r*   r   r   r   r   r   Ú	SetSecret)r   rO   Úcontent_typer   r   r   Ú
set_secretu   s
   zItem.set_secretc                 C   r$   )zlReturns UNIX timestamp (integer) representing the time
		when the item was created.

		.. versionadded:: 1.1ÚCreatedr   r   ©Úintr   r   r   r'   r   r   r   Úget_created   s   ÿzItem.get_createdc                 C   r$   )zYReturns UNIX timestamp (integer) representing the time
		when the item was last modified.ÚModifiedr   r   r]   r'   r   r   r   Úget_modified‡   r)   zItem.get_modifiedc                 C   s   |   ¡  |  ¡ |  ¡ fS )z?Returns (*attributes*, *secret*) tuple representing the
		item.)r*   r4   rT   r'   r   r   r   Úto_tuple�   s   zItem.to_tupler!   )rX   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r#   r(   r*   r,   r4   r8   r;   r<   r?   rT   rW   r[   r_   ra   rb   r   r   r   r   r      s$    



	
)rf   r   Úsecretstorage.definesr   Úsecretstorage.exceptionsr   Úsecretstorage.utilr   r   r   r   r   r	   Ú&cryptography.hazmat.primitives.ciphersr
   r   r   Úcryptography.hazmat.backendsr   r   Úobjectr   r   r   r   r   Ú<module>   s    