o
    ‹¨ÊhÁ&  ã                   @   s  d dl Z d dl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 d dlmZmZ ddlmZ zd dlZd dlZd dlZd dlZd dlZW n	 eyS   Y nw d	d
„ ZG dd„ deƒZG dd„ deƒZejdkrqdd„ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZ dS )é    N)Úconfigparser)Úerrors)Úescape)Ú	platform_Ú
properties)ÚKeyringBackendÚNullCrypteré   )Úkeyczarc                  C   s8   t  ¡ �} tj W d  ƒ n1 sw   Y  t| ƒ S )zŒ
    Does this environment have pyfs installed?
    Should return False even when Mercurial's Demand Import allowed import of
    fs.*.
    N)r   ÚExceptionRaisedContextÚfsÚ__name__Úbool)Úexc© r   úC/var/www/html/env/lib/python3.10/site-packages/keyrings/alt/pyfs.pyÚhas_pyfs   s   
ÿ
r   c                       s”   e Zd ZdZdZ		d‡ fdd„	Zejdd„ ƒZe	d	d
„ ƒZ
dd„ Zdd„ Zddd„Ze	dd„ ƒZdd„ Zdd„ Zdd„ Zejedd„ ƒƒZ‡  ZS )ÚBasicKeyringaž  BasicKeyring is a Pyfilesystem-based implementation of
    keyring.

    It stores the password directly in the file, and supports
    encryption and decryption. The encrypted password is stored in base64
    format.
    Being based on Pyfilesystem the file can be local or network-based and
    served by any of the filesystems supported by Pyfilesystem including Amazon
    S3, FTP, WebDAV, memory and more.
    zkeyring_pyf_pass.cfgNTc                    sD   t t| ƒ ¡  || _tj t ¡ | j	j
¡}|p|| _
|| _|| _d S ©N)Úsuperr   Ú__init__Ú_crypterÚosÚpathÚjoinr   Ú	data_rootÚ	__class__Ú	_filenameÚ_can_createÚ_cache_timeout)ÚselfÚcrypterÚfilenameÚ
can_createÚcache_timeoutÚdef_fn©r   r   r   r   0   s   

zBasicKeyring.__init__c                 C   s   t j t ¡ | j¡S )z”
        The path to the file where passwords are stored. This property
        may be overridden by the subclass or at the instance level.
        )r   r   r   r   r   r"   ©r    r   r   r   Ú	file_path9   s   zBasicKeyring.file_pathc                 C   s   | j S )z2The filename used to store the passwords.
        )r   r'   r   r   r   r"   A   s   zBasicKeyring.filenamec                 C   ó   |r| j s	|pdS | j  |¡S )zEncrypt the password.
        ó    )r   Úencrypt)r    Úpasswordr   r   r   r+   G   ó   
zBasicKeyring.encryptc                 C   r)   )zDecrypt the password.
        r*   )r   Údecrypt)r    Úpassword_encryptedr   r   r   r.   N   r-   zBasicKeyring.decryptÚrc                 C   s  d}d|v pd|v pd|v }zH| j  d¡s| j  d¡r)tj | j ¡ d|¡}W |S t| dƒsLtjjj| j |d	�\| _| _	| j
durLtjj| j| j
d
�| _| j | j	|¡}W |S  tjj�y   | jrýtjj | j ¡}|rÉ| ¡ \}}}}}	|s|J dƒ‚d}
d|v r™| dd¡}t|ƒdkr“|d }nd}|d }
tjj d||
f ¡}| j
dur²tjj|| j
d
�}tj |¡\}}|rÈ| |¡sÈ|j|dd� ntj | j ¡}tj |¡\}}tj |¡}z|| _|| _	| ||¡W  Y S  tjjyü   |rù‚ 	 Y nw |�r‚ 	 Y |S w )z5Open the password file in the specified mode
        NÚwÚaú+zmem://zram://zkr.cfgÚ_pyfs)Ú	writeable)r$   zShould be a remote filesystemÚ ú:ú/r	   r   z%s://%sT)Ú	recursive)r"   Ú
startswithr   ÚopenerÚ	fsopendirÚopenÚhasattrÚparser4   Ú_pathr   ÚremoteÚCacheFSr   ÚResourceNotFoundErrorr   Úsplit_segmentsÚgroupsÚsplitÚlenÚopendirr   r   ÚexistsÚmakedirÚ_expand_syspathÚosfsÚOSFS)r    ÚmodeÚ	open_filer5   ÚsegmentsÚfs_nameÚcredentialsÚurl1Úurl2r   ÚhostÚ
split_url2ÚpyfsÚ	url2_pathÚurl2_filenameÚfull_urlr   r   r   Ú_openU   sz   
ÿÿ>
Åÿ
ÿ3Î

ÿ
ÿ€üÎzBasicKeyring._openc                 C   s<   t | dƒst ¡ }|  ¡ }|r| |¡ | ¡  || _| jS )z0load the passwords from the config file
        Ú_config)r>   r   ÚRawConfigParserr[   ÚreadfpÚcloser\   )r    Ú
