Migrate to FastAPI
This commit is contained in:
parent
1bff8d20fb
commit
3a1432de0f
1 changed files with 33 additions and 37 deletions
68
spfn-main.py
68
spfn-main.py
|
|
@ -1,8 +1,10 @@
|
|||
from flask import Flask, request, redirect
|
||||
from fastapi import FastAPI, Request, Form
|
||||
from fastapi.responses import RedirectResponse, Response
|
||||
import requests
|
||||
import base64
|
||||
from typing import Optional
|
||||
|
||||
app = Flask(__name__)
|
||||
app = FastAPI()
|
||||
|
||||
SPFN_API_URL = "https://account.spfn.net/api/v2/oauth2/generate_token"
|
||||
|
||||
|
|
@ -11,40 +13,40 @@ def generate_spfn_token(encoded_creds):
|
|||
"Authorization": f"Basic {encoded_creds}",
|
||||
"User-Agent": "saturday & allison should lwk kiss..."
|
||||
}
|
||||
|
||||
response = requests.get(SPFN_API_URL, headers=headers, timeout=10)
|
||||
if not response.ok:
|
||||
return None
|
||||
return response.json().get("token") if response.ok else None
|
||||
|
||||
data = response.json()
|
||||
return data.get("token")
|
||||
|
||||
|
||||
@app.route('/api/v2/sso/spfn/generate_token', methods=['POST'])
|
||||
def handle_login():
|
||||
username = request.form.get('username')
|
||||
password = request.form.get('password')
|
||||
|
||||
# fuck you login_host
|
||||
login_host = request.referrer or "/"
|
||||
@app.post('/api/v2/sso/spfn/generate_token')
|
||||
async def handle_login(
|
||||
request: Request,
|
||||
username: Optional[str] = Form(None),
|
||||
password: Optional[str] = Form(None),
|
||||
# added optional so everything doesnt crash and burn if it doesnt exist... it probably still will though.
|
||||
frontend_origin: Optional[str] = Form(None)
|
||||
):
|
||||
login_host = frontend_origin or request.headers.get("referer") or "/"
|
||||
|
||||
if login_host.endswith('/'):
|
||||
login_host = login_host[:-1]
|
||||
|
||||
if not username or not password:
|
||||
sep = "&" if "?" in login_host else "?"
|
||||
error_url = f"{login_host}/users/auth/splatfestival/{sep}error=auth&username={username or ''}"
|
||||
return RedirectResponse(url=error_url, status_code=303)
|
||||
|
||||
creds = f"{username} {password}"
|
||||
encoded_creds = base64.b64encode(creds.encode()).decode()
|
||||
|
||||
try:
|
||||
token = generate_spfn_token(encoded_creds)
|
||||
|
||||
#if onika eat burgers then does oomi appear?
|
||||
#if onika eats burgers then does oomi appear?
|
||||
if token:
|
||||
print("=== TOKEN ===")
|
||||
print(token)
|
||||
print("=============")
|
||||
print("=== TOKEN ===", token, "===========", sep="\n")
|
||||
|
||||
profile_headers = {
|
||||
"Authorization": f"Bearer {token}",
|
||||
"User-Agent": "maple still awake"
|
||||
"User-Agent": "every update im going to change this user agent because im bored!!"
|
||||
}
|
||||
|
||||
profile_response = requests.get(
|
||||
|
|
@ -54,24 +56,18 @@ def handle_login():
|
|||
)
|
||||
|
||||
if profile_response.ok:
|
||||
print("=== PROFILE ===")
|
||||
print(profile_response.text)
|
||||
print("===============")
|
||||
else:
|
||||
print("Profile request failed:", profile_response.status_code)
|
||||
|
||||
redirect_to = request.args.get('redirect', f"{login_host}/friend_list/")
|
||||
return redirect(redirect_to)
|
||||
|
||||
print("=== PROFILE ===", profile_response.text, "===============", sep="\n")
|
||||
redirect_arg = request.query_params.get('redirect')
|
||||
redirect_to = redirect_arg if redirect_arg else f"{login_host}/friend_list/"
|
||||
return RedirectResponse(url=redirect_to, status_code=303)
|
||||
else:
|
||||
sep = "&" if "?" in login_host else "?"
|
||||
return redirect(
|
||||
f"{login_host}/users/auth/splatfestival/{sep}error=auth&username={username}"
|
||||
)
|
||||
error_url = f"{login_host}/users/auth/splatfestival/{sep}error=auth&username={username}"
|
||||
return RedirectResponse(url=error_url, status_code=303)
|
||||
|
||||
except Exception as e:
|
||||
return f"Proxy Error: {str(e)}", 500
|
||||
|
||||
return Response(content=f"Proxy Error: {str(e)}", status_code=500)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(port=5000)
|
||||
import uvicorn
|
||||
uvicorn.run(app, host="0.0.0.0", port=5000)
|
||||
Loading…
Reference in a new issue