Skip to content

Create a base api handler for authenticated web socket APIs #781

Open
@3coins

Description

@3coins

Problem

Current jupyter-server code doesn't have a base handler or utility for creating authenticated web socket APIs. An example of an authenticated web socket is present in AuthenticatedZMQStreamHandler.

class AuthenticatedZMQStreamHandler(ZMQStreamHandler, JupyterHandler):
def set_default_headers(self):
"""Undo the set_default_headers in JupyterHandler
which doesn't make sense for websockets
"""
pass
def pre_get(self):
"""Run before finishing the GET request
Extend this method to add logic that should fire before
the websocket finishes completing.
"""
# authenticate the request before opening the websocket
user = self.get_current_user()
if user is None:
self.log.warning("Couldn't authenticate WebSocket connection")
raise web.HTTPError(403)
# authorize the user.
if not self.authorizer:
# Warn if there is not authorizer.
warn_disabled_authorization()
elif not self.authorizer.is_authorized(self, user, "execute", "kernels"):
raise web.HTTPError(403)
if self.get_argument("session_id", False):
self.session.session = self.get_argument("session_id")
else:
self.log.warning("No session ID specified")
async def get(self, *args, **kwargs):
# pre_get can be a coroutine in subclasses
# assign and yield in two step to avoid tornado 3 issues
res = self.pre_get()
await res
res = super().get(*args, **kwargs)
await res
def initialize(self):
self.log.debug("Initializing websocket connection %s", self.request.path)
self.session = Session(config=self.config)
def get_compression_options(self):
return self.settings.get("websocket_compression_options", None)

When working with authenticated web socket APIs, this code will be duplicated for each handler.

Proposed Solution

  • Create a new WebSocketAPIHandler that new APIs can inherit from. This will contain functions that all jupyter web sockets should inherit, for example, set_default_handlers, initialize, get_compression_options etc.
  • Create a AuthWebSocketAPIHandler that additionally adds authentication for web sockets. This will contain functions, that add authentication for web sockets, for example pre_get and get.

Additional context

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions