From 78a34190138aab175e5d93d9a28558f8322b8ea2 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Thu, 23 Dec 2021 00:36:15 +0000 Subject: [PATCH] Write Flask app --- .gitignore | 1 + handler/__init__.py | 20 ++++++++++++++++++ handler/main.py | 49 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 handler/__init__.py create mode 100644 handler/main.py diff --git a/.gitignore b/.gitignore index 15a1c1b..f5f657d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ __pycache__/ env/ keys/ +handler/settings.py diff --git a/handler/__init__.py b/handler/__init__.py new file mode 100644 index 0000000..d11e2ad --- /dev/null +++ b/handler/__init__.py @@ -0,0 +1,20 @@ +from flask import Flask +from flask_sqlalchemy import SQLAlchemy + +# init SQLAlchemy so we can use it later in our models +db = SQLAlchemy() + + +def create_app(): + app = Flask(__name__) + + app.config["SECRET_KEY"] = "4c7c2f455ffa53edf78d2b2e9c3f24f6f9340975" + app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///db.sqlite" + + db.init_app(app) + + from .main import main as main_blueprint + + app.register_blueprint(main_blueprint) + + return app diff --git a/handler/main.py b/handler/main.py new file mode 100644 index 0000000..69f68b8 --- /dev/null +++ b/handler/main.py @@ -0,0 +1,49 @@ +from flask import Blueprint, request, jsonify +import pprint +import requests + +from .settings import * + +# SYSTEM VARIABLES BELOW # +access_token = "" + +main = Blueprint("main", __name__) + +pp = pprint.PrettyPrinter(indent=2) + +def get_new_token(): + headers = {"Content-Type": "application/x-www-form-urlencoded"} + data = { + "grant_type": "refresh_token", + "refresh_token": refresh_token, + "client_id": client_id, + "client_assertion_type": "urn:ietf:params:oauth:client-assertion-type:jwt-bearer", + "client_assertion": jwt, + } + r = requests.post(f"{api_base}/auth/token", data=data, headers=headers) + parsed = r.json() + if r.status_code == 200: + if "access_token" in parsed.keys(): + access_token = parsed["access_token"] + if len(access_token) == len(refresh_token): + return True + else: + main.logger.error(f"Token refresh didn't contain access token: {parsed}") + return False + else: + main.logger.error(f"Cannot refresh token: {parsed}") + return False + + +@main.route("/refresh", methods=["GET"]) +def refresh(): + rtrn = get_new_token() + return jsonify(success=rtrn) + + +@main.route("/callback", methods=["POST"]) +def callback(): + pp.pprint(request.json) + if request.json is None: + return jsonify(success=False) + return jsonify(success=True)