From 495d99fbe9b65b0324e7759be026181819a40fa5 Mon Sep 17 00:00:00 2001 From: Timur Duehr Date: Sat, 27 Apr 2019 23:51:24 -0500 Subject: [PATCH] Add support for remote users --- AUTHORS.md | 1 + docs/configuration/config_yaml.rst | 12 ++++++++++++ src/octoprint/server/__init__.py | 10 ++++++++++ src/octoprint/server/util/__init__.py | 20 ++++++++++++++++++++ src/octoprint/settings.py | 5 ++++- 5 files changed, 47 insertions(+), 1 deletion(-) diff --git a/AUTHORS.md b/AUTHORS.md index ed547c2fc..2b018c0e6 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -101,6 +101,7 @@ date of first contribution): * [Fabio Santos](https://github.com/Fabi0San) * [Jack Wilsdon](https://github.com/jackwilsdon) * [Ryan Finnie](https://github.com/rfinnie) + * [Timur Duehr](https://github.com/tduehr) OctoPrint started off as a fork of [Cura](https://github.com/daid/Cura) by [Daid Braam](https://github.com/daid). Parts of its communication layer and diff --git a/docs/configuration/config_yaml.rst b/docs/configuration/config_yaml.rst index 83ea5495d..68e36c098 100644 --- a/docs/configuration/config_yaml.rst +++ b/docs/configuration/config_yaml.rst @@ -62,6 +62,18 @@ Use the following settings to enable access control: # header and login the user without further checks. Use with caution. checkBasicAuthenticationPassword: true + # Whether to trust remote user headers. If you have setup authentication in front of + # OctoPrint and the user names you use there match OctoPrint accounts, by setting this to true users will + # be logged into OctoPrint as the user provided in the header. Your should ONLY ENABLE THIS if your + # OctoPrint instance is only accessible through a connection locked down through an authenitating reverse proxy! + trustBasicAuthentication: false + + # Header used by the reverse proxy to convey the authenticated user. + remoteUserHeader: REMOTE_USER + + # If a remote user is not found, add them. Use this only if all users from the remote system can use OctoPrint. + addRemoteUsers: false + .. _sec-configuration-config_yaml-api: API diff --git a/src/octoprint/server/__init__.py b/src/octoprint/server/__init__.py index 103873b1c..bf16a5f68 100644 --- a/src/octoprint/server/__init__.py +++ b/src/octoprint/server/__init__.py @@ -147,9 +147,15 @@ def load_user(id): return None + +def load_remote_user_from_request(request): + return util.get_user_for_remote_user_header(request) + + def load_user_from_request(request): return util.get_user_for_authorization_header(request.headers.get('Authorization')) + def unauthorized_user(): from flask import abort abort(403) @@ -1602,6 +1608,10 @@ class Server(object): if self._settings.get(["accessControl", "trustBasicAuthentication"]): loginManager.request_callback = load_user_from_request + # Login via REMOTE_USER header + if self._settings.get(["accessControl", "useRemoteUser"]): + loginManager.request_callback = load_remote_user_from_request + if not userManager.enabled: loginManager.anonymous_user = users.DummyUser principals.identity_loaders.appendleft(users.dummy_identity_loader) diff --git a/src/octoprint/server/util/__init__.py b/src/octoprint/server/util/__init__.py index e2cea3630..4121997c2 100644 --- a/src/octoprint/server/util/__init__.py +++ b/src/octoprint/server/util/__init__.py @@ -199,6 +199,26 @@ def get_user_for_apikey(apikey): return None +def get_user_for_remote_user_header(request): + if not octoprint.server.userManager.enabled: + return None + + if not settings().getBoolean(["accessControl", "trustRemoteUser"]): + return None + + header = request.headers.get(settings().get(["accessControl", "remoteUserHeader"])) + if header is None: + return None + + user = octoprint.server.userManager.findUser(userid=header) + + if user is None and settings().getBoolean(["accessControl", "addRemoteUsers"]): + octoprint.server.userManager.addUser(header, settings().generateApiKey(), active=True) + user = octoprint.server.userManager.findUser(userid=header) + + return user + + def get_user_for_authorization_header(header): if not settings().getBoolean(["accessControl", "trustBasicAuthentication"]): return None diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 404dc0153..74dd986d6 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -337,7 +337,10 @@ default_settings = { "localNetworks": ["127.0.0.0/8", "::1/128"], "autologinAs": None, "trustBasicAuthentication": False, - "checkBasicAuthenticationPassword": True + "checkBasicAuthenticationPassword": True, + "trustRemoteUser": False, + "remoteUserHeader": "REMOTE_USER", + "addRemoteUsers": False }, "slicing": { "enabled": True,