diff --git a/monkey/monkey_island/cc/environment/user_creds.py b/monkey/monkey_island/cc/environment/user_creds.py index 6cd483f70..a86472cd9 100644 --- a/monkey/monkey_island/cc/environment/user_creds.py +++ b/monkey/monkey_island/cc/environment/user_creds.py @@ -1,6 +1,5 @@ from __future__ import annotations -import json from typing import Dict import bcrypt @@ -32,19 +31,3 @@ class UserCreds: password_hash = bcrypt.hashpw(cleartext_password.encode("utf-8"), bcrypt.gensalt()).decode() return cls(username, password_hash) - - @staticmethod - def get_from_new_registration_dict(data_dict: Dict) -> UserCreds: - creds = UserCreds() - if "user" in data_dict: - creds.username = data_dict["user"] - if "password" in data_dict: - creds.password_hash = bcrypt.hashpw( - data_dict["password"].encode("utf-8"), bcrypt.gensalt() - ).decode() - return creds - - @staticmethod - def get_from_json(json_data: bytes) -> UserCreds: - cred_dict = json.loads(json_data) - return UserCreds.get_from_new_registration_dict(cred_dict) diff --git a/monkey/monkey_island/cc/resources/auth/registration.py b/monkey/monkey_island/cc/resources/auth/registration.py index e5ca99232..8803a7d12 100644 --- a/monkey/monkey_island/cc/resources/auth/registration.py +++ b/monkey/monkey_island/cc/resources/auth/registration.py @@ -1,3 +1,5 @@ +import json + import flask_restful from flask import make_response, request @@ -11,9 +13,19 @@ class Registration(flask_restful.Resource): return {"needs_registration": env_singleton.env.needs_registration()} def post(self): - credentials = UserCreds.get_from_json(request.data) + credentials = _get_user_credentials_from_request(request) + try: env_singleton.env.try_add_user(credentials) return make_response({"error": ""}, 200) except (InvalidRegistrationCredentialsError, RegistrationNotNeededError) as e: return make_response({"error": str(e)}, 400) + + +def _get_user_credentials_from_request(request): + cred_dict = json.loads(request.data) + + username = cred_dict.get("user", "") + password = cred_dict.get("password", "") + + return UserCreds.from_cleartext(username, password)