1
1
import os
2
+ import json
2
3
import subprocess
3
4
from tkinter import simpledialog
4
5
from bin .pathUtil import CURRENT_PATH
5
6
6
7
CREDENTIALS_FILENAME = "credentials"
8
+ credentials_file_path = CURRENT_PATH + "credentials"
9
+ credentials_ikev2_file_path = CURRENT_PATH + "credentials.ikev2"
7
10
8
11
9
12
class NoCredentialsProvidedException (Exception ):
@@ -53,6 +56,48 @@ def save_credentials():
53
56
print ("IOError while creating 'credentials' file." )
54
57
55
58
59
+ def check_credentials_ikev2 ():
60
+ """
61
+ checks if exists a file with the credentials for ikev2 protocol
62
+ :return: True if exists, False otherwise
63
+ """
64
+ return os .path .exists (credentials_ikev2_file_path )
65
+
66
+
67
+ def save_credentials_ikev2 ():
68
+ """
69
+ Stores credentials in a root-password-protected file. Raises a NoCredentialsProvidedException if some
70
+ credentials info were not inserted
71
+ """
72
+ print ("Storing credentials in " + "'" + credentials_ikev2_file_path + "'" )
73
+
74
+ username = askIkev2Username ()
75
+ if username is None :
76
+ raise NoCredentialsProvidedException
77
+
78
+ password = askIkev2Password ()
79
+ if password is None :
80
+ raise NoCredentialsProvidedException
81
+
82
+ try :
83
+ with open (credentials_ikev2_file_path , 'w' ) as creds :
84
+ json .dump ({'username' : username , 'password' : password }, creds )
85
+
86
+ # Change file permissions
87
+ subprocess .check_call (["sudo" , "chown" , "root" , credentials_file_path ],
88
+ universal_newlines = True , stdout = subprocess .DEVNULL ,
89
+ stderr = subprocess .DEVNULL )
90
+ subprocess .check_call (["sudo" , "chmod" , "600" , credentials_file_path ],
91
+ universal_newlines = True , stdout = subprocess .DEVNULL ,
92
+ stderr = subprocess .DEVNULL )
93
+
94
+ print ("Awesome, the credentials have been saved in " +
95
+ "'" + credentials_ikev2_file_path + "'" + "\n " )
96
+ except (IOError , OSError ):
97
+ print (f"IOError while creating { credentials_ikev2_file_path } file." )
98
+
99
+
100
+
56
101
def askVPNUsername ():
57
102
"""
58
103
Asks VPN username by a dialog window
@@ -69,6 +114,22 @@ def askVPNPassword():
69
114
return simpledialog .askstring ("Password NordVPN" , "Enter password:" , show = "*" )
70
115
71
116
117
+ def askIkev2Username ():
118
+ """
119
+ Asks Ikev2 username by a dialog window
120
+ :return: the username inserted
121
+ """
122
+ return simpledialog .askstring ("Ikev2 NordVPN username" , "Enter username (see shorturl.at/lszBX):" )
123
+
124
+
125
+ def askIkev2Password ():
126
+ """
127
+ Asks Ikev2 password by a window dialog
128
+ :return: the password inserted
129
+ """
130
+ return simpledialog .askstring ("Ikev2 NordVPN password" , "Enter password (see shorturl.at/lszBX):" , show = "*" )
131
+
132
+
72
133
def read_saved_credentials ():
73
134
"""
74
135
reads saved credentials
@@ -83,4 +144,15 @@ def read_saved_credentials():
83
144
return cred [0 ], cred [1 ]
84
145
85
146
86
- credentials_file_path = CURRENT_PATH + "credentials"
147
+ def read_saved_credentials_ikev2 ():
148
+ """
149
+ reads saved credentials
150
+ :return: a tuple containing (username, password)
151
+ """
152
+ args = ['sudo' , 'cat' , credentials_ikev2_file_path ]
153
+ reading_process = subprocess .Popen (args , universal_newlines = True , stdout = subprocess .PIPE )
154
+ (out , _ ) = reading_process .communicate ()
155
+
156
+ cred = json .loads (out )
157
+
158
+ return cred ["username" ], cred ["password" ]
0 commit comments