o
    †€!`r  ã                   @   sn  d Z ddlZddlmZmZmZ ddlmZmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZ ddlmZmZ ddlmZm Z  dd	l!m"Z"m#Z#m$Z$ dd
l%m&Z& dZ'ed Z(ed Z)G dd„ deƒZ*dedefdd„Z+dede,de-dee-e,e,e-f fdd„Z.dede-dee/ee- f fdd„Z0dedee- de/fdd„Z1deddfdd „Z2dS )!zkThis module provides some utility functions, but these shouldn't
normally be used by external applications.é    N)ÚAnyÚListÚTuple)ÚDBusAddressÚDBusErrorResponseÚ	MatchRuleÚMessageÚMessageTypeÚnew_method_callÚ
Properties)ÚDBusConnection)
ÚDBUS_UNKNOWN_METHODÚDBUS_NO_SUCH_OBJECTÚDBUS_SERVICE_UNKNOWNÚDBUS_NO_REPLYÚDBUS_NOT_SUPPORTEDÚDBUS_EXEC_FAILEDÚSS_PATHÚ	SS_PREFIXÚALGORITHM_DHÚALGORITHM_PLAIN)ÚSessionÚint_to_bytes)ÚItemNotFoundExceptionÚ"SecretServiceNotAvailableException)ÚCipherÚ
algorithmsÚmodes)Údefault_backendzorg.freedesktop.secretsÚServiceÚPromptc                   @   s‚   e Zd ZdZdedededdfdd„Zd	edefd
d„Z	dedededefdd„Z
dedefdd„Zdedededdfdd„ZdS )ÚDBusAddressWrapperzÞA wrapper class around :class:`jeepney.wrappers.DBusAddress`
	that adds some additional methods for calling and working with
	properties, and converts error responses to SecretStorage
	exceptions.

	.. versionadded:: 3.0
	ÚpathÚ	interfaceÚ
connectionÚreturnNc                 C   s   t  | |t|¡ || _d S ©N)r   Ú__init__ÚBUS_NAMEÚ_connection)Úselfr"   r#   r$   © r+   ú4/usr/lib/python3/dist-packages/secretstorage/util.pyr'   '   s   
zDBusAddressWrapper.__init__Úmsgc              
   C   sx   z	| j j|dd�W S  ty; } z&|jttfv rtdƒ|‚|jttt	fv r6|j
}t|tƒr1|d }t|ƒ|‚‚ d }~ww )NT)ÚunwrapzItem does not exist!r   )r)   Úsend_and_get_replyr   Únamer   r   r   r   r   r   ÚdataÚ
isinstanceÚtupler   )r*   r-   Úrespr1   r+   r+   r,   r/   ,   s   
ÿ

€÷z%DBusAddressWrapper.send_and_get_replyÚmethodÚ	signatureÚbodyc                 G   s   t | |||ƒ}|  |¡S r&   )r
   r/   )r*   r5   r6   r7   r-   r+   r+   r,   Úcall:   s   
zDBusAddressWrapper.callr0   c                 C   s"   t | ƒ |¡}|  |¡\\}}|S r&   )r   Úgetr/   )r*   r0   r-   r6   Úvaluer+   r+   r,   Úget_property>   s   zDBusAddressWrapper.get_propertyr:   c                 C   s    t | ƒ |||¡}|  |¡ d S r&   )r   Úsetr/   )r*   r0   r6   r:   r-   r+   r+   r,   Úset_propertyC   s   zDBusAddressWrapper.set_property)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   r'   r   r   r/   r8   r;   r=   r+   r+   r+   r,   r!      s    
ÿ
ÿr!   r$   r%   c           	   
   C   s´   t tt| ƒ}tƒ }z| ddtdt|jƒf¡\}}W n% ty? } z|j	t
kr(‚ | ddtd¡\}}d|_W Y d}~nd}~ww |\}}|dksJJ ‚t |d¡}| |¡ ||_|S )z%Returns a new Secret Service session.ÚOpenSessionÚsvÚay)ÚsÚ FNÚbig)r!   r   ÚSERVICE_IFACEr   r8   r   r   Úmy_public_keyr   r0   r   r   Ú	encryptedÚintÚ
from_bytesÚset_server_public_keyÚobject_path)	r$   ÚserviceÚsessionÚoutputÚresultr4   r6   r:   Úkeyr+   r+   r,   Úopen_sessionH   s.   þ
þ€ú
rU   rQ   ÚsecretÚcontent_typec                 C   sÈ   t |tƒr| d¡}n	t |tƒstdƒ‚| jdusJ ‚| js%| jd||fS | jdus,J ‚dt|ƒd@  }|t|f| ƒ7 }t	 
d¡}t | j¡}t|t |¡tƒ ƒ ¡ }| |¡| ¡  }| j|||fS )zHFormats `secret` to make possible to pass it to the
	Secret Service API.zutf-8zsecret must be bytesNó    é   é   )r2   rB   ÚencodeÚbytesÚ	TypeErrorrO   rK   Úaes_keyÚlenÚosÚurandomr   ÚAESr   r   ÚCBCr   Ú	encryptorÚupdateÚfinalize)rQ   rV   rW   ÚpaddingÚaes_ivÚaesrd   Úencrypted_secretr+   r+   r,   Úformat_secret_   s&   


ürk   Úprompt_pathc                 C   s†   t |t| ƒ}t|tdtjd�}|  |¡�}| ddd¡ |  |¡j\}}W d  ƒ n1 s.w   Y  |dus9J ‚|dus?J ‚||fS )zÜExecutes the prompt in a blocking mode.

	:returns: a tuple; the first element is a boolean value showing
	          whether the operation was dismissed, the second element
	          is a list of unlocked object paths
	Ú	Completed)r"   r#   ÚmemberÚtyper    rF   rG   N)	r!   ÚPROMPT_IFACEr   r	   ÚsignalÚfilterr8   Úrecv_until_filteredr7   )r$   rl   ÚpromptÚruleÚsignalsÚ	dismissedrS   r+   r+   r,   Úexec_promptz   s   üþrx   Úpathsc                 C   sP   t tt| ƒ}| dd|¡\}}t|ƒdkr&t| |ƒ\}\}}|dks$J ‚|S dS )z�Requests unlocking objects specified in `paths`.
	Returns a boolean representing whether the operation was dismissed.

	.. versionadded:: 2.1.2ÚUnlockÚaoé   F)r!   r   rI   r8   r_   rx   )r$   ry   rP   Úunlocked_pathsrt   rw   r6   Úunlockedr+   r+   r,   Úunlock_objects‘   s   r   c                 C   s6   t ttd�}tdd| d�}d|_| dd| ¡ ¡ dS )zÙAdds match rules for the given connection.

	Currently it matches all messages from the Prompt interface, as the
	mock service (unlike GNOME Keyring) does not specify the signal
	destination.

	.. versionadded:: 3.1
	)Úsenderr#   z/org/freedesktop/DBuszorg.freedesktop.DBus)r"   r#   r$   ÚAddMatchrF   N)r   r(   rp   r!   Úbus_namer8   Ú	serialise)r$   ru   Údbusr+   r+   r,   Úadd_match_rulesŸ   s   	þr…   )3rA   r`   Útypingr   r   r   Újeepneyr   r   r   r   r	   r
   r   Újeepney.io.blockingr   Úsecretstorage.definesr   r   r   r   r   r   r   r   r   r   Úsecretstorage.dhcryptor   r   Úsecretstorage.exceptionsr   r   Ú&cryptography.hazmat.primitives.ciphersr   r   r   Úcryptography.hazmat.backendsr   r(   rI   rp   r!   rU   r\   rB   rk   Úboolrx   r   r…   r+   r+   r+   r,   Ú<module>   s6   $0)
ÿ
ÿÿ
ÿ