forcelogin: Keep message backlog while not authed

First part of the fix for #2898
This commit is contained in:
Gina Häußge committed 2018-11-19 15:22:33 +01:00
1 parent d318ff82b8
commit 6556d555bd
3 files changed
+54 -1

No files matched your search

+14
View File
@@ -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 <sec-bundledplugins-forcelogin>` 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
+30 -1
View File
@@ -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
}
+10
View File
@@ -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: