Skip to content

Commit

Permalink
Get user profile (#4)
Browse files Browse the repository at this point in the history
* fix: use request

* fix: result

* support fullname
  • Loading branch information
mosoriob authored Dec 12, 2024
1 parent 1b5bea7 commit 3baaf9f
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions ckanext/oauth2/oauth2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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}')

Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 3baaf9f

Please sign in to comment.