Skip to content

feat(medcat-trainer): Set token expiry for OIDC sessions#543

Merged
jocelyneholdbrook merged 3 commits into
mainfrom
feat/medcat-trainer/refresh-auth-token-periodically-in-mctclient
Jun 15, 2026
Merged

feat(medcat-trainer): Set token expiry for OIDC sessions#543
jocelyneholdbrook merged 3 commits into
mainfrom
feat/medcat-trainer/refresh-auth-token-periodically-in-mctclient

Conversation

@jocelyneholdbrook

@jocelyneholdbrook jocelyneholdbrook commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Summary

This Pull Request implements a token refreshing mechanism for OIDC (OpenID Connect) sessions in the MedCATTrainer client and introduces corresponding unit tests to validate the feature.

  • Added _refresh_oidc_token and ensure_token_fresh methods for handling OIDC token refreshes and ensuring token validity.
  • Updated existing authentication code to integrate with the new refreshing mechanism, including storing token expiry.
  • Introduced unit tests for verifying different scenarios of the OIDC token refresh logic (test_ensure_token_fresh suite).
  • Adjusted relevant test setups for proper mocking and validation of token refresh behavior.
    This change improves authentication session management and adds robustness to OIDC workflows.

… refresh and use a valid one throughout the session
@jocelyneholdbrook jocelyneholdbrook changed the title feat(medcat-trainer): Update mctclient to set token expiry so that we refresh and use a valid one throughout the session feat(medcat-trainer): Set token expiry for OIDC sessions Jun 15, 2026
Comment thread medcat-trainer/client/mctclient.py Outdated
# Refresh 60s before the typical 5-minute Keycloak access token lifetime (i.e. 4 minutes from now)
self._token_expiry = time.monotonic() + 240

def ensure_token_fresh(self) -> None:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Does this need to be wired in somewhere? Wondering how this is triggered

@jocelyneholdbrook jocelyneholdbrook Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it can be called by whoever uses this client. The idea is that you check whether the current OIDC token is about to expire, and if so, fetches a new one from Keycloak before the next API call goes out. I will be using it in medcattery's medcat-trainer.py. But in an ideal scenario it should be checked in every medcat-trainer HTTP request but I wanted to avoid making a load of changes at this stage.

@jocelyneholdbrook jocelyneholdbrook merged commit 6414663 into main Jun 15, 2026
10 checks passed
@jocelyneholdbrook jocelyneholdbrook deleted the feat/medcat-trainer/refresh-auth-token-periodically-in-mctclient branch June 15, 2026 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants