From 6db73b9928df86606fe3fe2fc0475381da3f40d8 Mon Sep 17 00:00:00 2001 From: TrustyJAID Date: Thu, 20 Oct 2022 11:38:18 -0600 Subject: [PATCH 1/3] Add support for generic OAuth for cogs --- reddash/app/api/routes.py | 32 +++++++++++++ .../templates/third_party/oauth.html | 45 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 reddash/app/dashboard/templates/third_party/oauth.html diff --git a/reddash/app/api/routes.py b/reddash/app/api/routes.py index 26a95c35..02f884df 100644 --- a/reddash/app/api/routes.py +++ b/reddash/app/api/routes.py @@ -611,3 +611,35 @@ def third_party_spotify_callback(): except Exception as e: app.progress.print("".join(traceback.format_exception(type(e), e, e.__traceback__))) return render_template("third_party/spotify.html", context="2", msg=str(e)) + + +@blueprint.route("/third_party/callback/") +def third_party_oauth_callback(provider): + args = request.args.copy() + args["provider"] = provider + if not session.get("id"): + session["login_redirect"] = { + "route": f"api_blueprint.third_party_oauth_callback", + "kwargs": args, + } + return redirect(url_for("base_blueprint.login")) + + try: + requeststr = { + "jsonrpc": "2.0", + "id": 0, + "method": "DASHBOARDRPC_OAUTH__OAUTH_RECEIVE", + "params": [str(g.id), args], + } + with app.lock: + result = get_result(app, requeststr).json + if result["status"] == 0: + return render_template("third_party/oauth.html", context="2", msg=result["message"]) + if result["data"]["status"] == 0: + return render_template( + "third_party/oauth.html", context="3", msg=result["data"]["message"] + ) + return render_template("third_party/oauth.html", context="1", msg="") + except Exception as e: + app.progress.print("".join(traceback.format_exception(type(e), e, e.__traceback__))) + return render_template("third_party/oauth.html", context="2", msg=str(e)) diff --git a/reddash/app/dashboard/templates/third_party/oauth.html b/reddash/app/dashboard/templates/third_party/oauth.html new file mode 100644 index 00000000..05f81977 --- /dev/null +++ b/reddash/app/dashboard/templates/third_party/oauth.html @@ -0,0 +1,45 @@ +{% extends "base-site.html" %} + +{% block title %} {{ _('Third Party OAuth') }} {% endblock %} + + +{% block stylesheets %}{% endblock stylesheets %} + +{% block content %} + +
+
+
+
+
{{ _('OAuth Redirect') }}
+
+
+
+ {% if context == '1' %} +
{{ _("Successfully connected your account. You may now close this tab.") }} +
+ {% else %} + {% if context == '2' %} +
{{ _("We had trouble contacting the bot to authenticate: {message}").format(message=msg) }} +
+ {% else %} +
{{ _("We failed to authenticate you: {message}").format(message=msg) }} +
+ {% endif %} + {% endif %} +
+
+
+
+ +
+
+
+ +{% endblock content %} + + +{% block javascripts %} +{% endblock javascripts %} \ No newline at end of file From aae1334bbfa8dcb0eebc04edd993db07691db299 Mon Sep 17 00:00:00 2001 From: TrustyJAID Date: Thu, 20 Oct 2022 13:20:28 -0600 Subject: [PATCH 2/3] Include the full url because some OAuth libs ask for it to parse themselves --- reddash/app/api/routes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/reddash/app/api/routes.py b/reddash/app/api/routes.py index 02f884df..1e6f56a7 100644 --- a/reddash/app/api/routes.py +++ b/reddash/app/api/routes.py @@ -617,6 +617,7 @@ def third_party_spotify_callback(): def third_party_oauth_callback(provider): args = request.args.copy() args["provider"] = provider + args["url"] = request.url if not session.get("id"): session["login_redirect"] = { "route": f"api_blueprint.third_party_oauth_callback", From 35ade7baf1f19d46faef329db6ece1c89d9b4cdd Mon Sep 17 00:00:00 2001 From: TrustyJAID Date: Thu, 20 Oct 2022 20:27:51 -0600 Subject: [PATCH 3/3] Include url arg after login --- reddash/app/api/routes.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/reddash/app/api/routes.py b/reddash/app/api/routes.py index 1e6f56a7..726ceda2 100644 --- a/reddash/app/api/routes.py +++ b/reddash/app/api/routes.py @@ -617,14 +617,13 @@ def third_party_spotify_callback(): def third_party_oauth_callback(provider): args = request.args.copy() args["provider"] = provider - args["url"] = request.url if not session.get("id"): session["login_redirect"] = { "route": f"api_blueprint.third_party_oauth_callback", "kwargs": args, } return redirect(url_for("base_blueprint.login")) - + args["url"] = request.url try: requeststr = { "jsonrpc": "2.0",