132 lines
5.0 KiB
Python

from getpass import getpass
from helpers import SignatureHelper as Signature
from helpers import UtilityHelper
class User:
def __init__(self, db, private_ser=None, public_ser=None, username=None, password=None):
self.db = db
self.private_ser = private_ser
self.public_ser = public_ser
self.username = username
self.password = password
def login(self):
input_username = input("Username: ")
input_password = getpass("Password: ")
hashed_password = UtilityHelper.computeHash(input_password)
user = self.db.loginUser(input_username, hashed_password)
# check if user exists
if user:
self.private_ser = user[0]
self.public_ser = user[1]
self.username = user[2]
return True
return False
def register(self):
input_username = input("Username: ")
input_password = getpass("Password: ")
if len(input_username) < 4 or len(input_password) < 4:
print(f"{UtilityHelper.errorMessage('Username or password is too short, inputs should be atleast 4 characters long')}")
return False
hashed_password = UtilityHelper.computeHash(input_password)
# check if username is already taken
if self.db.fetchUserByUsername(input_username):
print(f"{UtilityHelper.errorMessage('Username already taken')}")
return False
# create sig for user
private_key, public_key = Signature.generateKeys()
private_ser, public_ser = Signature.keysToBytes(private_key, public_key)
# register user
if self.db.createUser( private_ser, public_ser, input_username, hashed_password):
self.private_ser = private_ser
self.public_ser = public_ser
self.username = input_username
return True
return False
def logout(self):
self.private_ser = None
self.public_ser = None
self.username = None
return True
def updatePassword(self):
# Get new password
new_password = getpass("Enter your new password: ")
check_new_password = getpass("Enter your new password again: ")
while new_password != check_new_password and len(new_password) < 4:
if len(new_password) < 4:
print(f"{UtilityHelper.warningMessage('Password should be atleast 4 characters long')}")
if new_password != check_new_password:
print(f"{UtilityHelper.warningMessage('Passwords do not match')}")
new_password = getpass("Enter your new password: ")
check_new_password = getpass("Enter your new password again: ")
old_password = getpass("Enter your old password: ")
hashed_new_password = UtilityHelper.computeHash(new_password)
hashed_old_password = UtilityHelper.computeHash(old_password)
if self.db.changePassword(self.private_ser, hashed_old_password, hashed_new_password) == True:
print(f"{UtilityHelper.successMessage('Password updated')}")
else:
print(f"{UtilityHelper.errorMessage('Something went wrong while trying to update password..')}")
def updateAccount(self):
# Get new username
new_username = input("Enter your new username: ")
if len(new_username) < 4:
print(f"{UtilityHelper.errorMessage('Username should be atleast 4 characters long')}")
return False
# check if username is already taken
if self.db.fetchUserByUsername(new_username):
print(f"{UtilityHelper.errorMessage('Username already taken')}")
return False
if self.db.changeUsername(self.private_ser, new_username) == True:
self.username = new_username
print(f"{UtilityHelper.successMessage('Username updated')}")
else:
print(f"{UtilityHelper.errorMessage('Something went wrong while trying to update username..')}")
def deleteAccount(self):
while True:
print(f"Are you sure you want to delete this account?\n{UtilityHelper.warningMessage('(this will burn all coins available in this account)')} (y/n)")
confirm = input(">>: ")
if confirm == "y" or confirm == "Y":
print(f"{UtilityHelper.warningMessage('Deleting account..')}")
break
elif confirm == "n" or confirm == "N":
return False
else:
print(f"{UtilityHelper.warningMessage('Invalid input')}")
if self.db.deleteUser(self.private_ser) == True:
print(f"{UtilityHelper.successMessage('Account deleted')}")
self.logout()
return True
else:
print(f"{UtilityHelper.errorMessage('Something went wrong while trying to delete account..')}")
return False
def printAccountInfo(self):
print("Username: " + self.username)
print(self.public_ser)
print(self.private_ser)