add validators

This commit is contained in:
lpm0073 2022-10-05 20:51:52 -05:00
parent 1b668c5029
commit 85c60e31e1

View File

@ -103,6 +103,18 @@ class StepwiseMathWPOAuth2(BaseOAuth2):
def _urlopen(self, url): def _urlopen(self, url):
return urlopen(url).read().decode("utf-8") return urlopen(url).read().decode("utf-8")
def is_valid_user_details(self, response) -> bool:
if not type(response) == dict: return False
qc_keys = ['id', 'date_joined', 'email', 'first_name', 'fullname', 'is_staff', 'is_superuser', 'last_name', 'username']
if all(key in response for key in qc_keys): return True
return False
def is_wp_oauth_response(self, response) -> bool:
if not type(response) == dict: return False
qc_keys = ['ID' 'display_name', 'user_email', 'user_login', 'user_roles']
if all(key in response for key in qc_keys): return True
return False
# override Python Social Auth default end points. # override Python Social Auth default end points.
# see https://wp-oauth.com/docs/general/endpoints/ # see https://wp-oauth.com/docs/general/endpoints/
# #
@ -135,13 +147,12 @@ class StepwiseMathWPOAuth2(BaseOAuth2):
@user_details.setter @user_details.setter
def user_details(self, value: dict): def user_details(self, value: dict):
self._user_details = value if self.is_valid_user_details(value):
self._user_details = value
def is_valid_user_details(self, response) -> bool: else:
if not type(response) == dict: return False logger.error('user_details.setter: tried to pass an invalid object {value}'.format(
qc_keys = ['id', 'date_joined', 'email', 'first_name', 'fullname', 'is_staff', 'is_superuser', 'last_name', 'username'] value=json.dumps(value, sort_keys=True, indent=4)
if all(key in response for key in qc_keys): return True ))
return False
# see https://python-social-auth.readthedocs.io/en/latest/backends/implementation.html # see https://python-social-auth.readthedocs.io/en/latest/backends/implementation.html
# Return user details from the Wordpress user account # Return user details from the Wordpress user account
@ -179,8 +190,7 @@ class StepwiseMathWPOAuth2(BaseOAuth2):
# otherwise we pobably received the default response from the oauth provider based on # otherwise we pobably received the default response from the oauth provider based on
# the scopes 'basic' 'email' 'profile'. We'll check a few of the most important keys to see # the scopes 'basic' 'email' 'profile'. We'll check a few of the most important keys to see
# if they exist. # if they exist.
qc_keys = ['ID' 'display_name', 'user_email', 'user_login', 'user_roles'] if not self.is_wp_oauth_response(response):
if not all(key in response for key in qc_keys):
logger.warning('get_user_details() - response object is missing one or more required keys: {response}'.format( logger.warning('get_user_details() - response object is missing one or more required keys: {response}'.format(
response=json.dumps(response, sort_keys=True, indent=4) response=json.dumps(response, sort_keys=True, indent=4)
)) ))