From 52f461f425ca529409c5ac1efaee56f29a942a81 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 18 Nov 2021 08:52:16 -0500 Subject: [PATCH] Island: Add IUserDatastore --- monkey/common/utils/exceptions.py | 4 +++ .../authentication/i_user_datastore.py | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 monkey/monkey_island/cc/services/authentication/i_user_datastore.py diff --git a/monkey/common/utils/exceptions.py b/monkey/common/utils/exceptions.py index 50dcb2d6b..dd84380f6 100644 --- a/monkey/common/utils/exceptions.py +++ b/monkey/common/utils/exceptions.py @@ -18,6 +18,10 @@ class AlreadyRegisteredError(RegistrationNotNeededError): """ Raise to indicate the reason why registration is not required """ +class UnknownUserError(Exception): + """ Raise to indicate that authentication failed """ + + class IncorrectCredentialsError(Exception): """ Raise to indicate that authentication failed """ diff --git a/monkey/monkey_island/cc/services/authentication/i_user_datastore.py b/monkey/monkey_island/cc/services/authentication/i_user_datastore.py new file mode 100644 index 000000000..13331963a --- /dev/null +++ b/monkey/monkey_island/cc/services/authentication/i_user_datastore.py @@ -0,0 +1,36 @@ +import abc + +from monkey_island.cc.environment.user_creds import UserCreds + + +class IUserDatastore(metaclass=abc.ABCMeta): + """ + Allows user credentials to be stored and retrieved. + """ + + @abc.abstractmethod + def has_registered_users(self) -> bool: + """ + Checks if there are any registered user. + :return: True if any users have been registered, False otherwise + :rtype: bool + """ + + @abc.abstractmethod + def add_user(self, credentials: UserCreds): + """ + Adds a new user to the datastore. + :param UserCreds credentials: New user credentials to persistant storage. + :raises InvalidRegistrationCredentialsError: if the credentials are malformed + :raises AlreadyRegisteredError: if the user has already been registered + """ + + @abc.abstractmethod + def get_user_credentials(self, username: str) -> UserCreds: + """ + Gets the user matching `username` from storage. + :param str username: The username for which credentials will be retrieved + :return: User credentials for username + :rtype: UserCreds + :raises UnknownUserError: if the username does not exist + """