island: Replace private static functions in Authenticator with functions

In python, private static methods serve no purpose. Python has
first-class functions; let's use them.
This commit is contained in:
Mike Salvatore 2021-05-04 12:28:17 -04:00
parent c7d47fee9c
commit 904e51a365
1 changed files with 22 additions and 24 deletions

View File

@ -38,16 +38,16 @@ class Authenticate(flask_restful.Resource):
"password": "my_password" "password": "my_password"
} }
""" """
(username, password) = Authenticate._get_credentials_from_request(request) (username, password) = _get_credentials_from_request(request)
if self._credentials_match_registered_user(username, password): if _credentials_match_registered_user(username, password):
access_token = Authenticate._create_access_token(username) access_token = _create_access_token(username)
return make_response({"access_token": access_token, "error": ""}, 200) return make_response({"access_token": access_token, "error": ""}, 200)
else: else:
return make_response({"error": "Invalid credentials"}, 401) return make_response({"error": "Invalid credentials"}, 401)
@staticmethod
def _get_credentials_from_request(request): def _get_credentials_from_request(request):
credentials = json.loads(request.data) credentials = json.loads(request.data)
username = credentials["username"] username = credentials["username"]
@ -55,22 +55,20 @@ class Authenticate(flask_restful.Resource):
return (username, password) return (username, password)
@staticmethod
def _credentials_match_registered_user(username, password): def _credentials_match_registered_user(username, password):
user = user_store.UserStore.username_table.get(username, None) user = user_store.UserStore.username_table.get(username, None)
if user and bcrypt.checkpw(password.encode("utf-8"), user.secret.encode("utf-8")): if user and bcrypt.checkpw(password.encode("utf-8"), user.secret.encode("utf-8")):
return True return True
return False return False
@staticmethod
def _create_access_token(username): def _create_access_token(username):
access_token = flask_jwt_extended.create_access_token( access_token = flask_jwt_extended.create_access_token(
identity=user_store.UserStore.username_table[username].id identity=user_store.UserStore.username_table[username].id
) )
logger.debug( logger.debug(f"Created access token for user {username} that begins with {access_token[:4]}")
f"Created access token for user {username} that begins with {access_token[:4]}"
)
return access_token return access_token