diff --git a/src/core/config.py b/src/core/config.py index fe5590e..d907ba0 100644 --- a/src/core/config.py +++ b/src/core/config.py @@ -29,6 +29,8 @@ class Config(BaseModel): REDIS_URI: str + WEB_APP_HOST: str + def get_config() -> Config: settings = Settings() # type: ignore diff --git a/src/modules/web_app/views/auth.py b/src/modules/web_app/views/auth.py index 63e2de9..db99143 100644 --- a/src/modules/web_app/views/auth.py +++ b/src/modules/web_app/views/auth.py @@ -1,5 +1,46 @@ from fastapi import APIRouter +from httpx_oauth.oauth2 import OAuth2 -auth_router = APIRouter() +from core.config import config + +auth_router = APIRouter(prefix="/auth", tags=["auth"]) + + +twitch_oauth = OAuth2( + config.TWITCH_CLIENT_ID, + config.TWITCH_CLIENT_SECRET, + "https://id.twitch.tv/oauth2/authorize", + "https://id.twitch.tv/oauth2/token", +) + + +REDIRECT_URI_TEMPLATE = f"https://{config.WEB_APP_HOST}/" + "auth/callback/{service}/" + + +@auth_router.get("/get_authorization_url/{service}/") +async def get_authorization_url(service: str): + link = None + + if service == "twitch": + link = await twitch_oauth.get_authorization_url( + redirect_uri=REDIRECT_URI_TEMPLATE.format(service="twitch"), + ) + + return {"link": link} + + +@auth_router.get("/callback/{service}/") +async def callback(service: str, code: str): + user_data = None + + if service == "twitch": + token = await twitch_oauth.get_access_token( + code, + redirect_uri=REDIRECT_URI_TEMPLATE.format(service="twitch"), + ) + + user_data = await twitch_oauth.get_id_email(token["access_token"]) + + return {"user_data": user_data}