B
    cI\V                 @   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)eeee ddddZde	ddd	Z
e	d
ddZdd
ddZe	d
ddZeeef d
ddZeeef ddddZed
ddZeddddZdd
ddZed
ddZed
dd Zd*eedd"d#d$Zed
d%d&Zed
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   1lib/python3.7/site-packages/secretstorage/item.py__init__   s
    zItem.__init__r   )otherr   c             C   s   t |jtst| j|jkS )N)
isinstancer   strAssertionError)r   r   r   r   r   __eq__#   s    zItem.__eq__)r   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)
attributesr   c             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   s   | j d}t|tst|S )z$Returns item label (unicode string).r   )r   r   r    r!   r"   )r   labelr   r   r   	get_labelJ   s    zItem.get_label)r.   r   c             C   s   |    | jdd| dS )zSets item label to `label`.r   sN)r&   r   r,   )r   r.   r   r   r   	set_labelP   s    zItem.set_labelc             C   s@   |    | jdd\}|dkr<t| j|\}}|r<td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sDt|d S t	
| jj}t|d }t|t|t  }|d }|t||  }t|tst|d|d   S )z!Returns item secret (bytestring).	GetSecreto      N)r&   r   r   r   r   r4   object_pathZ	encryptedbytesr   ZAESZaes_keyr   r   ZCBCr   	decryptorupdatefinalizer    r"   )r   secretZaesZaes_ivr>   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).r7   r8      )r&   r   r   r   r   r4   r<   r!   )r   rA   r   r   r   get_secret_content_typen   s
    zItem.get_secret_content_type
text/plain)rA   content_typer   c             C   s<   |    | jst| j| _t| j||}| jdd| dS )zSets 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   r4   )r   rA   rF   Z_secretr   r   r   
set_secretv   s
    zItem.set_secretc             C   s   | j d}t|tst|S )zlReturns UNIX timestamp (integer) representing the time
		when the item was created.

		.. versionadded:: 1.1ZCreated)r   r   r    intr"   )r   Zcreatedr   r   r   get_created   s    zItem.get_createdc             C   s   | j d}t|tst|S )zYReturns UNIX timestamp (integer) representing the time
		when the item was last modified.ZModified)r   r   r    rH   r"   )r   Zmodifiedr   r   r   get_modified   s    zItem.get_modified)N)rE   )__name__
__module____qualname____doc__r   r!   r   r   r   r$   r#   r%   r&   r'   r   r*   r-   r/   r1   r6   r=   rB   rD   rG   rH   rI   rJ   r   r   r   r   r      s    		
	N)rN   typingr   r   Zjeepney.integrate.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   