U
    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Z	signature)	item_pathr   sessionbusr   
ITEM_IFACE
item_ifacedbusZPROPERTIES_IFACEitem_props_ifaceGet)selfr   r   r   Zitem_obj r   4/usr/lib/python3/dist-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   s   t | jjtdddS )zDReturns :const:`True` if item is locked, otherwise
		:const:`False`.ZLockedr   r   )boolr   r   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 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   Z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   Z
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   Z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   r-   r   N)r#   r   r.   r   r0   r   r   r   	set_labelP   s    zItem.set_labelc                 C   s   |    | jjddS )zDeletes the item. r   )r#   r   ZDeleter!   r   r   r   deleteV   s    zItem.deletec                 C   s   |    | jst| j| _| jj| jjdd}| jjsFt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_pathZ	encryptedbytes	bytearrayr   ZAESZaes_keyr
   r   ZCBCr   	decryptorupdatefinalize)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j| jjdd}t|d S )z-Returns content type of item secret (string).r5   r      )r#   r   r   r   r   r9   r:   str)r   r@   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 )zSets 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   Z	SetSecret)r   r@   Zcontent_typer   r   r   
set_secretu   s
    zItem.set_secretc                 C   s   t | jjtdddS )zlReturns UNIX timestamp (integer) representing the time
		when the item was created.

		.. versionadded:: 1.1ZCreatedr   r   intr   r   r   r!   r   r   r   get_created   s    zItem.get_createdc                 C   s   t | jjtdddS )zYReturns UNIX timestamp (integer) representing the time
		when the item was last modified.ZModifiedr   r   rG   r!   r   r   r   get_modified   s    zItem.get_modifiedc                 C   s   |    |  |  fS )z?Returns (*attributes*, *secret*) tuple representing the
		item.)r#   r,   rA   r!   r   r   r   to_tuple   s    zItem.to_tuple)N)N)rE   )__name__
__module____qualname____doc__r   r   r"   r#   r%   r,   r/   r1   r2   r4   rA   rD   rF   rI   rJ   rK   r   r   r   r   r      s"   


	

)rO   r   Zsecretstorage.definesr   Zsecretstorage.exceptionsr   Zsecretstorage.utilr   r   r   r   r   r	   Z&cryptography.hazmat.primitives.ciphersr
   r   r   Zcryptography.hazmat.backendsr   r   objectr   r   r   r   r   <module>   s    