appkeys: central settings page with all keys

This commit is contained in:
Gina Häußge committed 2018-10-22 18:44:55 +02:00
1 parent e95e546c08
commit 2746139fa6
4 files changed
+167 -18

No files matched your search

+51 -15
View File
@@ -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
@@ -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);
};
@@ -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"]
])
});
@@ -0,0 +1,32 @@
<div data-bind="visible: keys.allSize() <= 0">
<p>{{ _('There are no application keys registered yet.') }}</p>
</div>
<div data-bind="visible: keys.allSize() > 0">
<table class="table table-condensed table-hover" id="settings_plugin_appauth_keys_table">
<thead>
<tr>
<th class="settings_plugin_appauth_keys_user">{{ _('User') }}</th>
<th class="settings_plugin_appauth_keys_app">{{ _('Application identifier') }}</th>
<th class="settings_plugin_appauth_keys_actions">{{ _('Action') }}</th>
</tr>
</thead>
<tbody data-bind="foreach: keys.paginatedItems">
<tr>
<td class="settings_plugin_appauth_keys_user"><span data-bind="text: user_id"></span></td>
<td class="settings_plugin_appauth_keys_app"><span data-bind="text: app_id"></span><br /><small class="muted">{{ _('API Key') }}: <span data-bind="text: api_key"></span> <a href="javascript:void(0)" title="{{ _('Copy API Key to clipboard') }}" data-bind="click: function() { copyToClipboard(api_key) }"><i class="fa fa-copy"></i></a></small></td>
<td class="settings_plugin_appauth_keys_actions"><a href="javascript:void(0)" title="{{ _('Revoke') }}" class="fa fa-trash-o" data-bind="click: function() { $parent.revokeKey($data.api_key, $data.user_id) }"></a></td>
</tr>
</tbody>
</table>
<div class="pagination pagination-mini pagination-centered">
<ul>
<li data-bind="css: {disabled: keys.currentPage() === 0}"><a href="javascript:void(0)" data-bind="click: keys.prevPage">«</a></li>
</ul>
<ul data-bind="foreach: keys.pages">
<li data-bind="css: { active: $data.number === $root.keys.currentPage(), disabled: $data.number === -1 }"><a href="javascript:void(0)" data-bind="text: $data.text, click: function() { $root.keys.changePage($data.number); }"></a></li>
</ul>
<ul>
<li data-bind="css: {disabled: keys.currentPage() === keys.lastPage()}"><a href="javascript:void(0)" data-bind="click: keys.nextPage">»</a></li>
</ul>
</div>
</div>