Files
bitwardenbackup/bitwardenBackup.py

170 lines
7.8 KiB
Python

# library needed
import sys
import os
import subprocess
import platform
import json
import configparser
import getpass
os_detected = platform.system()
script_directory = os.path.dirname(os.path.realpath(__file__))
secrets_ini_file = script_directory + "/secrets.ini"
if os_detected == "Windows":
bitwarden_cli_executable = script_directory + "lib/bw.exe"
elif os_detected == "Linux":
bitwarden_cli_executable = script_directory + "lib/bw"
else:
print("Your OS is not supported. Only Windows and Linux are currently supported.\nDetected OS: {0}".format(os_detected))
sys.exit(1)
# INI format, mulitple accounts can be used.
#[Account Email Address]
#encrypted_api_client_id =
#encrypted_api_secret =
#encrypted_vault_password =
def does_file_exist(filepath):
return os.path.exists(filepath)
def ask_for_account_details():
print("Requesting account details to build the ini file.")
account_email_address = input("Please enter Bitwarden account email address: ")
#encrypted_account_email_address = encrypt(client, account_email_address)
account_api_client_id = input("Please enter Bitwarden account API client ID: ")
#encrypted_account_api_client_id = encrypt(client, account_api_client_id)
while True:
account_api_secret = getpass.getpass("Please enter Bitwarden account API secret: ")
account_api_secret2 = getpass.getpass("Please confirm Bitwarden account API secret: ")
if account_api_secret == account_api_secret2:
#encrypted_account_api_secret = encrypt(client, account_api_secret)
break
else:
print("The Bitwarden account API secrets do not match, please try again.")
while True:
account_vault_password = getpass.getpass("Please enter Bitwarden account vault password: ")
account_vault_password2 = getpass.getpass("Please confirm Bitwarden account vault password: ")
if account_vault_password == account_vault_password2:
#encrypted_account_vault_password = encrypt(client, account_vault_password)
break
else:
print("The Bitwarden account vault passwords do not match, please try again.")
array = dict()
array["account_email_address"] = account_email_address
#array["encrypted_account_email_address"] = encrypted_account_email_address
array["account_api_client_id"] = account_api_client_id
#array["encrypted_account_api_client_id"] = encrypted_account_api_client_id
array["account_api_secret"] = account_api_secret
#array["encrypted_account_api_secret"] = encrypted_account_api_secret
array["account_vault_password"] = account_vault_password
#array["encrypted_account_vault_password"] = encrypted_account_vault_password
return array
def select_account(accounts, wording = "edit"):
print("Which account would you like to {}:".format(wording))
for i in range(0, len(accounts)):
pretty_number = i + 1
print("{}) {}".format(pretty_number, accounts[i]))
while True:
account_to_edit = int(input("Please enter number relating to the account you wish to {}: ".format(wording))) - 1
try:
return accounts[account_to_edit]
except IndexError as error:
print("you entered a number out of range, please try again")
if __name__ == "__main__":
# INI config does not exist
#if not does_file_exist(secrets_ini_file):
# account_details = ask_for_account_details()
# config = configparser.ConfigParser()
# config[account_details["account_email_address"]] = {}
# config[account_details["account_email_address"]]["account_api_client_id"] = account_details["account_api_client_id"]
# config[account_details["account_email_address"]]["account_api_secret"] = account_details["account_api_secret"]
# config[account_details["account_email_address"]]["account_vault_password"] = account_details["account_vault_password"]
# with open(secrets_ini_file, "w") as configfile:
# config.write(configfile)
#config = configparser.ConfigParser()
#config.read(secrets_ini_file)
#accounts = config.sections()
# decrypt all values for easy update a
if True:
#if opts.config:
if not does_file_exist(secrets_ini_file):
print("No Bitwarden accounts found, do you want to make a new one?")
print("n) New account")
print("q) Quit config")
while True:
user_input = input("n/q> ")
if user_input.casefold() == "n":
account_details = ask_for_account_details()
config = configparser.ConfigParser()
config.add_section(account_details["account_email_address"])
for key in account_details.keys():
if not key == "account_email_address":
config.set(account_details["account_email_address"], key, account_details[key])
with open(secrets_ini_file, "w") as configfile:
config.write(configfile)
break
elif user_input.casefold() == "q":
sys.exit(0)
else:
print("This value must be one of the following characters: n, q.")
while True:
config = configparser.ConfigParser()
config.read(secrets_ini_file)
accounts = config.sections()
print("Current Bitwarden accounts:")
print(" ")
for account in accounts:
print(account)
print(" ")
print("e) Edit account")
print("n) New account")
print("d) Delete account")
print("q) Quit config")
while True:
user_input = input("e/n/d/q> ")
if user_input.casefold() == "e":
account_section_to_edit = select_account(accounts)
elif user_input.casefold() == "n":
account_details = ask_for_account_details()
config.add_section(account_details["account_email_address"])
for key in account_details.keys():
if not key == "account_email_address":
config.set(account_details["account_email_address"], key, account_details[key])
with open(secrets_ini_file, "w") as configfile:
config.write(configfile)
break
elif user_input.casefold() == "d":
account_section_to_delete = select_account(accounts, "delete")
print("Are you sure you wish to delete {} account? ".format(account_section_to_delete))
confirmation = input("y/n> ")
if not confirmation.casefold() in ["y","yes"]:
break
config.remove_section(account_section_to_delete)
with open(secrets_ini_file, "w") as configfile:
config.write(configfile)
break
elif user_input.casefold() == "q":
sys.exit(0)
else:
print("This value must be one of the following characters: e, n, d, q.")
sys.exit(0)
print("Which account would you like to edit:")
for i in range(0, len(accounts)):
pretty_number = i + 1
print("{}) {}".format(pretty_number, accounts[i]))
while True:
account_to_edit = int(input("Please enter number relating to the account you wish to edit: ")) - 1
try:
print("selected account: {}".format(accounts[account_to_edit]))
break
except IndexError as error:
print("you entered a number out of range, please try again")