
    NM:h5              	       h   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Zde j        j        fdZdedede j        j        de j        fdZd Zd Zdede j        j        dz  fdZde j        j        dz  fdZdededede j        j        dz  f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                    |                                  } |                     dd          } t          |           dz  }|r| dd|z
  z  z  } t          j        | d          }t          t          j                    |z            }t          j        d|          }t          j
        ||t          j                                                  }|d         d	z  }t          j        d
|||dz                      d         dz  }	|	d|z  z  }
|
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otps              </home/seanl/mnt/public/Repos/hover-dyn-dns/authentication.pygenerate_totpr.      s
   
 \\^^F^^C##F&kkAoO .#_,--

6D
1
1
1Cty{{i/00LT<00J(3
GL99@@BBK_t#F]4VFQJ->!?@@CjPF
B&L
!CF    c                 l    t          j        d| d          }t          d|j                    |j        S )z?
    Initializes a session with Hover to retrieve cookies.
    zhttps://www.hover.com/signinT)proxiesverifyz#Init session response status code: )requestsgetr   status_codecookies)r1   responses     r-   init_sessionr8   +   s?     |:GTXYYYHKX5IKKLLLr/   usernamepasswordr6   c                     | |dd}t          j        t          ||d|          }t          d|            t          d|j                    t          d|j                    t          d|            |S )	zC
    Logs in to Hover with the provided username and password.
    N)r9   r:   tokenT)jsonr1   r2   r6   	Payload: zLogin response status code: zLogin response content: 	Cookies: )r3   postAUTH_ENDPOINTr   r5   content)r9   r:   r6   r1   login_payloadr7   s         r-   submit_username_passwordrD   4   s    
  M
 }]X\fmnnnH*=**+++Dh.BDDEEE<(*:<<===$7$$%%%Or/   c                     d| i}t          j        t          |||d          }t          d|            t          d|j                    t          d|j                    t          d|            |S )z9
    Performs 2FA login with the provided TOTP code.
    codeT)r=   r1   r6   r2   r>   z Login 2FA response status code: zLogin 2FA response content: r?   )r3   r@   AUTH_2FA_ENDPOINTr   r5   rB   )	totp_coder6   r1   rC   r7   s        r-   submit_2fa_coderI   E   s    
 		M }.]G]dmqrrrH*=**+++H(2FHHIII@h.>@@AAA$7$$%%%Or/   c                     t          t          d          5 }t          j        |                                 |           ddd           dS # 1 swxY w Y   dS )z'
    Saves cookies to a JSON file.
    wN)openCOOKIES_FILEr=   dumpget_dict)r6   files     r-   save_cookiesrQ   T   s     
lC	 	  ,D	'""$$d+++, , , , , , , , , , , , , , , , , ,s   (AAAcookies_filenamec                    t           j                            |           r`t          t          d          5 }t          j        |          }t          j        	                    |          cddd           S # 1 swxY w Y   dS )z)
    Loads cookies from a JSON file.
    rN)
ospathisfilerL   rM   r=   loadr3   r6   cookiejar_from_dict)rR   rP   cookies_dicts      r-   load_cookiesr[   \   s     
w~~&'' F,$$ 	F9T??L#77EE	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 4s   3A55A9<A9c                 Z    | dS t          j        t          | |d          }|j        dk    S )za
    Checks if the cookies are still valid by making a request to an authenticated endpoint.
    NFT)r6   r1   r2      )r3   r4   DOMAIN_CHECK_URLr5   )r6   r1   r7   s      r-   check_cookies_validityr_   g   s6     u|,gwW[\\\H3&&r/   totp_secretc                    t          d           t          t                    }t          ||          rt          d           |S t	          |          }	 t          | |||          }|j        }|                                }n;# t          j        $ r) t          dt           d           t                       Y nw xY wt          |t                    s)t          dt           d| d           t                       |                    d          }|r(t          dt           d|            t                       |                    d	          d
k    r|}n6|                    d          }|dk    rt          |          }		 t          |	||          }
|
j        }|
                                }
n;# t          j        $ r) t          dt            d           t                       Y nw xY wt          |
t                    s)t          dt            d|
 d           t                       |
                    d          dk    r#t          d|	 d|
            t                       n(|d k    rn!t          d| d           t                       t          ||          r t          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   r[   rM   r_   r8   rD   r6   r=   JSONDecodeErrorrA   exit
isinstancedictr4   r.   rI   rG   rQ   )r9   r:   r`   r1   r6   login_responselogin_response_cookieslogin_error	auth_typetotptotp_responses              r-   loginrt   q   s   '(((<((Ggw// MNNN 7##G1(HgwWW!/!7',,..   iMiiijjj nd++ tMtt^tttuuu $$W--K rMrrbprrsss(##{22("&&v..	 --D /gw G G'/ - 2 2 4 4'     FL]  F  F  F  G  G  G mT22 <MYf  A  A  A  --55  A  A  Aq~  A  A  B  B  B$  OY  O  O  O  P  P  PFFF gw// #$$$W  Jnunyny  {F  oG  oG  J  J  J  	K  	K  	Ks$   -B 5B;:B;,F0 05G('G()r   r   )r3   requests.cookiesr   r   r!   r   r   r=   rU   logging_outr   rA   rG   r^   rM   strr   r.   r6   RequestsCookieJarr8   ResponserD   rI   rQ   r[   r_   rt    r/   r-   <module>r{      s              				 " " " " " "8= 6  #   #    0X-?    s c HDTDf t|  uF    "  , , ,3 8+;+MPT+T    'H$4$F$M ' ' ' '>C >3 >S >hFVFhkoFo > > > > > >r/   