From 6556d555bdb5191cf67abe101b8cd18c93ebfccb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 19 Nov 2018 14:17:29 +0100 Subject: [PATCH] forcelogin: Keep message backlog while not authed First part of the fix for #2898 --- docs/plugins/hooks.rst | 14 +++++++++ src/octoprint/plugins/forcelogin/__init__.py | 31 +++++++++++++++++++- src/octoprint/server/util/sockjs.py | 10 +++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/docs/plugins/hooks.rst b/docs/plugins/hooks.rst index 4d20367da..77a3fbaec 100644 --- a/docs/plugins/hooks.rst +++ b/docs/plugins/hooks.rst @@ -1238,6 +1238,20 @@ octoprint.server.http.routes :return: a list of 3-tuples with additional routes as defined above :rtype: list +.. _sec-plugins-hook-server-sockjs-authed: + +octoprint.server.sockjs.authed +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. py:function:: socket_authed_hook(socket, user, *args, **kwargs): + + Allows plugins to be notified that a user got authenticated or deauthenticated on the socket (e.g. due to logout). + + See the bundled :ref:`Forcelogin Plugin ` for an example on how to utilize this. + + :param object socket: the socket object which is about to be registered + :param object user: the user that got authenticated on the socket, or None if the user got deauthenticated + .. _sec-plugins-hook-server-sockjs-register: octoprint.server.sockjs.register diff --git a/src/octoprint/plugins/forcelogin/__init__.py b/src/octoprint/plugins/forcelogin/__init__.py index 126d0b30b..0f6ea9cdd 100644 --- a/src/octoprint/plugins/forcelogin/__init__.py +++ b/src/octoprint/plugins/forcelogin/__init__.py @@ -10,9 +10,18 @@ import flask import flask_login from flask_babel import gettext +from collections import defaultdict +import threading + class ForceLoginPlugin(octoprint.plugin.UiPlugin, octoprint.plugin.TemplatePlugin, octoprint.plugin.AssetPlugin): + MAX_BACKLOG_LEN = 100 + + # noinspection PyMissingConstructor + def __init__(self): + self._message_backlog = defaultdict(list) + self._message_backlog_mutex = threading.RLock() def get_assets(self): return dict( @@ -123,6 +132,15 @@ class ForceLoginPlugin(octoprint.plugin.UiPlugin, return user is not None and not user.is_anonymous() and user.is_active() + def socket_authed(self, socket, user): + with self._message_backlog_mutex: + backlog = self._message_backlog.pop(socket, []) + + if len(backlog): + for message, payload in backlog: + socket._do_emit(message, payload) + self._logger.debug("Sent backlog of {} message(s) via socket".format(len(backlog))) + def socket_emit_validator(self, socket, user, message, payload): if self._user_manager.enabled and not self._user_manager.hasBeenCustomized(): # ACL hasn't been configured yet, make an exception @@ -134,7 +152,17 @@ class ForceLoginPlugin(octoprint.plugin.UiPlugin, if message in ("connected", "reauthRequired"): return True - return user is not None and not user.is_anonymous() and user.is_active() + if user is not None and not user.is_anonymous() and user.is_active(): + return True + + with self._message_backlog_mutex: + if len(self._message_backlog[socket]) < self.MAX_BACKLOG_LEN: + self._message_backlog[socket].append((message, payload)) + self._logger.debug("Socket message held back until authed, added to backlog: {}".format(message)) + else: + self._logger.warn("Socket message held back, but backlog full. Throwing message away: {}".format(message)) + + return False __plugin_name__ = "Force Login" @@ -150,5 +178,6 @@ __plugin_hooks__ = { "octoprint.server.api.before_request": __plugin_implementation__.get_before_request_handlers, "octoprint.server.http.access_validator": __plugin_implementation__.access_validator, "octoprint.server.sockjs.register": __plugin_implementation__.socket_register_validator, + "octoprint.server.sockjs.authed": __plugin_implementation__.socket_authed, "octoprint.server.sockjs.emit": __plugin_implementation__.socket_emit_validator } diff --git a/src/octoprint/server/util/sockjs.py b/src/octoprint/server/util/sockjs.py index 656c32613..e63223f2c 100644 --- a/src/octoprint/server/util/sockjs.py +++ b/src/octoprint/server/util/sockjs.py @@ -95,6 +95,7 @@ class PrinterStateConnection(octoprint.vendor.sockjs.tornado.SockJSConnection, o self._baseRateLimit = 0.5 self._register_hooks = self._pluginManager.get_hooks("octoprint.server.sockjs.register") + self._authed_hooks = self._pluginManager.get_hooks("octoprint.server.sockjs.authed") self._emit_hooks = self._pluginManager.get_hooks("octoprint.server.sockjs.emit") self._registered = False @@ -176,6 +177,12 @@ class PrinterStateConnection(octoprint.vendor.sockjs.tornado.SockJSConnection, o self._user = None self._logger.warn("Unknown user/session combo: {}:{}".format(user_id, user_session)) + for name, hook in self._authed_hooks.items(): + try: + hook(self, self._user) + except: + self._logger.exception("Error processing authed hook handler for plugin {}".format(name)) + self._register() elif "throttle" in message: @@ -270,6 +277,9 @@ class PrinterStateConnection(octoprint.vendor.sockjs.tornado.SockJSConnection, o if not proceed: return + self._do_emit(type, payload) + + def _do_emit(self, type, payload): try: self.send({type: payload}) except Exception as e: