Application & API Security

Backend for Frontend (BFF) Pattern

Pattern where a server-side backend acts as the confidential OAuth/OIDC client for a single-page app, keeping tokens out of the browser and giving the SPA only an HttpOnly session cookie.

The Backend for Frontend (BFF) pattern moves the OAuth 2.0 / OpenID Connect client role from a single-page application (SPA) running in the browser to a server-side backend. The backend is a confidential client: it runs the authorization code flow, stores the ID, access, and refresh tokens, and calls APIs on the SPA's behalf. The SPA never sees a token. It only holds a session cookie that is valid solely against the BFF.

This diagram shows the login through the BFF followed by an API call proxied by the backend.

Protected APIAuthorization ServerBackend (BFF)Browser (SPA)UserProtected APIAuthorization ServerBackend (BFF)Browser (SPA)UserClick "Login"1GET /login2Generate state, nonce, and PKCE code verifier/challenge3302 Redirect to authorization endpoint4GET /authorize5Login6Credentials7Consent prompt8Scope approval9302 Redirect to /callback with code and state10GET /callback?code&state11Validate state12POST /oauth/token13ID token, access token, refresh token14Validate ID token and store tokens in session15302 Redirect to SPA with session cookie16GET /api/orders with session cookie17Validate session and CSRF protections18GET /orders with access token19200 OK - orders20200 OK - orders21