def add_handler(sub_args): alias = sub_args.alias service = sub_args.service account = sub_args.account if alias is None: alias = str(click.prompt("Please type an alias to store the credential (this alias must be unique)")) if not _alias_valid(alias): select = Account.select().where(Account.alias == alias) service = str(select[0].service) account = str(select[0].account) sys.exit("Aborting , this alias already exist for the service " + service + " and the account " + account) if service is None: service = str(click.prompt("Please type the name of the service you want to use")) if not sub_args.noformat: if service.split("://")[0].lower() == "http": service = urlparse(service).netloc service = service.lower() if account is None: account = str(click.prompt("Please enter the account for the credential (aka login)")) select = Account.select().where((Account.service == service) & (Account.account == account)) if len(select) != 0: print "The account " + account + " associated to the service " + service + " already exist" if not click.confirm("Do you wish to continue adding this credential ?"): sys.exit("Aborting") passphrase = getpass.getpass("Enter passphrase:") Account.create(service=service, account=account, passphrase=passphrase, alias=alias)
def list_handler(sub_args): if sub_args.service is None: # list all with indication of the number of account stored for each service service_dict = {} for i in Account.select(): if i.service in service_dict: service_dict[i.service] += 1 else: service_dict[i.service] = 1 _list_service_printer(service_dict, ["service name", "nbs account"]) else: # list only the account of the given service if the service is used account_dict = {} for i in Account.select().where(Account.service == sub_args.service): account_dict[i.alias] = i.account _list_account_printer(account_dict, ["alias", "account name"])
def get_handler(sub_args): credential = Account.select().where(Account.alias == sub_args.alias) alias = sub_args.alias[0] if len(credential) == 0: sys.exit("There is no credential for the alias " + alias) credential = credential[0] selection = sub_args.selection[0] get = credential.account if selection == "account" \ else credential.passphrase if selection == "passphrase" \ else credential.service if sub_args.prompt: print get else: import pyperclip pyperclip.copy(get)
def remove_handler(sub_args): alias = sub_args.alias[0] credential = Account.select().where(Account.alias == alias) if len(credential) == 0: sys.exit("There is no credential with alias " + alias) else: if sub_args.force: _delete_alias(alias) else: account = str(credential[0].account) service = str(credential[0].service) print "The credential for the account " + account + " of the service " + service + " will be removed." if click.confirm("Confirm credential removal:"): _delete_alias(alias) else: sys.exit("Removal aborted")
def _alias_valid(alias): select = Account.select().where(Account.alias == alias) return True if len(select) == 0 else False