def updateEmail(file_path, new_email, key): """Update Email Verifies that new_email is of an acceptable format then accesses file at path file_name and retrieves the contents of the file. Finds the email section of the dictionary and reassigns it's value to new_email. Encrypts new dictionary data and writes over old contents of file. Args: file_path: A string containing the full path to the file new_email: A string containing the user's new email Returns: A tuple with two values, first is whether operations of the function executed fully. Second is a context string for where in the process the execution was terminated. """ exit_tuple = None if not emailFormatCheck(new_email): exit_tuple = (False, "Invalid email.") return exit_tuple data = getEncryptedData(file_path, key) data["email"] = new_email data_json_string = json.dumps(data) encrypted_data = encryption.encryptData(data_json_string, key) updateFile(file_path, encrypted_data, True) exit_tuple = (True, "Success") return exit_tuple
def updateUserCredentialUsername(file_path, new_username, key): ################## Decide whether to include name field in the files, if going with just username and want to update username then remember to update username in users.json as well, also decide on if other values will be stored in files ex. date_created """Update Username Verifies that new_username is of an acceptable format then accesses file at path file_name and retrieves the contents of the file. Finds the username section of the dictionary and reassigns it's value to new_username. Encrypts new dictionary data and writes over old contents of file. Args: file_path: A string containing the full path to the file new_username: A string containing the user's new username Returns: A tuple with two values, first is whether operations of the function executed fully. Second is a context string for where in the process the execution was terminated. """ exit_tuple = None if not usernamePasswordFormatCheck(new_username): exit_tuple = (False, "Invalid username. Please enter only letters or numbers.") return exit_tuple data = getEncryptedData(file_path, key) data["username"] = new_username data_json_string = json.dumps(data) encrypted_data = encryption.encryptData(data_json_string, key) updateFile(file_path, encrypted_data, True) exit_tuple = (True, "Success") return exit_tuple
def createTestFile(file_name, username, password, email, key): """For purposes of test functions to create a formatted test file """ file_contents = {"username": username, "password": password, "email": email} json_file_contents = json.dumps(file_contents) encrypted_data = encryption.encryptData(json_file_contents, key) file = open(file_name, "wb") file.write(encrypted_data) file.close()
def updateUserUsername(file_path, old_username, new_username, key): exit_tuple = None if not usernamePasswordFormatCheck(new_username): exit_tuple = (False, "Invalid username. Please enter only letters or numbers.") return exit_tuple data = getEncryptedData(file_path, key) for users in data["users"]: if users["username"] == old_username: users["username"] = new_username data_json_string = json.dumps(data) encrypted_data = encryption.encryptData(data_json_string, key) updateFile(file_path, encrypted_data, True) exit_tuple = (True, "Success") return exit_tuple
def encryptAPICredentials(): key_file = open("systemKey.key","rb") key = key_file.read() key_file.close() file = open("apiCredentials.json","r") raw_data = file.read() file.close() raw_json_data = json.dumps(raw_data) encrypted_data = encryption.encryptData(raw_json_data,key) file = open("apiCredentials.json","wb") file.write(encrypted_data) file.close()
def register(username, password, email): """Register the user Verifies args are of acceptable format then checks to see if users.json exists. If not, creates users.json and adds user. If yes, then checks if user already exists and if not adds user to file. If user was added to users.json then proceeds to create the userCredentials.json in the user's new directory. Args: username: A string containing the user's username password: A string containing the user's password email: A string containing the user's email Returns: A tuple with two values, first is whether operations of the function executed fully. Second is a context string for where in the process the execution was terminated. """ exit_tuple = None if not usernamePasswordFormatCheck(username): exit_tuple = ( False, "Invalid username. Please enter only letters or numbers.") return exit_tuple if not usernamePasswordFormatCheck(password): exit_tuple = ( False, "Invalid password. Please enter only letters or numbers.") return exit_tuple if not emailFormatCheck(email): exit_tuple = (False, "Invalid email.") return exit_tuple # code to send email to validated address with a generated code # Prompt user to enter the code they received to validate email exists = False # if not os.path.exists("systemKey.key"): # encryption.generateSystemKey() # file = open("systemKey.key", "rb") # key = file.read() # file.close() key = globals.general_key if not os.path.exists("users.json"): # Have to verify that generateUserID is not the same as a previous # user, very small chance but possible result = (False, "") while result[0] == False: user_ID = generateUserId() result = generateUserDir(user_ID) user_dir = result[1] initialize_users = { "users": [{ "username": username, "path": user_dir, "user_id": str(user_ID) }] } json_string = json.dumps(initialize_users) encrypted_data = encryption.encryptData(json_string, key) users_file = fileCreation("users.json", encrypted_data, True) else: data = getEncryptedData("users.json", key) for users in data["users"]: if users["username"] == username: exists = True exit_tuple = (False, "Username already exists") return exit_tuple if exists == False: result = (False, "") while result[0] == False: user_ID = generateUserId() result = generateUserDir(user_ID) user_dir = result[1] entry = {"username": username, "path": user_dir} data["users"].append(entry) data_json_string = json.dumps(data) encrypted_data = encryption.encryptData(data_json_string, key) updateFile("users.json", encrypted_data, True) data_user_credentials = { "username": username, "user_id": user_ID, "password": password, "email": email, "creation_date": str(datetime.datetime.now()), "secret_key": str(encryption.generateKey().decode()) } data_user_credentials_json_string = json.dumps(data_user_credentials) encrypted_data = encryption.encryptData(data_user_credentials_json_string, key) user_credential_path = user_dir + "\\userCredentials.json" fileCreation(user_credential_path, encrypted_data, True) fileCreation(user_dir + "\\userSubscriptions.json", "[]", False) # data = getEncryptedData("users.json", key) # json_string = json.dumps(data) # fileCreation("temp_testing.json", json_string) # data = getEncryptedData(user_dir + "\\userCredentials.json", key) # json_string = json.dumps(data) # fileCreation("temp_testing_credentials.json", json_string) exit_tuple = (True, user_credential_path) return exit_tuple