4
\$\begingroup\$

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) Register')
    print('x - Exit')
    choice = input('>')

    while choice not in ['1','2','x']:
        choice = input('>')

    if choice == '1':
        login(state)
    elif choice == '2':
        register()
        main()
    elif choice == 'x':
        saveData()
        quit()

        if __name__ == '__main__':
    main()
\$\endgroup\$
3
\$\begingroup\$

The biggest problem you currently have is keeping the passwords in plain text - use a one-way irreversible hash function, 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.

\$\endgroup\$
  • \$\begingroup\$ Thanks for answer, passlib library and getpass module are awesome... \$\endgroup\$ – Newbie Feb 4 '17 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.