raw_configÚfr   r   r   Úconfig    s   

zBasicKeyring.configc              	   C   sd   t |ƒ}t |ƒ}z| j ||¡ ¡ }t |¡}|  |¡ d¡}W |S  tj	tj
fy1   d}Y |S w )z)Read the password from the file.
        úutf-8N)Úescape_for_inirb   ÚgetÚencodeÚbase64Údecodestringr.   Údecoder   ÚNoOptionErrorÚNoSectionError)r    ÚserviceÚusernameÚpassword_base64r/   r,   r   r   r   Úget_password­   s   
þþzBasicKeyring.get_passwordc                 C   s„   t |ƒ}t |ƒ}|pd}|  | d¡¡}t |¡ ¡ }| j |¡s'| j |¡ | j 	|||¡ t
|  d¡ƒ}| j |¡ | ¡  dS )z(Write the password in the file.
        r6   rc   r1   N)rd   r+   rf   rg   Úencodestringri   rb   Úhas_sectionÚadd_sectionÚsetÚUnicodeWriterAdapterr[   Úwriter_   )r    rl   rm   r,   r/   rn   Úconfig_filer   r   r   Úset_password¾   s   zBasicKeyring.set_passwordc                 C   sd   t |ƒ}t |ƒ}z	| j ||¡ W n tjy   t d¡‚w t|  d¡ƒ}| j 	|¡ | 
¡  d S )NzPassword not foundr1   )rd   rb   Úremove_optionr   rk   r   ÚPasswordDeleteErrorrt   r[   ru   r_   )r    rl   rm   rv   r   r   r   Údelete_passwordÒ   s   
ÿzBasicKeyring.delete_passwordc                 C   s   t ƒ stdƒ‚dS )Nzpyfs requiredé   )r   ÚRuntimeError)Úclsr   r   r   ÚpriorityÞ   s   zBasicKeyring.priority©NTN)r0   )r   Ú
__module__Ú__qualname__Ú__doc__r   r   r   ÚNonDataPropertyr(   Úpropertyr"   r+   r.   r[   rb   ro   rw   rz   ÚClassPropertyÚclassmethodr~   Ú__classcell__r   r   r&   r   r   "   s*    ÿ	


K
r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	rt   zk
    Wrap an object with a .write method to accept 'str' on Python 2
    and make it a Unicode string.
    c                 C   s
   || _ d S r   )Ú_orig)r    Úorigr   r   r   r   ë   s   
zUnicodeWriterAdapter.__init__c                 O   s   t | jg|¢R i |¤ŽS r   )Úgetattrrˆ   )r    ÚargsÚkwargsr   r   r   Ú__getattr__î   s   z UnicodeWriterAdapter.__getattr__c                 C   s    t |tƒr
| d¡}| j |¡S )NÚascii)Ú
isinstanceÚstrri   rˆ   ru   )r    Úvaluer   r   r   ru   ñ   s   

zUnicodeWriterAdapter.writeN)r   r€   r�   r‚   r   r�   ru   r   r   r   r   rt   æ   s
    rt   )é   c                 C   s   | S r   r   )Úxr   r   r   rt   ø   s   c                       s"   e Zd ZdZd‡ fdd„	Z‡  ZS )ÚPlaintextKeyringz%Unencrypted Pyfilesystem Keyring
    NTc                    s   t t| ƒjtƒ |||d� d S ©N)r"   r#   r$   )r   r”   r   r   )r    r"   r#   r$   r&   r   r   r      s   

þzPlaintextKeyring.__init__r   ©r   r€   r�   r‚   r   r‡   r   r   r&   r   r”   ü   s    r”   c                       s*   e Zd ZdZdZ		d‡ fdd„	Z‡  ZS )ÚEncryptedKeyringz#Encrypted Pyfilesystem Keyring
    zcrypted_pyf_pass.cfgNTc                    s   t t| ƒj||||d� d S r•   )r   r—   r   )r    r!   r"   r#   r$   r&   r   r   r     s   

þzEncryptedKeyring.__init__r   )r   r€   r�   r‚   r   r   r‡   r   r   r&   r   r—     s    ÿr—   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚKeyczarKeyringz[Encrypted Pyfilesystem Keyring using Keyczar keysets specified in
    environment vars
    c                    s   t t| ƒ t ¡ ¡ d S r   )r   r˜   r   r
   ÚEnvironCrypterr'   r&   r   r   r     s   
ÿzKeyczarKeyring.__init__r–   r   r   r&   r   r˜     s    r˜   )!r   rg   ÚsysÚ	six.movesr   Úkeyringr   Úkeyring.util.escaper   rd   Úkeyring.utilr   r   Úkeyring.backendr   r   r6   r
   Ú	fs.openerr   Úfs.osfsÚ	fs.errorsÚfs.pathÚ	fs.remoteÚImportErrorr   r   Úobjectrt   Úversion_infor”   r—   r˜   r   r   r   r   Ú<module>   s6    ÿ E

