o
    /h                     @   s  d Z ddlZddlZddlZddlZzddlmZmZmZm	Z	m
Z
 ddlmZ W n eyE   ddlmZmZmZmZ ddlm	Z	m
Z
 Y nw zeZW n	 eyS   Y nw dZdZd	d
 Zdd Zdd Zdd Zdd Zd ddZdd Zdd Zdd ZdedefddZdS )!a-  
Adapted from:
http://blog.macuyiko.com/post/2016/how-to-send-html-mails-with-oauth2-and-gmail-in-python.html

1. Generate and authorize an OAuth2 (generate_oauth2_token)
2. Generate a new access tokens using a refresh token(refresh_token)
3. Generate an OAuth2 string to use for login (access_token)
    N)	urlencodequoteunquoteparse_qsurlsplit)urlopen)r   r   r   r   )r   r   zhttps://accounts.google.comzhttp://localhostc                 C   s   dt | f S )Nz%s/%s)GOOGLE_ACCOUNTS_BASE_URL)command r
   B/var/www/html/myenv/lib/python3.10/site-packages/yagmail/oauth2.pycommand_to_url   s   r   c                 C   sN   g }t |  dd dD ]}t|d dd}|d|d |f  qd	|S )
Nc                 S   s   | d S )Nr   r
   )xr
   r
   r   <lambda>$   s    z#url_format_params.<locals>.<lambda>)key   z~-._)safez%s=%sr   &)sorteditemsr   appendjoin)paramsparam_fragmentsparamescaped_urlr
   r
   r   url_format_params"   s
   
r   c                 C   s8   i }| |d< t |d< d|d< d|d< dtdt|f S )	N	client_idredirect_urizhttps://mail.google.com/scopecoderesponse_typez%s?%szo/oauth2/auth)REDIRECT_URIr   r   )r   r   r
   r
   r   generate_permission_url*   s   r"   c                 C   s`   i }| |d< ||d< ||d< t |d< d|d< td}t|d}t|| d}t|S )	Nr   client_secretr   r   authorization_code
grant_typeo/oauth2/tokenUTF-8)	r!   r   r   encoder   readdecodejsonloads)r   r#   r$   r   request_urlencoded_paramsresponser
   r
   r   call_authorize_tokens3   s   
r0   c                 C   sX   i }| |d< ||d< ||d< d|d< t d}t|d}t|| d}t|S )Nr   r#   refresh_tokenr%   r&   r'   )r   r   r(   r   r)   r*   r+   r,   )r   r#   r1   r   r-   r.   r/   r
   r
   r   call_refresh_token@   s   
r2   Fc                 C   s*   d| |f }|rt |dd}|S )Nzuser=%sauth=Bearer %sascii)base64	b64encoder(   r*   )usernameaccess_token	as_base64auth_stringr
   r
   r   generate_oauth2_stringL   s   r:   c                 C   sT   t | }td|  td}tt|jd d }t| ||}|d |d |d fS )Nz'Navigate to the following URL to auth:
z0Enter the localhost URL you were redirected to: r   r   r1   r7   
expires_in)r"   printinputr   r   queryr0   )google_client_idgoogle_client_secretpermission_urlurlr$   r/   r
   r
   r   get_authorizationS   s   rC   c                 C   s   t | ||}|d |d fS )Nr7   r;   )r2   )r?   r@   google_refresh_tokenr/   r
   r
   r   refresh_authorization\   s   rE   c                 C   s$   t di |\}}t| |dd}|S )NT)r8   r
   )rE   r:   )useroauth2_infor7   r;   r9   r
   r
   r   get_oauth_stringa   s   rH   oauth2_file
email_addrc           	      C   s  d}t j| } t j| rt| }t|}W d    n1 s"w   Y  z|d }W n ty8   | Y S w t| |d u rEt	d}|d }|d }t
||\}}}|| | | d}t| d}t|| W d    |S 1 syw   Y  |S td td	 td
 t| |d u rt	d}t	d}td}t
||\}}}|| | | d}t| d}t|| W d    |S 1 sw   Y  |S )NzSee readme for proper setup, preventing authorization from expiring after 7 days! https://github.com/kootenpv/yagmail/blob/master/README.md#preventing-oauth-authorization-from-expiring-after-7-days	installedzYour 'email address': r   r#   )email_addressr?   r@   rD   wzLIf you do not have an app registered for your email sending purposes, visit:z%https://console.developers.google.comzand create a new project.
zYour 'google_client_id': zYour 'google_client_secret': )ospath
expanduserisfileopenr+   loadKeyErrorr<   r=   rC   stripdumpgetpass)	rI   rJ   oauth_setup_readme_linkfrG   r?   r@   rD   _r
   r
   r   get_oauth2_infog   s`   



r[   )F) __doc__rN   r4   r+   rW   urllib.parser   r   r   r   r   urllib.requestr   ImportErrorurlliburlparse	raw_inputr=   	NameErrorr   r!   r   r   r"   r0   r2   r:   rC   rE   rH   strr[   r
   r
   r
   r   <module>   s:    	
	