From 26ba02a1d093ef465cd6be80a7c9e62a6fd2fad8 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Fri, 1 Oct 2021 15:33:46 +0300 Subject: [PATCH] Refactor get_credentials_from_request to get_username_password_from_request This better indicates that get_username_password_from_request returns a username/password pair rather than UserCreds structure --- monkey/monkey_island/cc/resources/auth/auth.py | 4 ++-- monkey/monkey_island/cc/resources/auth/credential_utils.py | 4 ++-- monkey/monkey_island/cc/resources/auth/registration.py | 7 +++++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/monkey/monkey_island/cc/resources/auth/auth.py b/monkey/monkey_island/cc/resources/auth/auth.py index 124cd1f71..9a693e80d 100644 --- a/monkey/monkey_island/cc/resources/auth/auth.py +++ b/monkey/monkey_island/cc/resources/auth/auth.py @@ -10,7 +10,7 @@ from jwt import PyJWTError import monkey_island.cc.environment.environment_singleton as env_singleton import monkey_island.cc.resources.auth.user_store as user_store from monkey_island.cc.resources.auth.credential_utils import ( - get_credentials_from_request, + get_username_password_from_request, password_matches_hash, ) from monkey_island.cc.server_utils.encryption import ( @@ -44,7 +44,7 @@ class Authenticate(flask_restful.Resource): "password": "my_password" } """ - username, password = get_credentials_from_request(request) + username, password = get_username_password_from_request(request) if _credentials_match_registered_user(username, password): if not get_datastore_encryptor(): diff --git a/monkey/monkey_island/cc/resources/auth/credential_utils.py b/monkey/monkey_island/cc/resources/auth/credential_utils.py index 874906edb..a0823d42b 100644 --- a/monkey/monkey_island/cc/resources/auth/credential_utils.py +++ b/monkey/monkey_island/cc/resources/auth/credential_utils.py @@ -19,13 +19,13 @@ def password_matches_hash(plaintext_password, password_hash): def get_user_credentials_from_request(_request) -> UserCreds: - username, password = get_credentials_from_request(_request) + username, password = get_username_password_from_request(_request) password_hash = hash_password(password) return UserCreds(username, password_hash) -def get_credentials_from_request(_request: Request) -> Tuple[str, str]: +def get_username_password_from_request(_request: Request) -> Tuple[str, str]: cred_dict = json.loads(request.data) username = cred_dict.get("username", "") password = cred_dict.get("password", "") diff --git a/monkey/monkey_island/cc/resources/auth/registration.py b/monkey/monkey_island/cc/resources/auth/registration.py index 064e80179..82dbcfe3a 100644 --- a/monkey/monkey_island/cc/resources/auth/registration.py +++ b/monkey/monkey_island/cc/resources/auth/registration.py @@ -5,7 +5,10 @@ from flask import make_response, request import monkey_island.cc.environment.environment_singleton as env_singleton from common.utils.exceptions import InvalidRegistrationCredentialsError, RegistrationNotNeededError -from monkey_island.cc.resources.auth.credential_utils import get_user_credentials_from_request +from monkey_island.cc.resources.auth.credential_utils import ( + get_user_credentials_from_request, + get_username_password_from_request, +) from monkey_island.cc.server_utils.encryption import ( initialize_datastore_encryptor, remove_old_datastore_key, @@ -26,7 +29,7 @@ class Registration(flask_restful.Resource): try: env_singleton.env.try_add_user(credentials) remove_old_datastore_key() - username, password = get_user_credentials_from_request(request) + username, password = get_username_password_from_request(request) initialize_datastore_encryptor(username, password) reset_database() return make_response({"error": ""}, 200)