
    $3i?                        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	 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mZ dd	l m!Z! dd
l"m#Z#m$Z$ dZ%edz   Z&edz   Z' G d de      Z(dede!fdZ)de!de*de+de,e+e*e*e+f   fdZ-dddede+de.dz  de,e/e,e+ef   f   fdZ0dddede1e+   de.dz  de/fdZ2deddfdZ3y)zkThis module provides some utility functions, but these shouldn't
normally be used by external applications.    N)Any)default_backend)Cipher
algorithmsmodes)DBusAddressDBusErrorResponse	MatchRuleMessageMessageType
Propertiesnew_method_call)DBusConnection)ALGORITHM_DHALGORITHM_PLAINDBUS_EXEC_FAILEDDBUS_NO_REPLYDBUS_NO_SUCH_OBJECTDBUS_NOT_SUPPORTEDDBUS_SERVICE_UNKNOWNDBUS_UNKNOWN_METHODDBUS_UNKNOWN_OBJECTSS_PATH	SS_PREFIX)Session)ItemNotFoundException"SecretServiceNotAvailableExceptionzorg.freedesktop.secretsServicePromptc                   x    e Zd ZdZdedededdfdZdedefd	Z	d
edededefdZ
dedefdZdedededdfdZy)DBusAddressWrapperzA 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                 J    t        j                  | |t        |       || _        y N)r   __init__BUS_NAME_connection)selfr"   r#   r$   s       \/mnt/e/genesis-system/.venvs/voice-bridge/lib/python3.12/site-packages/secretstorage/util.pyr(   zDBusAddressWrapper.__init__9   s    T49=%    msgc                    	 | j                   j                  |      }|j                  j                  t        j
                  k(  rt        |      |j                  S # t        $ ry}|j                  t        t        t        fv rt        d      ||j                  t        t        t        fv r-|j                   }t#        |t$              r|d   }t'        |      | d }~ww xY w)NzItem does not exist!r   )r*   send_and_get_replyheadermessage_typer   errorr	   bodynamer   r   r   r   r   r   r   data
isinstancetupler   )r+   r.   resp_msgrespr6   s        r,   r0   z%DBusAddressWrapper.send_and_get_reply>   s    	 $ 0 0 C CC HH++{/@/@@'11==   	yy### 
 ,,BCM35E,. .yydE*7D8>DH	s   AA 	C$A4CCmethod	signaturer4   c                 @    t        | |||      }| j                  |      S r'   )r   r0   )r+   r;   r<   r4   r.   s        r,   callzDBusAddressWrapper.callS   s#    dFIt<&&s++r-   r5   c                 f    t        |       j                  |      }| j                  |      \  \  }}|S r'   )r   getr0   )r+   r5   r.   r<   values        r,   get_propertyzDBusAddressWrapper.get_propertyW   s4    ""4("55c:Er-   rA   c                 ^    t        |       j                  |||      }| j                  |       y r'   )r   setr0   )r+   r5   r<   rA   r.   s        r,   set_propertyzDBusAddressWrapper.set_property\   s)    ""4E:$r-   )__name__
__module____qualname____doc__strr   r(   r   r   r0   r>   rB   rE    r-   r,   r!   r!   1   s    &S &S &+&04&
g # *,3 ,3 ,s ,s ,  
% % %S %T %r-   r!   r$   r%   c           
         t        t        t        |       }t               }	 |j	                  ddt
        d|j                  j                  dd      f      \  }}|\  }}|dk(  sJ t        j                  |d      }|j                  |       ||_        |S # t        $ r@}|j                  t        k7  r |j	                  ddt        d      \  }}d|_        Y d}~Md}~ww xY w)	z%Returns a new Secret Service session.OpenSessionsvay   big)s FN)r!   r   SERVICE_IFACEr   r>   r   my_public_keyto_bytesint
