Last active
February 6, 2022 15:01
-
-
Save TheVeryStarlk/6ddfcb683b556b1de1c63f4c8e766b79 to your computer and use it in GitHub Desktop.
Simple but yet buggy login system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Simple but yet buggy login system. | |
You need to have a "database.txt" file. | |
""" | |
import tkinter | |
class AccountHandler: | |
def __init__(self, file_path): | |
self.file_path = file_path | |
self.file_found = True | |
try: | |
with open(self.file_path, "r"): | |
self.file_found = True | |
except (FileNotFoundError, FileExistsError): | |
self.file_found = False | |
self._encryption_form = {"~": "%1", "!": "%2", "@": "%3", | |
"#": "%4", "$": "%5", "%": "%6", | |
"^": "%7", "&": "%8", "*": "%9", | |
"(": r"%a", ")": r"%b", "_": r"%c", | |
"+": r"%d", " ": r"%e"} | |
self._decryption_form = {"%1": "~", "%2": "!", "%3": "@", | |
"%4": "#", "%5": "$", "%6": "%", | |
"%7": "^", "%8": "&", "%9": "*", | |
r"%a": "(", r"%b": ")", r"%c": "_", | |
r"%d": "%+", r"%e": " "} | |
# Only for passwords; No need to encrypt usernames | |
def encrypt_password(self, password): | |
# ~!@#$%^&*()_+ | |
encrypted = "" | |
encrypted = encrypted.maketrans(self._encryption_form) | |
return str(password.translate(encrypted)) + r"%END" | |
def decrypt_password(self, password): | |
# ~!@#$%^&*()_+ | |
for letter in self._decryption_form: | |
password = password.replace(letter, self._decryption_form[letter]) | |
return password.replace(r"%END", "") | |
def read_account(self, username=""): | |
""" | |
Gets the account data, username and password | |
Optionally, you can get the account data by username | |
""" | |
with open(self.file_path, "r") as file: | |
if username != "": | |
for line in file.readlines(): | |
if line[:line.find(":")] == username: | |
return line[:line.find(":")], True | |
else: | |
return file.readlines() | |
def check_account(self, username, password): | |
password = self.encrypt_password(password) | |
with open(self.file_path, "r") as file: | |
for line in file.readlines(): | |
line = line.replace("\n", "") | |
if line[:line.find(":")] == username: | |
index = line.find(r"%END") + 4 | |
if line[line.find(":") + 1:index] == password: | |
return True | |
else: | |
return False | |
def write_account(self, username, password): | |
""" | |
Password is going to be encrypted | |
""" | |
password = self.encrypt_password(password) | |
with open(self.file_path, "a") as file: | |
exist = self.read_account(username) | |
if (exist is not None) and (exist[1]): | |
return False | |
else: | |
file.write(f"{username}:{password}\n") | |
def edit_account(self, old, new): | |
""" | |
You can only change usernames | |
""" | |
# Check if the new username exists already or not | |
exist = self.read_account(new) | |
if (exist is not None) and (exist[1]): | |
return False | |
""" | |
Firstly, open the file and copies everything to a variable | |
Then replace the necessary stuff | |
Lastly write the new data again | |
""" | |
data = "" | |
with open(self.file_path, "r") as file: | |
for line in file.readlines(): | |
data += line | |
if line[:line.find(":")] == old: | |
data = data.replace(old, new, 1) | |
with open(self.file_path, "w") as file: | |
file.truncate() | |
file.write(data) | |
class Window: | |
def __init__(self): | |
self.master = tkinter.Tk() | |
self.master.geometry("500x500") | |
self.master.title("Login System") | |
self.master.resizable(False, False) | |
self.account_handler = AccountHandler("database.txt") | |
if self.account_handler.file_found: | |
self.login_frame_message_notify = "Enter your accout information" | |
else: | |
self.login_frame_message_notify = "'database.txt' not found, please reopen" | |
self.login_frame = tkinter.Frame(self.master) | |
self.login_message_label = tkinter.Label(self.login_frame, text=self.login_frame_message_notify) | |
self.login_username_entry = tkinter.Entry(self.login_frame) | |
self.login_password_entry = tkinter.Entry(self.login_frame) | |
self.login_enter_button = tkinter.Button(self.login_frame, text="Enter") | |
if self.account_handler.file_found is False: | |
self.login_enter_button.configure(state=tkinter.DISABLED) | |
else: | |
self.login_enter_button.configure(state=tkinter.NORMAL) | |
self.login_message_label.place(x=250, y=220, anchor=tkinter.CENTER) | |
self.login_username_entry.place(x=250, y=250, anchor=tkinter.CENTER) | |
self.login_password_entry.place(x=250, y=275, anchor=tkinter.CENTER) | |
self.login_enter_button.place(x=250, y=305, anchor=tkinter.CENTER) | |
self.account_manager_frame = tkinter.Frame() | |
self.account_manager_message_label = tkinter.Label(self.account_manager_frame, text="You need to put your password to change your username\nYou can only change usernames") | |
self.account_manager_username_entry = tkinter.Entry(self.account_manager_frame) | |
self.account_manager_password_entry = tkinter.Entry(self.account_manager_frame) | |
self.account_manager_enter_button = tkinter.Button(self.account_manager_frame, text="Edit") | |
self.account_manager_message_label.place(x=250, y=220, anchor=tkinter.CENTER) | |
self.account_manager_username_entry.place(x=250, y=250, anchor=tkinter.CENTER) | |
self.account_manager_password_entry.place(x=250, y=275, anchor=tkinter.CENTER) | |
self.account_manager_enter_button.place(x=250, y=305, anchor=tkinter.CENTER) | |
self.old_username = None | |
self.old_password = None | |
def change_account_username(self, old, new, password): | |
if self.account_handler.check_account(old, password): | |
if self.account_handler.edit_account(old, new): | |
self.account_manager_message_label.configure(text=f"Changed username from {old} to {new}") | |
self.master.title(f"Login System - {new}") | |
else: | |
self.account_manager_message_label.configure(text="Username already exists or password is incorrect") | |
def account_manager_frame_manager(self): | |
self.account_manager_enter_button.configure(command=lambda: self.change_account_username(self.old_username, self.account_manager_username_entry.get(), self.account_manager_password_entry.get())) | |
def check_account(self, username, password): | |
if self.account_handler.check_account(username, password): | |
self.old_password = self.login_password_entry.get() | |
self.old_username = self.login_username_entry.get() | |
self.master.title(f"Login System - {self.login_username_entry.get()}") | |
# Clear the frame | |
for widget in self.login_frame.winfo_children(): | |
self.login_frame.destroy() | |
widget.destroy() | |
self.account_manager_frame.pack(fill="both", expand=True) | |
self.account_manager_frame_manager() | |
else: | |
self.account_handler.write_account(username, password) | |
self.login_message_label.configure(text="Created new account. Renter username and password to log-in") | |
def login_frame_manager(self): | |
self.login_frame.pack(fill="both", expand=True) | |
self.login_enter_button.configure(command=lambda: self.check_account(self.login_username_entry.get(), self.login_password_entry.get())) | |
def show(self): | |
self.login_frame_manager() | |
self.master.mainloop() | |
if __name__ == "__main__": | |
window = Window() | |
window.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's a good work