✨ appkeys: support manual generation in settings

Also add a detail view for individual keys, also shown after
generation of a new key.
This commit is contained in:
Gina Häußge committed 2020-04-28 15:14:10 +02:00
1 parent d97fdb5ce8
commit 15f5873cc2
7 files changed
+154 -7

No files matched your search

+6 -1
View File
@@ -256,7 +256,12 @@ class AppKeysPlugin(octoprint.plugin.AssetPlugin,
if not app_name:
return flask.abort(400)
self._add_api_key(user_id, app_name.strip())
selected_user_id = data.get("user", user_id)
if selected_user_id != user_id and not Permissions.PLUGIN_APPKEYS_ADMIN.can():
return flask.abort(403)
key = self._add_api_key(selected_user_id, app_name.strip())
return flask.jsonify(user_id=selected_user_id, app_id=app_name, api_key=key)
return NO_CONTENT
@@ -21,6 +21,10 @@
return this.base.simpleApiCommand("appkeys", "generate", {"app": app}, opts);
};
OctoPrintAppKeysClient.prototype.generateKeyForUser = function(user, app, opts) {
return this.base.simpleApiCommand("appkeys", "generate", {"app": app, "user": user}, opts);
};
OctoPrintAppKeysClient.prototype.revokeKey = function(key, opts) {
return this.base.simpleApiCommand("appkeys", "revoke", {"key": key}, opts);
};
@@ -1 +1 @@
#settings_plugin_appkeys_allkeys_table .settings_plugin_appkeys_actions,#settings_plugin_appkeys_userkeys_table .settings_plugin_appkeys_actions{width:75px;text-align:center}#settings_plugin_appkeys_allkeys_table .settings_plugin_appkeys_actions a,#settings_plugin_appkeys_userkeys_table .settings_plugin_appkeys_actions a{color:#000}#settings_plugin_appkeys_allkeys_table .settings_plugin_appkeys_user,#settings_plugin_appkeys_userkeys_table .settings_plugin_appkeys_user{width:100px}#settings_plugin_appkeys_allkeys_table .settings_plugin_appkeys_checkbox,#settings_plugin_appkeys_userkeys_table .settings_plugin_appkeys_checkbox{text-align:center;width:10px}#settings_plugin_appkeys_allkeys_table .settings_plugin_appkeys_checkbox input[type=checkbox],#settings_plugin_appkeys_userkeys_table .settings_plugin_appkeys_checkbox input[type=checkbox]{margin-top:0}
#settings_plugin_appkeys_allkeys_table .settings_plugin_appkeys_actions,#settings_plugin_appkeys_userkeys_table .settings_plugin_appkeys_actions{width:75px;text-align:center}#settings_plugin_appkeys_allkeys_table .settings_plugin_appkeys_actions a,#settings_plugin_appkeys_userkeys_table .settings_plugin_appkeys_actions a{color:#000}#settings_plugin_appkeys_allkeys_table .settings_plugin_appkeys_user,#settings_plugin_appkeys_userkeys_table .settings_plugin_appkeys_user{width:100px}#settings_plugin_appkeys_allkeys_table .settings_plugin_appkeys_checkbox,#settings_plugin_appkeys_userkeys_table .settings_plugin_appkeys_checkbox{text-align:center;width:10px}#settings_plugin_appkeys_allkeys_table .settings_plugin_appkeys_checkbox input[type=checkbox],#settings_plugin_appkeys_userkeys_table .settings_plugin_appkeys_checkbox input[type=checkbox]{margin-top:0}#plugin_appkeys_keygenerated .control-text{display:inline-block}
@@ -1,7 +1,49 @@
$(function() {
function AppKeysDialogViewModel(parameters) {
var self = this;
self.dialog = undefined;
self.onStartup = function() {
self.dialog = $("#plugin_appkeys_keygenerated");
};
self.showDialog = function(title, data) {
if (self.dialog === undefined) return;
var qrcode = {
text: data.api_key,
size: 180,
fill: "#000",
background: null,
label: "",
fontname: "sans",
fontcolor: "#000",
radius: 0,
ecLevel: "L"
};
self.dialog.find("#plugin_appkeys_keygenerated_title").text(title);
self.dialog.find("#plugin_appkeys_keygenerated_user").text(data.user_id);
self.dialog.find("#plugin_appkeys_keygenerated_app").text(data.app_id);
self.dialog.find("#plugin_appkeys_keygenerated_key_text").text(data.api_key);
self.dialog.find("#plugin_appkeys_keygenerated_key_copy")
.off()
.click(function() {
copyToClipboard(data.api_key);
});
self.dialog.find("#plugin_appkeys_keygenerated_key_qrcode")
.empty()
.qrcode(qrcode);
self.dialog.modal("show");
}
}
function UserAppKeysViewModel(parameters) {
var self = this;
self.loginState = parameters[0];
self.dialog = parameters[0];
self.loginState = parameters[1];
self.keys = new ItemListHelper(
"plugin.appkeys.userkeys",
@@ -144,7 +186,9 @@ $(function() {
function AllAppKeysViewModel(parameters) {
var self = this;
self.loginState = parameters[0];
self.dialog = parameters[0];
self.loginState = parameters[1];
self.access = parameters[2];
self.keys = new ItemListHelper(
"plugin.appkeys.allkeys",
@@ -170,14 +214,21 @@ $(function() {
self.users = ko.observableArray([]);
self.apps = ko.observableArray([]);
self.editorApp = ko.observable();
self.editorUser = ko.observable();
self.markedForDeletion = ko.observableArray([]);
self.onSettingsShown = function() {
self.requestData();
self.editorUser(self.loginState.username());
self.editorApp("");
};
self.onUserLoggedIn = function() {
self.requestData();
self.editorUser(self.loginState.username());
self.editorApp("");
};
self.requestData = function() {
@@ -204,6 +255,18 @@ $(function() {
self.apps(apps);
};
self.generateKey = function() {
return OctoPrint.plugins.appkeys.generateKeyForUser(self.editorUser(), self.editorApp())
.done(self.requestData)
.done(function() {
self.editorUser(self.loginState.username());
self.editorApp("");
})
.done(function(data) {
self.dialog.showDialog(gettext("New key generated!"), data);
});
}
self.revokeKey = function(key) {
var perform = function() {
OctoPrint.plugins.appkeys.revokeKey(key)
@@ -294,15 +357,21 @@ $(function() {
};
}
OCTOPRINT_VIEWMODELS.push([
AppKeysDialogViewModel,
[],
[]
]);
OCTOPRINT_VIEWMODELS.push([
UserAppKeysViewModel,
["loginStateViewModel"],
["appKeysDialogViewModel", "loginStateViewModel"],
["#usersettings_plugin_appkeys"]
]);
OCTOPRINT_VIEWMODELS.push([
AllAppKeysViewModel,
["loginStateViewModel"],
["appKeysDialogViewModel", "loginStateViewModel", "accessViewModel"],
["#settings_plugin_appkeys"]
])
});
@@ -22,3 +22,9 @@
}
}
}
#plugin_appkeys_keygenerated {
.control-text {
display: inline-block;
}
}
@@ -0,0 +1,39 @@
<div id="plugin_appkeys_keygenerated" class="modal hide fade">
<div class="modal-header">
<a href="javascript:void(0)" class="close" data-dismiss="modal" aria-hidden="true">&times;</a>
<h3 id="plugin_appkeys_keygenerated_title"></h3>
</div>
<div class="modal-body">
<form class="form-horizontal">
<div class="control-group">
<label class="control-label">{{ _('User') }}</label>
<div class="controls">
<strong id="plugin_appkeys_keygenerated_user" class="control-text"></strong>
</div>
</div>
<div class="control-group">
<label class="control-label">{{ _('Application identifier') }}</label>
<div class="controls">
<strong id="plugin_appkeys_keygenerated_app" class="control-text"></strong>
</div>
</div>
<div class="control-group">
<label class="control-label">{{ _('API Key') }}</label>
<div class="controls">
<strong id="plugin_appkeys_keygenerated_key_text" class="control-text"></strong>
<a href="javascript:void(0)" id="plugin_appkeys_keygenerated_key_copy" title="{{ _('Copy API Key to clipboard')|edq }}" class="control-text"><i class="fa fa-copy"></i></a>
</div>
</div>
<div class="control-group">
<label class="control-label">{{ _('QR Code') }}</label>
<div class="controls">
<div id="plugin_appkeys_keygenerated_key_qrcode"></div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">{{ _('Close') }}</button>
</div>
</div>
@@ -1,3 +1,5 @@
<legend>{{ _('Registered application keys') }}</legend>
<div data-bind="visible: keys.allSize() <= 0">
<p>{{ _('There are no application keys registered yet.') }}</p>
</div>
@@ -41,7 +43,7 @@
<td class="settings_plugin_appkeys_checkbox"><input type="checkbox" data-bind="value: api_key, checked: $root.markedForDeletion"></td>
<td class="settings_plugin_appkeys_user"><span data-bind="text: user_id"></span></td>
<td class="settings_plugin_appkeys_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')|edq }}" data-bind="click: function() { copyToClipboard(api_key) }"><i class="fa fa-copy"></i></a></small></td>
<td class="settings_plugin_appkeys_actions"><a href="javascript:void(0)" title="{{ _('Revoke')|edq }}" class="fa fa-trash-o" data-bind="click: function() { $parent.revokeKey($data.api_key, $data.user_id) }"></a></td>
<td class="settings_plugin_appkeys_actions"><a href="javascript:void(0)" title="{{ _('Revoke')|edq }}" class="fa fa-trash-o" data-bind="click: function() { $parent.revokeKey($data.api_key, $data.user_id) }"></a>&nbsp;|&nbsp;<a href="javascript:void(0)" title="{{ _('Details')|edq }}" class="fa fa-search" data-bind="click: function() { $parent.dialog.showDialog(gettext('Details'), $data) }"></a></td>
</tr>
</tbody>
</table>
@@ -56,4 +58,26 @@
<li data-bind="css: {disabled: keys.currentPage() === keys.lastPage()}"><a href="javascript:void(0)" data-bind="click: keys.nextPage">»</a></li>
</ul>
</div>
<legend>{{ _('Manually generate an application key') }}</legend>
<form class="form-horizontal" onsubmit="return false;">
<div class="control-group">
<label class="control-label">{{ _('User') }}</label>
<div class="controls">
<select data-bind="options: access.users.listHelper.items, optionsText: 'name', optionsValue: 'name', value: editorUser"></select>
</div>
</div>
<div class="control-group">
<label class="control-label">{{ _('Application identifier') }}</label>
<div class="controls">
<input type="text" data-bind="value: editorApp">
</div>
</div>
<div class="control-group">
<div class="controls">
<button class="btn btn-primary" data-bind="click: generateKey">Generate</button>
</div>
</div>
</form>
</div>