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$    ÿ
