Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tasks/WP-190: Handle concurrency with Tapis OAuth Token Refresh #932

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
58 changes: 51 additions & 7 deletions server/portal/apps/auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ def expired(self):
:return: True or False, depending if the token is expired.
:rtype: bool
"""
current_time = time.time()
return self.created + self.expires_in - current_time - TOKEN_EXPIRY_THRESHOLD <= 0
return self.is_token_expired(self.created, self.expires_in)

@property
def created_at(self):
Expand Down Expand Up @@ -72,11 +71,11 @@ def client(self):
:return: Tapis client using refresh token.
:rtype: :class:Tapis
"""
client = Tapis(base_url=getattr(settings, 'TAPIS_TENANT_BASEURL'),
client_id=getattr(settings, 'TAPIS_CLIENT_ID'),
client_key=getattr(settings, 'TAPIS_CLIENT_KEY'),
access_token=self.access_token,
refresh_token=self.refresh_token)
# Optimize build only if enabled by the portal via `ENABLE_OPTIMIZED_OAUTH_REFRESH`
if settings.ENABLE_OPTIMIZED_OAUTH_REFRESH:
return self.optimized_client()

client = self.build_client()

with transaction.atomic():
if self.expired:
Expand All @@ -92,6 +91,46 @@ def client(self):

return client

def optimized_client(self):
"""Tapis client to limit one request to Tapis per User.

:return: Tapis client using refresh token.
:rtype: :class:Tapis
"""
client = self.build_client()
if self.expired:
logger.info('Tapis OAuth token expired')
with transaction.atomic():
# Get a lock on this user's token row in db.
refreshed_token = TapisOAuthToken.objects.select_for_update().filter(user=self.user).first()
if self.is_token_expired(refreshed_token.created, refreshed_token.expires_in):
try:
logger.info('Refreshing tapis oauth token')
client.refresh_tokens()
except Exception:
logger.exception('Tapis Token refresh failed')
raise
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On failure, perhaps we should call logout

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, will test it and share info.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current logic is all in a model, can't do http redirect or control view responses from here. Have to do from view. I setup an custom exception and handled it in Base View to send 401 back to client. On testing, by forcing an error - it turned the 401 to redirect to tapis oauth, but that failed due to CORS policy issue, I have to check if this is local setup or something else.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I came to that realization as well today. I tried another solution with DesignSafe, which is to put the refresh logic in a middleware. In CEP we originally moved that logic from middleware to the client() method, but I think the solution you propose here might solve the original issue there?

Haven't tested yet, what are your thoughts?
https://github.com/DesignSafe-CI/portal/blob/task/DES-2702--tapis-v3-oauth/designsafe/apps/auth/middleware.py

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rstijerina - sorry for delay in response, I missed this note.
I looked at the code in that branch. It looks good, one comment on overall integration:

  • Should you do this also for logout?
    logout(request)
    return HttpResponseRedirect(reverse('designsafe_auth:login'))

Some testing aspects:

  • Behavior on xhr requests when tapis token expiry fails. If xhr does not handle 302 cleanly, some extra check and redirect might be needed.
  • Walking through the code, it is protected from infinite loop through this(which is good). If refresh fails, goes to login, which has to authenticate with tapis, if authentication works, this middleware immediately returns (because there is no expiry) and move away from this middleware.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use do this also for logout?
logout(request)
return HttpResponseRedirect(reverse('designsafe_auth:login'))

Yes, thanks. Added:
https://github.com/DesignSafe-CI/portal/blob/task/DES-2709--v3-apps-views/designsafe/apps/auth/middleware.py

Behavior on xhr requests when tapis token expiry fails. If xhr does not handle 302 cleanly, some extra check and redirect might be needed.

Can you expand more on this part? Where would tapis token expiry fail?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Behavior on xhr requests when tapis token expiry fails. If xhr does not handle 302 cleanly, some extra check and redirect might be needed.

Can you expand more on this part? Where would tapis token expiry fail?
I meant - "Behavior on xhr requests when tapis token expires, and the refresh fails - this will hit the logout code and send a 302 back to client. If javascript side of response handling does not handle 302 cleanly (page rendering after 302, etc), may be extra logic need be needed to check for 302 status and specific error type(token expired) and then setting location href to logout".

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rstijerina - regarding this PR, if middleware is the right place for auth and if it is working in designsafe, should I do the same here and start testing. What is your opinion?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The solution I have in DesginSafe does work, here are example logs from a refresh that just occurred for me:

des_django         | [DJANGO] INFO 2024-04-12 14:28:57,764 middleware designsafe.apps.auth.middleware.process_request:49: Tapis OAuth token expired for user sal. Refreshing token
des_django         | [DJANGO] INFO 2024-04-12 14:28:57,769 middleware designsafe.apps.auth.middleware.process_request:49: Tapis OAuth token expired for user sal. Refreshing token
des_django         | [DJANGO] INFO 2024-04-12 14:28:57,775 middleware designsafe.apps.auth.middleware.process_request:61: Refreshing Tapis OAuth token
des_django         | [DJANGO] INFO 2024-04-12 14:29:02,626 middleware designsafe.apps.auth.middleware.process_request:72: Token updated by another request. Refreshing token from DB.

It might not be fool-proof though, and could definitely use more testing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could talk about best place for token refresh in the next infra scrum?


self.update(created=int(time.time()),
access_token=client.access_token.access_token,
expires_in=client.access_token.expires_in().total_seconds())
else:
logger.info('Token updated by another request. Refreshing token from DB.')
# Token is no longer expired, refresh latest token info from DB and update client
self.refresh_from_db()
client = self.build_client()

return client

def build_client(self):
return Tapis(
base_url=getattr(settings, "TAPIS_TENANT_BASEURL"),
client_id=getattr(settings, "TAPIS_CLIENT_ID"),
client_key=getattr(settings, "TAPIS_CLIENT_KEY"),
access_token=self.access_token,
refresh_token=self.refresh_token,
)

def update(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
Expand All @@ -107,3 +146,8 @@ def __str__(self):
access_token_masked = self.access_token[-5:]
refresh_token_masked = self.refresh_token[-5:]
return f'access_token:{access_token_masked} refresh_token:{refresh_token_masked} expires_in:{self.expires_in} created:{self.created}'

@staticmethod
def is_token_expired(created, expires_in):
current_time = time.time()
return created + expires_in - current_time - TOKEN_EXPIRY_THRESHOLD <= 0
5 changes: 5 additions & 0 deletions server/portal/settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,11 @@

PORTAL_ELEVATED_ROLES = getattr(settings_custom, '_PORTAL_ELEVATED_ROLES', {})

"""
SETTINGS: OPTIONAL FEATURES. Edit settings_custom
"""
ENABLE_OPTIMIZED_OAUTH_REFRESH = getattr(settings_custom, '_ENABLE_OPTIMIZED_OAUTH_REFRESH', False)

"""
SETTINGS: LOCAL OVERRIDES
"""
Expand Down