o
    h¤´Ws  ã                   @   sö   d Z ddlZddl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dlmZ d	Zed
 ZG dd„ dejƒZd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S )zkThis module provides some utility functions, but these shouldn't
normally be used by external applications.é    N)
Ú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_backend)Úint_from_byteszorg.freedesktop.secretsÚServicec                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚInterfaceWrapperzxWraps :cls:`dbus.Interface` class and replaces some D-Bus exceptions
	with :doc:`SecretStorage exceptions <exceptions>`.c                    s   ‡ fdd„}|S )Nc               
      sx   zˆ | i |¤ŽW S  t jjy; } z%| ¡ tkrtdƒ‚| ¡ tkr(t| ¡ ƒ‚| ¡ tt	fv r6t
| ¡ ƒ‚‚ d }~ww )NzItem does not exist!)ÚdbusÚ
exceptionsÚDBusExceptionÚget_dbus_namer   r   r   Úget_dbus_messager   r   r   )ÚargsÚkwargsÚe©Úfunction_in© úD/var/www/html/env/lib/python3.10/site-packages/secretstorage/util.pyÚfunction_out   s   ÿ€øz3InterfaceWrapper.catch_errors.<locals>.function_outr!   )Úselfr    r#   r!   r   r"   Úcatch_errors   s   zInterfaceWrapper.catch_errorsc                 C   s$   t j | |¡}t|ƒr|  |¡}|S ©N)r   Ú	InterfaceÚ__getattr__Úcallabler%   )r$   Ú	attributeÚresultr!   r!   r"   r(   +   s   
zInterfaceWrapper.__getattr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   r(   r!   r!   r!   r"   r      s    r   c              
   C   sX   |pt }z	| j||dd�W S  tjjy+ } z| ¡ tttfv r&t	| 
¡ ƒ‚‚ d}~ww )z’A wrapper around :meth:`SessionBus.get_object` that raises
	:exc:`~secretstorage.exceptions.SecretServiceNotAvailableException`
	when appropriate.F)Ú
introspectN)ÚBUS_NAMEÚ
get_objectr   r   r   r   r   r   r   r   r   )ÚbusÚobject_pathÚservice_nameÚnamer   r!   r!   r"   Úbus_get_object1   s   
ÿ€ür7   c              
   C   s¸   t | tƒ}t |td ¡}tƒ }z|jtt t	|j
ƒ¡dd�\}}W n( tjjyJ } z| ¡ tkr3‚ |jtddd�\}}d|_W Y d}~nd}~ww tt|ƒdƒ}| |¡ ||_|S )z%Returns a new Secret Service session.r   Úsv©Ú	signatureÚ FNÚbig)r7   r   r   r'   r	   r   ÚOpenSessionr
   Ú	ByteArrayr   Úmy_public_keyr   r   r   r   r   Ú	encryptedr   Ú	bytearrayÚset_server_public_keyr4   )r3   Úservice_objÚservice_ifaceÚsessionÚoutputr+   r   r!   r!   r"   Úopen_session>   s0   
ý
ý€ø

rG   c                 C   s¾   t |tƒs
| d¡}| jst | jdt |¡|f¡S dt|ƒd@  }|tt	|fƒ| ƒ7 }t
 d¡}t | j¡}t|t |¡tƒ ƒ ¡ }| |¡| ¡  }t | jt |¡t t	|ƒ¡|f¡S )zHFormats `secret` to make possible to pass it to the
	Secret Service API.zutf-8r;   é   é   )Ú
isinstanceÚbytesÚencoder@   r   ÚStructr4   r>   ÚlenrA   ÚosÚurandomr   ÚAESÚaes_keyr   r   ÚCBCr   Ú	encryptorÚupdateÚfinalizeÚArray)rE   ÚsecretÚcontent_typeÚpaddingÚaes_ivÚaesrT   Úencrypted_secretr!   r!   r"   Úformat_secretX   s$   



ÿ
ür^   c                    sD   t | |ƒ}t |td ¡}|jddd� ‡ fdd„}| d|¡ dS )	a  Executes the given `prompt`, when complete calls `callback`
	function with two arguments: a boolean representing whether the
	operation was dismissed and a list of unlocked item paths. A main
	loop should be running and registered for this function to work.ÚPromptr;   Úsr9   c                    s&   t |tjƒr
t|ƒ}ˆ t| ƒ|ƒ d S r&   )rJ   r   rW   ÚlistÚbool©Ú	dismissedÚunlocked©Úcallbackr!   r"   Únew_callbackv   s   z!exec_prompt.<locals>.new_callbackÚ	CompletedN)r7   r   r'   r	   r_   Úconnect_to_signal)r3   Úpromptrg   Ú
prompt_objÚprompt_ifacerh   r!   rf   r"   Úexec_promptn   s
   
rn   c                    sJ   ddl m} | ¡ ‰ g ‰‡ ‡fdd„}t| ||ƒ ˆ  ¡  ˆd ˆd fS )znLike :func:`exec_prompt`, but synchronous (uses loop from GLib
	API). Returns (*dismissed*, *unlocked*) tuple.r   )ÚGLibc                    ó    ˆ  | ¡ ˆ  |¡ ˆ  ¡  d S r&   ©ÚappendÚquitrc   ©Úloopr+   r!   r"   rg   ‚   ó   

z"exec_prompt_glib.<locals>.callbacké   )Úgi.repositoryro   ÚMainLooprn   Úrun)r3   rk   ro   rg   r!   rt   r"   Úexec_prompt_glib|   ó   r{   c                    sJ   ddl m} |g ƒ‰ g ‰‡ ‡fdd„}t| ||ƒ ˆ  ¡  ˆd ˆd fS )zoLike :func:`exec_prompt`, but synchronous (uses loop from PyQt5
	API). Returns (*dismissed*, *unlocked*) tuple.r   )ÚQCoreApplicationc                    rp   r&   rq   rc   ©Úappr+   r!   r"   rg   �   rv   z exec_prompt_qt.<locals>.callbackrw   )ÚPyQt5.QtCorer}   rn   Úexec_)r3   rk   r}   rg   r!   r~   r"   Úexec_prompt_qtŠ   r|   r‚   c                 C   sr   t | tƒ}t|tƒ}|j|dd�\}}t|ƒ}t|ƒdkr.|r't| ||ƒ dS t| |ƒd S |r7|d|ƒ dS dS )a0  Requests unlocking objects specified in `paths`. If `callback`
	is specified, calls it when unlocking is complete (see
	:func:`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Úaor9   rw   r   FN)	r7   r   r   ÚSERVICE_IFACEÚUnlockra   rN   rn   r{   )r3   Úpathsrg   rC   rD   Úunlocked_pathsrk   r!   r!   r"   Úunlock_objects˜   s   

þrˆ   c                 C   s&   zt | ƒW S  ty   t| ƒ Y S w )z(Converts D-Bus string to unicode string.)ÚunicodeÚ	NameErrorÚstr)Ústringr!   r!   r"   Ú
to_unicode­   s
   
þr�   r&   )(r/   r   rO   Ú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   Úcryptography.utilsr   r1   r„   r'   r   r7   rG   r^   rn   r{   r‚   rˆ   r�   r!   r!   r!   r"   Ú<module>   s(   0

