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

Set leeway as configurable #137

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions egi_notebooks_hub/egiauthenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from oauthenticator.generic import GenericOAuthenticator
from tornado import web
from tornado.httpclient import AsyncHTTPClient, HTTPClientError, HTTPError, HTTPRequest
from traitlets import Bool, List, Unicode, default, validate
from traitlets import Bool, Int, List, Unicode, default, validate


class JWTHandler(BaseHandler):
Expand Down Expand Up @@ -238,6 +238,13 @@ def _validate_scope(self, proposal):
help="""A prefix for the the anonymous users""",
)

auth_refresh_leeway = Int(
60,
config=True,
help="""Additional leeway time (in seconds) on top
of the auth_refresh_age to renew tokens""",
)

@default("manage_groups")
def _manage_groups_default(self):
return True
Expand Down Expand Up @@ -345,11 +352,12 @@ async def refresh_user(self, user, handler=None):

try:
# We want to fall on the safe side for refreshing, hence using
# the auth_refresh_age plus one second and negative as the code
# checks that the token is valid as of (now - leeway)
# the auth_refresh_age plus a configurable leeway
# Set as negative as the code checks that the token is
# valid as of (now - leeway)
# See PyJWT code here:
# https://github.com/jpadilla/pyjwt/blob/868cf4ab2ca5a0a39da40e5a14dd740b203662b2/jwt/api_jwt.py#L306
leeway = -float(self.auth_refresh_age + 1)
leeway = -float(self.auth_refresh_age + self.auth_refresh_leeway)
if jwt.decode(
access_token,
options=dict(
Expand Down