26 lines
1.1 KiB
Python
26 lines
1.1 KiB
Python
from pykeepass import PyKeePass
|
|
from config import KeePassConfig
|
|
import getpass
|
|
class KeePass:
|
|
_kee_pass: PyKeePass
|
|
_kee_pass_config: KeePassConfig
|
|
_password: str
|
|
def __init__(self, config: KeePassConfig):
|
|
self._kee_pass_config = config
|
|
self._password = getpass.getpass(f'KeePass password for {config.path}: ')
|
|
self._kee_pass = PyKeePass(config.path, password=self._password)
|
|
|
|
def get_db_credentials(self):
|
|
group = self._kee_pass
|
|
if self._kee_pass_config.db_credentials_group.strip() != "" and self._kee_pass_config.db_credentials_group.strip() is not None:
|
|
group = self._kee_pass.find_entries(name=self._kee_pass_config.db_credentials_name)
|
|
|
|
group = group.find_entries(title=self._kee_pass_config.db_credentials_name)
|
|
|
|
if group is None:
|
|
raise Exception(f'Group {self._kee_pass_config.db_credentials_group} not found')
|
|
|
|
if len(group) != 1:
|
|
raise Exception(f'Could not find password, found {len(group)} entries')
|
|
|
|
return group[0].username, group[0].password |