island: Add UserCreds.from_cleartext()

This commit is contained in:
Mike Salvatore 2021-05-04 15:14:41 -04:00
parent 1be07a4828
commit 5fa08f0447
2 changed files with 17 additions and 0 deletions

View File

@ -27,6 +27,12 @@ class UserCreds:
def to_auth_user(self) -> User: def to_auth_user(self) -> User:
return User(1, self.username, self.password_hash) return User(1, self.username, self.password_hash)
@classmethod
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 @staticmethod
def get_from_new_registration_dict(data_dict: Dict) -> UserCreds: def get_from_new_registration_dict(data_dict: Dict) -> UserCreds:
creds = UserCreds() creds = UserCreds()

View File

@ -1,5 +1,9 @@
import bcrypt
from monkey_island.cc.environment.user_creds import UserCreds from monkey_island.cc.environment.user_creds import UserCreds
TEST_SALT = b"$2b$12$JA7GdT1iyfIsquF2cTZv2."
def test_to_dict_empty_creds(): def test_to_dict_empty_creds():
user_creds = UserCreds() user_creds = UserCreds()
@ -25,3 +29,10 @@ def test_to_auth_user_username_only():
assert auth_user.id == 1 assert auth_user.id == 1
assert auth_user.username == "Test" assert auth_user.username == "Test"
assert auth_user.secret == "" assert auth_user.secret == ""
def test_get_from_cleartext(monkeypatch):
monkeypatch.setattr(bcrypt, "gensalt", lambda: TEST_SALT)
creds = UserCreds.from_cleartext("Test", "Test_Password")
assert creds.password_hash == "$2b$12$JA7GdT1iyfIsquF2cTZv2.NdGFuYbX1WGfQAOyHlpEsgDTNGZ0TXG"