forked from spacebar/SplatNet-Backend
make into multiple different scripts to prevent breaking changes
This commit is contained in:
parent
3a1432de0f
commit
3ca5cc73f0
5 changed files with 70 additions and 73 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
__pycache__
|
||||
9
main.py
Normal file
9
main.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
from fastapi import FastAPI
|
||||
from routes import sso
|
||||
import uvicorn
|
||||
|
||||
app = FastAPI()
|
||||
app.include_router(sso.router, prefix="/api/v2/sso")
|
||||
|
||||
if __name__ == '__main__':
|
||||
uvicorn.run(app, host="0.0.0.0", port=5000)
|
||||
36
routes/sso.py
Normal file
36
routes/sso.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
from fastapi import APIRouter, Request, Form, Response
|
||||
from fastapi.responses import RedirectResponse
|
||||
from typing import Optional
|
||||
from services import auth
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post('/spfn/generate_token')
|
||||
async def login(
|
||||
request: Request,
|
||||
username: Optional[str] = Form(None),
|
||||
password: Optional[str] = Form(None),
|
||||
frontend_origin: Optional[str] = Form(None)
|
||||
):
|
||||
host = (frontend_origin or request.headers.get("referer") or "/").rstrip('/')
|
||||
sep = "&" if "?" in host else "?"
|
||||
|
||||
if not username or not password:
|
||||
return RedirectResponse(f"{host}/users/auth/splatfestival/{sep}error=auth&username={username or ''}", 303)
|
||||
|
||||
try:
|
||||
token = auth.get_token(username, password)
|
||||
if not token:
|
||||
return RedirectResponse(f"{host}/users/auth/splatfestival/{sep}error=auth&username={username}", 303)
|
||||
|
||||
# Logging profile for debugging
|
||||
profile = auth.get_profile(token)
|
||||
if profile:
|
||||
print(f"=== PROFILE ===\n{profile}\n===============")
|
||||
|
||||
redirect_arg = request.query_params.get('redirect')
|
||||
target = redirect_arg if redirect_arg else f"{host}/friend_list/"
|
||||
return RedirectResponse(url=target, status_code=303)
|
||||
|
||||
except Exception as e:
|
||||
return Response(content=f"Proxy Error: {str(e)}", status_code=500)
|
||||
24
services/auth.py
Normal file
24
services/auth.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import requests
|
||||
import base64
|
||||
|
||||
API_URL = "https://account.spfn.net/api/v2"
|
||||
|
||||
def get_token(username, password):
|
||||
creds = f"{username} {password}"
|
||||
encoded = base64.b64encode(creds.encode()).decode()
|
||||
|
||||
headers = {
|
||||
"Authorization": f"Basic {encoded}",
|
||||
"User-Agent": "saturday & allison should lwk kiss..."
|
||||
}
|
||||
|
||||
response = requests.get(f"{API_URL}/oauth2/generate_token", headers=headers, timeout=10)
|
||||
return response.json().get("token") if response.ok else None
|
||||
|
||||
def get_profile(token):
|
||||
headers = {
|
||||
"Authorization": f"Bearer {token}",
|
||||
"User-Agent": "every update im going to change this user agent because im bored!!"
|
||||
}
|
||||
response = requests.get(f"{API_URL}/users/@me/profile", headers=headers, timeout=10)
|
||||
return response.text if response.ok else None
|
||||
73
spfn-main.py
73
spfn-main.py
|
|
@ -1,73 +0,0 @@
|
|||
from fastapi import FastAPI, Request, Form
|
||||
from fastapi.responses import RedirectResponse, Response
|
||||
import requests
|
||||
import base64
|
||||
from typing import Optional
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
SPFN_API_URL = "https://account.spfn.net/api/v2/oauth2/generate_token"
|
||||
|
||||
def generate_spfn_token(encoded_creds):
|
||||
headers = {
|
||||
"Authorization": f"Basic {encoded_creds}",
|
||||
"User-Agent": "saturday & allison should lwk kiss..."
|
||||
}
|
||||
response = requests.get(SPFN_API_URL, headers=headers, timeout=10)
|
||||
return response.json().get("token") if response.ok else None
|
||||
|
||||
@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 eats burgers then does oomi appear?
|
||||
if token:
|
||||
print("=== TOKEN ===", token, "===========", sep="\n")
|
||||
|
||||
profile_headers = {
|
||||
"Authorization": f"Bearer {token}",
|
||||
"User-Agent": "every update im going to change this user agent because im bored!!"
|
||||
}
|
||||
|
||||
profile_response = requests.get(
|
||||
"https://account.spfn.net/api/v2/users/@me/profile",
|
||||
headers=profile_headers,
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if profile_response.ok:
|
||||
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 "?"
|
||||
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 Response(content=f"Proxy Error: {str(e)}", status_code=500)
|
||||
|
||||
if __name__ == '__main__':
|
||||
import uvicorn
|
||||
uvicorn.run(app, host="0.0.0.0", port=5000)
|
||||
Loading…
Reference in a new issue