41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
from pykeepass import PyKeePass
|
|
from config import KeePassConfig
|
|
import getpass
|
|
from dataclasses import dataclass
|
|
|
|
@dataclass
|
|
class KeePassEntry:
|
|
def __init__(self, name: str, password: str):
|
|
self._name = name
|
|
self._password = password
|
|
|
|
@property
|
|
def password(self) -> str:
|
|
return self._password
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
return self._name
|
|
|
|
|
|
|
|
class KeePass:
|
|
def __init__(self, config: KeePassConfig):
|
|
self._kee_pass_config: KeePassConfig = config
|
|
self._password: str = getpass.getpass(f'KeePass password for {config.path}: ')
|
|
self._kee_pass: PyKeePass = PyKeePass(config.path, password=self._password)
|
|
|
|
def get_db_credentials(self) -> KeePassEntry:
|
|
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 KeePassEntry(group[0].username, group[0].password) |