From 1d0829a1db366a727b878ee5eb8df59ea5f15384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 21 Jan 2020 15:53:52 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Introduce=20ClientAuthed=20event?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit May act as entry point for any kind of push messages a plugin might wish to send to the client after a connect happened. Since depending on the current permission configuration this might only be possible once the client has authenticated on the socket, this is required. Should close #3400 --- docs/events/index.rst | 18 ++++++++++++++---- src/octoprint/events.py | 2 ++ src/octoprint/server/util/sockjs.py | 5 +++++ 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/docs/events/index.rst b/docs/events/index.rst index 93a55066b..224656b3f 100644 --- a/docs/events/index.rst +++ b/docs/events/index.rst @@ -100,7 +100,7 @@ Shutdown The server is shutting down. ClientOpened - A client has connected to the web server. + A client has connected to the push socket. Payload: @@ -109,12 +109,22 @@ ClientOpened **Note:** Name changed in version 1.1.0 -ClientClosed - A client has disconnected from the webserver +ClientAuthed + A client has authenticated a user session on the push socket. Payload: - * ``remoteAddress``: the remote address (IP) of the client that disconnected + * ``remoteAddress``: the remote address (IP) of the client that authed. On the push socket only available with a + valid login session. + * ``username``: the name of the user who authed. On the push socket only available with a valid login session. + +ClientClosed + A client has disconnected from the push socket. + + Payload: + + * ``remoteAddress``: the remote address (IP) of the client that disconnected. On the push socket only available + with a valid login session. UserLoggedIn A user logged in. On the push socket only available with a valid login session with admin rights. diff --git a/src/octoprint/events.py b/src/octoprint/events.py index 526e0e3e7..7ac01ea00 100644 --- a/src/octoprint/events.py +++ b/src/octoprint/events.py @@ -46,6 +46,8 @@ class Events(object): # connect/disconnect by client CLIENT_OPENED = "ClientOpened" CLIENT_CLOSED = "ClientClosed" + CLIENT_AUTHED = "ClientAuthed" + CLIENT_DEAUTHED = "ClientDeauthed" # user login/logout USER_LOGGED_IN = "UserLoggedIn" diff --git a/src/octoprint/server/util/sockjs.py b/src/octoprint/server/util/sockjs.py index bf3b5ab92..96c1f0697 100644 --- a/src/octoprint/server/util/sockjs.py +++ b/src/octoprint/server/util/sockjs.py @@ -78,6 +78,7 @@ class PrinterStateConnection(octoprint.vendor.sockjs.tornado.SockJSConnection, "*": []} _event_payload_processors = {Events.CLIENT_OPENED: [lambda user, payload: payload if user.has_permission(Permissions.ADMIN) else dict()], + Events.CLIENT_AUTHED: [lambda user, payload: payload if user.has_permission(Permissions.ADMIN) else dict()], "*": []} _emit_permissions = {"connected": [], @@ -436,6 +437,10 @@ class PrinterStateConnection(octoprint.vendor.sockjs.tornado.SockJSConnection, self._logger.exception("Error processing authed hook handler for plugin {}".format(name), extra=dict(plugin=name)) + octoprint.events.eventManager().fire(octoprint.events.Events.CLIENT_AUTHED, + payload=dict(username=user.get_name(), + remoteAddress=self._remoteAddress)) + def _on_logout(self): self._user = self._userManager.anonymous_user_factory()