From 2ca63febee440cb3294f3f4c57c8f308ee595504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 31 Aug 2018 20:45:16 +0200 Subject: [PATCH] Appkeys: allow fetching of all keys Also added TODO list ;) --- src/octoprint/plugins/appkeys/__init__.py | 35 ++++++++++++++++--- .../appkeys/static/clientjs/appkeys.js | 4 +++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/octoprint/plugins/appkeys/__init__.py b/src/octoprint/plugins/appkeys/__init__.py index 91b0ca960..2666047b9 100644 --- a/src/octoprint/plugins/appkeys/__init__.py +++ b/src/octoprint/plugins/appkeys/__init__.py @@ -11,7 +11,18 @@ from flask_babel import gettext import octoprint.plugin from octoprint.settings import valid_boolean_trues from octoprint.server.util.flask import restricted_access, no_firstrun_access -from octoprint.server import NO_CONTENT, current_user, user_permission +from octoprint.server import NO_CONTENT, current_user, admin_permission + + +""" +TODO: + * add key persistence + * add central management settings panel for *all* user's keys + * bulk delete: delete all, all on page, all by user? + * write documentation + * API docs + * JS client docs +""" class AppAlreadyExists(Exception): @@ -43,13 +54,15 @@ class ReadyDecision(object): class ActiveKey(object): - def __init__(self, app_id, api_key): + def __init__(self, app_id, api_key, user_id): self.app_id = app_id self.api_key = api_key + self.user_id = user_id def external(self): return dict(app_id=self.app_id, - api_key=self.api_key) + api_key=self.api_key, + user_id=self.user_id) class AppKeysPlugin(octoprint.plugin.AssetPlugin, @@ -148,7 +161,12 @@ class AppKeysPlugin(octoprint.plugin.AssetPlugin, if not user_id: return flask.abort(403) - return flask.jsonify(keys=map(lambda x: x.external(), self._api_keys_for_user(user_id)), + if request.values.get("all") in valid_boolean_trues and admin_permission.can(): + keys = self._all_api_keys() + else: + keys = self._api_keys_for_user(user_id) + + return flask.jsonify(keys=map(lambda x: x.external(), keys), pending=dict((x.user_token, x.external()) for x in self._get_pending(user_id))) def on_api_command(self, command, data): @@ -246,7 +264,7 @@ class AppKeysPlugin(octoprint.plugin.AssetPlugin, if key.app_id.lower() == app_name.lower(): return key.api_key - key = ActiveKey(app_name, self._generate_key()) + key = ActiveKey(app_name, self._generate_key(), user_id) self._keys[user_id].append(key) return key.api_key @@ -266,6 +284,13 @@ class AppKeysPlugin(octoprint.plugin.AssetPlugin, with self._keys_lock: return self._keys.get(user_id, []) + def _all_api_keys(self): + with self._keys_lock: + result = [] + for user_id, keys in self._keys.items(): + result += keys + return result + def _generate_key(self): return hexlify(os.urandom(16)) diff --git a/src/octoprint/plugins/appkeys/static/clientjs/appkeys.js b/src/octoprint/plugins/appkeys/static/clientjs/appkeys.js index 1e70f4b89..580ad1364 100644 --- a/src/octoprint/plugins/appkeys/static/clientjs/appkeys.js +++ b/src/octoprint/plugins/appkeys/static/clientjs/appkeys.js @@ -13,6 +13,10 @@ return this.base.simpleApiGet("appkeys", opts); }; + OctoPrintAppKeysClient.prototype.getAllKeys = function(opts) { + return this.base.get(OctoPrintClient.prototype.getSimpleApiUrl("appkeys") + "?all=true", opts); + }; + OctoPrintAppKeysClient.prototype.generateKey = function(app, opts) { return this.base.simpleApiCommand("appkeys", "generate", {"app": app}, opts); };