equinor / equinor/template-fastapi-react

Implement BFF OAuth2 Proxy

Abierto
#391 1 comentario 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
TypeScript
Estrellas
103
Forks
13
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

## Describe Problem

The application requires secure authentication for the SPA frontend to communicate with the FastAPI backend API. Direct OAuth2 flows from the browser (implicit flow or authorization code with PKCE stored in browser) expose tokens to XSS attacks and require complex token management in the frontend. Without a Backend-for-Frontend (BFF) pattern, the frontend must handle token refresh, storage, and secure transmission, increasing attack surface and complexity.

## Suggest Solution

Implement a BFF OAuth2 proxy using **oauth2-proxy**  with the following architecture:

1. **Deploy oauth2-proxy** as an intermediary between the SPA and backend API
- Configure OIDC authentication with Microsoft Entra ID (Azure AD)
- Use PKCE (`code_challenge_method: S256`) for secure authorization code flow
- Enable Redis-backed session storage for horizontal scalability
2. **Configure Nginx reverse proxy** with `auth_request` directive
- Route `/oauth2/*` paths directly to oauth2-proxy
- Enforce authentication on `/api/*` routes (return 401 if unauthenticated)
- Redirect unauthenticated users to sign-in for all other routes
3. **Token injection via headers**
- Inject `Authorization: Bearer ` header for API requests
- Forward `X-Auth-Request-Access-Token` for additional validation
4. **Secure cookie configuration**
- `cookie_secure: true`, `cookie_httponly: true`, `cookie_samesite: lax`
- 15-minute token refresh interval, 1-hour session expiry
- CSRF protection with per-request tokens
5. **Backend JWT validation** in FastAPI
- Validate tokens against Azure AD JWKS endpoint
- Extract user claims (oid, name, email, roles) for authorization

## Additional Details

* [OAuth 2 Proxy](https://oauth2-proxy.github.io/oauth2-proxy/configuration/integrations/nginx/)

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.