Skip to content

Commit

Permalink
Refactor method names
Browse files Browse the repository at this point in the history
  • Loading branch information
nuwang committed Nov 3, 2023
1 parent 0e01b93 commit c08a915
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
13 changes: 13 additions & 0 deletions lib/galaxy/authnz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,16 @@ def logout(self, trans, post_user_logout_href=None):
:param post_user_logout_href: Optional URL to redirect to after logging out of IDP.
"""
raise NotImplementedError()

def find_user_by_access_token(self, sa_session, access_token):
"""
Locates a user by access_token. The access token must be verified prior
to returning the relevant user.
:type sa_session: sqlalchemy.orm.scoping.scoped_session
:param sa_session: SQLAlchemy database handle.
:type access_token: string
:param access_token: An OIDC access token
"""
raise NotImplementedError()
2 changes: 1 addition & 1 deletion lib/galaxy/authnz/custos_authnz.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def _username_from_userinfo(trans, userinfo):
else:
return username

def match_access_token_to_user(self, sa_session, access_token):
def find_user_by_access_token(self, sa_session, access_token):
signing_key = self.jwks_client.get_signing_key_from_jwt(access_token)
decoded_jwt = jwt.decode(
access_token,
Expand Down
10 changes: 6 additions & 4 deletions lib/galaxy/authnz/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,26 +408,28 @@ def create_user(self, provider, token, trans, login_redirect_url):
log.exception(msg)
return False, msg, (None, None)

def find_user_by_access_token_in_provider(self, sa_session, provider, access_token):
def _find_user_by_access_token_in_provider(self, sa_session, provider, access_token):
try:
success, message, backend = self._get_authnz_backend(provider)
if success is False:
msg = f"An error occurred when obtaining user by token with provider `{provider}`: {message}"
log.error(msg)
return None
user = backend.match_access_token_to_user(sa_session, access_token)
user = backend.find_user_by_access_token(sa_session, access_token)
if user:
log.debug(f"Found user: {user} via `{provider}` identity provider")
return user
return None
except NotImplementedError:
return None
except Exception as e:
msg = f"An error occurred with provider: {provider} when finding user by token: {e}"
log.error(msg)
return None

def find_user_by_access_token(self, sa_session, access_token):
def match_access_token_to_user(self, sa_session, access_token):
for provider in self.oidc_backends_config:
user = self.find_user_by_access_token_in_provider(sa_session, provider, access_token)
user = self._find_user_by_access_token_in_provider(sa_session, provider, access_token)
if user:
return user
return None
Expand Down
2 changes: 1 addition & 1 deletion lib/galaxy/managers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def by_api_key(self, api_key: str, sa_session=None):

def by_oidc_access_token(self, access_token: str):
if hasattr(self.app, "authnz_manager") and self.app.authnz_manager:
user = self.app.authnz_manager.find_user_by_access_token(self.app.model.session, access_token) # type: ignore[attr-defined]
user = self.app.authnz_manager.match_access_token_to_user(self.app.model.session, access_token) # type: ignore[attr-defined]
return user
else:
return None
Expand Down

0 comments on commit c08a915

Please sign in to comment.