Skip to content

Commit

Permalink
monkey: Remove coupling between Registration and UserCreds
Browse files Browse the repository at this point in the history
  • Loading branch information
mssalvatore committed May 4, 2021
1 parent 4b3b7af commit 1aed5f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
17 changes: 0 additions & 17 deletions monkey/monkey_island/cc/environment/user_creds.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import json
from typing import Dict

import bcrypt
Expand Down Expand Up @@ -32,19 +31,3 @@ def from_cleartext(cls, username, cleartext_password):
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)
14 changes: 13 additions & 1 deletion monkey/monkey_island/cc/resources/auth/registration.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

import flask_restful
from flask import make_response, request

Expand All @@ -11,9 +13,19 @@ def get(self):
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)

0 comments on commit 1aed5f3

Please sign in to comment.