Merge branch 'improve/trustBasicAuth' into maintenance

This commit is contained in:
Gina Häußge committed 2018-02-12 15:27:24 +01:00
commit de9fdf79c4
6 files changed
+90 -6

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
+12 -1
View File
@@ -36,7 +36,7 @@ Use the following settings to enable access control:
# If set to true, will automatically log on clients originating from any of the networks defined in
# "localNetworks" as the user defined in "autologinAs". Defaults to false.
autologinLocal: true
autologinLocal: false
# The name of the user to automatically log on clients originating from "localNetworks" as. Must
# be the name of one of your configured users.
@@ -51,6 +51,17 @@ Use the following settings to enable access control:
- 127.0.0.0/8
- 192.168.1.0/24
# Whether to trust Basic Authentication headers. If you have setup Basic 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 user during Basic Authentication. Your should ONLY ENABLE THIS if your
# OctoPrint instance is only accessible through a connection locked down through Basic Authentication!
trustBasicAuthentication: false
# Whether to also check the password provided through Basic Authentication if the Basic Authentication
# header is to be trusted. Disabling this will only match the user name in the Basic Authentication
# header and login the user without further checks. Use with caution.
checkBasicAuthenticationPassword: true
.. _sec-configuration-config_yaml-api:
API
+7
View File
@@ -147,6 +147,8 @@ def load_user(id):
return None
def load_user_from_request(request):
return util.get_user_for_authorization_header(request.headers.get('Authorization'))
def unauthorized_user():
from flask import abort
@@ -446,6 +448,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, loginFromAuthorizationHeaderRequestHandler, corsRequestHandler, corsResponseHandler
from octoprint.server.util.flask import restricted_access, get_json_command_from_request, passive_login
@@ -46,6 +46,7 @@ api.after_request(noCachingExceptGetResponseHandler)
api.before_request(corsRequestHandler)
api.before_request(enforceApiKeyRequestHandler)
api.before_request(loginFromAuthorizationHeaderRequestHandler)
api.before_request(loginFromApiKeyRequestHandler)
api.after_request(corsResponseHandler)
+65 -3
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
@@ -68,11 +70,36 @@ def loginFromApiKeyRequestHandler():
return
user = get_user_for_apikey(apikey)
if user is not None and _flask.ext.login.login_user(user, remember=False):
if not loginUser(user):
return _flask.make_response("Invalid API key", 403)
def loginFromAuthorizationHeaderRequestHandler():
"""
``before_request`` handler for creating login sessions based on the Authorization header.
"""
authorization_header = get_authorization_header(_flask.request)
user = get_user_for_authorization_header(authorization_header)
loginUser(user)
def loginUser(user, remember=False):
"""
Logs the provided ``user`` into Flask Login and Principal if not None and active
Args:
user: the User to login. May be None in which case the login will fail
remember: Whether to set the ``remember`` flag on the Flask Login operation
Returns: (bool) True if the login succeeded, False otherwise
"""
if user is not None and user.is_active() and _flask.ext.login.login_user(user, remember=remember):
_flask.ext.principal.identity_changed.send(_flask.current_app._get_current_object(),
identity=_flask.ext.principal.Identity(user.get_id()))
else:
return _flask.make_response("Invalid API key", 403)
return True
return False
def corsRequestHandler():
@@ -171,6 +198,36 @@ def get_user_for_apikey(apikey):
return None
def get_user_for_authorization_header(header):
if not settings().getBoolean(["accessControl", "trustBasicAuthentication"]):
return None
if header is None:
return None
if not header.startswith("Basic "):
# we currently only support Basic Authentication
return None
header = header.replace('Basic ', '', 1)
try:
header = base64.b64decode(header)
except TypeError:
return None
name, password = header.split(':', 1)
if not octoprint.server.userManager.enabled:
return None
user = octoprint.server.userManager.findUser(userid=name)
if settings().getBoolean(["accessControl", "checkBasicAuthenticationPassword"]) \
and not octoprint.server.userManager.checkPassword(name, password):
# password check enabled and password don't match
return None
return user
def get_api_key(request):
# Check Flask GET/POST arguments
if hasattr(request, "values") and "apikey" in request.values:
@@ -188,6 +245,11 @@ def get_api_key(request):
return None
def get_authorization_header(request):
# Tornado and Flask headers
return request.headers.get("Authorization")
def get_plugin_hash():
from octoprint.plugin import plugin_manager
+3 -1
View File
@@ -301,7 +301,9 @@ default_settings = {
"userfile": None,
"autologinLocal": False,
"localNetworks": ["127.0.0.0/8"],
"autologinAs": None
"autologinAs": None,
"trustBasicAuthentication": False,
"checkBasicAuthenticationPassword": True
},
"slicing": {
"enabled": True,