mirror of https://gitlab.com/bashrc2/epicyon
Tidy credentials storage
parent
4c69380348
commit
dabbba296d
36
auth.py
36
auth.py
|
|
@ -22,6 +22,7 @@ from utils import valid_nickname
|
||||||
from timeFunctions import date_utcnow
|
from timeFunctions import date_utcnow
|
||||||
from data import append_string
|
from data import append_string
|
||||||
from data import save_string
|
from data import save_string
|
||||||
|
from data import load_list
|
||||||
|
|
||||||
|
|
||||||
def _hash_password(password: str) -> str:
|
def _hash_password(password: str) -> str:
|
||||||
|
|
@ -194,20 +195,31 @@ def store_basic_credentials(base_dir: str,
|
||||||
store_str = nickname + ':' + _hash_password(password)
|
store_str = nickname + ':' + _hash_password(password)
|
||||||
if os.path.isfile(password_file):
|
if os.path.isfile(password_file):
|
||||||
if text_in_file(nickname + ':', password_file):
|
if text_in_file(nickname + ':', password_file):
|
||||||
try:
|
# get the existing passwords
|
||||||
with open(password_file, 'r', encoding='utf-8') as fp_in:
|
passwords_list = \
|
||||||
with open(password_file + '.new', 'w+',
|
load_list(password_file,
|
||||||
encoding='utf-8') as fout:
|
'EX: unable to load passwords ' + password_file +
|
||||||
for line in fp_in:
|
' [ex]')
|
||||||
if not line.startswith(nickname + ':'):
|
if passwords_list is None:
|
||||||
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
|
return False
|
||||||
|
|
||||||
|
# create the altered passwords list
|
||||||
|
passwords_list_new = ''
|
||||||
|
for login_str in passwords_list:
|
||||||
|
if not login_str.startswith(nickname + ':'):
|
||||||
|
passwords_list_new += login_str
|
||||||
|
else:
|
||||||
|
passwords_list_new += store_str + '\n'
|
||||||
|
|
||||||
|
# save the altered passwords list
|
||||||
|
if not save_string(passwords_list_new, password_file + '.new',
|
||||||
|
'EX: unable to update password ' +
|
||||||
|
password_file + '.new' + ' [ex]'):
|
||||||
|
passwords_list.clear()
|
||||||
|
return False
|
||||||
|
|
||||||
|
passwords_list.clear()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
os.rename(password_file + '.new', password_file)
|
os.rename(password_file + '.new', password_file)
|
||||||
except OSError:
|
except OSError:
|
||||||
|
|
|
||||||
2
data.py
2
data.py
|
|
@ -71,6 +71,8 @@ def load_list(filename: str, exception_text: str) -> str:
|
||||||
"""
|
"""
|
||||||
lines: list[str] = []
|
lines: list[str] = []
|
||||||
lines_str = load_string(filename, exception_text)
|
lines_str = load_string(filename, exception_text)
|
||||||
|
if lines_str is None:
|
||||||
|
return None
|
||||||
if lines_str:
|
if lines_str:
|
||||||
lines2 = lines_str.split('\n')
|
lines2 = lines_str.split('\n')
|
||||||
for line in lines2:
|
for line in lines2:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue