✨ appkeys: New app request dialog
URL will be provided as "auth_dialog" property on app token request. Clients may open the URL in a browser window to request access directly. The dialog acts identical to the popups in OctoPrint's UI.
This commit is contained in:
1 parent
ae32445fea
commit
ed10fa47cf
5 files changed
+262
-4
No files matched your search
@@ -9,10 +9,15 @@ import flask
|
||||
from flask_babel import gettext
|
||||
|
||||
import octoprint.plugin
|
||||
from octoprint.access import ADMIN_GROUP
|
||||
from octoprint.access import ADMIN_GROUP, USER_GROUP
|
||||
from octoprint.access.permissions import Permissions
|
||||
from octoprint.server import NO_CONTENT, admin_permission, current_user
|
||||
from octoprint.server.util.flask import no_firstrun_access, restricted_access
|
||||
from octoprint.server.util import require_login_with
|
||||
from octoprint.server.util.flask import (
|
||||
add_non_caching_response_headers,
|
||||
no_firstrun_access,
|
||||
restricted_access,
|
||||
)
|
||||
from octoprint.settings import valid_boolean_trues
|
||||
from octoprint.util import (
|
||||
ResettableTimer,
|
||||
@@ -128,7 +133,14 @@ class AppKeysPlugin(
|
||||
"roles": ["admin"],
|
||||
"dangerous": True,
|
||||
"default_groups": [ADMIN_GROUP],
|
||||
}
|
||||
},
|
||||
{
|
||||
"key": "GRANT",
|
||||
"name": "Grant access",
|
||||
"description": gettext("Allows to grant app access"),
|
||||
"roles": ["user"],
|
||||
"default_groups": [USER_GROUP],
|
||||
},
|
||||
]
|
||||
|
||||
##~~ TemplatePlugin
|
||||
@@ -172,6 +184,9 @@ class AppKeysPlugin(
|
||||
user_id = data["user"]
|
||||
|
||||
app_token, user_token = self._add_pending_decision(app_name, user_id=user_id)
|
||||
auth_dialog = flask.url_for(
|
||||
"plugin.appkeys.handle_auth_dialog", app_token=app_token, _external=True
|
||||
) + ("?user={}".format(user_id) if user_id else "")
|
||||
|
||||
self._plugin_manager.send_plugin_message(
|
||||
self._identifier,
|
||||
@@ -182,7 +197,7 @@ class AppKeysPlugin(
|
||||
"user_id": user_id,
|
||||
},
|
||||
)
|
||||
response = flask.jsonify(app_token=app_token)
|
||||
response = flask.jsonify(app_token=app_token, auth_dialog=auth_dialog)
|
||||
response.status_code = 201
|
||||
response.headers["Location"] = flask.url_for(
|
||||
".handle_decision_poll", app_token=app_token, _external=True
|
||||
@@ -207,12 +222,59 @@ class AppKeysPlugin(
|
||||
|
||||
return flask.abort(404)
|
||||
|
||||
@octoprint.plugin.BlueprintPlugin.route("/auth/<app_token>")
|
||||
@no_firstrun_access
|
||||
def handle_auth_dialog(self, app_token):
|
||||
user_id = current_user.get_name()
|
||||
required_user = flask.request.args.get("user", None)
|
||||
|
||||
pendings = self._get_pending_by_app_token(app_token)
|
||||
if not pendings:
|
||||
return flask.abort(404)
|
||||
|
||||
response = require_login_with(
|
||||
permissions=[Permissions.PLUGIN_APPKEYS_GRANT], user_id=required_user
|
||||
)
|
||||
if response:
|
||||
return response
|
||||
|
||||
pending = None
|
||||
for p in pendings:
|
||||
if p.user_id == required_user or (not required_user and p.user_id == user_id):
|
||||
pending = p
|
||||
break
|
||||
else:
|
||||
return flask.abort(404)
|
||||
|
||||
app_id = pending.app_id
|
||||
user_token = pending.user_token
|
||||
redirect_url = flask.request.args.get("redirect", "")
|
||||
|
||||
response = flask.make_response(
|
||||
flask.render_template(
|
||||
"plugin_appkeys/appkeys_authdialog.jinja2",
|
||||
app=app_id,
|
||||
user=user_id,
|
||||
user_token=user_token,
|
||||
redirect_url=redirect_url,
|
||||
theming=[],
|
||||
request_text=gettext(
|
||||
'"<strong>%(app)s</strong>" has requested access to control OctoPrint through the API.'
|
||||
).replace("%(app)s", app_id),
|
||||
)
|
||||
)
|
||||
return add_non_caching_response_headers(response)
|
||||
|
||||
@octoprint.plugin.BlueprintPlugin.route("/decision/<user_token>", methods=["POST"])
|
||||
@restricted_access
|
||||
def handle_decision(self, user_token):
|
||||
data = flask.request.json
|
||||
if "decision" not in data:
|
||||
flask.abort(400, description="No decision provided")
|
||||
|
||||
if not Permissions.PLUGIN_APPKEYS_GRANT.can():
|
||||
flask.abort(403, description="No permission to grant app access")
|
||||
|
||||
decision = data["decision"] in valid_boolean_trues
|
||||
user_id = current_user.get_name()
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
body{padding-top:40px;padding-bottom:40px;background-color:#f5f5f5}#auth-choice,#auth-done,#auth-error,#auth-offline{max-width:300px;padding:19px 29px 29px;margin:0 auto 20px;background-color:#fff;border:1px solid #e5e5e5;-webkit-border-radius:5px;-moz-border-radius:5px;border-radius:5px;-webkit-box-shadow:0 1px 2px rgba(0,0,0,.05);-moz-box-shadow:0 1px 2px rgba(0,0,0,.05);box-shadow:0 1px 2px rgba(0,0,0,.05);text-align:center}.form-auth-heading{margin-bottom:10px}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Will get included into the auth dialog, NOT into the regular OctoPrint
|
||||
* web application.
|
||||
*/
|
||||
|
||||
$(function () {
|
||||
var OctoPrint = window.OctoPrint;
|
||||
|
||||
OctoPrint.authui = {
|
||||
startedUp: false
|
||||
};
|
||||
|
||||
var offlineElement = $("#auth-offline");
|
||||
var grantButtonElement = $("#auth-grant");
|
||||
var rejectButtonElement = $("#auth-reject");
|
||||
|
||||
var choiceElement = $("#auth-choice");
|
||||
var doneElement = $("#auth-done");
|
||||
var errorElement = $("#auth-error");
|
||||
|
||||
var showDone = function () {
|
||||
choiceElement.hide();
|
||||
doneElement.show();
|
||||
};
|
||||
var showError = function () {
|
||||
choiceElement.hide();
|
||||
errorElement.show();
|
||||
};
|
||||
var showOffline = function () {
|
||||
choiceElement.hide();
|
||||
offlineElement.show();
|
||||
};
|
||||
|
||||
grantButtonElement.click(function () {
|
||||
errorElement.removeClass("in");
|
||||
|
||||
OctoPrint.plugins.appkeys
|
||||
.decide(USER_TOKEN, true)
|
||||
.done(function () {
|
||||
if (REDIRECT_URL) {
|
||||
window.location.href = REDIRECT_URL;
|
||||
} else {
|
||||
showDone();
|
||||
}
|
||||
})
|
||||
.fail(showError);
|
||||
|
||||
return false;
|
||||
});
|
||||
rejectButtonElement.click(function () {
|
||||
OctoPrint.plugins.appkeys
|
||||
.decide(USER_TOKEN, false)
|
||||
.done(function () {
|
||||
if (REDIRECT_URL) {
|
||||
window.location.href = REDIRECT_URL;
|
||||
} else {
|
||||
showDone();
|
||||
}
|
||||
})
|
||||
.fail(showError);
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
OctoPrint.options.baseurl = BASE_URL;
|
||||
|
||||
OctoPrint.socket.onConnected = function () {
|
||||
OctoPrint.browser.passiveLogin().done(function (login) {
|
||||
OctoPrint.socket.sendAuth(login.name, login.session);
|
||||
OctoPrint.authui.startedUp = true;
|
||||
});
|
||||
};
|
||||
|
||||
OctoPrint.socket.onDisconnected = function () {
|
||||
showOffline();
|
||||
};
|
||||
|
||||
OctoPrint.socket.onMessage("plugin", function (event) {
|
||||
if (event.data.plugin !== "appkeys") return;
|
||||
|
||||
var type = event.data.data.type;
|
||||
if (type !== "end_request") return;
|
||||
|
||||
var token = event.data.data.user_token;
|
||||
if (token !== USER_TOKEN) return;
|
||||
|
||||
showDone();
|
||||
});
|
||||
|
||||
OctoPrint.socket.connect();
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
body {
|
||||
padding-top: 40px;
|
||||
padding-bottom: 40px;
|
||||
background-color: #f5f5f5;
|
||||
}
|
||||
|
||||
#auth-choice,
|
||||
#auth-done,
|
||||
#auth-error,
|
||||
#auth-offline {
|
||||
max-width: 300px;
|
||||
padding: 19px 29px 29px;
|
||||
margin: 0 auto 20px;
|
||||
background-color: #fff;
|
||||
border: 1px solid #e5e5e5;
|
||||
-webkit-border-radius: 5px;
|
||||
-moz-border-radius: 5px;
|
||||
border-radius: 5px;
|
||||
-webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
|
||||
-moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
|
||||
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.form-auth-heading {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>OctoPrint App Authentication</title>
|
||||
|
||||
<link rel="shortcut icon" href="{{ url_for('static', filename='img/tentacle-32x32.png') }}">
|
||||
<link rel="mask-icon" href="{{ url_for('static', filename='img/mask.svg') }}" color="#56BE37">
|
||||
<link rel="mask-icon-theme" href="{{ url_for('static', filename='img/mask-theme.svg') }}" color="#56BE37">
|
||||
<link rel="apple-touch-icon" sizes="114x114" href="{{ url_for('static', filename='img/apple-touch-icon-114x114.png') }}">
|
||||
<link rel="apple-touch-icon" sizes="144x144" href="{{ url_for('static', filename='img/apple-touch-icon-144x144.png') }}">
|
||||
|
||||
<!-- le CSS -->
|
||||
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='css/bootstrap.min.css') }}">
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='css/bootstrap-responsive.min.css') }}">
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='vendor/font-awesome-5.15.1/css/all.min.css') }}">
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='vendor/font-awesome-5.15.1/css/v4-shims.css') }}">
|
||||
<link rel="stylesheet" href="{{ url_for('plugin.appkeys.static', filename='css/authdialog.css') }}">
|
||||
|
||||
{% for url in theming %}
|
||||
<link rel="stylesheet" href="{{ url }}">
|
||||
{% endfor %}
|
||||
|
||||
<!-- le javascript -->
|
||||
|
||||
<script>
|
||||
var BASE_URL = "{{ url_for('index') }}";
|
||||
var USER_TOKEN = "{{ user_token|e }}";
|
||||
var DECISION_URL = "{{ url_for('plugin.appkeys.handle_decision', user_token=user_token) }}";
|
||||
var REDIRECT_URL = "{{ redirect_url|e }}";
|
||||
</script>
|
||||
<script src="{{ url_for('static', filename='js/lib/jquery/jquery.min.js') }}"></script>
|
||||
<script src="{{ url_for('static', filename='js/lib/sockjs.min.js') }}"></script>
|
||||
<script src="{{ url_for('static', filename='js/lib/bootstrap/bootstrap.js') }}"></script>
|
||||
<script src="{{ url_for('static', filename='js/lib/lodash.min.js') }}"></script>
|
||||
|
||||
{% assets "js_client" %}
|
||||
<script type="text/javascript" src="{{ ASSET_URL }}"></script>
|
||||
{% endassets %}
|
||||
|
||||
<script src="{{ url_for('plugin.appkeys.static', filename='js/authdialog.js') }}"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<form id="auth-choice" class="form-auth">
|
||||
<h2 class="form-auth-heading" data-test-id="login-title">{{ _('Access Request') }}</h2>
|
||||
|
||||
<p>{% trans %}Hello {{ user }}!{% endtrans %}</p>
|
||||
<p>{{ request_text }}</p>
|
||||
<p>{% trans %}
|
||||
Do you want to allow access to this application with your user account?
|
||||
{% endtrans %}</p>
|
||||
|
||||
<button class="btn btn-block btn-primary" id="auth-grant" data-test-id="auth-grant" type="submit">{{ _('Allow') }}</button>
|
||||
<button class="btn btn-block" id="auth-reject" data-test-id="auth-reject" type="submit">{{ _('Deny') }}</button>
|
||||
</form>
|
||||
|
||||
<div id="auth-done" class="hide">
|
||||
<p>{% trans %}
|
||||
The access request has been processed. You may now close this tab.
|
||||
{% endtrans %}</p>
|
||||
</div>
|
||||
|
||||
<div id="auth-error" class="hide">
|
||||
<p class="text-error">{% trans %}
|
||||
Something went wrong while processing the access request. You may now close this tab.
|
||||
{% endtrans %}</p>
|
||||
</div>
|
||||
|
||||
<div id="auth-offline" class="hide">
|
||||
<p class="text-error">{% trans %}
|
||||
The server has gone offline. You may now close this tab.
|
||||
{% endtrans %}</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user