o
    j­_y  ã                   @   s    d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZmZ ddl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.é    )ÚDictÚOptional)ÚDBusConnection)Ú	SS_PREFIX)ÚSession)ÚLockedExceptionÚPromptDismissedException)ÚDBusAddressWrapperÚexec_promptÚopen_sessionÚformat_secretÚunlock_objects)ÚCipherÚ
algorithmsÚmodes)Údefault_backendÚItemc                	   @   s  e Zd ZdZ	d,dededee ddfdd„Zd	d
de	fdd„Z
de	fdd„Zd-dd„Zde	fdd„Zdeeef fdd„Zdeeef ddfdd„Zdefdd„Zdeddfdd„Zd-dd„Zdefdd „Zdefd!d"„Z	#d.d$ed%eddfd&d'„Zdefd(d)„Zdefd*d+„ZdS )/r   zRepresents a secret item.NÚ
connectionÚ	item_pathÚsessionÚreturnc                 C   s0   || _ t|t|ƒ| _| j d¡ || _|| _d S )NÚLabel)r   r	   Ú
ITEM_IFACEÚ_itemÚget_propertyr   r   )Úselfr   r   r   © r   ú4/usr/lib/python3/dist-packages/secretstorage/item.pyÚ__init__   s
   
zItem.__init__Úotherr   c                 C   s   t |jtƒsJ ‚| j|jkS ©N)Ú
isinstancer   Ústr)r   r   r   r   r   Ú__eq__#   s   zItem.__eq__c                 C   s   t | j d¡ƒS )zDReturns :const:`True` if item is locked, otherwise
		:const:`False`.ZLocked)Úboolr   r   ©r   r   r   r   Ú	is_locked'   s   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 means that the
		whole collection containing this item will be unlocked.

		Returns a boolean representing whether the prompt has been
		dismissed; that means :const:`False` on successful unlocking
		and :const:`True` if it has been dismissed.

		.. versionadded:: 2.1.2

		.. versionchanged:: 3.0
		   No longer accepts the ``callback`` argument.
		)r   r   r   r%   r   r   r   Úunlock2   s   zItem.unlockc                 C   s   | j  d¡}t|ƒS )z%Returns item attributes (dictionary).Ú
Attributes)r   r   Údict)r   Zattrsr   r   r   Úget_attributesA   s   zItem.get_attributesÚ
attributesc                 C   s   | j  dd|¡ dS )z2Sets item attributes to `attributes` (dictionary).r)   za{ss}N)r   Úset_property)r   r,   r   r   r   Úset_attributesF   s   zItem.set_attributesc                 C   ó   | j  d¡}t|tƒsJ ‚|S )z$Returns item label (unicode string).r   )r   r   r!   r"   ©r   Úlabelr   r   r   Ú	get_labelJ   s   zItem.get_labelr1   c                 C   s   |   ¡  | j dd|¡ dS )zSets item label to `label`.r   ÚsN)r'   r   r-   r0   r   r   r   Ú	set_labelP   s   zItem.set_labelc                 C   sD   |   ¡  | j dd¡\}|dkrt| j|ƒ\}}|r tdƒ‚dS dS )zDeletes the item.ZDeleteÚ ú/zPrompt dismissed.N)r'   r   Úcallr
   r   r   )r   ÚpromptZ	dismissedZ_resultr   r   r   ÚdeleteU   s   ýzItem.deletec                 C   sÄ   |   ¡  | jst| jƒ| _| j dd| jj¡\}| jjs"t|d ƒS | jj	dus*J ‚t
 | jj	¡}t|d ƒ}t|t |¡tƒ ƒ ¡ }|d }| t|ƒ¡| ¡  }t|tƒsYJ ‚|d|d  … S )z!Returns item secret (bytestring).Ú	GetSecretÚoé   Né   éÿÿÿÿ)r'   r   r   r   r   r7   Úobject_pathZ	encryptedÚbytesZaes_keyr   ZAESr   r   ZCBCr   Ú	decryptorÚupdateZfinalizer!   )r   ÚsecretZaesZaes_ivrA   Zencrypted_secretZpadded_secretr   r   r   Ú
get_secret^   s   zItem.get_secretc                 C   s<   |   ¡  | jst| jƒ| _| j dd| jj¡\}t|d ƒS )z-Returns content type of item secret (string).r:   r;   é   )r'   r   r   r   r   r7   r?   r"   )r   rC   r   r   r   Úget_secret_content_typeo   s
   zItem.get_secret_content_typeú
text/plainrC   Úcontent_typec                 C   s<   |   ¡  | jst| jƒ| _t| 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	SetSecretz(oayays)N)r'   r   r   r   r   r   r7   )r   rC   rH   Z_secretr   r   r   Ú
set_secretw   s
   zItem.set_secretc                 C   r/   )zlReturns UNIX timestamp (integer) representing the time
		when the item was created.

		.. versionadded:: 1.1ZCreated©r   r   r!   Úint)r   Zcreatedr   r   r   Úget_created‚   s   zItem.get_createdc                 C   r/   )zYReturns UNIX timestamp (integer) representing the time
		when the item was last modified.ZModifiedrJ   )r   Zmodifiedr   r   r   Úget_modified‹   s   zItem.get_modifiedr    )r   N)rG   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r"   r   r   r   r$   r#   r&   r'   r(   r   r+   r.   r2   r4   r9   r@   rD   rF   rI   rK   rL   rM   r   r   r   r   r      s:    ÿÿÿ
ÿ

		ÿÿ
ÿ	N)rQ   Útypingr   r   Zjeepney.io.blockingr   Zsecretstorage.definesr   Zsecretstorage.dhcryptor   Zsecretstorage.exceptionsr   r   Zsecretstorage.utilr	   r
   r   r   r   Z&cryptography.hazmat.primitives.ciphersr   r   r   Zcryptography.hazmat.backendsr   r   Úobjectr   r   r   r   r   Ú<module>   s   