Skip to content

[All] Fix handling of expired id tokens when userdata_from_id_token is True #795

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

consideRatio
Copy link
Member

@consideRatio consideRatio commented Jun 6, 2025

This is meant to resolve #793, which is an issue including an excellent overview of the situation.

DRAFT: I don't trust my own work yet, and think maybe the interaction between refresh_user and userdata_from_id_token doesn't make sense. If refresh_user is called, we shouldn't reuse the id_token no matter if its expired or not, right? I think we currently try to reuse it if refresh_user is called. Anyone is most welcome to take over by opening a new PR or pushing commits to this PR, I'm not confident I'll find time to figure this out properly.

This fix is only be relevant when `userdata_from_id_token` is True.
@consideRatio consideRatio changed the title Fix handling of expired id tokens [All] Fix handling of expired id tokens when userdata_from_id_token is True Jun 6, 2025
@consideRatio consideRatio marked this pull request as draft June 6, 2025 21:29
Copy link
Member

@manics manics left a comment

Choose a reason for hiding this comment

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

As you've pointed out this code is difficult to navigate, so ideally we should add a test.
test_refresh_user currently deletes the access_token to trigger a refresh

generic_client.access_tokens.pop(refreshed_state["access_token"])
refreshed = await authenticator.refresh_user(user, handler)

so maybe we can parametrize it to test an expired id_token? Or refactor the test to mock jwt.decode?

# opt-in to what we want to check
verify_signature=False,
verify_aud=True,
verify_exp=True,
),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
),
require=["exp"],
),

https://pyjwt.readthedocs.io/en/stable/api.html

Warning

exp, iat and nbf will only be verified if present. Please pass respective value to require if you want to make sure that they are always present (and therefore always verified if verify_exp, verify_iat, and verify_nbf respectively is set to True).

@minrk
Copy link
Member

minrk commented Jun 11, 2025

This code looks good and sensible to me.

To get started on a test, here's a way to create and validate an expired token:

key = b'asdf'
expired_token = jwt.encode({"exp": time.time() - 10}, key)
jwt.decode(expired_token, key, algorithms=["HS256"])
---------------------------------------------------------------------------
ExpiredSignatureError

We could make it valid for one second and then wait for it to expire and call refresh. I'm not sure how easy mocking pyjwt verification into the future is via public APIs, but mocking _validate_exp would do it.

@jabbera
Copy link
Contributor

jabbera commented Jun 17, 2025

If you are looking at this I would encourage you to combine this: #790 into the feature. Instead of looking at if the token is expired, look at if it is or is about to expire within some buffer. id tokens can be used outside the context of the refresh process for lots of useful things. Knowing they are not expired exactly when the refresh is called is not as helpful IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Unable to decode id token" on singleuser pod spawn with short-lived id tokens
4 participants