diff --git a/src/octoprint/plugins/appkeys/__init__.py b/src/octoprint/plugins/appkeys/__init__.py index d61f5ed8f..0d177e198 100644 --- a/src/octoprint/plugins/appkeys/__init__.py +++ b/src/octoprint/plugins/appkeys/__init__.py @@ -20,8 +20,7 @@ from octoprint.util import atomic_write """ TODO: - * add central management settings panel for *all* user's keys - * bulk delete: delete all, all on page, all by user? + * delete all by user/all by app * write documentation * API docs * JS client docs @@ -100,7 +99,8 @@ class AppKeysPlugin(octoprint.plugin.AssetPlugin, ##~~ TemplatePlugin def get_template_configs(self): - return [dict(type="usersettings", name=gettext("Application Keys"))] + return [dict(type="usersettings", name=gettext("Application Keys")), + dict(type="settings", name=gettext("Application Keys"))] ##~~ AssetPlugin @@ -171,7 +171,8 @@ class AppKeysPlugin(octoprint.plugin.AssetPlugin, def get_api_commands(self): return dict(generate=["app"], - revoke=["key"]) + revoke=["key"], + revoke_for=["user_id", "key"]) def on_api_get(self, request): user_id = current_user.get_name() @@ -187,24 +188,47 @@ class AppKeysPlugin(octoprint.plugin.AssetPlugin, pending=dict((x.user_token, x.external()) for x in self._get_pending(user_id))) def on_api_command(self, command, data): - user_id = current_user.get_name() - if not user_id: - return flask.abort(403) - - if command == "revoke": + if command == "revoke_for" and admin_permission.can(): + user_id = data.get("user_id") api_key = data.get("key") - if not api_key: + if not api_key or not user_id: return flask.abort(400) self._delete_api_key(user_id, api_key) - elif command == "generate": - # manual generateKey - app_name = data.get("app") + elif command == "revoke_all_for_user" and admin_permission.can(): + user_id = data.get("user_id") + if not user_id: + return flask.abort(400) + + self._delete_api_keys_for_user(user_id) + + elif command == "revoke_all_for_app" and admin_permission.can(): + app_name = data.get("app_name") if not app_name: return flask.abort(400) - self._add_api_key(user_id, app_name.strip()) + self._delete_api_keys_for_app(app_name) + + else: + user_id = current_user.get_name() + if not user_id: + return flask.abort(403) + + if command == "revoke": + api_key = data.get("key") + if not api_key: + return flask.abort(400) + + self._delete_api_key(user_id, api_key) + + elif command == "generate": + # manual generateKey + app_name = data.get("app") + if not app_name: + return flask.abort(400) + + self._add_api_key(user_id, app_name.strip()) return NO_CONTENT @@ -290,6 +314,18 @@ class AppKeysPlugin(octoprint.plugin.AssetPlugin, self._keys[user_id] = filter(lambda x: x.api_key != api_key, self._keys[user_id]) self._save_keys() + def _delete_api_keys_for_user(self, user_id): + with self._keys_lock: + del self._keys[user_id] + self._save_keys() + + def _delete_api_keys_for_app(self, app_name): + app_name = app_name.lower() + with self._keys_lock: + for user_id in self._keys: + self._keys[user_id] = filter(lambda x: x.app_id != app_name, self._keys[user_id]) + self._save_keys() + def _user_for_api_key(self, api_key): with self._keys_lock: for user_id, data in self._keys.items(): @@ -326,7 +362,7 @@ class AppKeysPlugin(octoprint.plugin.AssetPlugin, if not isinstance(persisted, dict): return - keys = dict() + keys = defaultdict(list) for user_id, persisted_keys in persisted.items(): keys[user_id] = [ActiveKey.for_internal(x, user_id) for x in persisted_keys] self._keys = keys diff --git a/src/octoprint/plugins/appkeys/static/clientjs/appkeys.js b/src/octoprint/plugins/appkeys/static/clientjs/appkeys.js index 580ad1364..f8a2415e3 100644 --- a/src/octoprint/plugins/appkeys/static/clientjs/appkeys.js +++ b/src/octoprint/plugins/appkeys/static/clientjs/appkeys.js @@ -25,6 +25,18 @@ return this.base.simpleApiCommand("appkeys", "revoke", {"key": key}, opts); }; + OctoPrintAppKeysClient.prototype.revokeKeyFor = function(key, user_id, opts) { + return this.base.simpleApiCommand("appkeys", "revoke_for", {"user_id": user_id, "key": key}, opts); + }; + + OctoPrintAppKeysClient.prototype.revokeAllForUser = function(user_id, opts) { + return this.base.simpleApiCommand("appkeys", "revoke_all_for_user", {"user_id": user_id}, opts); + }; + + OctoPrintAppKeysClient.prototype.revokeAllForApp = function(app_id, opts) { + return this.base.simpleApiCommand("appkeys", "revoke_all_for_app", {"app_id": app_id}, opts); + }; + OctoPrintAppKeysClient.prototype.decide = function(token, decision, opts) { return this.base.postJson(this.base.getBlueprintUrl("appkeys") + "decision/" + token, {decision: !!decision}, opts); }; diff --git a/src/octoprint/plugins/appkeys/static/js/appkeys.js b/src/octoprint/plugins/appkeys/static/js/appkeys.js index 015493f8f..eca382698 100644 --- a/src/octoprint/plugins/appkeys/static/js/appkeys.js +++ b/src/octoprint/plugins/appkeys/static/js/appkeys.js @@ -1,10 +1,10 @@ $(function() { - function AppKeysViewModel(parameters) { + function UserAppKeysViewModel(parameters) { var self = this; self.loginState = parameters[0]; self.keys = new ItemListHelper( - "plugin.appkeys.keys", + "plugin.appkeys.userkeys", { "app": function (a, b) { // sorts descending @@ -134,9 +134,78 @@ $(function() { }; } + function AllAppKeysViewModel(parameters) { + var self = this; + self.loginState = parameters[0]; + + self.keys = new ItemListHelper( + "plugin.appkeys.allkeys", + { + "user": function (a, b) { + // sorts ascending + if (a["user_id"] > b["user_id"]) return 1; + if (a["user_id"] < b["user_id"]) return -1; + return 0; + }, + "app": function (a, b) { + // sorts ascending + if (a["app"] > b["app"]) return 1; + if (a["app"] < b["app"]) return -1; + return 0; + } + }, + { + }, + "user", + [], + [], + 10 + ); + + self.onSettingsShown = function() { + self.requestData(); + }; + + self.onUserLoggedIn = function() { + self.requestData(); + }; + + self.requestData = function() { + OctoPrint.plugins.appkeys.getAllKeys() + .done(self.fromResponse); + }; + + self.fromResponse = function(response) { + self.keys.updateItems(response.keys); + }; + + self.revokeKey = function(key, user) { + return OctoPrint.plugins.appkeys.revokeKeyFor(key, user) + .done(self.requestData); + }; + + self.revokeAllForUser = function() { + var user = self.revokeUsername(); + return OctoPrint.plugins.appkeys.revokeAllForUser(user) + .done(self.requestData); + }; + + self.revokeAllForApp = function() { + var app = self.revokeApp(); + return OctoPrint.plugins.appkeys.revokeAlLForApp(app) + .done(self.requestData); + }; + } + OCTOPRINT_VIEWMODELS.push([ - AppKeysViewModel, + UserAppKeysViewModel, ["loginStateViewModel"], ["#usersettings_plugin_appkeys"] ]); + + OCTOPRINT_VIEWMODELS.push([ + AllAppKeysViewModel, + ["loginStateViewModel"], + ["#settings_plugin_appkeys"] + ]) }); diff --git a/src/octoprint/plugins/appkeys/templates/appkeys_settings.jinja2 b/src/octoprint/plugins/appkeys/templates/appkeys_settings.jinja2 new file mode 100644 index 000000000..6ab81a78e --- /dev/null +++ b/src/octoprint/plugins/appkeys/templates/appkeys_settings.jinja2 @@ -0,0 +1,32 @@ +
{{ _('There are no application keys registered yet.') }}
+