forked from mi8882255/3dprinteros-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_login.py
144 lines (129 loc) · 5.26 KB
/
user_login.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#Copyright (c) 2015 3D Control Systems LTD
#3DPrinterOS client is free software: you can redistribute it and/or modify
#it under the terms of the GNU Affero General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#3DPrinterOS client is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU Affero General Public License for more details.
#You should have received a copy of the GNU Affero General Public License
#along with 3DPrinterOS client. If not, see <http://www.gnu.org/licenses/>.
# Author: Vladimir Avdeev <another.vic@yandex.ru>, Oleg Panasevych <panasevychol@gmail.com>
import os
import sys
import json
import base64
import zipfile
import logging
import paths
import http_client
from checker_waiter import CheckerWaiter
def pack_login_zip(package_name, path, *args):
logger = logging.getLogger('app.' + __name__)
path = path
package_path = os.path.join(path, package_name)
temp_file_path = os.path.join(path, 'info')
temp_file = open(temp_file_path, 'w')
for arg in args:
arg = base64.b64encode(arg)
temp_file.write(arg + '\n')
temp_file.close()
try:
zf = zipfile.ZipFile(package_path, mode='w')
if sys.platform.startswith('win'):
s = "\\"
else:
s = "/"
zf.write(temp_file_path, temp_file_path.split(s)[-1])
zf.close()
except Exception as e:
logger.error('Packing error: ' + e.message)
return
os.remove(temp_file_path)
return True
def read_login_zip(package_name, path):
logger = logging.getLogger('app.' + __name__)
path = path
package_path = os.path.join(path, package_name)
if os.path.exists(package_path):
zf = zipfile.ZipFile(package_path, 'r')
packed_info = zf.read('info')
packed_info = packed_info.split('\n')
packed_info.remove('')
for number in range(0, len(packed_info)):
packed_info[number] = base64.b64decode(packed_info[number])
return packed_info
else:
logger.error(package_name + ' not found')
def read_login():
logger = logging.getLogger('app.' + __name__)
pack_name = 'login_info.bin'
paths_to_settings = paths.get_paths_to_settings_folder()
for path in paths_to_settings:
logger.info("Searching for login info in %s" % path)
try:
login_info = read_login_zip(pack_name, path)
if login_info:
logger.info('Login info loaded from ' + path)
return login_info
except Exception as e:
logger.warning('Failed loading login from ' + path + '. Error: ' + e.message)
logger.info("Can't read login info in %s" % str(path))
logger.info('No login info found')
return (None, None)
def write_login(login, password):
logger = logging.getLogger('app.' + __name__)
package_name = 'login_info.bin' # probably it shoud be read from config
path = paths.current_settings_folder()
try:
result = pack_login_zip(package_name, path, login, password)
except Exception as e:
logger.warning('Login info writing error! ' + e.message)
else:
if result == True:
logger.info('Login info was written and packed.')
else:
logger.warning("Login info wasn't written.")
return True
return False
class UserLogin(CheckerWaiter):
NAME_FOR_LOGGING = 'User login'
def __init__(self, app):
self.app = app
self.login = None
self.user_token = None
self.http_client = http_client.HTTPClient()
CheckerWaiter.__init__(self, app, self.check_if_login_complete)
login, password = read_login()
if login:
error = self.login_as_user(login, password)
if error:
self.logger.info(str(error))
def check_if_login_complete(self):
return bool(self.user_token)
def login_as_user(self, login, password):
answer = self.http_client.pack_and_send('user_login', login, password)
if not answer:
return 0, "No connection to server"
else:
user_token = answer.get('user_token', None)
profiles_str = answer.get('all_profiles', None)
error = answer.get('error', None)
if error:
self.logger.warning("Error processing user_login " + str(error))
self.logger.error("Login rejected")
return error['code'], error['message']
elif user_token and profiles_str:
if write_login(login, password):
self.login = login # for web_interface to display
self.user_token = user_token
try:
profiles = json.loads(profiles_str)
except Exception as e:
self.logger.warning("Error while parsing profiles: " + str(e))
return 42, "Error parsing profiles"
self.profiles = profiles
self.logger.info("Successful login from user " + login)
return
return 43, "Error saving login and password"