You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

76 lines
2.0 KiB

import streamlit_authenticator as stauth
from sys import argv
import yaml
def load_credentials():
with open('credentials.yaml','r') as f:
return yaml.safe_load(f)
def credentials(username, name, pwd):
pwd = stauth.Hasher([pwd]).generate()[0]
if username in list(load_credentials()['credentials']['usernames'].keys()):
print('Användarnamnet finns redan.')
return 'Användarnamnet finns redan.', False
credentials = {
'text': f'''
{username}:
email: '',
name: {name}
password: '***'
''',
'data': {username:
{'email': '',
'name': name,
'password': pwd}
}
}
for k, v in credentials.items():
print(k, v)
return credentials, True
def update(c, print_out=True, update=False):
if print_out:
print(c['text'])
if not update:
if input('Update credentials file? (y/n)') in ['y', 'yes']:
update = True
if update:
try:
cur_yaml = load_credentials()
cur_yaml['credentials']['usernames'].update(c['data'])
if cur_yaml:
with open('credentials.yaml','w') as f:
yaml.safe_dump(cur_yaml, f)
except FileNotFoundError:
print('Found no yaml file')
return c['text'], False
if __name__ == '__main__':
if len(argv) == 2:
if argv[1] == 'help':
print('username, name, pwd')
exit()
pwd = argv[1]
print(stauth.Hasher([pwd]).generate()[0])
elif len(argv) == 4:
username = argv[1]
name = argv[2]
pwd = argv[3]
c = credentials(username, name, pwd)
update(c)
else:
pwd = input('Password: ').strip()
username = input('Username: ')
if username != '':
name = input('Name: ')
c = credentials(username, name, pwd)
update(c[0])
else:
print(stauth.Hasher([pwd]).generate()[0])