def gen_key(self, path, length, symbols=True, force=False, inplace=False): """Generate a new password for a key. :param str path: The path of the key. :param int length: The length of the new password. :param bool symbols: (optional) If ``True`` non alphanumeric characters will also be used in the new password. :param bool force: (optional) If ``True`` an existing key at `path` will be overwritten. :param bool inplace: (optional) If ``True`` only the first line of an existing key at `path` will be overwritten with the new password. """ if path is None or path == '': return None path = os.path.normpath(path) key_path = os.path.join(self.store_dir, path + '.gpg') key_dir = os.path.dirname(key_path) if os.path.exists(key_path) and not (force or inplace): if self.interactive: answer = input('An entry already exists for {0}. ' 'Overwrite it? [y/N] '.format(path)) if answer.lower() != 'y': return None else: raise FileExistsError( 'An entry already exists for {0}.'.format(path)) os.makedirs(os.path.join(self.store_dir, key_dir), exist_ok=True) password = gen_password(length, symbols=symbols) action = 'Add' if not inplace: write_key(key_path, password, self.gpg_bin, self.gpg_opts) action = 'Add' else: action = 'Replace' key_data = read_key(key_path, gpg_bin=self.gpg_bin, gpg_opts=self.gpg_opts) lines = key_data.split('\n') lines[0] = password write_key(key_path, '\n'.join(lines), gpg_bin=self.gpg_bin, gpg_opts=self.gpg_opts) git_add_path(self.repo, key_path, '{0} generated password for {1}.'.format(action, path), verbose=self.verbose) return password
def gen_key(self, path, length, symbols=True, force=False, inplace=False): """Generate a new password for a key. :param str path: The path of the key. :param int length: The length of the new password. :param bool symbols: (optional) If ``True`` non alphanumeric characters will also be used in the new password. :param bool force: (optional) If ``True`` an existing key at `path` will be overwritten. :param bool inplace: (optional) If ``True`` only the first line of an existing key at `path` will be overwritten with the new password. """ if path is None or path == '': return None path = os.path.normpath(path) key_path = os.path.join(self.store_dir, path + '.gpg') key_dir = os.path.dirname(key_path) if os.path.exists(key_path) and not (force or inplace): if self.interactive: answer = input('An entry already exists for {0}. ' 'Overwrite it? [y/N] '.format(path)) if answer.lower() != 'y': return None else: raise FileExistsError('An entry already exists for {0}.' .format(path)) os.makedirs(os.path.join(self.store_dir, key_dir), exist_ok=True) password = gen_password(length, symbols=symbols) action = 'Add' if not inplace: write_key(key_path, password, self.gpg_bin, self.gpg_opts) action = 'Add' else: action = 'Replace' key_data = read_key(key_path, gpg_bin=self.gpg_bin, gpg_opts=self.gpg_opts) lines = key_data.split('\n') lines[0] = password write_key(key_path, '\n'.join(lines), gpg_bin=self.gpg_bin, gpg_opts=self.gpg_opts) git_add_path(self.repo, key_path, '{0} generated password for {1}.'.format(action, path), verbose=self.verbose) return password
def decrypt_entry(reader: PassReader, entry: str) -> str: """Decrypt the entry using pass and return it as a string.""" if reader.password is None or reader.password == "": entry = reader.store.get_key(entry) else: # implement my own get_key and pass a custom gpg pass gpg_opts = reader.store.gpg_opts + [ "--pinentry-mode=loopback", f"--passphrase={reader.password}" ] entry = read_key(reader.path + f"/{entry}.gpg", reader.store.gpg_bin, gpg_opts) return entry
def get_key(self, path): """Reads the data of the key at path. :param str path: The path to the key (without '.gpg' ending) relative to :attr:`passpy.store.Store.store_dir`. :rtype: str :returns: The key data as a string or ``None``, if the key does not exist. :raises FileNotFoundError: if `path` is not a file. """ if path is None or path == '': return None path = os.path.normpath(path) key_path = os.path.join(self.store_dir, path + '.gpg') if os.path.isfile(key_path): return read_key(key_path, self.gpg_bin, self.gpg_opts) raise FileNotFoundError( '{0} is not in the password store.'.format(path))
def get_key(self, path): """Reads the data of the key at path. :param str path: The path to the key (without '.gpg' ending) relative to :attr:`passpy.store.Store.store_dir`. :rtype: str :returns: The key data as a string or ``None``, if the key does not exist. :raises FileNotFoundError: if `path` is not a file. """ if path is None or path == '': return None path = os.path.normpath(path) key_path = os.path.join(self.store_dir, path + '.gpg') if os.path.isfile(key_path): return read_key(key_path, self.gpg_bin, self.gpg_opts) raise FileNotFoundError('{0} is not in the password store.' .format(path))