No longer require API keys for anonymous access

Old behaviour of insisting on API key presence for each and every
request can however still be enforced.

Making this part of the granular permission system epic since only then
will people be able to more finely tune what data will be available for
anonymous users.

Solves #1681
This commit is contained in:
Gina Häußge committed 2017-11-17 15:57:45 +01:00
1 parent 84cc89832b
commit 7540aa6b72
8 files changed
+25 -5

No files matched your search

+2
View File
@@ -84,6 +84,7 @@ def getSettings():
data = {
"api": {
"enabled": s.getBoolean(["api", "enabled"]),
"keyEnforced": s.getBoolean(["api", "keyEnforced"]),
"key": s.get(["api", "key"]) if Permissions.ADMIN.can() else None,
"allowCrossOrigin": s.get(["api", "allowCrossOrigin"])
},
@@ -314,6 +315,7 @@ def _saveSettings(data):
if "api" in data.keys():
if "enabled" in data["api"]: s.setBoolean(["api", "enabled"], data["api"]["enabled"])
if "keyEnforced" in data["api"]: s.setBoolean(["api", "keyEnforced"], data["api"]["keyEnforced"])
if "allowCrossOrigin" in data["api"]: s.setBoolean(["api", "allowCrossOrigin"], data["api"]["allowCrossOrigin"])
if "appearance" in data.keys():
+2 -2
View File
@@ -25,7 +25,7 @@ from . import serialization
def enforceApiKeyRequestHandler():
"""
``before_request`` handler for blueprints which makes sure an API key is provided
``before_request`` handler for blueprints which makes sure an API key is provided if configured as such
"""
import octoprint.server
@@ -40,7 +40,7 @@ def enforceApiKeyRequestHandler():
apikey = get_api_key(_flask.request)
if apikey is None:
if apikey is None and settings().getBoolean(["api", "keyEnforced"]):
return _flask.make_response("No API key provided", 401)
if apikey != octoprint.server.UI_API_KEY and not settings().getBoolean(["api", "enabled"]):
+1
View File
@@ -313,6 +313,7 @@ default_settings = {
},
"api": {
"enabled": True,
"keyEnforced": False,
"key": None,
"allowCrossOrigin": False,
"apps": {}
+4 -2
View File
@@ -103,9 +103,11 @@
additional = additional || {};
var headers = $.extend({}, additional);
headers["X-Api-Key"] = this.options.apikey;
if (this.options.locale !== undefined) {
if (this.options.apikey) {
headers["X-Api-Key"] = this.options.apikey;
}
if (this.options.locale) {
headers["X-Locale"] = this.options.locale;
}
@@ -48,7 +48,11 @@ $(function() {
self.requestData = function() {
return OctoPrint.browser.passiveLogin()
.done(self.fromResponse);
.done(self.fromResponse)
.fail(function() {
// something went very wrong, still, proceed
self.fromResponse();
});
};
self.fromResponse = function(response) {
@@ -108,6 +108,7 @@ $(function() {
self.locale_languages = _.keys(AVAILABLE_LOCALES);
self.api_enabled = ko.observable(undefined);
self.api_keyEnforced = ko.observable(undefined);
self.api_key = ko.observable(undefined);
self.api_allowCrossOrigin = ko.observable(undefined);
@@ -6,6 +6,14 @@
</label>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox" id="settings-apiEnforcedKey" data-bind="checked: api_keyEnforced"> {{ _('Enforce key') }}
</label>
<span class="help-block">{{ _('If you check this, requests to any of OctoPrint\'s APIs without an API key will be denied instead of handled like an anonymous/not logged in user. This is OctoPrint\'s old default behaviour.') }}</span>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
@@ -26,7 +26,9 @@
// sockjs should define CLOSE_NORMAL for us, but they don't (from ws spec)
var SOCKJS_CLOSE_NORMAL = 1000;
// only needed if API keys are enforced, only provides anonymous access on its own
var UI_API_KEY = "{{ uiApiKey|e }}";
var VERSION = "{{ version.number|e }}";
var DISPLAY_VERSION = "{{ version.display|e }}";
var BRANCH = "{{ version.branch|e }}";