Trust basic authentication headers for user login.

This is my suggestion on how to handle basic authentication for users
mananaged by OctoPrint's UserManager.

In my opinion OctoPrint should not be doing Basic Authentication on its
own. The commen case is that it runs on some slow hardware which its not
able to handle stupid bots trying out thousands of passwords in short
time. Also you want to protect it as much as possible from the outside
to ensure that the basd internet can't break your 72h print after 71
hours by brute-forcing passwords.

So I'm introducing an
  accessControl: trustBasicAuthentication
config option. If enabled, it will take the user name from the
Authorization header, check if the user exists and is enabled - and if
so, just login the user.
This commit is contained in:
Bernd Zeimetz committed 2018-01-22 21:32:04 +01:00
1 parent 67005577b7
commit 57a58b3bd7
5 files changed
+54 -2

No files matched your search

+1
View File
@@ -86,6 +86,7 @@ date of first contribution):
* [Peter Backx](https://github.com/pbackx)
* [Josh Major](https://github.com/astateofblank)
* ["alex-gh"](https://github.com/alex-gh)
* [Bernd Zeimetz](https://github.com/bzed)
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
+18
View File
@@ -147,6 +147,19 @@ def load_user(id):
return None
def load_user_from_request(request):
header = request.headers.get('Authorization')
if header:
header = header.replace('Basic ', '', 1)
try:
header = base64.b64decode(header)
except TypeError:
return None
id = header.split(':')[0]
user = userManager.findUser(userid=id)
if user:
return user
return None
def unauthorized_user():
from flask import abort
@@ -446,6 +459,11 @@ class Server(object):
loginManager.session_protection = "strong"
loginManager.user_callback = load_user
loginManager.unauthorized_callback = unauthorized_user
# login users authenticated by basic auth
if self._settings.get(["accessControl", "trustBasicAuthentication"]):
loginManager.request_callback = load_user_from_request
if not userManager.enabled:
loginManager.anonymous_user = users.DummyUser
principals.identity_loaders.appendleft(users.dummy_identity_loader)
+2 -1
View File
@@ -19,7 +19,7 @@ import octoprint.server
import octoprint.plugin
from octoprint.server import admin_permission, NO_CONTENT
from octoprint.settings import settings as s, valid_boolean_trues
from octoprint.server.util import noCachingExceptGetResponseHandler, enforceApiKeyRequestHandler, loginFromApiKeyRequestHandler, corsRequestHandler, corsResponseHandler
from octoprint.server.util import noCachingExceptGetResponseHandler, enforceApiKeyRequestHandler, loginFromApiKeyRequestHandler, loginFromRequest, corsRequestHandler, corsResponseHandler
from octoprint.server.util.flask import restricted_access, get_json_command_from_request, passive_login
@@ -47,6 +47,7 @@ api.after_request(noCachingExceptGetResponseHandler)
api.before_request(corsRequestHandler)
api.before_request(enforceApiKeyRequestHandler)
api.before_request(loginFromRequest)
api.before_request(loginFromApiKeyRequestHandler)
api.after_request(corsResponseHandler)
+31
View File
@@ -5,6 +5,8 @@ __author__ = "Gina Häußge <osd@foosel.net>"
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
__copyright__ = "Copyright (C) 2014 The OctoPrint Project - Released under terms of the AGPLv3 License"
import base64
from octoprint.settings import settings
import octoprint.timelapse
import octoprint.server
@@ -75,6 +77,35 @@ def loginFromApiKeyRequestHandler():
return _flask.make_response("Invalid API key", 403)
def loginFromRequest():
"""
``before_request`` handler for creating login sessions based on the Authorization header.
"""
if not settings().get(["accessControl", "trustBasicAuthentication"]):
return
header = _flask.request.headers.get('Authorization')
if header:
header = header.replace('Basic ', '', 1)
try:
header = base64.b64decode(header)
except TypeError:
return
id = header.split(':')[0]
if octoprint.server.userManager.enabled:
user = octoprint.server.userManager.findUser(userid=id)
else:
# what should we really do if the user manager is disabled
# and basic auth activated?
return
if user is not None and _flask.ext.login.login_user(user, remember=False):
_flask.ext.principal.identity_changed.send(_flask.current_app._get_current_object(),
identity=_flask.ext.principal.Identity(user.get_id()))
return
def corsRequestHandler():
"""
``before_request`` handler for blueprints which sets CORS headers for OPTIONS requests if enabled
+2 -1
View File
@@ -301,7 +301,8 @@ default_settings = {
"userfile": None,
"autologinLocal": False,
"localNetworks": ["127.0.0.0/8"],
"autologinAs": None
"autologinAs": None,
"trustBasicAuthentication": False
},
"slicing": {
"enabled": True,