Compare login info to csv data and grant access - python

My program asks the user whether they have an account or not. If the user doesn't have an account, then the program creates a new account for the user and stores their details in a csv file. This part works fine. But if the user runs the program again (after account creation) and tries to log in with the same details (username, password), it doesn't work. How do I compare the login info to csv data and grant access?
##Two Subjects----> Computer Science and History
import csv
def main():
userAccount()
def userAccount():
print('-------------------------------')
userAccount = input('Do you already have an account: ')
if userAccount == 'No' or userAccount== 'N' or userAccount == 'no' or userAccount == 'n':
userSName = input('Name : ')
userSAge = input('Age : ')
userSYgroup = input('Year Group : ')
userGname = userSName[:3] + userSAge
print('Your username is : ',userGname.lower())
userSword = input('Password: ')
detail1 = [userSName,userSAge,userSYgroup,userGname,userSword]
with open('userData.csv','w',newline = '') as csvfile:
w = csv.writer(csvfile,delimiter = ',')
w.writerow(detail1)
elif userAccount == 'Yes' or userAccount == 'Y' or userAccount == 'yes' or userAccount == 'y':
userLName = input('Username: ')
passLWord = input('Password: ')
f = open('userData.csv','r',newline = '')
for i in f:
if userLName == f[0,3] and passLWord == f[0,4]:
print('Log In Successfull')
else:
print('Incorrect Username or Password')
main()
main()