from_bytesset_server_public_keyr	   r5   r   r   	encryptedobject_path)	r$   servicesessionoutputresultr<   rA   keyr:   s	            r,   open_sessionra   a   s     -DGiG+ 47((11#u=>@ "	5D   nnUE*%%c* GN  "99** 4 ""s   7B 	C6CCr]   secretcontent_typec                 j   t        |t              r|j                  d      }nt        |t              st	        d      | j
                  J | j                  s| j
                  d||fS | j                  J dt        |      dz  z
  }|t        |f|z        z  }t        j                  d      }t        j                  | j                        }t        |t        j                  |      t!                     j#                         }|j%                  |      |j'                         z   }| j
                  |||fS )zKFormats `secret` to make possible to pass it to the
    Secret Service API.zutf-8zsecret must be bytesr-         )r7   rJ   encodebytes	TypeErrorr[   rZ   aes_keylenosurandomr   AESr   r   CBCr   	encryptorupdatefinalize)r]   rb   rc   paddingaes_ivaesrp   encrypted_secrets           r,   format_secretrw   {   s    &#w'&.//***##S&,????&&&c&kC'(G
eWJ())FZZF
..
)CsEIIf-/@AKKMI ''/)2D2D2FF	 r-   timeoutprompt_pathry   c                4   t        |t        |       }t        |t        dt        j                        }| j                  |      5 }|j                  ddd       | j                  ||      }|j                  \  }}ddd       J J ||fS # 1 sw Y   xY w)a  Executes the prompt in a blocking mode.

    :returns: a two-element tuple:

       - The first element is a boolean value indicating whether the operation was
         dismissed.
       - The second element is a (signature, result) tuple. For creating items and
         collections, ``signature`` is ``'o'`` and ``result`` is a single object
         path. For unlocking, ``signature`` is ``'ao'`` and ``result`` is a list of
         object paths.

    .. versionchanged:: 3.5
       Added ``timeout`` keyword argument.
    	Completed)r"   r#   membertyper   rR   rS   rx   N)	r!   PROMPT_IFACEr
   r   signalfilterr>   recv_until_filteredr4   )	r$   rz   ry   promptrulesignalsmessage	dismissedr_   s	            r,   exec_promptr      s    (  \:FF	D 
		4	  )GHc2&00'0J#LL	6)    f) )s   6BBpathsc                    t        t        t        |       }|j                  dd|      \  }}t	        |      dkD  rt        | ||      \  }\  }}|dk(  sJ |S y)zRequests unlocking objects specified in `paths`.
    Returns a boolean representing whether the operation was dismissed.

    .. versionadded:: 2.1.2

    .. versionchanged:: 3.5
       Added ``timeout`` keyword argument.
    Unlockao   rx   F)r!   r   rT   r>   rk   r   )	r$   r   ry   r\   unlocked_pathsr   r   r<   unlockeds	            r,   unlock_objectsr      sj     !-DG$\\(D%@NF
6{Q+6,
(	(Ix
 D   r-   c                     t        t        t              }t        dd|       }d|_        |j                  dd|j                                y)zAdds 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$   AddMatchrR   N)r
   r)   r   r!   bus_namer>   	serialise)r$   r   dbuss      r,   add_match_rulesr      sC     H=D#:(>)35D +DMIIj#t~~/0r-   )4rI   rl   typingr   cryptography.hazmat.backendsr   &cryptography.hazmat.primitives.ciphersr   r   r   jeepneyr   r	   r
   r   r   r   r   jeepney.io.blockingr   secretstorage.definesr   r   r   r   r   r   r   r   r   r   r   secretstorage.dhcryptor   secretstorage.exceptionsr   r   r)   rT   r   r!   ra   rh   rJ   r8   rw   floatboolr   listr   r   rK   r-   r,   <module>r      sG  . 
  8 L L   /    +
 %I%8#-% -%`^  47 E  #(-c5%.D(E> !	!!! T\	!
 4sCx !!P !	9 T\	
 
61 14 1r-   