Skip to content

Commit

Permalink
default to auth extensions with Jupyter Server 2
Browse files Browse the repository at this point in the history
  • Loading branch information
minrk committed Feb 14, 2023
1 parent 5f9283c commit 6b6784f
Showing 1 changed file with 51 additions and 16 deletions.
67 changes: 51 additions & 16 deletions jupyterhub/singleuser/__init__.py
@@ -1,40 +1,75 @@
"""JupyterHub single-user server entrypoints
Contains default notebook-app subclass and mixins
Defaults to:
- Jupyter server extension with Jupyter Server >=2
- Subclass with Jupyter Server <2 or clasic notebook
Application subclass can be controlled with environment variables:
- JUPYTERHUB_SINGLEUSER_EXTENSION=1 to opt-in to the extension (requires Jupyter Server 2)
- JUPYTERHUB_SINGLEUSER_APP=notebook (or jupyter-server) to opt-in
"""
import os

from .mixins import HubAuthenticatedHandler, make_singleuser_app

if os.environ.get("JUPYTERHUB_SINGLEUSER_EXTENSION", "") not in ("", "0"):
_as_extension = False
_extension_env = os.environ.get("JUPYTERHUB_SINGLEUSER_EXTENSION", "")
_app_env = os.environ.get("JUPYTERHUB_SINGLEUSER_APP", "")

if not _extension_env:
# extension env not set, check app env
if not _app_env or 'jupyter_server' in _app_env.replace("-", "_"):
# no app env set or using jupyter-server, this is the default branch
# default behavior:
# - extension, if jupyter server 2
# - older subclass app, otherwise
try:
import jupyter_server

_server_major = int(jupyter_server.__version__.split(".", 1)[0])
except Exception:
# don't have jupyter-server, assume classic notebook
_as_extension = False
else:
# default to extension if jupyter-server >=2
_as_extension = _server_major >= 2

elif _app_env == "extension":
_as_extension = True
else:
# app env set and not to jupyter-server, that opts out of extension
_as_extension = False
elif _extension_env == "0":
_as_extension = False
else:
# extension env set to anything non-empty other than '0' enables the extension
_as_extension = True

if _as_extension:
# check for conflict in singleuser entrypoint environment variables
if os.environ.get("JUPYTERHUB_SINGLEUSER_APP", "") not in {
if _app_env not in {
"",
"jupyter_server",
"jupyter-server",
"extension",
"jupyter_server.serverapp.ServerApp",
}:
ext = os.environ["JUPYTERHUB_SINGLEUSER_EXTENSION"]
app = os.environ["JUPYTERHUB_SINGLEUSER_APP"]
raise ValueError(
f"Cannot use JUPYTERHUB_SINGLEUSER_EXTENSION={ext} with JUPYTERHUB_SINGLEUSER_APP={app}."
f"Cannot use JUPYTERHUB_SINGLEUSER_EXTENSION={_extension_env} with JUPYTERHUB_SINGLEUSER_APP={_app_env}."
" Please pick one or the other."
)
from .extension import main
else:
_as_extension = False
try:
from .app import SingleUserNotebookApp, main
except ImportError:
# check for Jupyter Server 2.0 ?
from .extension import main
else:
# backward-compatibility
JupyterHubLoginHandler = SingleUserNotebookApp.login_handler_class
JupyterHubLogoutHandler = SingleUserNotebookApp.logout_handler_class
OAuthCallbackHandler = SingleUserNotebookApp.oauth_callback_handler_class
from .app import SingleUserNotebookApp, main

# backward-compatibility
JupyterHubLoginHandler = SingleUserNotebookApp.login_handler_class
JupyterHubLogoutHandler = SingleUserNotebookApp.logout_handler_class
OAuthCallbackHandler = SingleUserNotebookApp.oauth_callback_handler_class


__all__ = [
Expand Down

0 comments on commit 6b6784f

Please sign in to comment.