2021-05-11 00:27:43 +02:00
|
|
|
from typing import Optional, Tuple
|
|
|
|
|
|
|
|
from ..config import Config
|
2021-05-18 22:43:46 +02:00
|
|
|
from ..logging import log
|
2021-05-11 00:27:43 +02:00
|
|
|
from ..utils import agetpass, ainput
|
2021-05-23 19:16:42 +02:00
|
|
|
from .authenticator import Authenticator, AuthException, AuthSection
|
2021-05-11 00:27:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
) -> None:
|
2021-05-18 22:43:46 +02:00
|
|
|
super().__init__(name, section, config)
|
2021-05-11 00:27:43 +02:00
|
|
|
|
2021-05-15 18:24:03 +02:00
|
|
|
self._username = section.username()
|
|
|
|
self._password = section.password()
|
2021-05-11 00:27:43 +02:00
|
|
|
|
2021-05-15 18:24:03 +02:00
|
|
|
self._username_fixed = self.username is not None
|
|
|
|
self._password_fixed = self.password is not None
|
2021-05-11 00:27:43 +02:00
|
|
|
|
|
|
|
async def credentials(self) -> Tuple[str, str]:
|
2021-05-15 18:24:03 +02:00
|
|
|
if self._username is not None and self._password is not None:
|
|
|
|
return self._username, self._password
|
2021-05-11 00:27:43 +02:00
|
|
|
|
2021-05-18 22:43:46 +02:00
|
|
|
async with log.exclusive_output():
|
2021-05-15 18:24:03 +02:00
|
|
|
if self._username is None:
|
|
|
|
self._username = await ainput("Username: ")
|
2021-05-11 00:27:43 +02:00
|
|
|
else:
|
2021-05-23 10:44:59 +02:00
|
|
|
print(f"Username: {self._username}")
|
2021-05-11 00:27:43 +02:00
|
|
|
|
2021-05-15 18:24:03 +02:00
|
|
|
if self._password is None:
|
|
|
|
self._password = await agetpass("Password: ")
|
2021-05-11 00:27:43 +02:00
|
|
|
|
2021-05-15 18:24:03 +02:00
|
|
|
# Intentionally returned inside the context manager so we know
|
|
|
|
# they're both not None
|
|
|
|
return self._username, self._password
|
2021-05-15 17:37:05 +02:00
|
|
|
|
|
|
|
def invalidate_credentials(self) -> None:
|
2021-05-15 18:24:03 +02:00
|
|
|
if self._username_fixed and self._password_fixed:
|
2021-05-15 17:37:05 +02:00
|
|
|
raise AuthException("Configured credentials are invalid")
|
|
|
|
|
2021-05-15 18:24:03 +02:00
|
|
|
if not self._username_fixed:
|
|
|
|
self._username = None
|
|
|
|
if not self._password_fixed:
|
|
|
|
self._password = None
|
2021-05-15 17:37:05 +02:00
|
|
|
|
|
|
|
def invalidate_username(self) -> None:
|
2021-05-15 18:24:03 +02:00
|
|
|
if self._username_fixed:
|
2021-05-15 17:37:05 +02:00
|
|
|
raise AuthException("Configured username is invalid")
|
|
|
|
else:
|
2021-05-15 18:24:03 +02:00
|
|
|
self._username = None
|
2021-05-15 17:37:05 +02:00
|
|
|
|
|
|
|
def invalidate_password(self) -> None:
|
2021-05-15 18:24:03 +02:00
|
|
|
if self._password_fixed:
|
2021-05-15 17:37:05 +02:00
|
|
|
raise AuthException("Configured password is invalid")
|
|
|
|
else:
|
2021-05-15 18:24:03 +02:00
|
|
|
self._password = None
|