Skip to content

Commit 093bee2

Browse files
fix(ikev2): correct ikev2 credentials storing
Save ikev2 credentials in a separate file
1 parent e662b27 commit 093bee2

File tree

4 files changed

+98
-12
lines changed

4 files changed

+98
-12
lines changed

bin/credentials.py

+73-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import os
2+
import json
23
import subprocess
34
from tkinter import simpledialog
45
from bin.pathUtil import CURRENT_PATH
56

67
CREDENTIALS_FILENAME = "credentials"
8+
credentials_file_path = CURRENT_PATH + "credentials"
9+
credentials_ikev2_file_path = CURRENT_PATH + "credentials.ikev2"
710

811

912
class NoCredentialsProvidedException(Exception):
@@ -53,6 +56,48 @@ def save_credentials():
5356
print("IOError while creating 'credentials' file.")
5457

5558

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+
56101
def askVPNUsername():
57102
"""
58103
Asks VPN username by a dialog window
@@ -69,6 +114,22 @@ def askVPNPassword():
69114
return simpledialog.askstring("Password NordVPN", "Enter password:", show="*")
70115

71116

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+
72133
def read_saved_credentials():
73134
"""
74135
reads saved credentials
@@ -83,4 +144,15 @@ def read_saved_credentials():
83144
return cred[0], cred[1]
84145

85146

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"]

bin/gui.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ def connect_to_VPN(self, server, protocol):
216216
return
217217
except LoginError:
218218
messagebox.showwarning(title="Error", message="Wrong credentials")
219-
os.remove(credentials_file_path)
219+
if protocol == IKEV2_PROTOCOL_NUMBER:
220+
os.remove(credentials_ikev2_file_path)
221+
else:
222+
os.remove(credentials_file_path)
220223
self.setStatusDisconnected()
221224
return
222225
except OpenresolvError:

bin/vpn_util/ikev2.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
' load = no' + linesep + \
3838
'}' + linesep
3939

40+
SUCCESS_STRING = "connection 'NordVPN' established successfully"
41+
FAILURE_STRING = "establishing connection 'NordVPN' failed"
42+
AUTH_FAILURE_STRING = "EAP authentication failed"
43+
CONFIG_NOT_FOUND_STRING = "no config named 'NordVPN'"
44+
4045
logger = get_logger(__name__)
4146

4247
def ipsec_exists():
@@ -112,11 +117,6 @@ def __ikev2_reset_load__():
112117
return
113118

114119

115-
SUCCESS_STRING = "connection 'NordVPN' established successfully"
116-
FAILURE_STRING = "establishing connection 'NordVPN' failed"
117-
AUTH_FAILURE_STRING = "EAP authentication failed"
118-
CONFIG_NOT_FOUND_STRING = "no config named 'NordVPN'"
119-
120120
def __ikev2_launch__():
121121
"""
122122
Launches the command the start the ikev2 connection. Raise a LoginError if credentials are wrong, a ConnectionError
@@ -175,10 +175,11 @@ def __ikev2_ipsec_reload__():
175175
restarts ipsec (used to load saved settings)
176176
"""
177177
args = ['sudo', 'ipsec', 'restart']
178-
(out, _) = Popen(args, stdout=PIPE, universal_newlines=True).communicate()
178+
(_, _) = Popen(args, stdout=PIPE, universal_newlines=True).communicate()
179179

180180
return
181181

182+
182183
def ikev2_connect(username, password, server):
183184
"""
184185
starts a ikev2 connection. Launches a ConnectionError if no connection is available, a LoginError if the

bin/vpn_util/vpn.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from bin.vpn_util.ikev2 import ikev2_connect, ikev2_is_running, ikev2_disconnect, ipsec_exists
22
from bin.vpn_util.openvpn import *
3+
from bin.credentials import read_saved_credentials_ikev2, check_credentials_ikev2, save_credentials_ikev2
34
from bin.vpn_util.nm import nm_running_vpn, nm_disconnect, nm_connect, nm_openvpn_exists
45
IPSEC_EXISTS = ipsec_exists()
56

@@ -14,6 +15,18 @@ def startVPN(server, protocol, nm):
1415
:param nm: a boolean: True if network manager should be used, false otherwise
1516
:return: a string representing the connection established
1617
"""
18+
19+
if protocol == IKEV2_PROTOCOL_NUMBER: # if it is ikev2/ipvsec
20+
if not check_credentials_ikev2():
21+
try:
22+
save_credentials_ikev2()
23+
except NoCredentialsProvidedException:
24+
return None
25+
username, password = read_saved_credentials_ikev2()
26+
27+
ikev2_connect(username, password, server)
28+
return IPSEC_CONNECTION_STRING
29+
1730
if not check_credentials():
1831
try:
1932
save_credentials()
@@ -22,10 +35,7 @@ def startVPN(server, protocol, nm):
2235

2336
username, password = read_saved_credentials()
2437

25-
if protocol == IKEV2_PROTOCOL_NUMBER: # if it is ikev2/ipvsec
26-
ikev2_connect(username, password, server)
27-
return IPSEC_CONNECTION_STRING
28-
elif nm and nm_openvpn_exists():
38+
if nm and nm_openvpn_exists():
2939
nm_connect(server, protocol, username, password)
3040
return NM_CONNECTION_STRING
3141
else:

0 commit comments

Comments
 (0)