U
    ¶[Öeƒ  ã                   @   s®   d dl mZ 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 zd dlZd dlmZ W n& ek
r|   Y n ek
rŽ   Y nX e e¡ZG dd	„ d	eƒZdS )
é    )ÚclosingNé   )Ú
properties)ÚKeyringBackend)ÚSimpleCredential)Ú	InitErrorÚPasswordDeleteErrorÚExceptionRaisedContextÚKeyringLockedc                   @   sV   e Zd ZdZdZejedd„ ƒƒZdd„ Z	dd„ Z
d	d
„ Zdd„ Zdd„ Zdd„ ZdS )ÚKeyringzSecret Service KeyringzPython keyring libraryc              
   C   sš   t ƒ }tj W 5 Q R X |r$tdƒ‚tjdk r6tdƒ‚z.tt ¡ ƒ}t |¡sXtdƒ‚W 5 Q R X W n0 tj	k
r” } ztd| ƒ‚W 5 d }~X Y nX dS )NzSecretStorage required)é   r   z#SecretStorage 3.2 or newer requiredzJThe Secret Service daemon is neither running nor activatable through D-Busz&Unable to initialize SecretService: %sé   )
r	   ÚsecretstorageÚ__name__ÚRuntimeErrorÚ__version_tuple__r   Ú	dbus_initZcheck_service_availabilityÚ
exceptionsÚSecretStorageException)ÚclsÚexcÚ
connectionÚe© r   ú`/var/www/html/services/stratfitenv/lib/python3.8/site-packages/keyring/backends/SecretService.pyÚpriority   s    

ÿzKeyring.priorityc              
   C   s†   t  ¡ }z(t| dƒr$t  || j¡}n
t  |¡}W n0 tjk
r` } ztd| ƒ‚W 5 d}~X Y nX | 	¡ r‚| 
¡  | 	¡ r‚tdƒ‚|S )z§If self.preferred_collection contains a D-Bus path,
        the collection at that address is returned. Otherwise,
        the default collection is returned.
        Úpreferred_collectionz$Failed to create the collection: %s.Nz Failed to unlock the collection!)r   r   ÚhasattrÚ
Collectionr   Zget_default_collectionr   r   r   Ú	is_lockedÚunlockr
   )ÚselfÚbusÚ
collectionr   r   r   r   Úget_preferred_collection3   s    
z Keyring.get_preferred_collectionc                 C   s&   t |dƒr| ¡  | ¡ r"tdƒ‚d S )Nr    zFailed to unlock the item!)r   r    r   r
   )r!   Úitemr   r   r   r    F   s    
zKeyring.unlockc              
   C   sb   |   ¡ }t|jƒF | ||dœ¡}|D ]*}|  |¡ | ¡  d¡  W  5 Q R £ S W 5 Q R X dS )z,Get password of the username for the service©ÚusernameÚserviceúutf-8N)r$   r   r   Úsearch_itemsr    Ú
get_secretÚdecode©r!   r(   r'   r#   Úitemsr%   r   r   r   Úget_passwordL   s    
zKeyring.get_passwordc              	   C   sN   |   ¡ }| j||dœ}d ||¡}t|jƒ |j|||dd W 5 Q R X dS )z,Set password for the username of the service)Zapplicationr(   r'   zPassword for '{}' on '{}'T)ÚreplaceN)r$   ÚappidÚformatr   r   Zcreate_item)r!   r(   r'   Úpasswordr#   Ú
attributesÚlabelr   r   r   Úset_passwordU   s    ýzKeyring.set_passwordc              
   C   sZ   |   ¡ }t|jƒ6 | ||dœ¡}|D ]}| ¡   W  5 Q R £ S W 5 Q R X tdƒ‚dS )z/Delete the stored password (only the first one)r&   zNo such password!N)r$   r   r   r*   Údeleter   r-   r   r   r   Údelete_passworda   s    "zKeyring.delete_passwordc              
   C   s„   d|i}|r||d< |   ¡ }t|jƒT | |¡}|D ]>}|  |¡ | ¡  d¡}t|| ¡  	d¡ƒ  W  5 Q R £ S W 5 Q R X dS )aY  Gets the first username and password for a service.
        Returns 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'   r)   N)
r$   r   r   r*   r    Zget_attributesÚgetr   r+   r,   )r!   r(   r'   Úqueryr#   r.   r%   r   r   r   Úget_credentialj   s    	

zKeyring.get_credentialN)r   Ú
__module__Ú__qualname__Ú__doc__r1   r   ZClassPropertyÚclassmethodr   r$   r    r/   r6   r8   r;   r   r   r   r   r      s   		r   )Ú
contextlibr   ÚloggingÚutilr   Úbackendr   Úcredentialsr   Úerrorsr   r   r	   r
   r   Zsecretstorage.exceptionsr   ÚImportErrorÚAttributeErrorÚ	getLoggerr   Úlogr   r   r   r   r   Ú<module>   s   
