aboutsummaryrefslogtreecommitdiff
path: root/yoshi
diff options
context:
space:
mode:
Diffstat (limited to 'yoshi')
-rw-r--r--yoshi/__init__.py0
-rw-r--r--yoshi/__main__.py3
-rw-r--r--yoshi/account.py43
-rw-r--r--yoshi/cli.py136
-rw-r--r--yoshi/crypto.py45
-rw-r--r--yoshi/database.py125
-rw-r--r--yoshi/process.py226
7 files changed, 578 insertions, 0 deletions
diff --git a/yoshi/__init__.py b/yoshi/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/yoshi/__init__.py
diff --git a/yoshi/__main__.py b/yoshi/__main__.py
new file mode 100644
index 0000000..7cadd21
--- /dev/null
+++ b/yoshi/__main__.py
@@ -0,0 +1,3 @@
+if __name__ == "__main__":
+ from yoshi.cli import yoshi
+ yoshi()
diff --git a/yoshi/account.py b/yoshi/account.py
new file mode 100644
index 0000000..bf97c23
--- /dev/null
+++ b/yoshi/account.py
@@ -0,0 +1,43 @@
+"""
+This script imports necessary modules for database interactions.
+
+Modules imported:
+ - database: A custom module providing database functionality.
+"""
+
+import yoshi.database as database
+
+
+class Account:
+ """Represents a login account."""
+
+ def __init__(self, uuid: str, application: str, #pylint: disable=R0913,R0917
+ username: str, #pylint: disable=R0913,R0917
+ password: str, url: str) -> None: #pylint: disable=R0913,R0917
+ self.uuid = uuid
+ self.application = application
+ self.username = username
+ self.password = password
+ self.url = url
+
+ def display_account(self) -> None:
+ """Print the account details."""
+ print('ID:', self.uuid)
+ print('Application:', self.application)
+ print('Username:', self.username)
+ print('Password:', self.password)
+ print('URL:', self.url)
+
+ def save_account(self) -> None:
+ """Save the account details to the database."""
+ database.add_account(
+ self.uuid, self.application, self.username, self.password, self.url)
+
+ def delete_account(self) -> bool:
+ """Delete the account from the database.
+
+ Returns:
+ bool: True if the deletion was successful.
+ """
+ database.delete_account(self.uuid)
+ return True
diff --git a/yoshi/cli.py b/yoshi/cli.py
new file mode 100644
index 0000000..56abae7
--- /dev/null
+++ b/yoshi/cli.py
@@ -0,0 +1,136 @@
+"""
+This script uses argparse to parse command line arguments.
+
+It imports the required modules and sets up a parser with basic options for demonstration purposes.
+"""
+
+import argparse
+import yoshi.crypto as crypto
+import yoshi.database as database
+import yoshi.process as process
+
+def yoshi():
+ parser = argparse.ArgumentParser(
+ description='Manage your username and passwords via a convenient CLI vault.'
+ )
+
+ # Top-level arguments
+ group_one = parser.add_mutually_exclusive_group()
+ group_one.add_argument(
+ '-n', '--new',
+ help='Create a new account.',
+ action='store_true'
+ )
+ group_one.add_argument(
+ '-l', '--list',
+ help='List all saved accounts.',
+ action='store_true'
+ )
+ group_one.add_argument(
+ '-e', '--edit',
+ help='Edit a saved account.',
+ action='store_true'
+ )
+ group_one.add_argument(
+ '-d', '--delete',
+ help='Delete a saved account.',
+ action='store_true'
+ )
+ group_one.add_argument(
+ '--purge',
+ help=(
+ 'Purge all accounts and delete the vault. '
+ '(Caution: this will irreversibly destroy your data.)'
+ ),
+ action='store_true'
+ )
+ group_one.add_argument(
+ '--encrypt',
+ help='Encrypt the vault.',
+ action='store_true'
+ )
+ group_one.add_argument(
+ '--decrypt',
+ help='Decrypt the vault.',
+ action='store_true'
+ )
+
+ # Encryption flags
+ group_two = parser.add_mutually_exclusive_group()
+ group_two.add_argument(
+ '-g', '--generate',
+ help=(
+ 'When using the --encrypt option, generate a new encryption key.'
+ ),
+ action='store_true'
+ )
+ group_two.add_argument(
+ '-k', '--keyfile',
+ help='Path to existing key file.',
+ action='store',
+ nargs=1,
+ type=str
+ )
+
+ # Edit flags
+ group_three = parser.add_argument_group()
+ group_three.add_argument(
+ '-u', '--uuid',
+ help=(
+ 'When using the --edit or --delete options, provide the account UUID.'
+ ),
+ action='store',
+ nargs=1,
+ type=str
+ )
+ group_three.add_argument(
+ '-f', '--field',
+ help=(
+ 'When using the --edit option, specify the field to edit (integer index).'
+ ),
+ action='store',
+ nargs=1,
+ type=int
+ )
+
+ args = parser.parse_args()
+
+ if args.decrypt:
+ if args.keyfile:
+ key = crypto.load_key(args.keyfile[0])
+ else:
+ key = input('Please enter your decryption key: ')
+ crypto.decrypt(key)
+ elif args.encrypt:
+ if args.generate:
+ key = crypto.generate_key()
+ print(
+ 'WRITE THIS KEY DOWN SOMEWHERE SAFE. YOU WILL NOT BE ABLE TO DECRYPT '
+ 'YOUR DATA WITHOUT IT!'
+ )
+ print(key.decode())
+ print('\n')
+ else:
+ if args.keyfile:
+ key = crypto.load_key(args.keyfile[0])
+ else:
+ key = input('Please enter your encryption key: ')
+ crypto.encrypt(key)
+ elif database.check_table():
+ if args.new:
+ process.create_account()
+ elif args.list:
+ process.list_accounts()
+ elif args.edit:
+ process.edit_account(args.uuid[0], args.field[0])
+ elif args.delete:
+ process.delete_account(args.uuid[0])
+ elif args.purge:
+ process.purge_accounts()
+ else:
+ raise TypeError(
+ 'Please specify a command or use the --help flag for more information.'
+ )
+
+if __name__ == "__main__":
+ yoshi()
diff --git a/yoshi/crypto.py b/yoshi/crypto.py
new file mode 100644
index 0000000..9b0a423
--- /dev/null
+++ b/yoshi/crypto.py
@@ -0,0 +1,45 @@
+"""
+This module imports the Fernet symmetric encryption algorithm from the cryptography library.
+
+It allows for secure encryption and decryption of data using a secret key.
+"""
+
+from cryptography.fernet import Fernet
+
+VAULT_FILE = 'vault.sqlite'
+
+
+def generate_key() -> bytes:
+ """Generates a new encryption key."""
+ return Fernet.generate_key()
+
+
+def load_key(key_file: str) -> bytes:
+ """
+ Loads an existing encryption key from the file.
+
+ Args:
+ key_file (str): Path to the key file.
+ """
+ with open(key_file, 'rb') as key:
+ return key.read()
+
+
+def encrypt(key: bytes, filename: str = VAULT_FILE) -> None:
+ """Encrypts the data in the specified file using the provided key."""
+ f = Fernet(key)
+ with open(filename, 'rb') as vault:
+ data = vault.read()
+ encrypted_data = f.encrypt(data)
+ with open(filename, 'wb') as vault:
+ vault.write(encrypted_data)
+
+
+def decrypt(key: bytes, filename: str = VAULT_FILE) -> None:
+ """Decrypts the data in the specified file using the provided key."""
+ f = Fernet(key)
+ with open(filename, 'rb') as vault:
+ encrypted_data = vault.read()
+ decrypted_data = f.decrypt(encrypted_data)
+ with open(filename, 'wb') as vault:
+ vault.write(decrypted_data)
diff --git a/yoshi/database.py b/yoshi/database.py
new file mode 100644
index 0000000..e1e2e78
--- /dev/null
+++ b/yoshi/database.py
@@ -0,0 +1,125 @@
+"""
+This module provides a basic interface for connecting to and interacting with a SQLite database.
+It includes functions for creating connections, executing queries, and retrieving results.
+"""
+
+import sqlite3
+import sys
+import os
+
+VAULT_DECRYPTED = 'vault.sqlite'
+VAULT_ENCRYPTED = 'vault.sqlite.aes'
+
+
+def create_table() -> None:
+ """Create the accounts table within the vault database."""
+ db_connection = sqlite3.connect(VAULT_DECRYPTED)
+ cursor = db_connection.cursor()
+ cursor.execute(
+ ''' CREATE TABLE IF NOT EXISTS accounts (uuid text, application text,
+ username text, password text, url text) '''
+ )
+ db_connection.commit()
+ db_connection.close()
+
+
+def check_table() -> bool:
+ """Check if the 'accounts' table exists within the vault database."""
+ check = False
+ db_connection = sqlite3.connect(VAULT_DECRYPTED)
+ cursor = db_connection.cursor()
+ cursor.execute(
+ ''' SELECT count(name) FROM sqlite_master WHERE type='table'
+ AND name='accounts' '''
+ )
+ if cursor.fetchone()[0] != 1:
+ user_choice = input(
+ 'Password vault does not exist. Would you like to create it now? (y/n): ')
+ if user_choice.lower() == 'y':
+ create_table()
+ check = True
+ else:
+ sys.exit('Program aborted upon user request.')
+ else:
+ check = True
+ db_connection.commit()
+ db_connection.close()
+ return check
+
+
+def add_account(uuid: str, application: str, username: str, password: str,
+ url: str) -> None:
+ """Add a new account within the vault database."""
+ db_connection = sqlite3.connect(VAULT_DECRYPTED)
+ cursor = db_connection.cursor()
+ cursor.execute(
+ ''' INSERT INTO accounts VALUES (:uuid,:application,:username,
+ :password,:url) ''', {
+ 'uuid': uuid, 'application': application, 'username': username,
+ 'password': password, 'url': url
+ }
+ )
+ db_connection.commit()
+ db_connection.close()
+
+
+def delete_account(uuid: str) -> None:
+ """Delete an account within the vault database by its unique ID."""
+ db_connection = sqlite3.connect(VAULT_DECRYPTED)
+ cursor = db_connection.cursor()
+ cursor.execute(
+ ''' DELETE FROM accounts WHERE uuid = :uuid ''', {'uuid': uuid}
+ )
+ db_connection.commit()
+ db_connection.close()
+
+
+def find_account(uuid: str) -> list:
+ """Find an account within the vault database by its unique ID."""
+ db_connection = sqlite3.connect(VAULT_DECRYPTED)
+ cursor = db_connection.cursor()
+ cursor.execute(
+ ''' SELECT * FROM accounts WHERE uuid = :uuid ''', {'uuid': uuid}
+ )
+ account = cursor.fetchall()
+ db_connection.close()
+ return account
+
+
+def find_accounts() -> list:
+ """Return all accounts stored within the vault database."""
+ db_connection = sqlite3.connect(VAULT_DECRYPTED)
+ cursor = db_connection.cursor()
+ cursor.execute(''' SELECT * FROM accounts ''')
+ accounts = cursor.fetchall()
+ db_connection.close()
+ return accounts
+
+
+def update_account(field_name: str, new_value: str, uuid: str) -> None:
+ """Update an account within the vault database by its unique ID."""
+ queries = {
+ 'application': 'UPDATE accounts SET application = :new_value WHERE uuid = :uuid',
+ 'username': 'UPDATE accounts SET username = :new_value WHERE uuid = :uuid',
+ 'password': 'UPDATE accounts SET password = :new_value WHERE uuid = :uuid',
+ 'url': 'UPDATE accounts SET url = :new_value WHERE uuid = :uuid'
+ }
+ db_connection = sqlite3.connect(VAULT_DECRYPTED)
+ cursor = db_connection.cursor()
+ cursor.execute(queries[field_name], {'new_value': new_value, 'uuid': uuid})
+ db_connection.commit()
+ db_connection.close()
+
+
+def purge_table() -> None:
+ """Purge the 'accounts' table within the vault database."""
+ db_connection = sqlite3.connect(VAULT_DECRYPTED)
+ cursor = db_connection.cursor()
+ cursor.execute(''' DROP TABLE accounts ''')
+ db_connection.commit()
+ db_connection.close()
+
+
+def purge_database() -> None:
+ """Purge the entire vault database."""
+ os.remove(VAULT_DECRYPTED)
diff --git a/yoshi/process.py b/yoshi/process.py
new file mode 100644
index 0000000..155f9b0
--- /dev/null
+++ b/yoshi/process.py
@@ -0,0 +1,226 @@
+"""
+Password Vault Manager
+
+This script provides various functions for managing password vaults.
+It allows users to create, list, edit and delete accounts.
+
+The `Account` class represents an individual account, with attributes for
+the application name, username, password, and URL. The database module is used
+to interact with the SQLite database file (`vault.sqlite`) that stores the
+accounts data.
+
+Functions:
+ generate_characters(n): generates a list of random characters
+ shuffle_characters(characters): shuffles the characters to create a password
+ generate_passphrase(n, sep): generates an XKCD-style passphrase with n words and separator
+ list_accounts(): lists all saved accounts in the database
+ delete_account(uuid): deletes an account by its UUID
+ purge_accounts(): purges the entire database (irreversible)
+ create_account(): creates a new account by prompting user for details
+ edit_account(uuid, edit_parameter): edits an existing account's details
+
+Usage:
+ Run this script in your terminal to access these functions.
+"""
+
+from string import ascii_letters, punctuation, digits
+import random
+import uuid
+from prettytable import PrettyTable
+from yoshi.account import Account
+import yoshi.database as database
+
+
+def generate_characters(n: int) -> list:
+ """
+ Generates a list of n random characters from the set of ASCII letters,
+ punctuation and digits.
+
+ Args:
+ n (int): The number of characters to generate
+
+ Returns:
+ list: A list of n random characters
+ """
+ characters = []
+ password_format = ascii_letters + punctuation + digits
+ for _ in range(n):
+ characters.append(random.choice(password_format))
+ return characters
+
+
+def shuffle_characters(characters: list) -> str:
+ """
+ Shuffles the characters to create a password.
+
+ Args:
+ characters (list): The list of characters
+
+ Returns:
+ str: A string representation of the shuffled characters
+ """
+ random.shuffle(characters)
+ character_string = ''.join(characters)
+ return character_string
+
+
+def generate_passphrase(n: int, sep: str) -> str:
+ """
+ Generates an XKCD-style passphrase with n words and separator.
+
+ Args:
+ n (int): The number of words to include
+ sep (str): The separator symbol
+
+ Returns:
+ str: A string representation of the passphrase
+ """
+ phrases = []
+ lucky_number = random.choice(range(0, n))
+ for _ in range(n):
+ with open('wordlist.txt', 'r', encoding='utf-8') as file:
+ line = random.choice(file.readlines())
+ line = line.replace('\n', '')
+ if _ == lucky_number:
+ phrases.append(line.strip().capitalize() + str(_))
+ else:
+ phrases.append(line.strip().capitalize())
+ passphrase = sep.join(phrases)
+ return passphrase
+
+
+def list_accounts() -> None:
+ """
+ Lists all saved accounts in the database.
+
+ Returns:
+ None
+ """
+ accounts = database.find_accounts()
+ t = PrettyTable(['UUID', 'Application', 'Username', 'Password', 'URL'])
+ for account in accounts:
+ t.add_row([account[0], account[1], account[2], account[3], account[4]])
+ print(t)
+
+
+def delete_account(account_uuid: str) -> None:
+ """
+ Deletes an account by its UUID.
+
+ Args:
+ account_uuid (str): The UUID of the account to delete
+
+ Returns:
+ None
+ """
+ account_record = database.find_account(account_uuid)
+ account = Account(account_record[0][0],
+ account_record[0][1],
+ account_record[0][2],
+ account_record[0][3],
+ account_record[0][4])
+ if account.delete_account():
+ print('Account successfully deleted.')
+
+
+def purge_accounts() -> None:
+ """
+ Purges the entire database (irreversible).
+
+ Returns:
+ None
+ """
+ check = input(
+ '''Are you absolutely sure you want to delete your password vault?
+ This action is irreversible. (y/n): ''')
+ if check.lower() == 'y':
+ database.purge_table()
+ database.purge_database()
+ print('The password vault has been purged. You may now exit or create a new one.')
+
+
+def create_account() -> None:
+ """
+ Creates a new account by prompting user for details.
+
+ Returns:
+ None
+ """
+ application_string = input('Please enter a name for this account: ')
+ username_string = input('Please enter your username for this account: ')
+ url_string = input('(Optional) Please enter a URL for this account: ')
+
+ password_type = input(
+ '''Do you want a random character password (p), an XKCD-style passphrase
+(x), or a custom password (c)? (p|x|c): '''
+ )
+ if password_type not in ['p', 'x', 'c']:
+ print('Error: Invalid choice. Please choose p, x, or c.')
+ return
+
+ if password_type == 'x':
+ password_length = int(
+ input('Please enter number of words to include (min. 2): ')
+ )
+ if password_length < 3:
+ print('Error: Your passphrase length must be at least 3 words.')
+ return
+ password_separator = input(
+ 'Please enter your desired separator symbol (_,-, ~, etc.): '
+ )
+ password_string = generate_passphrase(password_length, password_separator)
+ elif password_type == 'p':
+ password_length = int(
+ input('Please enter your desired password length (min. 8): ')
+ )
+ if password_length < 8:
+ print('Error: Your password length must be at least 8 characters.')
+ return
+ password_characters = generate_characters(password_length)
+ password_string = shuffle_characters(password_characters)
+ else:
+ password_string = input('Please enter your desired password: ')
+
+ account = Account(str(uuid.uuid4()), application_string,
+ username_string, password_string, url_string)
+ account.save_account()
+ print('Account saved to the vault. Use `--list` to see all saved accounts.')
+
+
+
+def edit_account(account_uuid: str, edit_parameter: int) -> None:
+ """
+ Allow users to edit any account information except the UUID.
+
+ Args:
+ account_uuid (str): Unique identifier of the account.
+ edit_parameter (int): Parameter indicating which field to edit.
+ Valid values are 1 for application name, 2 for username,
+ 3 for password, and 4 for URL.
+ """
+ field_name, new_value = ''
+ if edit_parameter == 1:
+ field_name = 'application'
+ new_value = input('Please enter your desired Application name: ')
+ elif edit_parameter == 2:
+ field_name = 'username'
+ new_value = input('Please enter your desired username: ')
+ elif edit_parameter == 3:
+ field_name = 'password'
+ type_check = input(
+ 'Do you want a new random password or to enter a custom password? '
+ '(random/custom): ').lower()
+ if type_check == 'random':
+ password_length = int(input('Please enter your desired password length: '))
+ if password_length < 8:
+ print('Error: Your password length must be at least 8 characters.')
+ else:
+ password_characters = generate_characters(password_length)
+ new_value = shuffle_characters(password_characters)
+ else:
+ new_value = input('Please enter your desired password: ')
+ elif edit_parameter == 4:
+ field_name = 'url'
+ new_value = input('Please enter your desired URL: ')
+ database.update_account(field_name, new_value, account_uuid)
+ print('Account successfully updated.')