From 3baaf9fcf6ba3ac0e074cb1523ddaae56f3d4db8 Mon Sep 17 00:00:00 2001 From: Maximiliano Osorio Date: Thu, 12 Dec 2024 14:43:05 -0300 Subject: [PATCH] Get user profile (#4) * fix: use request * fix: result * support fullname --- ckanext/oauth2/oauth2.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/ckanext/oauth2/oauth2.py b/ckanext/oauth2/oauth2.py index 4322a31..24bf560 100644 --- a/ckanext/oauth2/oauth2.py +++ b/ckanext/oauth2/oauth2.py @@ -75,6 +75,8 @@ def __init__(self): self.rememberer_name = six.text_type(os.environ.get('CKAN_OAUTH2_REMEMBER_NAME', toolkit.config.get('ckan.oauth2.rememberer_name', 'auth_tkt'))).strip() self.profile_api_user_field = six.text_type(os.environ.get('CKAN_OAUTH2_PROFILE_API_USER_FIELD', toolkit.config.get('ckan.oauth2.profile_api_user_field', ''))).strip() self.profile_api_fullname_field = six.text_type(os.environ.get('CKAN_OAUTH2_PROFILE_API_FULLNAME_FIELD', toolkit.config.get('ckan.oauth2.profile_api_fullname_field', ''))).strip() + self.profile_api_firstname_field = six.text_type(os.environ.get('CKAN_OAUTH2_PROFILE_API_FIRSTNAME_FIELD', toolkit.config.get('ckan.oauth2.profile_api_firstname_field', ''))).strip() + self.profile_api_lastname_field = six.text_type(os.environ.get('CKAN_OAUTH2_PROFILE_API_LASTNAME_FIELD', toolkit.config.get('ckan.oauth2.profile_api_lastname_field', ''))).strip() self.profile_api_mail_field = six.text_type(os.environ.get('CKAN_OAUTH2_PROFILE_API_MAIL_FIELD', toolkit.config.get('ckan.oauth2.profile_api_mail_field', ''))).strip() self.profile_api_groupmembership_field = six.text_type(os.environ.get('CKAN_OAUTH2_PROFILE_API_GROUPMEMBERSHIP_FIELD', toolkit.config.get('ckan.oauth2.profile_api_groupmembership_field', ''))).strip() self.sysadmin_group_name = six.text_type(os.environ.get('CKAN_OAUTH2_SYSADMIN_GROUP_NAME', toolkit.config.get('ckan.oauth2.sysadmin_group_name', ''))).strip() @@ -156,8 +158,11 @@ def identify(self, token): profile_response = requests.get(self.profile_api_url + '?access_token=%s' % token['access_token'], verify=self.verify_https) log.debug(f'profile response: {profile_response}') else: - oauth = OAuth2Session(self.client_id, token=token) - profile_response = oauth.get(self.profile_api_url) + log.debug(f'token: {token}') + headers = { + 'X-Tapis-Token': token['access_token'] + } + profile_response = requests.get(self.profile_api_url, headers=headers, verify=self.verify_https) log.debug(f'profile response_: {profile_response}') except requests.exceptions.SSLError as e: @@ -176,7 +181,8 @@ def identify(self, token): else: profile_response.raise_for_status() else: - user_data = profile_response.json() + log.debug(f'profile_response: {profile_response}') + user_data = profile_response.json()['result'] user = self.user_json(user_data) log.debug(f'user: {user}') @@ -220,6 +226,12 @@ def user_json(self, user_data): # Update optional fields if provided if self.profile_api_fullname_field and self.profile_api_fullname_field in user_data: user.fullname = user_data[self.profile_api_fullname_field] + elif self.profile_api_firstname_field and self.profile_api_lastname_field and self.profile_api_firstname_field in user_data and self.profile_api_lastname_field in user_data: + user.fullname = f"{user_data[self.profile_api_firstname_field]} {user_data[self.profile_api_lastname_field]}" + elif self.profile_api_firstname_field and self.profile_api_firstname_field in user_data: + user.fullname = user_data[self.profile_api_firstname_field] + elif self.profile_api_lastname_field and self.profile_api_lastname_field in user_data: + user.fullname = user_data[self.profile_api_lastname_field] if self.profile_api_groupmembership_field and self.profile_api_groupmembership_field in user_data: user.sysadmin = self.sysadmin_group_name in user_data[self.profile_api_groupmembership_field]