You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

73 lines
2.1 KiB

from dataclasses import dataclass
from typing import Tuple, List, Optional
@dataclass
class Password:
password: Optional[str]
login: Optional[str] = None
#-------------------------------------------------------------------------------
class PasswordManager( object ):
"""
An abstract password manager.
Instanciate concrete instances via the Manager() function
"""
def get( self, identifier: str ) -> Password:
"""
Gets a password from the manager.
This function is overriden by the concrete PasswordManager instances.
Args:
identifier: which password to get
Returns:
None if the password is not found,
the password otherwise
"""
return Password( None )
from .plaintext import PlainTextPasswordManager
from .gnupass import GNUPassPasswordManager
#-------------------------------------------------------------------------------
def verify( config: dict, path: str = "" ) -> Tuple[bool, List[str]]:
"""
Verifies that the configuration is correct
Args:
config: dict with password manager configuration
path: path to the "config" dict in the main configuration file
Returns:
bool: true if the config is correct
List[str]: list of errors if the config is not correct
"""
if "manager" not in config:
return (False, [f'Missing "{path}.manager" value'])
manager = config["manager"]
for cls in PasswordManager.__subclasses__():
if manager == cls.name():
return cls.verify(config, path)
return (False, [f'Unknown password manager "{manager}"'])
#-------------------------------------------------------------------------------
def Manager( config: dict ) -> PasswordManager:
"""
Instanciates a PasswordManager instance from a given configuration
"""
if "manager" not in config:
raise ValueError
manager = config["manager"]
for cls in PasswordManager.__subclasses__():
if manager == cls.name():
return cls(config)
raise ValueError