Added simple Discord auth

This commit is contained in:
2025-05-26 09:17:23 +02:00
parent ce86b95163
commit d08ea6240e
5 changed files with 78 additions and 5 deletions

View File

@@ -1,7 +1,24 @@
from urllib.parse import urljoin
from fastapi import FastAPI
from fastapi_discord import DiscordOAuthClient
from libbot.utils import config_get
discord_oauth: DiscordOAuthClient = DiscordOAuthClient(
config_get("client_id", "api", "oauth"),
config_get("client_secret", "api", "oauth"),
urljoin(config_get("public_url", "api"), "/v1/callback"),
("identify", "guilds"),
)
# TODO Add an integration for the contact information
app = FastAPI(
app: FastAPI = FastAPI(
title="Javelina",
version="0.0.1",
)
# TODO Replace this with a FastAPI lifespan
@app.on_event("startup")
async def on_startup():
await discord_oauth.init()

View File

@@ -0,0 +1,41 @@
from logging import Logger
from fastapi_discord import RateLimited, Unauthorized
from fastapi_discord.exceptions import ClientSessionNotInitialized
from starlette.responses import JSONResponse
from api.app import app, discord_oauth
from modules.utils import get_logger
logger: Logger = get_logger(__name__)
@app.get("/v1/login", response_class=JSONResponse)
async def login() -> JSONResponse:
return JSONResponse({"url": discord_oauth.oauth_login_url})
@app.get("/v1/callback", response_class=JSONResponse)
async def callback(code: str) -> JSONResponse:
token, refresh_token = await discord_oauth.get_access_token(code)
return JSONResponse({"access_token": token, "refresh_token": refresh_token})
@app.exception_handler(Unauthorized)
async def unauthorized_error_handler(_, __) -> JSONResponse:
return JSONResponse({"error": "Unauthorized"}, status_code=401)
@app.exception_handler(RateLimited)
async def rate_limit_error_handler(_, exc: RateLimited) -> JSONResponse:
return JSONResponse(
{"error": "RateLimited", "retry": exc.retry_after, "message": exc.message},
status_code=429,
)
@app.exception_handler(ClientSessionNotInitialized)
async def client_session_error_handler(_, exc: ClientSessionNotInitialized) -> JSONResponse:
logger.error("Client session was not initialized: %s", exc, exc_info=exc)
return JSONResponse({"error": "Internal Error"}, status_code=500)

View File

@@ -1,13 +1,20 @@
from logging import Logger
from fastapi import Depends
from fastapi_discord import User
from starlette.responses import JSONResponse
from api.app import app
from api.app import app, discord_oauth
from modules.utils import get_logger
logger: Logger = get_logger(__name__)
@app.get("/v1/me", response_class=JSONResponse)
async def get_me_v1() -> JSONResponse:
return JSONResponse({"error": "not implemented"})
@app.get(
"/v1/me",
dependencies=[Depends(discord_oauth.requires_authorization)],
response_model=User,
response_class=JSONResponse,
)
async def get_me_v1(user: User = Depends(discord_oauth.user)) -> User:
return user

View File

@@ -16,6 +16,13 @@
"activity_text": "The Game Of Life"
}
},
"api": {
"oauth": {
"client_id": null,
"client_secret": null
},
"public_url": "http://127.0.0.1:8000"
},
"database": {
"user": null,
"password": null,

View File

@@ -1,6 +1,7 @@
aiohttp>=3.6.0
apscheduler~=3.11.0
fastapi[all]~=0.115.0
fastapi_discord==0.2.7
libbot[speed,pycord,cache]==4.2.0
mongodb-migrations==1.3.1
pynacl~=1.5.0