o
    i                  	   @   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d dlZd dlZd dl	m
Z
 dZdZdZdZd"d	ed
ededefddZde jjfddZdedede jjde jfddZdd Zdd Zdede jjdB fddZde jjdB fddZdededede jjdB fd d!ZdS )#    N)
log_outputz&https://www.hover.com/signin/auth.jsonz'https://www.hover.com/signin/auth2.jsonz!https://www.hover.com/api/domainsz./cookies.json      secret	time_stepdigitsreturnc                 C   s   |   } | dd} t| d }|r| dd|  7 } tj| dd}tt | }td|}t	
||tj }|d d	@ }td
|||d  d d@ }	|	d|  }
|
d| dS )zQ
    Generates a one-time passcode for TFA using the provided shared secret.
         =T)casefoldz>Q   z>I   r   i
   0d)upperreplacelenbase64	b32decodeinttimestructpackhmacnewhashlibsha1digestunpack)r   r   r   missing_paddingkeycurrent_time
time_byteshmac_resultoffsetbinaryotp r+   >/home/seanl/mnt/public/Repos/hover-dyn-dns/./authentication.pygenerate_totp   s    r-   c                  C   s$   t jddd} td| j  | jS )z?
    Initializes a session with Hover to retrieve cookies.
    zhttps://www.hover.com/signinT)verifyz#Init session response status code: )requestsgetr   status_codecookies)responser+   r+   r,   init_session+   s   r4   usernamepasswordr2   c                 C   s^   | |dd}t jt|d|d}td|  td|j  td|j  td|  |S )	zC
    Logs in to Hover with the provided username and password.
    N)r5   r6   tokenT)jsonr.   r2   	Payload: zLogin response status code: zLogin response content: 	Cookies: )r/   postAUTH_ENDPOINTr   r1   content)r5   r6   r2   login_payloadr3   r+   r+   r,   submit_username_password4   s   r?   c                 C   sZ   d| i}t jt||dd}td|  td|j  td|j  td|  |S )z9
    Performs 2FA login with the provided TOTP code.
    codeT)r8   r2   r.   r9   z Login 2FA response status code: zLogin 2FA response content: r:   )r/   r;   AUTH_2FA_ENDPOINTr   r1   r=   )	totp_coder2   r>   r3   r+   r+   r,   submit_2fa_codeE   s   rC   c                 C   s@   t td}t|  | W d   dS 1 sw   Y  dS )z'
    Saves cookies to a JSON file.
    wN)openCOOKIES_FILEr8   dumpget_dict)r2   filer+   r+   r,   save_cookiesT   s   "rJ   cookies_filenamec                 C   sP   t j| r&ttd}t|}tj	|W  d   S 1 s!w   Y  dS )z)
    Loads cookies from a JSON file.
    rN)
ospathisfilerE   rF   r8   loadr/   r2   cookiejar_from_dict)rK   rI   cookies_dictr+   r+   r,   load_cookies\   s   

 rS   c                 C   s&   | du rdS t jt| dd}|jdkS )za
    Checks if the cookies are still valid by making a request to an authenticated endpoint.
    NFT)r2   r.      )r/   r0   DOMAIN_CHECK_URLr1   )r2   r3   r+   r+   r,   check_cookies_validityg   s   
rV   totp_secretc           
      C   s  t d tt}t|rt d |S t }zt| ||}|j}| }W n tjy9   t dt	 d t
  Y nw t|tsMt dt	 d| d t
  |d}|rat dt	 d|  t
  |d	d
krk|}ni|d}|dkrt|}zt||}	|	j}|	 }	W n tjy   t dt d t
  Y nw t|	tst dt d|	 d t
  |	ddkrt d| d|	  t
  n|d krnt d| d t
  t|rt d t| |S t d|d d t
  d S )NzLogging in to hover...z8Skipping login process because we are already logged in.zCould not login: the endpoint z& did not return JSON data as expected.z
 returned z instead of valid JSON.errorz returned the following error: status	completedtypeappz.Could not submit 2fa login code: the endpoint 	succeededTzCould not login: totp code z2 was not accepted. The server's response was was: z#Hover is asking for a 2FA type of 'z', which we cannot handle. In your account settings, disable 2FA and re-enable as app authentication. See the readme for more details.zLogin success.zXThe login process seemed to go well, but the server is not accepting our auth cookie of 	hoverauth.)r   rS   rF   rV   r4   r?   r2   r8   JSONDecodeErrorr<   exit
isinstancedictr0   r-   rC   rA   rJ   )
r5   r6   rW   r2   login_responselogin_response_cookieslogin_error	auth_typetotptotp_responser+   r+   r,   loginq   sf   







rj   )r   r   )r/   requests.cookiesr   r   r   r   r   r8   rM   logging_outr   r<   rA   rU   rF   strr   r-   r2   RequestsCookieJarr4   Responser?   rC   rJ   rS   rV   rj   r+   r+   r+   r,   <module>   s,     	&
