def _patch_app_base_handlers(app):
    """Patch Hub Authentication into the base handlers of an app

    Patches HubAuthenticatedHandler into:

    - App.base_handler_class (if defined)
    - jupyter_server's JupyterHandler (if already imported)
    - notebook's IPythonHandler (if already imported)
    """
    BaseHandler = app_base_handler = getattr(app, "base_handler_class", None)

    base_handlers = []
    if BaseHandler is not None:
        base_handlers.append(BaseHandler)

    # patch jupyter_server and notebook handlers if they have been imported
    for base_handler_name in [
        "jupyter_server.base.handlers.JupyterHandler",
        "notebook.base.handlers.IPythonHandler",
    ]:
        modname, _ = base_handler_name.rsplit(".", 1)
        if modname in sys.modules:
            root_mod = modname.partition(".")[0]
            if root_mod == "notebook":
                import notebook

                if int(notebook.__version__.partition(".")[0]) >= 7:
                    # notebook 7 is a server extension,
                    # it doesn't have IPythonHandler anymore
                    continue
            base_handlers.append(import_item(base_handler_name))

    if not base_handlers:
        pkg = detect_base_package(app.__class__)
        if pkg == "jupyter_server":
            BaseHandler = import_item("jupyter_server.base.handlers.JupyterHandler")
        elif pkg == "notebook":
            BaseHandler = import_item("notebook.base.handlers.IPythonHandler")
        else:
            raise ValueError(
                f"{app.__class__.__name__}.base_handler_class must be defined"
            )
        base_handlers.append(BaseHandler)

    # patch-in HubAuthenticatedHandler to base handler classes
    for BaseHandler in base_handlers:
        patch_base_handler(BaseHandler)

    # return the first entry
    return base_handlers[0]
Patch Hub Authentication into the base handlers of an app

Patches HubAuthenticatedHandler into:

- App.base_handler_class (if defined)
- jupyter_server's JupyterHandler (if already imported)
- notebook's IPythonHandler (if already imported)
jupyterhub/singleuser/mixins.py