Here's a fully working version with slight changes. Add it to your original code. Though it might break your current code, because I changed a few variables name. Rename them accordingly.
##Two Subjects----> Computer Science and History
import csv
def main():
userAccount()
print("rest of the code")
input() # keep window open
def userAccount():
print('-------------------------------')
# group possible inputs; no need for individual checks
noAccount = ["no", "NO", "n", "N"]
yesAccount = ["yes", "YES", "y", "Y"]
hasAccount = input('Do you already have an account: ')
if hasAccount in noAccount:
userName = input('Name : ')
userAge = input('Age : ')
userYear = input('Year Group : ')
userLogin = (userName[:3] + userAge).lower()
print('Your username is : ', userLogin)
userPW = input('Password: ')
details = [userName, userAge, userYear, userLogin, userPW]
# open file with 'a' instead of 'w' so multiple records can be added to csv
with open('userData.csv', 'a', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
writer.writerow(details)
# once the account is created, return to beginning
main()
elif hasAccount in yesAccount:
userLogin = input('Username: ')
userPW = input('Password: ')
granted = None
with open('userData.csv', 'r', newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
# loop every row (record) in the csv file
for row in reader:
csvLogin = row[3]
csvPW = row[4]
if userLogin == csvLogin and userPW == csvPW:
granted = True
print('Log In Successfull')
# a user is matched; no need to check other records
break
# if input doesn't match any csv record, return to beginning
if granted is None:
print('Incorrect Username or Password')
main()
else:
# if user enters something other than yes/no, return to beginning
main()
main()

Related

Failing to print value

I'm new to python, an I decided to write a simple password manager to learn. I'm having trouble retrieving one of the values out of the dictionary.
The function add_password write a key with 2 values (user, and password (encrypted))
And the function get_password read the key and supposed to get the values.
I can't get it to pull the value of user.
I have tried multiple methods, but so far no luck.
https://github.com/ae3erdion/Password_Manager/blob/main/main.py
import base64
import os
import string
import random
from unittest import TestCase
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
characters = list(string.ascii_letters + string.digits + "!##$%^&*()")
site = ""
user = ""
password = ""
password_file = {}
encrypted = ""
# Generate key for encryption
def generate_key():
password = input ("Enter password: ")
password = bytes(password, 'utf-8')
salt = b'\xceN\x01s\xabE\x15\x02\xd9pz(1\t\xbc4'
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=390000,
)
global key
key = base64.urlsafe_b64encode(kdf.derive(password))
# Check for the encryption key hash file exists and validate if the key is the same
if os.path.exists('password.hash'):
with open('password.hash', 'rb') as f:
key_validation = f.read()
if key_validation == key:
print("What would you like to do: ")
menu()
else:
print("Wrong password ")
exit
# If key hash file doesnt exist it create the file and write the encryption key hash to it
else:
with open('password.hash', 'wb') as f:
f.write(key)
with open('password.encode', 'wb') as f:
print("What would you like to do: ")
menu()
# Randon password generator
def generate_password():
length = int(16)
random.shuffle(characters)
random_password = []
for i in range(length):
random_password.append(random.choice(characters))
random.shuffle(random_password)
random_password = ("".join(random_password))
print(random_password)
# Write a new password to the pasword file
def add_password(site, user, password_site):
password_file[site] = password_site
with open('password.encode', 'a+') as f:
encrypted = Fernet(key).encrypt(password_site.encode())
f.write(site + " " + user + ":" + encrypted.decode() + "\n")
# Read password file and get the password
def get_password(site):
with open('password.encode', 'r') as f:
for line in f:
site, encrypted = line. split (":")
password_file[site] = Fernet(key).decrypt(encrypted.encode()).decode()
return password_file[site]
# Check for all files.
def main():
if os.path.exists('password.hash') & os.path.exists('password.encode'):
print ("Welcome Back!")
generate_key()
else:
print ("""
Welcome!
Create password""")
generate_key()
# Menu of options
def menu():
print("""
(1) Generate random password
(2) Add new password
(3) Get login information
(q) Quit""")
done = False
while not done:
choice = input("Enter choice: ")
if choice == "1":
generate_password()
elif choice == "2":
site = input("Enter the site: ")
user = input("Enter User: ")
password = input("Enter the password: ")
add_password(site, user, password)
elif choice == "3":
site = input("Enter site: ")
print(f"Your login information for {site} is ({get_password(site)})")
elif choice == "q":
done = True
print("Bye!")
else:
print("Invalid Choice")
if __name__== "__main__":
main()
If you change you get_password function to:
def get_password(site):
with open('password.encode', 'r') as f:
for line in f:
site, encrypted = line.split (":")
site, user = site.split()
password_file[site] = (user, Fernet(key).decrypt(encrypted.encode()).decode())
return password_file[site]
and change option 3 in your menu function
elif choice == "3":
site = input("Enter site: ")
user, passwd = get_password(site)
print(f"Your login information for {user}#{site} is ({passwd})")
You will still want to implement some error checking but that should will at least get you started.

How to save the data gathered in the address book?

I am using a file in text edit ubuntu. I am trying to figure out a way to save it. When I close the python shell and rerun it, it deletes all past info. Would I have to make a new function?
AdressBook = '/home/bookworm/AdressBook.txt'
contacts = {}
def write():
Data = open(AdressBook,'w')
for name, many in contacts.items():
Data.write(name + ',' + many)
Data.close()
def read():
Data = open(AdressBook, 'r')
stuff = Data.read()
print(stuff)
Data.close()
user = input('Would you like to acess your adress book?')
if user == 'yes' or 'yep' or 'y' or 'Yes' or 'YES!' or 'YES' or 'Yurp' or 'Yeppers' or
'si'or'1':
It is only the deleted and added contacts to be saved
while user != 5:
user = input('''Select One:
Press 1 to update
Press 2 to display all contacts
Press 3 to search adress book
Press 4 to delete contacts
Press 5 to quit your adress book.''')
if user == '1':
name = input('Please enter the name of the contact that you would like to add.')
contact = input('Please enter the contact information of %s.'%name)
Name = '\n' + name
contacts.update( {Name : contact} )
print('')
print('%s was added to your adress book!' %name)
print('')
write()
elif user == '2':
read()
To call read function
elif user == '3':
name = input('What is the name of the person whose contacts you need?')
print(contacts[name])
To delete a contact
elif user == '4':
name = input('Type in the name of the contact that you would like to delete.')
del contacts[name]
print('Your contact List has sucessfully deleted %s' %name)
write()
elif user == '5':
print('Thank you for acessing your adress book!')
exit()
else:
exit()
Your file becomes empty everytime because you are taking contacts = {} in the start of the program. So instead of creating an empty dictionary, you need to initialize the dictionary with the file contents in the starting.
AdressBook = '/home/bookworm/AdressBook.txt'
contacts = initialize_contacts()
def write():
Data = open(AdressBook,'w')
for name, many in contacts.items():
Data.write(name + ',' + many + '\n')
Data.close()
def read():
Data = open(AdressBook, 'r')
stuff = Data.read()
print(stuff)
Data.close()
return stuff
def initialize_contacts():
data = read()
contact_list = data.split('\n')
for contact in contact_list[:-1]:
name,cont = contact.split(',')
contacts[name] = cont

Append key and value to json file

I am making a user system. I need to store the usernames and passwords in a file.
Case 1: If existing user=
I need to read the username and password to check existing user.
I need to check if the passwords match from the keyed in value and from the dictionary value.
Case 2 : If new user=
I need to check if the username already exist in the database.
If not then, i need to append the username and password in existing file without overwriting.
The problem here, i have utilised json here but it seems to overwrite the existing dict.
I have tried writing to a simple text file and i encounter problem in case when reading the file and also when i check if username exists in case 2.
# Login System Management
import json
class LoginSystem:
def __init__(self): # Opening and reading the registered users json.file
self.users = json.load(open("Users.json"))
self.login_screen()
def login_screen(self): # Log on screen to verify new or old user.
while True:
main_log = input("Are you new user?\n[Y]es or [N]o: ")
if main_log == "Y":
self.new_user()
break
elif main_log == "N":
self.old_user()
break
else:
print("Invalid answer.\n")
def old_user(self): # Log in screen if old user.
while True:
user_id = input("Please enter your user id: ")
user_password = input("Please enter your password: ")
if len(user_id) <= 64 and len(user_password) <= 64 and self.check_system(user_id, user_password):
print("Logging In")
break
else:
print("Wrong password or username!\n")
def check_system(self, name, password): # Checking system to verify old user id and password.
data = self.users
try:
expected_password = data[name]
except KeyError:
return False
if password != expected_password:
return False
return True
def new_user(self): # Log in screen if new user.
while True:
print("\nMax Length is 64 chars.")
reg_id = input("Register your username: ")
reg_password = input("Key in password: ")
if len(reg_id) <= 64 and len(reg_password) <= 64:
if reg_id not in self.users:
print("Loading.....")
self.update_database(reg_id, reg_password)
print("Registered Successfully.")
break
else:
print("User already registered!\n")
self.old_user()
break
else:
print("Error. Max Length is 64 chars.\n")
def update_database(self, new_user_name, new_user_password): # Writing new username and password to json.file
new_username = new_user_name
new_password = new_user_password
field = [new_username, new_password]
with open("Users.json", "w") as f:
json.dump(field, f)
check = LoginSystem()
The problem is that you are opening the csv file in "write" mode. This mode replaces what you have written in the database so far with the new line. Use "append" instead.
with open("Users.json", "a") as f:
I had figure out the answer. If anyone wants to refer you can follow.
First, I have created a csv file with row of line username,password and save it in the same directory.
The rest as follows the code.
# Login System Management import csv import time
class LoginSystem:
def __init__(self): # Opening
self.login_screen()
def login_screen(self): # Log on screen to verify new or old user.
while True:
main_log = input("Are you new user?\n[Y]es or [N]o: ")
if main_log == "Y":
self.new_user()
break
elif main_log == "N":
self.old_user()
break
else:
print("Invalid answer.\n")
def old_user(self): # Log in screen if old user.
while True:
user_id = input("\nPlease enter your user id: ")
user_password = input("Please enter your password: ")
if len(user_id) <= 64 and len(user_password) <= 64 and self.read_database(user_id, user_password):
print("Successful")
break
else:
print("Wrong password or username!\n")
def new_user(self): # Log in screen if new user.
print("\nMax Length is 64 chars.")
reg_id = input("Register your username: ")
if self.check_database(reg_id) is True:
while True:
reg_password = input("Key in password: ")
if len(reg_id) <= 64 and len(reg_password) <= 64:
print("Loading.....")
time.sleep(2)
self.update_database(reg_id, reg_password)
print("Registered Successfully.")
break
else:
print("Error. Max Length is 64 chars.\n")
else:
print("User Already Exists.\n")
self.old_user()
def read_database(self, name, password): # Checking if password match to username
with open("Users.csv", "r") as f:
reader = csv.reader(f)
user_list = {}
for row in reader:
user_list[row[0]] = row[1]
try:
expected_password = user_list[name]
if password == expected_password:
print("Logging In")
time.sleep(2)
return True
except KeyError:
return False
if password != expected_password:
return False
def check_database(self, new_name): # Checking if new id exists in user database
with open("Users.csv", "r") as f:
reader = csv.reader(f)
user_list = {}
for row in reader:
user_list[row[0]] = row[1]
if new_name in user_list.keys():
return False
elif new_name not in user_list.keys():
return True
def update_database(self, new_user_name, new_user_password): # Writing new username and password to file
with open("Users.csv", "a", newline="\n") as f: # appends the new username and password to new row of line
writer = csv.writer(f)
writer.writerow([new_user_name, new_user_password])
check = LoginSystem()

csv - Creating a login system, python not properly reading from .csv file?

I am trying to make a login system that is looped basically and whenever I try to enter the correct details that are even stored in the .csv file, it outputs as incorrect username/password no matter what I put. This code works for python 3.6 but I need it to work for python 3.2.3.
loop1 = False #for this bit of code (logging in)
loop2 = False #for next bit of code
while loop1 == False:
choice = input("Login/SignUp [TYPE 'L' OR 'S']: ").lower()
if choice == "l":
username = input("Username: ")
password = input("Password: ")
f = open("usernamepassword.csv","r")
for line in f:
details = line.split(",")
if username == details[0] and password == details[1]:
print("Welcome")
break
#this whole bit of code is meant to read from the csv and check if the login details are correct
else:
print("Username/Password [INCORRECT]")
Allow me to refactor your code:
def login(username, password):
with open("usernamepassword.csv", "r") as csv:
all_details =
[[attr.strip() for attr in line.split(",")]
for line in csv]
return any(
username == details[0]
and password == details[1]
for details in all_details)
def login_action():
username = input("Username: ")
password = input("Password: ")
if not login(username, password):
raise ValueError("Username/Password [INCORRECT]")
return True
_USER_ACTIONS = {
'l': login_action
}
def main():
while True:
choice = input("Login/SignUp [TYPE 'L' or 'S']: ").lower()
action = _USER_ACTIONS[choice]
try:
if action():
break
except Exception as err:
print(err.message)
I think your unexpected behavior comes from not stripping the values you get after splitting by ,
Solved by replacing:
if username == details[0] and password == details[1]:
With:
if username == details[0] and (password+"\n") == details[1]:
You may have a bug in line.split(','), try line.strip().split(',')
TL; DR: posted a proper solution there : https://github.com/cgte/stackoverflow-issues/tree/master/47207293-csv-dict
I'll stuff up my answer later if needed.
Furthermore you have a poor code design here, and find yourself debugging in the middle of a loop.
So first of all : load the data file, store content to a dict.
f = open("usernamepassword.csv","r")
for line in f:
details = line.split(",")
if username == details[0] and password == details[1]:
print("Welcome")
break
Should become
user_pass = {}
f = open("usernamepassword.csv","r")
for line in f:
user, password = line.strip().split(",")
user_pass[user] = password
f.close()
or better
with open("usernamepassword.csv","r") as f:
for line in f.readlines():
user, password = line.split().split(",")
user_pass[user] = password
eventually run python -i yourfile.py and type "user_pass" to see what is actually stored when correct go on further code.
Think of using the csv module : https://docs.python.org/3/library/csv.html
Then get username and password from input and check:
if login in user_pass and user_pass[login] = password:
# or better `if user_pass.get(login, None) == password:`
do_stuff()

Dictionary empties after running program

I'm using python 2.7.5 and i'm trying to make a simple program that has a username, password, and checks if it exists in a dictionary. If true, it prints welcome + username, and ignores if false.
First: code.
#!/usr/bin/python
import csv
users = {}
with open('C:\\Users\\chef\\Python\\fn.csv', 'wb') as f: # Just use 'w' mode in 3.x
w = csv.DictWriter(f, users.keys())
w.writeheader()
w.writerow(users)
def new_user():
uname = raw_input("Choose a username: ")
while 1:
pwd = raw_input("Choose a password: ")
check = raw_input("Retype password: ")
if pwd == check:
print "Saved."
users[uname] = pwd
break
if uname in users.keys():
pass
def show_users():
for unames in users.keys():
print unames
def login():
uname = raw_input("Username: ")
pwd = raw_input("Password: ")
if uname in users and pwd in users.values():
print "Welcome, " + uname + "! "
def save():
f=open('C:\\Users\\chef\\Python\\fn.csv', "wb")
w = csv.writer(f)
for key, val in users.items():
w.writerow([key, val])
f.close()
def read():
with open('C:\\Users\\chef\\Python\\fn.csv', 'wb') as f: # Just use 'w' mode in 3.x
w = csv.DictWriter(f, users.keys())
w.writeheader()
w.writerow(users)
print "Welcome to Yubin's 'fake' Email server."
while 1:
read()
choice = raw_input("What would you like to do? ")
if choice == "signup":
new_user()
if choice == "login":
login()
if choice == "showusers":
show_users()
if choice == "logout":
print "You have successfully logged out."
if choice == "quit":
x = raw_input("Are you sure? (y/n) ")
if x == "y":
save()
break
else:
pass
else:
print "Please sign up, log in or see who have signed up."
Problems:
When I first "sign up", i can log in perfectly fine. But, after closing the program and running it again, i can't log in. i assume it's because i set the dictionary empty every time i start, but it's supposed to rewrite the contents into the dictionary. i use windows 7 and in the preview, when i rerun the program, the file becomes empty.
After i write either login, signup or showusers, it prints the last line,
"Please sign up, log in or see who have signed up. "
Please i ask for solutions, and thank you in advance.
EDIT: I solved problem #2, but #1 still is there.
The problem is, as hyades stated, that your read-method overwrites the csv-file instead of reading it. The csv-module offers a reader for this purpose. I have changed your read-method like this to make it work:
def read():
with open('C:\\Users\\chef\\Python\\fn.csv', 'r') as f:
usersReader = csv.reader(f)
for row in usersReader:
if row == []:
pass
else:
users[row[0]] = row[1]
f.close();
You can also remove the "with open..."-code block at the begining of the file (after import and users-declaration).
For Problem 1, might be an issue with the mode of the file. Change it to wb+
Opens a file for both writing and reading in binary format. Overwrites
the existing file if the file exists. If the file does not exist,
creates a new file for reading and writing.
Problem 2 will be solved if you use if..elif instead of if
Check this code above. I use only save and read function. I used read function only once, outside while loop (used 'rb' mode istaed 'wb' mode). I used csv.WriteDict and csv.ReadDict classes enter link description here. to read and save data from users dict. I think You can use shelve or json instead csv, propobly these way will by faster and simply ;)
#!/usr/bin/python
import csv
users = {}
'''def write():
with open('/home/marcin/fn.csv', 'wb+') as f: # Just use 'w' mode in 3.x
w = csv.DictWriter(f, users.keys())
w.writeheader()
w.writerow(users)'''
def new_user():
uname = raw_input("Choose a username: ")
while 1:
pwd = raw_input("Choose a password: ")
check = raw_input("Retype password: ")
if pwd == check:
print "Saved."
users[uname] = pwd
break
if uname in users.keys():
pass
def show_users():
for unames in users.keys():
print unames
def login():
uname = raw_input("Username: ")
pwd = raw_input("Password: ")
if uname in users and pwd in users.values():
print "Welcome, " + uname + "! "
def save():
with open('/home/marcin/fn.csv', "wb+") as f:
fieldnames=['user','pwd']
writer = csv.DictWriter(f,fieldnames=fieldnames)
writer.writeheader()
for key, val in users.items():
writer.writerow({'user' : key, 'pwd' : val})
def read():
with open('/home/marcin/fn.csv','rb') as f: # Just use 'w' mode in 3.x
w = csv.DictReader(f)
for row in w:
print row
users[row['user']]=row['pwd']
def main():
print "Welcome to Yubin's 'fake' Email server."
try:
read()
except IOError:
pass
while 1:
choice = raw_input("What would you like to do? ")
if choice == "signup":
new_user()
save()
if choice == "login":
login()
if choice == "showusers":
show_users()
if choice == "logout":
print "You have successfully logged out."
if choice == "quit":
x = raw_input("Are you sure? (y/n) ")
if x == "y":
save()
break
else:
pass
else:
print "Please sign up, log in or see who have signed up."
if __name__ == "__main__":
main()

Categories