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

Add ability to authenticate with a JWT loaded from a file. #54

Draft
wants to merge 13 commits into
base: v1.x.y
Choose a base branch
from

Conversation

aaronweeden
Copy link

@aaronweeden aaronweeden commented Jan 27, 2025

Description

This PR, along with an upcoming PR to xdmod, makes it possible for xdmod-data to authenticate to the XDMoD REST API via a JSON Web Token (JWT) loaded from a file. This is needed to support XDMoD being connected to a JupyterHub. The JupyterHub will periodically write temporary JWTs to the file ~/.xdmod-jwt.env; the changes in this PR make it so that every time a request is made by xdmod-data to the XDMoD REST API, that file is read and the token is added as a header Authentication: Bearer <JWT> to the request.

The current method of instead setting an XDMOD_API_TOKEN environment variable is still needed since users connecting outside of a JupyterHub will still be supported. This PR makes it so that if that environment variable is defined when the DataWarehouse is constructed, it will use that token in the request headers instead of the JWT from the file. This also supports the ability in the JupyterHub setup to interface with multiple XDMoD portals from the same notebook, as in the following:

# Assuming this JupyterHub is connected to the XDMoD portal at `URL1`,
# and `XDMOD_API_TOKEN` is not currently set, `dw1` will use the
# JWT from the file `~/.xdmod-jwt.env`:
dw1 = DataWarehouse(URL1)
# If we want `dw2` to pull from `URL2` with `API_TOKEN_2`,
# we can then set it as follows:
os.environ['XDMOD_API_TOKEN'] = API_TOKEN_2
dw2 = DataWarehouse(URL2)
# And similarly for `dw3`:
os.environ['XDMOD_API_TOKEN'] = API_TOKEN_3
dw3 = DataWarehouse(URL3)
# And then we can use each `DataWarehouse`:
with dw1:
    ...
with dw2:
    ...
with dw3:
    ...

Tests performed

Type of change:

  • Refactoring / documentation update (non-breaking change which does not change functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Release preparation

Checklist:

  • CHANGELOG.md has been updated
  • xdmod_data/__version__.py has been updated to the next development version
  • The milestone is set correctly on the pull request
  • The appropriate labels have been added to the pull request
  • Updates have been made to the xdmod-notebooks repository as necessary, and the notebooks all run successfully
  • The changes in this PR have been ported/backported to other branches as needed

@aaronweeden aaronweeden modified the milestones: 1.1.0, 2.0.0 Jan 27, 2025
@aaronweeden aaronweeden added the enhancement New feature or request label Jan 27, 2025
@aaronweeden aaronweeden changed the base branch from main to v1.x.y January 27, 2025 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant