Skip to content

Commit

Permalink
#209: Make login() use client cache
Browse files Browse the repository at this point in the history
- Extra attention should be paid to the flushing of session ID on the client object to previous users being logged out the next time backend.login() is called
  • Loading branch information
MRichards99 committed Mar 15, 2021
1 parent 10fceff commit 3c202a8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 9 additions & 1 deletion datagateway_api/common/icat/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
create_client,
create_entities,
delete_entity_by_id,
get_cached_client,
get_count_with_filters,
get_entity_by_id,
get_entity_with_filters,
Expand Down Expand Up @@ -39,7 +40,9 @@ def __init__(self):

def login(self, credentials):
log.info("Logging in to get session ID")
client = create_client()
# There is no session ID required for this endpoint, a client object will be
# fetched from cache with a blank `sessionId` attribute
client = get_cached_client(None)

# Syntax for Python ICAT
login_details = {
Expand All @@ -48,6 +51,11 @@ def login(self, credentials):
}
try:
session_id = client.login(credentials["mechanism"], login_details)
# Flushing client's session ID so the session ID returned in this request
# won't be logged out next time `client.login()` is used in this function.
# `login()` calls `self.logout()` if `sessionId` is set
client.sessionId = None

return session_id
except ICATSessionError:
raise AuthenticationError("User credentials are incorrect")
Expand Down
8 changes: 6 additions & 2 deletions datagateway_api/common/icat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ def get_cached_client(session_id):
"""
TODO - Add docstring
"""
log.debug(f"Caching, session ID: {session_id}")
client = create_client()
client.sessionId = session_id

# `session_id` of None suggests this function is being called from an endpoint that
# doesn't use the `requires_session_id` decorator (e.g. POST /sessions)
log.info("Caching, session ID: %s", session_id)
if session_id:
client.sessionId = session_id

return client

Expand Down

0 comments on commit 3c202a8

Please sign in to comment.