From f0ffbb38a793d3792eda60116e787889e3400144 Mon Sep 17 00:00:00 2001 From: AidanFromProgramming <46631427+AidanFromProgramming@users.noreply.github.com> Date: Sun, 18 Oct 2020 15:23:22 -0400 Subject: [PATCH 1/3] Got Oauth2 to flow working --- .gitignore | 3 + .idea/.gitignore | 3 + .idea/Dozer-Web.iml | 8 ++ .idea/discord.xml | 6 ++ .idea/encodings.xml | 4 + .idea/inspectionProfiles/Project_Default.xml | 26 +++++++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/misc.xml | 7 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ web/webhost.py | 77 +++++++++++++++++++ 11 files changed, 154 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/Dozer-Web.iml create mode 100644 .idea/discord.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 web/webhost.py diff --git a/.gitignore b/.gitignore index b6e4761..a731b3c 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + +config.json +.idea/ \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/Dozer-Web.iml b/.idea/Dozer-Web.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/Dozer-Web.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..cd711a0 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..15a15b2 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..98bd35f --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,26 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7a85af6 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..18d0393 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/web/webhost.py b/web/webhost.py new file mode 100644 index 0000000..8b541c6 --- /dev/null +++ b/web/webhost.py @@ -0,0 +1,77 @@ +import os, json +from flask import Flask, g, session, redirect, request, url_for, jsonify +from requests_oauthlib import OAuth2Session + +with open("config.json", "r") as f: + config = json.load(f) + +OAUTH2_CLIENT_ID = config["OAUTH2_CLIENT_ID"] +OAUTH2_CLIENT_SECRET = config["OAUTH2_CLIENT_SECRET"] +OAUTH2_REDIRECT_URI = config["OAUTH2_REDIRECT_URI"] + +API_BASE_URL = os.environ.get('API_BASE_URL', 'https://discordapp.com/api') +AUTHORIZATION_BASE_URL = API_BASE_URL + '/oauth2/authorize' +TOKEN_URL = API_BASE_URL + '/oauth2/token' + +app = Flask(__name__) +app.debug = True +app.config['SECRET_KEY'] = OAUTH2_CLIENT_SECRET + +if 'http://' in OAUTH2_REDIRECT_URI: + os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = 'true' + + +def token_updater(token): + session['oauth2_token'] = token + + +def make_session(token=None, state=None, scope=None): + return OAuth2Session( + client_id=OAUTH2_CLIENT_ID, + token=token, + state=state, + scope=scope, + redirect_uri=OAUTH2_REDIRECT_URI, + auto_refresh_kwargs={ + 'client_id': OAUTH2_CLIENT_ID, + 'client_secret': OAUTH2_CLIENT_SECRET, + }, + auto_refresh_url=TOKEN_URL, + token_updater=token_updater) + + +@app.route('/') +def index(): + scope = request.args.get( + 'scope', + 'identify guilds') + discord = make_session(scope=scope.split(' ')) + authorization_url, state = discord.authorization_url(AUTHORIZATION_BASE_URL) + session['oauth2_state'] = state + return redirect(authorization_url) + + +@app.route('/callback') +def callback(): + if request.values.get('error'): + return request.values['error'] + discord = make_session(state=session.get('oauth2_state')) + token = discord.fetch_token( + TOKEN_URL, + client_secret=OAUTH2_CLIENT_SECRET, + authorization_response=request.url) + session['oauth2_token'] = token + print("Redirect") + return redirect("http://localhost:47670/me", 302) + + +@app.route('/me/') +def me(): + discord = make_session(token=session.get('oauth2_token')) + user = discord.get(API_BASE_URL + '/users/@me').json() + guilds = discord.get(API_BASE_URL + '/users/@me/guilds').json() + return jsonify(user=user, guilds=guilds) + + +if __name__ == '__main__': + app.run(port=47670) \ No newline at end of file From 5c50a123d73951a1b1564ff36e000d95786113db Mon Sep 17 00:00:00 2001 From: Travis Weir Date: Sun, 18 Oct 2020 14:27:01 -0500 Subject: [PATCH 2/3] Cleaned up after Aidan --- .idea/.gitignore | 3 --- .idea/Dozer-Web.iml | 8 ------ .idea/discord.xml | 6 ----- .idea/encodings.xml | 4 --- .idea/inspectionProfiles/Project_Default.xml | 26 ------------------- .../inspectionProfiles/profiles_settings.xml | 6 ----- .idea/misc.xml | 7 ----- .idea/modules.xml | 8 ------ .idea/vcs.xml | 6 ----- 9 files changed, 74 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/Dozer-Web.iml delete mode 100644 .idea/discord.xml delete mode 100644 .idea/encodings.xml delete mode 100644 .idea/inspectionProfiles/Project_Default.xml delete mode 100644 .idea/inspectionProfiles/profiles_settings.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 26d3352..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml diff --git a/.idea/Dozer-Web.iml b/.idea/Dozer-Web.iml deleted file mode 100644 index d0876a7..0000000 --- a/.idea/Dozer-Web.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml deleted file mode 100644 index cd711a0..0000000 --- a/.idea/discord.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index 15a15b2..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index 98bd35f..0000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 105ce2d..0000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 7a85af6..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 18d0393..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From b36319708d83216032352667c2b4b24c67a89844 Mon Sep 17 00:00:00 2001 From: AidanFromProgramming <46631427+AidanFromProgramming@users.noreply.github.com> Date: Sun, 18 Oct 2020 18:37:50 -0400 Subject: [PATCH 3/3] Got Oauth2 session logging working --- web/{webhost.py => Oauth2.py} | 51 +++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 8 deletions(-) rename web/{webhost.py => Oauth2.py} (64%) diff --git a/web/webhost.py b/web/Oauth2.py similarity index 64% rename from web/webhost.py rename to web/Oauth2.py index 8b541c6..21c0ce8 100644 --- a/web/webhost.py +++ b/web/Oauth2.py @@ -1,5 +1,5 @@ import os, json -from flask import Flask, g, session, redirect, request, url_for, jsonify +from flask import Flask, g, session, redirect, request, url_for, jsonify, make_response, render_template from requests_oauthlib import OAuth2Session with open("config.json", "r") as f: @@ -13,10 +13,14 @@ AUTHORIZATION_BASE_URL = API_BASE_URL + '/oauth2/authorize' TOKEN_URL = API_BASE_URL + '/oauth2/token' -app = Flask(__name__) +app = Flask(__name__, template_folder=config['HTML_TEMPLATES']) app.debug = True app.config['SECRET_KEY'] = OAUTH2_CLIENT_SECRET +base_url = "http://127.0.0.1:47670" + +token_transfer = [] + if 'http://' in OAUTH2_REDIRECT_URI: os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = 'true' @@ -25,6 +29,10 @@ def token_updater(token): session['oauth2_token'] = token +def local_redirect(address): + return base_url + address + + def make_session(token=None, state=None, scope=None): return OAuth2Session( client_id=OAUTH2_CLIENT_ID, @@ -41,18 +49,40 @@ def make_session(token=None, state=None, scope=None): @app.route('/') -def index(): +def main(): + print(session) + if "userInfo" in session: + print("User has previous session") + return redirect(local_redirect("/home"), 302) + else: + print("User does not have previous session") + return redirect(local_redirect("/link"), 302) + + +@app.route('/home') +def home(): + try: + return render_template('home.html', user=session["userInfo"]["username"]) + except KeyError: + print("User's previous session is invalid") + return redirect(local_redirect("/link"), 302) + + +@app.route('/link') +def link(): scope = request.args.get( 'scope', 'identify guilds') discord = make_session(scope=scope.split(' ')) authorization_url, state = discord.authorization_url(AUTHORIZATION_BASE_URL) session['oauth2_state'] = state - return redirect(authorization_url) + print(session) + return redirect(authorization_url, 302) @app.route('/callback') def callback(): + global token_transfer if request.values.get('error'): return request.values['error'] discord = make_session(state=session.get('oauth2_state')) @@ -60,17 +90,22 @@ def callback(): TOKEN_URL, client_secret=OAUTH2_CLIENT_SECRET, authorization_response=request.url) - session['oauth2_token'] = token - print("Redirect") - return redirect("http://localhost:47670/me", 302) + token_transfer = token + print(session) + return redirect(local_redirect("/me"), 302) @app.route('/me/') def me(): + session['oauth2_token'] = token_transfer discord = make_session(token=session.get('oauth2_token')) user = discord.get(API_BASE_URL + '/users/@me').json() guilds = discord.get(API_BASE_URL + '/users/@me/guilds').json() - return jsonify(user=user, guilds=guilds) + + session['userInfo'] = user + session['guildInfo'] = guilds + + return redirect(local_redirect("/"), 302) if __name__ == '__main__':