
    i              	          S SK r S SKr S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	J
r
  SrSrSrSrSS\S\S	\S
\4S jjrS
\ R$                  R&                  4S jrS\S\S\ R$                  R&                  S
\ R*                  4S jrS rS rS\S
\ R$                  R&                  S-  4S jrS\ R$                  R&                  S-  4S jrS\S\S\S
\ R$                  R&                  S-  4S jrg)    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                    U R                  5       n U R                  SS5      n [        U 5      S-  nU(       a  U SSU-
  -  -  n [        R                  " U SS9n[        [        R                  " 5       U-  5      n[        R                  " SU5      n[        R                  " XF[        R                  5      R                  5       nUS   S	-  n[        R                  " S
XxUS-    5      S   S-  n	U	SU-  -  n
U
SU S3 $ )zI
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              A/mnt/mint/home/seanl/public/Repos/hover-dyn-dns/authentication.pygenerate_totpr,      s    
 \\^F^^C#F&kAoO#_,--


6D
1Ctyy{i/0LT<0J((3GLL9@@BK_t#F]]4FQJ!?@CjPF
B&L
!C!F81    c                  t    [         R                  " SSS9n [        SU R                   35        U R                  $ )z7
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)responses    r+   init_sessionr5   +   s8     ||:4HH4X5I5I4JKLr-   usernamepasswordr3   c                     U USS.n[         R                  " [        USUS9n[        SU 35        [        SUR                   35        [        SUR
                   35        [        SU 35        U$ )	z;
Logs in to Hover with the provided username and password.
N)r6   r7   tokenT)jsonr/   r3   	Payload: zLogin response status code: zLogin response content: 	Cookies: )r0   postAUTH_ENDPOINTr   r2   content)r6   r7   r3   login_payloadr4   s        r+   submit_username_passwordrA   4   s|    
 M
 }}]tU\]H=/*+-h.B.B-CDE)(*:*:);<=7)$%Or-   c                     SU 0n[         R                  " [        X!SS9n[        SU 35        [        SUR                   35        [        SUR
                   35        [        SU 35        U$ )z1
Performs 2FA login with the provided TOTP code.
codeT)r:   r3   r/   r;   z Login 2FA response status code: zLogin 2FA response content: r<   )r0   r=   AUTH_2FA_ENDPOINTr   r2   r?   )	totp_coder3   r@   r4   s       r+   submit_2fa_coderF   E   sw    
 		M }}.]\`aH=/*+1(2F2F1GHI-h.>.>-?@A7)$%Or-   c                     [        [        S5       n[        R                  " U R	                  5       U5        SSS5        g! , (       d  f       g= f)z
Saves cookies to a JSON file.
wN)openCOOKIES_FILEr:   dumpget_dict)r3   files     r+   save_cookiesrN   T   s3     
lC	 D		'""$d+ 
!	 	 s   &A  
Acookies_filenamec                    [         R                  R                  U 5      (       aO  [        [        S5       n[
        R                  " U5      n[        R                  R                  U5      sSSS5        $ g! , (       d  f       g= f)z!
Loads cookies from a JSON file.
rN)
ospathisfilerI   rJ   r:   loadr0   r3   cookiejar_from_dict)rO   rM   cookies_dicts      r+   load_cookiesrX   \   s`     
ww~~&'',$99T?L##77E %$  %$ s   5A55
Bc                 \    U c  g[         R                  " [        U SS9nUR                  S:H  $ )zY
Checks if the cookies are still valid by making a request to an authenticated endpoint.
FT)r3   r/      )r0   r1   DOMAIN_CHECK_URLr2   )r3   r4   s     r+   check_cookies_validityr\   g   s0     ||,gdKH3&&r-   totp_secretc                     [        S5        [        [        5      n[        U5      (       a  [        S5        U$ [	        5       n [        XU5      nUR                  nUR                  5       n[        W[        5      (       d   [        S[         SU S35        [        5         UR                  S5      nU(       a  [        S[         SU 35        [        5         UR                  S	5      S
:X  a  WnOUR                  S5      nUS:X  a  [        U5      n [        X5      n	U	R                  nU	R                  5       n	[        W	[        5      (       d   [        S[          SU	 S35        [        5         U	R                  S5      S:w  a  [        SU SU	 35        [        5         O US :X  a  O[        SU S35        [        5         [        U5      (       a  [        S5        [#        U5        U$ [        SUR                  S5       S35        [        5         g ! [        R                   a!    [        S[         S35        [        5          GNf = f! [        R                   a!    [        S[          S35        [        5          GNCf = f)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   rX   rJ   r\   r5   rA   r3   r:   JSONDecodeErrorr>   exit
isinstancedictr1   r,   rF   rD   rN   )
r6   r7   r]   r3   login_responselogin_response_cookieslogin_error	auth_typetotptotp_responses
             r+   loginrq   q   s   '(<(Gg&&MN nG1(gN!/!7!7',,.
 nd++3M?*^L\\stu $$W-K3M?Babpaqrs(#{2("&&v.	 -D / >'// - 2 2 4
 mT22;<M;NjYfXgg~  A  -58>pq~p  A  B$<YK  HN  O  PF g&&#$Wmnunyny  {F  oG  nH  HI  J  	K_  3M?Bhij0 '' KL]K^  _E  F  Gs$   (H 'I 1II1I=<I=)      )r0   requests.cookiesr   r   r   r   r   r:   rR   logging_outr   r>   rD   r[   rJ   strr   r,   r3   RequestsCookieJarr5   ResponserA   rF   rN   rX   r\   rq    r-   r+   <module>rz      s%           	 "8= 6 #   # 0h&&88 s c HDTDTDfDf ksk|k| ",3 8+;+;+M+MPT+T 'H$4$4$F$F$M '>C >3 >S >X=M=M=_=_bf=f >r-   