From 58e2a2d652c07c3fc0666d2d03c05cf3fb629a02 Mon Sep 17 00:00:00 2001 From: Jannick Date: Wed, 10 Apr 2024 14:38:50 +0200 Subject: Created the foundation for OAuth2 authentication with battle.net. also added a log in button. --- app.py | 30 +++++++++++++++++++++++++++++- views/base.html | 1 + views/index.html | 2 +- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index c58833c..7621b28 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,17 @@ -from bottle import Bottle, run, debug, static_file +from bottle import Bottle, run, debug, static_file, request, redirect, response from bottle import jinja2_template as template +from oauthlib.oauth2 import WebApplicationClient +from requests_oauthlib import OAuth2Session +import secrets + +CLIENT_ID = "x" # DOTENV ligger paa discorden, repoet er publkic saa det +CLIENT_SECRET = "x" # DOTENV PAHAHAH +REDIRECT_URI = "http://localhost:8080/callback" +# REDIRECT_URI = "https://google.com" + +AUTH_BASE_URL = 'https://oauth.battle.net/authorize' +TOKEN_URL = "https://oauth.battle.net/token" +client = WebApplicationClient(CLIENT_ID) app = Bottle() @@ -8,6 +20,22 @@ app = Bottle() def index(): return template("index") +@app.route("/battle") +def battle(): + state = secrets.token_urlsafe(16) + response.set_cookie('oauth_state', state) + authorization_url = client.prepare_request_uri(AUTH_BASE_URL, redirect_uri=REDIRECT_URI, state=state) + return redirect(authorization_url) + +@app.route('/callback') +def callback(): + state = request.get_cookie('oauth_state') + code = request.query.get('code') + oauth2_session = OAuth2Session(CLIENT_ID, state=state, redirect_uri=REDIRECT_URI) + token_response = oauth2_session.fetch_token(TOKEN_URL, authorization_response=request.url, client_secret=CLIENT_SECRET) + + return f'Access token: {token_response.get("access_token")}' + @app.route("/join.html") def join_form(): return template("join") diff --git a/views/base.html b/views/base.html index 1550fa4..4f17b34 100644 --- a/views/base.html +++ b/views/base.html @@ -12,6 +12,7 @@