-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
auth.py
312 lines (283 loc) · 11.1 KB
/
auth.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
__filename__ = "auth.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.5.0"
__maintainer__ = "Bob Mottram"
__email__ = "[email protected]"
__status__ = "Production"
__module_group__ = "Security"
import base64
import hashlib
import binascii
import os
import secrets
from flags import is_system_account
from flags import is_memorial_account
from utils import data_dir
from utils import has_users_path
from utils import text_in_file
from utils import remove_eol
from utils import date_utcnow
def _hash_password(password: str) -> str:
"""Hash a password for storing
"""
salt = hashlib.sha256(os.urandom(60)).hexdigest().encode('ascii')
pwdhash = hashlib.pbkdf2_hmac('sha512',
password.encode('utf-8'),
salt, 100000)
pwdhash = binascii.hexlify(pwdhash)
return (salt + pwdhash).decode('ascii')
def _get_password_hash(salt: str, provided_password: str) -> str:
"""Returns the hash of a password
"""
pwdhash = hashlib.pbkdf2_hmac('sha512',
provided_password.encode('utf-8'),
salt.encode('ascii'),
100000)
return binascii.hexlify(pwdhash).decode('ascii')
def constant_time_string_check(string1: str, string2: str) -> bool:
"""Compares two string and returns if they are the same
using a constant amount of time
See https://sqreen.github.io/DevelopersSecurityBestPractices/
timing-attack/python
"""
# strings must be of equal length
if len(string1) != len(string2):
return False
ctr = 0
matched = True
for char in string1:
if char != string2[ctr]:
matched = False
else:
# this is to make the timing more even
# and not provide clues
matched = matched
ctr += 1
return matched
def _verify_password(stored_password: str, provided_password: str) -> bool:
"""Verify a stored password against one provided by user
"""
if not stored_password:
return False
if not provided_password:
return False
salt = stored_password[:64]
stored_password = stored_password[64:]
pw_hash = _get_password_hash(salt, provided_password)
return constant_time_string_check(pw_hash, stored_password)
def create_basic_auth_header(nickname: str, password: str) -> str:
"""This is only used by tests
"""
auth_str = \
remove_eol(nickname) + \
':' + \
remove_eol(password)
return 'Basic ' + \
base64.b64encode(auth_str.encode('utf-8')).decode('utf-8')
def authorize_basic(base_dir: str, path: str, auth_header: str,
debug: bool) -> bool:
"""HTTP basic auth
"""
if ' ' not in auth_header:
if debug:
print('DEBUG: basic auth - Authorisation header does not ' +
'contain a space character')
return False
if not has_users_path(path):
if not path.startswith('/calendars/'):
if debug:
print('DEBUG: basic auth - ' +
'path for Authorization does not contain a user')
return False
if path.startswith('/calendars/'):
path_users_section = path.split('/calendars/')[1]
nickname_from_path = path_users_section
if '/' in nickname_from_path:
nickname_from_path = nickname_from_path.split('/')[0]
if '?' in nickname_from_path:
nickname_from_path = nickname_from_path.split('?')[0]
else:
path_users_section = path.split('/users/')[1]
if '/' not in path_users_section:
if debug:
print('DEBUG: basic auth - this is not a users endpoint')
return False
nickname_from_path = path_users_section.split('/')[0]
if is_system_account(nickname_from_path):
print('basic auth - attempted login using system account ' +
nickname_from_path + ' in path')
return False
base64_str1 = auth_header.split(' ')[1]
base64_str = remove_eol(base64_str1)
plain = base64.b64decode(base64_str).decode('utf-8')
if ':' not in plain:
if debug:
print('DEBUG: basic auth header does not contain a ":" ' +
'separator for username:password')
return False
nickname = plain.split(':')[0]
if is_system_account(nickname):
print('basic auth - attempted login using system account ' + nickname +
' in Auth header')
return False
if nickname != nickname_from_path:
if debug:
print('DEBUG: Nickname given in the path (' + nickname_from_path +
') does not match the one in the Authorization header (' +
nickname + ')')
return False
if is_memorial_account(base_dir, nickname):
print('basic auth - attempted login using memorial account ' +
nickname + ' in Auth header')
return False
password_file = data_dir(base_dir) + '/passwords'
if not os.path.isfile(password_file):
if debug:
print('DEBUG: passwords file missing')
return False
provided_password = plain.split(':')[1]
try:
with open(password_file, 'r', encoding='utf-8') as fp_pass:
for line in fp_pass:
if not line.startswith(nickname + ':'):
continue
stored_password_base = line.split(':')[1]
stored_password = remove_eol(stored_password_base)
success = _verify_password(stored_password, provided_password)
if not success:
if debug:
print('DEBUG: Password check failed for ' + nickname)
return success
except OSError:
print('EX: failed to open password file')
return False
print('DEBUG: Did not find credentials for ' + nickname +
' in ' + password_file)
return False
def store_basic_credentials(base_dir: str,
nickname: str, password: str) -> bool:
"""Stores login credentials to a file
"""
if ':' in nickname or ':' in password:
return False
nickname = remove_eol(nickname).strip()
password = remove_eol(password).strip()
dir_str = data_dir(base_dir)
if not os.path.isdir(dir_str):
os.mkdir(dir_str)
password_file = dir_str + '/passwords'
store_str = nickname + ':' + _hash_password(password)
if os.path.isfile(password_file):
if text_in_file(nickname + ':', password_file):
try:
with open(password_file, 'r', encoding='utf-8') as fp_in:
with open(password_file + '.new', 'w+',
encoding='utf-8') as fout:
for line in fp_in:
if not line.startswith(nickname + ':'):
fout.write(line)
else:
fout.write(store_str + '\n')
except OSError as ex:
print('EX: unable to save password ' + password_file +
' ' + str(ex))
return False
try:
os.rename(password_file + '.new', password_file)
except OSError:
print('EX: unable to save password 2')
return False
else:
# append to password file
try:
with open(password_file, 'a+', encoding='utf-8') as fp_pass:
fp_pass.write(store_str + '\n')
except OSError:
print('EX: unable to append password')
return False
else:
try:
with open(password_file, 'w+', encoding='utf-8') as fp_pass:
fp_pass.write(store_str + '\n')
except OSError:
print('EX: unable to create password file')
return False
return True
def remove_password(base_dir: str, nickname: str) -> None:
"""Removes the password entry for the given nickname
This is called during account removal
"""
password_file = data_dir(base_dir) + '/passwords'
if os.path.isfile(password_file):
try:
with open(password_file, 'r', encoding='utf-8') as fp_in:
with open(password_file + '.new', 'w+',
encoding='utf-8') as fp_out:
for line in fp_in:
if not line.startswith(nickname + ':'):
fp_out.write(line)
except OSError as ex:
print('EX: unable to remove password from file ' + str(ex))
return
try:
os.rename(password_file + '.new', password_file)
except OSError:
print('EX: unable to remove password from file 2')
return
def authorize(base_dir: str, path: str, auth_header: str, debug: bool) -> bool:
"""Authorize using http header
"""
if auth_header.lower().startswith('basic '):
return authorize_basic(base_dir, path, auth_header, debug)
return False
def create_password(length: int):
valid_chars = 'abcdefghijklmnopqrstuvwxyz' + \
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
return ''.join((secrets.choice(valid_chars) for i in range(length)))
def record_login_failure(base_dir: str, ip_address: str,
count_dict: {}, fail_time: int,
log_to_file: bool) -> None:
"""Keeps ip addresses and the number of times login failures
occured for them in a dict
"""
if not count_dict.get(ip_address):
while len(count_dict.items()) > 100:
oldest_time = 0
oldest_ip = None
for ip_addr, ip_item in count_dict.items():
if oldest_time == 0 or ip_item['time'] < oldest_time:
oldest_time = ip_item['time']
oldest_ip = ip_addr
if oldest_ip:
del count_dict[oldest_ip]
count_dict[ip_address] = {
"count": 1,
"time": fail_time
}
else:
count_dict[ip_address]['count'] += 1
count_dict[ip_address]['time'] = fail_time
fail_count = count_dict[ip_address]['count']
if fail_count > 4:
print('WARN: ' + str(ip_address) + ' failed to log in ' +
str(fail_count) + ' times')
if not log_to_file:
return
failure_log = data_dir(base_dir) + '/loginfailures.log'
write_type = 'a+'
if not os.path.isfile(failure_log):
write_type = 'w+'
curr_time = date_utcnow()
curr_time_str = curr_time.strftime("%Y-%m-%d %H:%M:%SZ")
try:
with open(failure_log, write_type, encoding='utf-8') as fp_fail:
# here we use a similar format to an ssh log, so that
# systems such as fail2ban can parse it
fp_fail.write(curr_time_str + ' ' +
'ip-127-0-0-1 sshd[20710]: ' +
'Disconnecting invalid user epicyon ' +
ip_address + ' port 443: ' +
'Too many authentication failures [preauth]\n')
except OSError:
print('EX: record_login_failure failed ' + str(failure_log))