33import os
44import json
55from rich .console import Console
6+ from keyring .errors import NoKeyringError
67
78console = Console ()
89
910class AuthManager :
1011 def __init__ (self ):
11- self .app_name = "hmv-cli-pro "
12+ self .app_name = "hmv-cli"
1213 self .config_dir = os .path .expanduser ("~/.hmv" )
1314 self .config_file = os .path .join (self .config_dir , "config.json" )
14-
15+
1516 if not os .path .exists (self .config_dir ):
1617 os .makedirs (self .config_dir )
1718
1819 def save_credentials (self , username , password ):
1920 """Save username to file and password to system keychain."""
20- with open (self .config_file , "w" ) as f :
21- json .dump ({"username" : username }, f )
21+ try :
22+ with open (self .config_file , "w" ) as f :
23+ json .dump ({"username" : username }, f )
2224
23- keyring .set_password (self .app_name , username , password )
24- console .print ("[bold green][+][/bold green] Credentials saved successfully to system vault!" )
25+ keyring .set_password (self .app_name , username , password )
26+ console .print ("[bold green][+][/bold green] Credentials saved successfully!" )
27+
28+ except NoKeyringError :
29+ console .print ("[bold red][!][/bold red] Error: No recommended keyring backend was found." )
30+ console .print ("[yellow][*][/yellow] Linux users: Please install [bold white]keyrings.alt[/bold white] or a secret service." )
31+ console .print (" Command: [cyan]pip install keyrings.alt[/cyan]" )
32+ except Exception as e :
33+ console .print (f"[bold red][!][/bold red] Failed to save password to system vault." )
34+ console .print (f"[dim]Error detail: { e } [/dim]" )
2535
2636 async def get_session (self ):
2737 """Establish and return an authenticated HTTPX session."""
2838 if not os .path .exists (self .config_file ):
2939 console .print ("[bold red][!][/bold red] Configuration missing. Please run 'hmv config' first." )
3040 return None
3141
32- with open (self .config_file , "r" ) as f :
33- username = json .load (f ).get ("username" )
42+ try :
43+ with open (self .config_file , "r" ) as f :
44+ username = json .load (f ).get ("username" )
3445
35- password = keyring .get_password (self .app_name , username )
46+ password = keyring .get_password (self .app_name , username )
47+ except NoKeyringError :
48+ console .print ("[bold red][!][/bold red] Keyring backend not found. Run [cyan]pip install keyrings.alt[/cyan]" )
49+ return None
50+ except Exception as e :
51+ console .print (f"[bold red][!][/bold red] Error accessing vault: { e } " )
52+ return None
53+
54+ if not password :
55+ console .print ("[bold red][!][/bold red] Password not found. Please run 'hmv config' again." )
56+ return None
57+
58+ user_agent = (
59+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
60+ "AppleWebKit/537.36 (KHTML, like Gecko) "
61+ "Chrome/120.0.0.0 Safari/537.36 HMV-CLI/0.1.0"
62+ )
3663
3764 timeout = httpx .Timeout (60.0 , connect = 15.0 )
3865 client = httpx .AsyncClient (
3966 base_url = "https://hackmyvm.eu" ,
4067 follow_redirects = True ,
4168 timeout = timeout ,
42- headers = {"User-Agent" : "HMV-CLI-Pro/0.1.0" }
69+ headers = {"User-Agent" : user_agent }
4370 )
4471
4572 try :
@@ -54,11 +81,7 @@ async def get_session(self):
5481 console .print ("[bold red][!][/bold red] Authentication failed. Check your credentials." )
5582 await client .aclose ()
5683 return None
57- except httpx .TimeoutException :
58- console .print ("[bold red][!][/bold red] Authentication timed out. The server is being slow." )
59- await client .aclose ()
60- return None
6184 except Exception as e :
62- console .print (f"[bold red][!][/bold red] Connection error during auth : { e } " )
85+ console .print (f"[bold red][!][/bold red] Connection error: { e } " )
6386 await client .aclose ()
6487 return None
0 commit comments