I am wondering is it possible to get the current logged in user info in Jupyterlab extension by making a call to Jupyterhub API, the only problem I am facing is that the tokens are assigned to the users. In the extension, I need to check if the user is an admin or not and then show the menu items accordingly?
You are right, but the /user API endpoint expects a token for that specific user and returns the model based on the user token so the question is how do I get a user-specific token in the jupyterlab extension.
I figured out how to get the user-specific token in the extension by using PageConfig.getToken() function to get the token but this function returns an empty string instead of returning the token for the user.
You could probably patch-in a similar page_config_hook via jupyter_server_config to work with current JupyterHub (note: you’d need to use the get_secure_cookie bit to make sure you still get the token for cookie-authenticated requests).
Something like:
# /etc/jupyter/jupyter_server_config.py
def page_config_hook(handler, page_config):
hub_auth = handler.settings["hub_auth"]
token = hub_auth.get_token(handler)
if not token:
token = handler.get_secure_cookie(self.cookie_name)
if token:
token = token.decode("ascii", "replace")
page_config["token"] = token
return page_config
c.ServerApp.tornado_settings.update({"page_config_hook": page_config_hook})
Hi @minrk, sorry I am new to JupyterHub configuration and here is what my configuration file looks like for tljh but still I am unable to get token in my Jupyterlab extension.
This needs to be configuration for the single-user server (a different config file), not JupyterHub. You can put it in e.g. /etc/jupyter/jupyter_server_config.py