From b0028cd3fb64ed68c73e386b82566d7c39bf90c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 28 Aug 2018 22:05:28 +0200 Subject: [PATCH] AppAuth: workflow fully implemented & client lib --- src/octoprint/plugins/appauth/__init__.py | 37 +++++---- .../appauth/static/clientjs/appauth.js | 83 +++++++++++++++++++ .../plugins/appauth/static/js/appauth.js | 41 ++------- 3 files changed, 109 insertions(+), 52 deletions(-) create mode 100644 src/octoprint/plugins/appauth/static/clientjs/appauth.js diff --git a/src/octoprint/plugins/appauth/__init__.py b/src/octoprint/plugins/appauth/__init__.py index 08d0bcf6b..398f2005f 100644 --- a/src/octoprint/plugins/appauth/__init__.py +++ b/src/octoprint/plugins/appauth/__init__.py @@ -31,15 +31,14 @@ class PendingDecision(object): class ReadyDecision(object): - def __init__(self, app_id, app_token, user_id, api_key): + def __init__(self, app_id, app_token, user_id): self.app_id = app_id self.app_token = app_token self.user_id = user_id - self.api_key = api_key @classmethod - def for_pending(cls, pending, api_key): - return cls(pending.app_id, pending.app_token, pending.user_id, api_key) + def for_pending(cls, pending, user_id): + return cls(pending.app_id, pending.app_token, user_id) class ActiveKey(object): @@ -70,7 +69,8 @@ class AppAuthPlugin(octoprint.plugin.AssetPlugin, ##-- AssetPlugin hooks def get_assets(self): - return dict(js=["js/appauth.js"]) + return dict(js=["js/appauth.js"], + clientjs=["clientjs/appauth.js"]) ##~~ BlueprintPlugin mixin @@ -101,10 +101,12 @@ class AppAuthPlugin(octoprint.plugin.AssetPlugin, @no_firstrun_access def handle_decision_poll(self, app_token): if self._is_pending(app_token): - return flask.make_response("Awaiting decision", 202) + response = flask.jsonify(message="Awaiting decision") + response.status_code = 202 + return response result = self._get_decision(app_token) - if result is not None: + if result: return flask.jsonify(api_key=result) return flask.abort(404) @@ -159,11 +161,7 @@ class AppAuthPlugin(octoprint.plugin.AssetPlugin, if not app_name: return flask.abort(400) - api_key = self._generate_key() - try: - self._add_api_key(user_id, app_name.trim(), api_key) - except AppAlreadyExists: - return flask.abort(409) + self._add_api_key(user_id, app_name.trim()) return NO_CONTENT @@ -210,7 +208,7 @@ class AppAuthPlugin(octoprint.plugin.AssetPlugin, if decision: with self._ready_lock: - self._ready_decisions.append(ReadyDecision.for_pending(pending, self._generate_key())) + self._ready_decisions.append(ReadyDecision.for_pending(pending, user_id)) with self._pending_lock: self._pending_decisions = filter(lambda x: x.user_token != user_token, self._pending_decisions) @@ -226,20 +224,23 @@ class AppAuthPlugin(octoprint.plugin.AssetPlugin, else: return False # not found - self._add_api_key(decision.user_id, decision.app_id, decision.api_key) + api_key = self._add_api_key(decision.user_id, decision.app_id) with self._ready_lock: self._ready_decisions = filter(lambda x: x.app_token != app_token, self._ready_decisions) - return decision.api_key + return api_key - def _add_api_key(self, user_id, app_name, api_key): + def _add_api_key(self, user_id, app_name): with self._keys_lock: # TODO: persist to disk for key in self._keys[user_id]: if key.app_id.lower() == app_name.lower(): - raise AppAlreadyExists("app {} does already exist".format(app_name)) - self._keys[user_id].append(ActiveKey(app_name, api_key)) + return key.api_key + + key = ActiveKey(app_name, self._generate_key()) + self._keys[user_id].append(key) + return key.api_key def _delete_api_key(self, user_id, api_key): with self._keys_lock: diff --git a/src/octoprint/plugins/appauth/static/clientjs/appauth.js b/src/octoprint/plugins/appauth/static/clientjs/appauth.js new file mode 100644 index 000000000..57800af43 --- /dev/null +++ b/src/octoprint/plugins/appauth/static/clientjs/appauth.js @@ -0,0 +1,83 @@ +(function (global, factory) { + if (typeof define === "function" && define.amd) { + define(["OctoPrintClient"], factory); + } else { + factory(global.OctoPrintClient); + } +})(this, function(OctoPrintClient) { + var OctoPrintAppAuthClient = function(base) { + this.base = base; + }; + + OctoPrintAppAuthClient.prototype.getKeys = function(opts) { + return this.base.simpleApiGet("appauth", opts); + }; + + OctoPrintAppAuthClient.prototype.generateKey = function(app, opts) { + return this.base.simpleApiCommand("appauth", "generate", {"app": app}, opts); + }; + + OctoPrintAppAuthClient.prototype.revokeKey = function(key, opts) { + return this.base.simpleApiCommand("appauth", "revoke", {"key": key}, opts); + }; + + OctoPrintAppAuthClient.prototype.decide = function(token, decision, opts) { + return this.base.postJson(this.base.getBlueprintUrl("appauth") + "decision/" + token, {decision: !!decision}, opts); + }; + + OctoPrintAppAuthClient.prototype.request = function(app, opts) { + return this.requestForUser(app, undefined, opts); + }; + + OctoPrintAppAuthClient.prototype.requestForUser = function(app, user, opts) { + return this.base.postJson(this.base.getBlueprintUrl("appauth") + "request", {app: app, user: user}, opts); + }; + + OctoPrintAppAuthClient.prototype.checkDecision = function(token, opts) { + return this.base.get(this.base.getBlueprintUrl("appauth") + "request/" + token, opts); + }; + + OctoPrintAppAuthClient.prototype.authenticate = function(app, user) { + var deferred = $.Deferred(); + var client = this; + + client.requestForUser(app, user) + .done(function(response) { + var token = response.app_token; + if (!token) { + // no token received, something went wrong + deferred.reject(); + return; + } + + var interval = 1000; + var poll = function() { + client.checkDecision(token) + .done(function(response) { + if (response.api_key) { + // got a decision, resolve the promise + deferred.resolve(response.api_key); + } else { + // no decision yet, poll a bit more + deferred.notify(); + window.setTimeout(poll, interval); + } + }) + .fail(function() { + // something went wrong + deferred.reject(); + }); + }; + window.setTimeout(poll, interval); + }) + .fail(function() { + // something went wrong + deferred.reject(); + }); + + return deferred.promise(); + }; + + OctoPrintClient.registerPluginComponent("appauth", OctoPrintAppAuthClient); + return OctoPrintAppAuthClient; +}); diff --git a/src/octoprint/plugins/appauth/static/js/appauth.js b/src/octoprint/plugins/appauth/static/js/appauth.js index 74ec1e6e1..1cc353935 100644 --- a/src/octoprint/plugins/appauth/static/js/appauth.js +++ b/src/octoprint/plugins/appauth/static/js/appauth.js @@ -1,34 +1,3 @@ -(function (global, factory) { - if (typeof define === "function" && define.amd) { - define(["OctoPrintClient"], factory); - } else { - factory(global.OctoPrintClient); - } -})(this, function(OctoPrintClient) { - var OctoPrintAppAuthClient = function(base) { - this.base = base; - }; - - OctoPrintAppAuthClient.prototype.getKeys = function(opts) { - return this.base.simpleApiGet("appauth", opts); - }; - - OctoPrintAppAuthClient.prototype.generateKey = function(app, opts) { - return this.base.simpleApiCommand("appauth", "generate", {"app": app}, opts); - }; - - OctoPrintAppAuthClient.prototype.revokeKey = function(key, opts) { - return this.base.simpleApiCommand("appauth", "revoke", {"key": key}, opts); - }; - - OctoPrintAppAuthClient.prototype.decide = function(token, decision, opts) { - return this.base.postJson(this.base.getBlueprintUrl("appauth") + "decision/" + token, {decision: !!decision}, opts); - }; - - OctoPrintClient.registerPluginComponent("appauth", OctoPrintAppAuthClient); - return OctoPrintAppAuthClient; -}); - $(function() { function AppAuthViewModel(parameters) { var self = this; @@ -90,23 +59,27 @@ $(function() { }; self.promptForAccess = function(app, token) { - var message = gettext("\"%(app)s\" has requested access to control OctoPrint through the API. Do you want to allow access to this application with your user account?"); + var message = gettext("\"%(app)s\" has requested access to control OctoPrint through the API."); message = _.sprintf(message, {app: app}); - + message = "

" + message + "

" + gettext("Do you want to allow access to this application with your user account?") + "

"; return new PNotify({ title: gettext("Access Request"), text: message, + hide: false, + icon: "fa fa-key", confirm: { confirm: true, buttons: [{ text: gettext("Allow"), click: function(notice) { self.allowApp(token); + notice.remove(); } }, { text: gettext("Deny"), click: function(notice) { self.denyApp(token); + notice.remove(); } }] }, @@ -141,7 +114,7 @@ $(function() { return; } - if (self.pending[token] === undefined) { + if (self.pending[token] !== undefined) { return; }