From 5990098ef81156508603a401c01da9aa30258079 Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 20 Apr 2020 13:26:45 +0000 Subject: [PATCH] Add UserPassAuthenticator --- PFERD/__init__.py | 4 -- PFERD/authenticators.py | 81 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 PFERD/authenticators.py diff --git a/PFERD/__init__.py b/PFERD/__init__.py index 18ec1c0..3f44c6e 100644 --- a/PFERD/__init__.py +++ b/PFERD/__init__.py @@ -1,9 +1,5 @@ import logging -# from .ilias import * -# from .utils import * -from .temp_folder import * - STYLE = "{" FORMAT = "[{levelname:<7}] {message}" DATE_FORMAT = "%F %T" diff --git a/PFERD/authenticators.py b/PFERD/authenticators.py new file mode 100644 index 0000000..2f8da8c --- /dev/null +++ b/PFERD/authenticators.py @@ -0,0 +1,81 @@ +import getpass +from typing import Optional, Tuple + + +class UserPassAuthenticator: + + def __init__(self, username: Optional[str] = None, password: Optional[str] = None) -> None: + self._given_username = username + self._given_password = password + + self._username = username + self._password = password + + def get_credentials(self, reason: str) -> Tuple[str, str]: + """ + Returns a tuple (username, password). Prompts user for username or + password when necessary. Must be called with a "reason" that will be + displayed in the credentials prompt. + """ + + if self._username is None and self._given_username is not None: + self._username = self._given_username + + if self._password is None and self._given_password is not None: + self._password = self._given_password + + if self._username is None or self._password is None: + print(f"Enter credentials ({reason})") + + username: str + if self._username is None: + username = input("Username: ") + self._username = username + else: + username = self._username + + password: str + if self._password is None: + password = getpass.getpass(prompt="Password: ") + self._password = password + else: + password = self._password + + return (username, password) + + @property + def username(self) -> str: + """ + The username. Accessing this property may cause the authenticator to + prompt the user. + """ + + (username, _) = self.get_credentials() + return username + + @property + def password(self) -> str: + """ + The password. Accessing this property may cause the authenticator to + prompt the user. + """ + + (_, path) = self.get_credentials() + return password + + def invalidate_credentials(self) -> None: + """ + Marks the credentials as invalid. If only a username was supplied in + the constructor, assumes that the username is valid and only the + password is invalid. If only a password was supplied in the + constructor, assumes that the password is valid and only the username + is invalid. Otherwise, assumes that username and password are both + invalid. + """ + + self._username = None + self._password = None + + if self._given_username is not None and self._given_password is not None: + self._given_username = None + self._given_password = None