Revert "Replace save and append with functions"

This reverts commit fc811a3652.
main
bashrc 2026-04-26 17:14:10 +01:00
parent c7e945567b
commit 306472eb3d
1 changed files with 21 additions and 18 deletions

39
auth.py
View File

@ -20,8 +20,6 @@ from utils import text_in_file
from utils import remove_eol from utils import remove_eol
from utils import valid_nickname from utils import valid_nickname
from timeFunctions import date_utcnow from timeFunctions import date_utcnow
from data import append_string
from data import save_string
def _hash_password(password: str) -> str: def _hash_password(password: str) -> str:
@ -215,12 +213,18 @@ def store_basic_credentials(base_dir: str,
return False return False
else: else:
# append to password file # append to password file
if not append_string(store_str + '\n', password_file, try:
'EX: unable to append password'): 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 return False
else: else:
if not save_string(store_str + '\n', password_file, try:
'EX: unable to create password file'): 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 False
return True return True
@ -301,15 +305,14 @@ def record_login_failure(base_dir: str, ip_address: str,
write_type: str = 'w+' write_type: str = 'w+'
curr_time = date_utcnow() curr_time = date_utcnow()
curr_time_str = curr_time.strftime("%Y-%m-%d %H:%M:%SZ") curr_time_str = curr_time.strftime("%Y-%m-%d %H:%M:%SZ")
log_str = \ try:
curr_time_str + ' ' + 'ip-127-0-0-1 sshd[20710]: ' + \ with open(failure_log, write_type, encoding='utf-8') as fp_fail:
'Disconnecting invalid user epicyon ' + ip_address + ' port 443: ' + \ # here we use a similar format to an ssh log, so that
'Too many authentication failures [preauth]\n' # systems such as fail2ban can parse it
# here we use a similar format to an ssh log, so that fp_fail.write(curr_time_str + ' ' +
# systems such as fail2ban can parse it 'ip-127-0-0-1 sshd[20710]: ' +
if write_type == 'a+': 'Disconnecting invalid user epicyon ' +
append_string(log_str, failure_log, ip_address + ' port 443: ' +
'EX: record_login_failure failed ' + str(failure_log)) 'Too many authentication failures [preauth]\n')
else: except OSError:
save_string(log_str, failure_log, print('EX: record_login_failure failed ' + str(failure_log))
'EX: record_login_failure failed ' + str(failure_log))