island: Remove UserCreds.from_cleartext()

This commit is contained in:
Mike Salvatore 2021-05-04 18:53:43 -04:00
parent f73b048169
commit 0f49a2c96a
3 changed files with 3 additions and 20 deletions

View File

@ -2,8 +2,6 @@ from __future__ import annotations
from typing import Dict from typing import Dict
import bcrypt
from monkey_island.cc.resources.auth.auth_user import User from monkey_island.cc.resources.auth.auth_user import User
@ -25,9 +23,3 @@ 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)

View File

@ -1,5 +1,6 @@
import json import json
import bcrypt
import flask_restful import flask_restful
from flask import make_response, request from flask import make_response, request
@ -27,5 +28,6 @@ def _get_user_credentials_from_request(request):
username = cred_dict.get("user", "") username = cred_dict.get("user", "")
password = cred_dict.get("password", "") password = cred_dict.get("password", "")
password_hash = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode()
return UserCreds.from_cleartext(username, password) return UserCreds(username, password_hash)

View File

@ -1,5 +1,3 @@
import bcrypt
from monkey_island.cc.environment.user_creds import UserCreds from monkey_island.cc.environment.user_creds import UserCreds
TEST_USER = "Test" TEST_USER = "Test"
@ -41,16 +39,7 @@ def test_to_auth_user_full_credentials():
assert auth_user.secret == TEST_HASH assert auth_user.secret == TEST_HASH
def test_get_from_cleartext(monkeypatch):
monkeypatch.setattr(bcrypt, "gensalt", lambda: TEST_SALT)
creds = UserCreds.from_cleartext(TEST_USER, "Test_Password")
assert creds.password_hash == "$2b$12$JA7GdT1iyfIsquF2cTZv2.NdGFuYbX1WGfQAOyHlpEsgDTNGZ0TXG"
def test_member_values(monkeypatch): def test_member_values(monkeypatch):
monkeypatch.setattr(bcrypt, "gensalt", lambda: TEST_SALT)
creds = UserCreds(TEST_USER, TEST_HASH) creds = UserCreds(TEST_USER, TEST_HASH)
assert creds.username == TEST_USER assert creds.username == TEST_USER
assert creds.password_hash == TEST_HASH assert creds.password_hash == TEST_HASH