forked from p15670423/monkey
monkey: Remove coupling between Registration and UserCreds
This commit is contained in:
parent
4b3b7af3d2
commit
1aed5f37d1
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue