o
    Vh6                  
   @   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 ddlm	Z	m
Z
mZ dZzd dlZd d	lmZ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jeZdS )    N   )backend)KeyringBackend)
properties)SimpleCredential)KeyringLockedPasswordDeleteErrorPasswordSetErrorF)GioGLibSecret1)r   Tc                   @   sb   e Zd ZdZdZedd Zejdd Z	ej
defdd	Zd
d Zdd Zdd Zdd ZdS )Keyringzlibsecret KeyringzPython keyring libraryc              	   C   s,   t jdt jj| jt jjt jjt jjdS )Nzorg.freedesktop.Secret.Genericapplication)r   SchemanewSchemaFlagsNONE_querySchemaAttributeTypeSTRINGself r   n/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/keyring/backends/libsecret.pyschema!   s   zKeyring.schemac                 C   s   t jS )N)r   COLLECTION_DEFAULTr   r   r   r   
collection-   s   zKeyring.collectionreturnc              
   C   sJ   t stdztjtjjd  W dS  tjy$ } ztd|d }~ww )Nzlibsecret requiredz*Can't open a session to the secret serviceg333333@)		availableRuntimeErrorr   Serviceget_syncServiceFlagsOPEN_SESSIONr   Error)clserrorr   r   r   priority1   s   
zKeyring.priorityc                 C   s   | j ||| jd}zt| j|tjjd}W n  tjy6 } zt	d}|
|tjjr1td| d}~ww |D ]+}z	|  W   S  tjyd } 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)r   appidr   password_search_syncr   SearchFlagsUNLOCKr   r&   quark_try_stringmatchesr
   IOErrorEnumFAILEDr   retrieve_secret_syncget_text	IS_LOCKED)r   serviceusername
attributesitemsr(   quarkitemr   r   r   get_password>   s.   



zKeyring.get_passwordc           	   
   C   s   | j ||| jd}d| d| d}zt| j|| j||d}W n2 tjyR } z%td}|	|tjj
r;td|td}|	|tjjrMtd| d}~ww |sYtd	dS )
z,Set password for the username of the servicer   zPassword for 'z' on ''Nr,   z Failed to unlock the collection!r*   zFailed to store password!)r   r-   r   password_store_syncr   r   r   r&   r1   r2   r7   r   r
   r3   r4   r	   )	r   r8   r9   passwordr:   labelstoredr(   r<   r   r   r   set_passwordS   s&   



zKeyring.set_passwordc           	      C   s   | j ||| jd}zt| j|tjjd}W n  tjy6 } zt	d}|
|tjjr1td| d}~ww |D ]1}zt| j| d}W |  S  tjyj } zt	d}|
|tjjret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/   r0   r   r&   r1   r2   r
   r3   r4   r   password_clear_syncget_attributesr7   r   )	r   r8   r9   r:   r;   r(   r<   r=   removedr   r   r   delete_passwordf   s4   



zKeyring.delete_passwordc                 C   s   |  ||}zt| j|tjjd}W n  tjy3 } ztd}|	|t
jjr.td| d}~ww |D ]5}| d}zt||  W   S  tjyk } ztd}|	|tjjrft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.
        Nr*   r+   r9   r,   )r   r   r.   r   r/   r0   r   r&   r1   r2   r
   r3   r4   r   rF   getr   r5   r6   r7   )r   r8   r9   queryr;   r(   r<   r=   r   r   r   get_credential   s4   




zKeyring.get_credentialN)__name__
__module____qualname____doc__r-   propertyr   r   NonDataPropertyr   classpropertyfloatr)   r>   rD   rH   rK   r   r   r   r   r      s    

r   )logging r   r   compatr   credentialsr   errorsr   r   r	   r    gigi.repositoryr
   r   require_versionr   AttributeErrorImportError
ValueError	getLoggerrL   logSchemeSelectabler   r   r   r   r   <module>   s$    
