All user servers have access to a JUPYTERHUB_API_TOKEN, which allows access to the JupyterHub API.
import os
for key, value in sorted(os.environ.items()):
if key.startswith('JUPYTERHUB_'):
print(f"{key}: {value}")
JUPYTERHUB_API_TOKEN: ee9adfd02b274391848d65022a6e8e3e JUPYTERHUB_API_URL: http://127.0.0.1:8081/hub/api JUPYTERHUB_BASE_URL: / JUPYTERHUB_CLIENT_ID: jupyterhub-user-test JUPYTERHUB_HOST: JUPYTERHUB_OAUTH_CALLBACK_URL: /user/test/oauth_callback JUPYTERHUB_SERVICE_PREFIX: /user/test/ JUPYTERHUB_USER: test
Make a POST request to /hub/api/onetimetoken authorized with our JUPYTERHUB_API_TOKEN in order to create a shareable link.
import os
import requests
from urllib.parse import urlparse
from jupyterhub.utils import url_path_join
api_token = os.environ['JUPYTERHUB_API_TOKEN']
api_url = os.environ['JUPYTERHUB_API_URL']
base_url = os.environ['JUPYTERHUB_BASE_URL']
onetime_url = url_path_join(api_url, "onetimetoken")
r = requests.post(onetime_url, headers={'Authorization': f'token {api_token}'})
r.raise_for_status()
reply = r.json()
reply
{'token': 'bf7c9d072c4a4b30be92d2a799010042', 'url': '/hub/api/onetimetoken?onetimetoken=bf7c9d072c4a4b30be92d2a799010042'}
Now we can make an HTML link that can be shared, to grant one-time logins to someone with the link:
from IPython.display import HTML
HTML(f"<a href={reply['url']}>This is your one-time link</a>")
Copy that link and share it to allow login as you with one-time tokens. The link will only work once.