o
    l>j/                     @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZzd dlZW n	 e	y+   Y nw d dl
mZ d dlmZ d dlmZmZ d dlmZ ddlmZ G dd	 d	ejZG d
d deZG dd deZdS )    N)
properties)ExceptionTrap)credentialserrors)KeyringBackend   )keyczarc                       s    e Zd ZdZ fddZ  ZS )EnvironCredentialzBRetrieve credentials from specifically named environment variablesc                    s   t  dd d S )NGOOGLE_KEYRING_USERGOOGLE_KEYRING_PASSWORD)super__init__self	__class__ H/home/thesage/.local/lib/python3.10/site-packages/keyrings/alt/Google.pyr      s   zEnvironCredential.__init__)__name__
__module____qualname____doc__r   __classcell__r   r   r   r   r	      s    r	   c                   @   s   e Zd ZdZdZdZdZdZdddefdd	Z	e
jd
d Ze jZeedd Zdd Zdd Zdd Zedd Zedd Zedd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' ZdS )(DocsKeyringzBackend that stores keyring on Google Docs.
    Note that login and any other initialisation is deferred until it is
    actually required to allow this keyring class to be added to the
    global _all_keyring list.
    GoogleKeyringr   r   NTc                 C   s^   || _ || _|| _|| _|| _|| _d | _|stjj	
 | _n|| _|| j_d| j_d| _d S )NT)
credentialcryptersource_collection
can_createinput_getter_keyring_dictgdatadocsserviceDocsService_clientssl_login_reqd)r   r   r   r   
collectionclientr    r!   r   r   r   r   *   s   

zDocsKeyring.__init__c                 C   s$   |   stdt stddS )NzRequires gdatazRequires keyczar   )
_has_gdataRuntimeErrorr   has_keyczarclsr   r   r   priorityE   s
   zDocsKeyring.priorityc                 C   s
   t j d S N)r#   r   r0   r   r   r   r-   P   s   
zDocsKeyring._has_gdatac                 C   s"   |  | j||}|r| |}|S )z,Get password of the username for the service)
_get_entry_keyring_decrypt)r   r%   usernameresultr   r   r   get_passwordU   s   
zDocsKeyring.get_passwordc                 C   s  |  |pd}t| j}||}|si }|||< |||< | |}|| jkr-|| _dS || jkr| 	 \| _
}| | j||}| |||}	|	|krQ|| _dS |	du sY|	|kr{||i }
||
|< |
||< | |}|| jkrv|| _dS tdtd| d| dtd)z,Set password for the username of the service Nz$Failed write after conflict detectedzConflict detected, service:z and username:z- was set to a different value by someone elsezCould not save keyring)_encryptcopydeepcopyr5   get_save_keyringOKr"   CONFLICT_read
docs_entryr4   r   PasswordSetError)r   r%   r7   passwordkeyring_working_copyservice_entriessave_resultkeyring_dictexisting_pwdconflicting_pwdnew_service_entriesr   r   r   set_password\   sB   






zDocsKeyring.set_passwordc                 C   s   |  | j||S r3   )
_del_entryr5   )r   r%   r7   r   r   r   delete_password   s   zDocsKeyring.delete_passwordc              
   C   s   | j  smz| j | jj| jj| j j W | j S  tjj	yR   t
jd| j j  | d}| jj| j _| jj| j _| j j| jj| jj| j j| j j|d Y | j S  tjjy_   td tjjyl   tdw | j S )NzPlease visit zAnswer to the challenge? )captcha_tokencaptcha_responsez"Users credential were unrecognizedzLogin Error)r'   GetClientLoginTokenClientLoginr   r7   rE   r   r#   r%   CaptchaRequiredsysstdoutwritecaptcha_urlr!   emailrP   BadAuthenticationr   	InitErrorError)r   answerr   r   r   r+      s6   



zDocsKeyring.clientc                 C   s   | j p| jjdd S )N@r   )r   r   r7   splitr   r   r   r   r*      s   zDocsKeyring.collectionc                 C   s    | j d u r|  \| _| _ | j S r3   )r"   rB   rC   r   r   r   r   r5      s   
zDocsKeyring._keyringc                 C   s    d }| |}|r| |}|S r3   )r>   )r   rI   r%   r7   r8   rG   r   r   r   r4      s
   

zDocsKeyring._get_entryc                 C   sN   | |}|stdz||= W n ty   tdw |s%||= d S d S )NzNo matching servicez	Not found)r>   r   PasswordDeleteErrorKeyError)r   rI   r%   r7   rG   r   r   r   rN      s   




zDocsKeyring._del_entryc                 C      |sdS | j |S Nr:   )r   decryptr   valuer   r   r   r6         zDocsKeyring._decryptc                 C   rb   rc   )r   encryptre   r   r   r   r;      rg   zDocsKeyring._encryptc                 C   s
   d| j  S )Nz%s)keyring_titler   r   r   r   _get_doc_title   s   
zDocsKeyring._get_doc_titlec           
   
   C   sF  ddl m} || jgd}|  |d< d|d< | j| }|js;| jr-d }i }||fS t	
|   d| j d|jd }d	}z<|jj}|d
7 }| jd|}|jdkr\t	
d| }|tjro|ttjd  }tt|d}W ||fS  tjy }	 zt	
d|	d }	~	w ty }	 zt	
d|	d }	~	ww )Nr   DocumentQuery
categoriestitletruetitle-exactz not found in z and create not permittedr:   z&exportFormat=txtGET   z+Could not read existing Google Docs keyringzstring-escapez/Could not unpickle existing Google Docs keyringz-Could not decode existing Google Docs keyring)gdata.docs.servicerl   r*   rj   r+   QueryDocumentListFeedToUrientryr    r   r[   contentsrcrequeststatusread
startswithcodecsBOM_UTF8lenpickleloadsbase64urlsafe_b64decodedecodeUnpicklingError	TypeError)
r   rl   title_queryr$   rC   rI   file_contentsurlserver_responseexr   r   r   rB      sR   

zDocsKeyring._readc                 C   s  ddl }| j}tt|}zg| jr+dt|d}| jj	|| j
 j|d| _W |S ddlm} |dgd}| j|d	< d
|d< | j| }|jrQ|jd }	n| j| j}	t|}
|j|
dt|dd}| jj||  |	d| _W |S  |jjy } zNz3|jd  ddkr| j}n!d}|jd |dkr|  \}}||kr| j}n| j}n| j}W n t y   | j}Y nw W Y d}~|S W Y d}~|S d}~ww )z.Helper to actually write the keyring to Googler   Nz
text/plain)zContent-TypezContent-Length)extra_headersrk   folderrm   ro   rp   rq   temp)file_handlecontent_typecontent_length	file_name)folder_or_urireasonconflictr   z)Sorry, there was an error saving the filebody)!r#   r@   r   urlsafe_b64encoder   dumpsrC   r   r+   PutGetEditMediaLinkhrefrt   rl   r*   ru   rv   rw   CreateFolderioBytesIOMediaSourceUploadrj   r%   RequestErrormessagelowerfindrA   rB   FAIL	Exception)r   rI   r#   r8   r   r   rl   folder_queryr$   folder_entryr   media_sourcer   msgnew_docs_entrynew_keyring_dictr   r   r   r?      sl   

2






zDocsKeyring._save_keyring)r   r   r   r   ri   r@   r   rA   inputr   r   classpropertyr2   r   passesclassmethodr-   r9   rM   rO   propertyr+   r*   r5   r4   rN   r6   r;   rj   rB   r?   r   r   r   r   r      sB    

+


,r   c                       s,   e Zd ZdZ fddZ fddZ  ZS )KeyczarDocsKeyringzQGoogle Docs keyring using keyczar initialized from environment
    variables
    c                    s.   t  }t }tjd}t ||| d S )NGOOGLE_KEYRING_SOURCE)r   EnvironCrypterr	   osenvironr>   r   r   )r   r   r   r   r   r   r   r   A  s   zKeyczarDocsKeyring.__init__c                    s*   z
t d t  W S  ty   Y dS w )z
        Return if this keyring supports current environment:
        -1: not applicable
        0: suitable
        1: recommended
        zkeyczar.keyczarr   )
__import__r   	supportedImportErrorr   r   r   r   r   G  s   zKeyczarDocsKeyring.supported)r   r   r   r   r   r   r   r   r   r   r   r   <  s    r   )r   r~   r<   r   r   r   rU   rt   r#   r   jaraco.classesr   jaraco.contextr   keyringr   r   keyring.backendr   r:   r   r	   r   r   r   r   r   r   <module>   s,      !