Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Integrating OAuth2 Login for Swagger API with firebase auth

$
0
0
class AuthenticationMiddleware(BaseHTTPMiddleware):"""Middleware to authenticate requests using Firebase Auth."""    async def dispatch(self, request: Request, call_next: Callable):        # This is where you can modify the request if needed        path = request.url.path        # Exclude specific paths from middleware        if path in ["/health","/auth/login","/docs","/openapi.json",        ]:            return await call_next(request)  # Continue with the request        headers = request.headers        token = headers.get("Authorization")        if not token:            raise HTTPException(status_code=401, detail="Authorization token is missing")        try:            # Verify and decode the token using Firebase Admin SDK            user_info = auth.verify_id_token(token)            # Check if the token is still valid            if user_info.get("exp") < time.time():                raise HTTPException(status_code=401, detail="Token expired")            # If the token is valid, you can access user information            request.state.user = user_info            return await call_next(request)  # Continue with the request        except Exception as err:            raise HTTPException(status_code=401, detail="Invalid token") from err

I have written a middleware above - which basically tries to find the firebase token from the headers. The problem that I see here is that for any API that does use a token, I need to use postman/thudnerclient.

I'm interested in integrating a user-friendly OAuth2 login directly into the Swagger UI for easier testing. I've come across the FastAPI documentation on simple OAuth2 (https://fastapi.tiangolo.com/tutorial/security/simple-oauth2/#see-it-in-action), and I'm wondering if there's a way to adapt this for my Firebase authentication middleware.

Could someone guide me on incorporating OAuth2 login into the Swagger UI for FastAPI, allowing seamless token testing directly within the Swagger interface?


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>