patternpythonMinor
Login program in Python
Viewed 0 times
loginprogrampython
Problem
I wrote this login program as part of my project for school. Should I use my implementation of a login program, or is there some standardized patterns of login programs, like the ones in encryption where we use made packages instead of writing our own?
Also, please correct me if am doing anything wrong or if there is a faster and more elegant way.
```
users = []
"""
Look of data.txt
john|mckinly|tree|ssap321
elie|rose|sweet|pass123
"""
def loadData():
with open('data.txt','r') as data:
for line in data:
user = makeUser(line)
users.append(user)
return True
def saveData():
with open('data.txt','w') as data:
for user in users:
print(user['name']+'|'+user['surname']+'|'+user['username']+'|'+user['password'],file = data)
def makeUser(line):
name, surname, username, password = line.split('|')
if password[-1:] == '\n':
password = password[:-1]
return {'name':name,
'surname':surname,
'username':username,
'password':password
}
def register():
name = input('Name:')
surname = input('Surname:')
while True:
username = input('Username:')
if checkLen(username):
break
while True:
password = input('Password:')
if checkLen(password):
break
users.append({'name':name,'surname':surname,'username':username,'password':password})
def checkLen(info):
if len(info) > 0:
return True
else:
print('Can\'t be blank!')
def login(state):
while state:
username = input('Username:')
password = input('Password:')
for user in users:
if user['username'] == username and user['password'] == password:
print('You are logged in.')
state = False
break
else:
print('Wrong input.')
def main():
state = loadData()
print('1) Login')
print('2)
Also, please correct me if am doing anything wrong or if there is a faster and more elegant way.
```
users = []
"""
Look of data.txt
john|mckinly|tree|ssap321
elie|rose|sweet|pass123
"""
def loadData():
with open('data.txt','r') as data:
for line in data:
user = makeUser(line)
users.append(user)
return True
def saveData():
with open('data.txt','w') as data:
for user in users:
print(user['name']+'|'+user['surname']+'|'+user['username']+'|'+user['password'],file = data)
def makeUser(line):
name, surname, username, password = line.split('|')
if password[-1:] == '\n':
password = password[:-1]
return {'name':name,
'surname':surname,
'username':username,
'password':password
}
def register():
name = input('Name:')
surname = input('Surname:')
while True:
username = input('Username:')
if checkLen(username):
break
while True:
password = input('Password:')
if checkLen(password):
break
users.append({'name':name,'surname':surname,'username':username,'password':password})
def checkLen(info):
if len(info) > 0:
return True
else:
print('Can\'t be blank!')
def login(state):
while state:
username = input('Username:')
password = input('Password:')
for user in users:
if user['username'] == username and user['password'] == password:
print('You are logged in.')
state = False
break
else:
print('Wrong input.')
def main():
state = loadData()
print('1) Login')
print('2)
Solution
The biggest problem you currently have is keeping the passwords in plain text - use a one-way irreversible hash function, like
Going even further, you should not be doing it yourself and instead trust something stable that have already proved itself of being mature and safe - like
If you know that user authentication part is going to become more complicated, think about using a specialized User Directory like
Also, when a password is entered, the input should be hidden - there is a
There are also different third-party libraries, like
argon2 or bcrypt. Going even further, you should not be doing it yourself and instead trust something stable that have already proved itself of being mature and safe - like
passlib library.If you know that user authentication part is going to become more complicated, think about using a specialized User Directory like
LDAP or Active Directory - there is a third-party ldap package that you can use for your client implementation. Also, when a password is entered, the input should be hidden - there is a
getpass module specifically for this kind of use case. Also, look into getuser() function for the login prompt.There are also different third-party libraries, like
click or cement that might help in making building the CLI apps easier. Explore the other CLI libraries at the awesome-python list.Context
StackExchange Code Review Q#154374, answer score: 3
Revisions (0)
No revisions yet.