mirror of
https://github.com/Garmelon/PFERD.git
synced 2023-12-21 10:23:01 +01:00
Add simple authenticator
... including some required authenticator infrastructure
This commit is contained in:
19
PFERD/authenticators/__init__.py
Normal file
19
PFERD/authenticators/__init__.py
Normal 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),
|
||||
}
|
48
PFERD/authenticators/simple.py
Normal file
48
PFERD/authenticators/simple.py
Normal 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
|
Reference in New Issue
Block a user