Add simple authenticator

... including some required authenticator infrastructure
This commit is contained in:
Joscha
2021-05-11 00:27:43 +02:00
parent d5f29f01c5
commit 0459ed093e
5 changed files with 155 additions and 0 deletions

View File

@ -0,0 +1,19 @@
from configparser import SectionProxy
from typing import Callable, Dict
from ..authenticator import Authenticator
from ..conductor import TerminalConductor
from ..config import Config
from .simple import SimpleAuthenticator, SimpleAuthSection
AuthConstructor = Callable[[
str, # Name (without the "auth:" prefix)
SectionProxy, # Authenticator's section of global config
Config, # Global config
TerminalConductor, # Global conductor instance
], Authenticator]
AUTHENTICATORS: Dict[str, AuthConstructor] = {
"simple": lambda n, s, c, t:
SimpleAuthenticator(n, SimpleAuthSection(s), c, t),
}

View File

@ -0,0 +1,48 @@
from typing import Optional, Tuple
from ..authenticator import Authenticator, AuthSection
from ..conductor import TerminalConductor
from ..config import Config
from ..utils import agetpass, ainput
class SimpleAuthSection(AuthSection):
def username(self) -> Optional[str]:
return self.s.get("username")
def password(self) -> Optional[str]:
return self.s.get("password")
class SimpleAuthenticator(Authenticator):
def __init__(
self,
name: str,
section: SimpleAuthSection,
config: Config,
conductor: TerminalConductor,
) -> None:
super().__init__(name, section, config, conductor)
self.username = section.username()
self.password = section.password()
self.username_fixed = self.username is not None
self.password_fixed = self.password is not None
async def credentials(self) -> Tuple[str, str]:
if self.username is not None and self.password is not None:
return self.username, self.password
async with self.conductor.exclusive_output():
if self.username is None:
self.username = await ainput("Username: ")
else:
print(f"Username: {self.username}")
if self.password is None:
self.password = await agetpass("Password: ")
else:
print("Password: *******")
return self.username, self.password