pluto/handler/main.py

50 lines
1.4 KiB
Python

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)