o
    a/                  
   @   s   d dl Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
mZ dZz d dlZd dlmZ d d	lmZ ed
d d dlmZ dZW n eeefyP   Y nw e eZG dd deZdS )    N   )
properties)KeyringBackend)SimpleCredential)PasswordDeleteErrorPasswordSetErrorExceptionRaisedContextKeyringLockedF)Gio)GLibSecret1)r   Tc                   @   sx   e Zd ZdZdZer!ejdej	j
ejjejjejjdZejZejedd Zdd Zd	d
 Zdd Zdd ZdS )Keyringzlibsecret KeyringzPython keyring libraryzorg.freedesktop.Secret.Genericapplicationserviceusernamec                 C   s<   t  }tj W d    n1 sw   Y  |rtddS )Nzlibsecret requiredg333333@)r   r   __name__RuntimeError)clsexc r   </usr/lib/python3/dist-packages/keyring/backends/libsecret.pypriority-   s   zKeyring.priorityc                 C   s   | j ||d}zt| j|tjjd}W n  tjy4 } ztd}|	|t
jjr/td| d}~ww |D ]+}z	|  W   S  tjyb } ztd}|	|tjjr]td| d}~ww dS )z,Get password of the username for the servicer   Ng-io-error-quarkFailed to unlock the item!secret-error)appidr   password_search_syncschemaSearchFlagsUNLOCKr   Errorquark_try_stringmatchesr
   IOErrorEnumFAILEDr	   retrieve_secret_syncget_text	IS_LOCKED)selfr   r   
attributesitemserrorquarkitemr   r   r   get_password6   s4   



zKeyring.get_passwordc           	   
   C   s   | j ||d}d||}zt| j|| j||d}W n2 tjyM } z%td}|	|tjj
r6td|td}|	|tjjrHtd| d}~ww |sTtddS )z,Set password for the username of the servicer   zPassword for '{}' on '{}'Nr   z Failed to unlock the collection!r   zFailed to store password!)r   formatr   password_store_syncr   
collectionr   r"   r#   r$   r)   r	   r
   r%   r&   r   )	r*   r   r   passwordr+   labelstoredr-   r.   r   r   r   set_passwordO   s,   



zKeyring.set_passwordc           	      C   s   | j ||d}zt| j|tjjd}W n  tjy4 } ztd}|	|t
jjr/td| d}~ww |D ]1}zt| j| d}W |  S  tjyh } ztd}|	|tjjrctd| d}~ww td)z/Delete the stored password (only the first one)r   Nr   r   r   zNo such password!)r   r   r   r   r    r!   r   r"   r#   r$   r
   r%   r&   r	   password_clear_syncget_attributesr)   r   )	r*   r   r   r+   r,   r-   r.   r/   removedr   r   r   delete_passwordf   s:   



zKeyring.delete_passwordc                 C   s   d|i}|r
||d< zt | j|t jjd}W n  tjy7 } ztd}||t	j
jr2td| d}~ww |D ]5}| d}zt||  W   S  tjyo } ztd}||t jjrjtd| d}~ww dS )aW  Get the first username and password for a service.
        Return a Credential instance

        The username can be omitted, but if there is one, it will use get_password
        and return a SimpleCredential containing  the username and password
        Otherwise, it will return the first username and password combo that it finds.
        r   r   Nr   r   r   )r   r   r   r    r!   r   r"   r#   r$   r
   r%   r&   r	   r9   getr   r'   r(   r)   )r*   r   r   queryr,   r-   r.   r/   r   r   r   get_credential   s8   




zKeyring.get_credentialN)r   
__module____qualname____doc__r   	availabler   SchemanewSchemaFlagsNONESchemaAttributeTypeSTRINGr   COLLECTION_DEFAULTr3   r   ClassPropertyclassmethodr   r0   r7   r;   r>   r   r   r   r   r      s(    	r   )loggingutilr   backendr   credentialsr   errorsr   r   r   r	   rB   gigi.repositoryr
   r   require_versionr   AttributeErrorImportError
ValueError	getLoggerr   logr   r   r   r   r   <module>   s$    
