Working on: The Twitch authentication
This commit is contained in:
9
auth.py
9
auth.py
@@ -1,5 +1,6 @@
|
||||
import httpx
|
||||
import secrets
|
||||
import logging
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
||||
from fastapi.responses import RedirectResponse
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -11,6 +12,9 @@ import security
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Dependency to get a DB session
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
@@ -27,6 +31,7 @@ async def login_with_twitch(request: Request):
|
||||
# Generate a random state token for CSRF protection
|
||||
state = secrets.token_urlsafe(16)
|
||||
request.session['oauth_state'] = state
|
||||
logger.info(f"Generated OAuth state: {state} for session.")
|
||||
|
||||
# As per RESEARCH_REPORT.md, these are the minimum required scopes
|
||||
scopes = "chat:read"
|
||||
@@ -48,7 +53,9 @@ async def auth_twitch_callback(code: str, state: str, request: Request, db: Sess
|
||||
Step 2 of OAuth flow: Handle the callback from Twitch after user authorization.
|
||||
"""
|
||||
# CSRF Protection: Validate the state
|
||||
if state != request.session.pop('oauth_state', None):
|
||||
session_state = request.session.pop('oauth_state', None)
|
||||
if state != session_state:
|
||||
logger.error(f"OAuth state mismatch! Received state: '{state}', Session state: '{session_state}'")
|
||||
raise HTTPException(status_code=403, detail="Invalid state parameter. CSRF attack suspected.")
|
||||
|
||||
# Step 4: Exchange the authorization code for an access token
|
||||
|
||||
Reference in New Issue
Block a user