← Back to the journal

OAuth Architectures for SPAs: Who Holds Tokens, Who Calls the API, and What XSS Can Do

Choose an OAuth architecture by tracing one authenticated request, then deciding which credentials and actions injected JavaScript should be able to reach.

On this page

OAuth Architectures for SPAs: Who Holds Tokens, Who Calls the API, and What XSS Can Do

Reading position1 / 5
Editorial illustration for 3 OAuth Architectures for SPAs: Who Holds Tokens, Who Calls the API, and What XSS Can Do
AI-generated editorial illustration

An injected script does not need to read a session cookie to cause damage. HttpOnly blocks JavaScript from reading the cookie, but the browser can still attach it to requests initiated by that script. Protecting a credential and preventing unauthorized actions are different problems. (cheatsheetseries.owasp.org)

RFC 10017, published in August 2026 as an IETF Best Current Practice, describes three browser-app architectures. Use them to decide where authentication responsibilities belong, not just where to store a token. (rfc-editor.org)

1. Backend for Frontend: keep OAuth tokens behind the server

Browser -- session cookie --> BFF -- access token --> API

The BFF handles OAuth and forwards API requests. Browser JavaScript receives no OAuth tokens. XSS can nevertheless make authenticated requests through the victim’s browser. (rfc-editor.org)

Choose this when keeping reusable OAuth credentials away from frontend code is a requirement. For a hypothetical account-management app, that would be a reasonable starting constraint.

Before committing, review the actual request inventory: ordinary JSON calls, uploads, downloads, streaming responses and cancellation. Assign an owner for each proxied path, its failure behavior and its capacity requirements. “We already have a backend” is not a sufficient implementation plan.

Also decide which operations the BFF should expose. Prefer explicit application routes over a browser-controlled destination URL. Treat that as a boundary to review, not merely plumbing.

2. Token-mediating backend: keep refresh tokens behind the server

Browser -- session cookie --> Backend -- access token --> Browser
Browser -- access token --> API

The backend handles OAuth and retains refresh tokens; JavaScript receives access tokens and calls APIs directly. Injected code can obtain access tokens too. The backend’s cookie-authenticated endpoints still require protection. (rfc-editor.org)

Choose this only with an explicit reason for direct browser-to-API traffic. For example, a team might require direct transfers while wanting server-side ownership of refresh credentials.

Write down the accepted exposure: an attacker executing in the frontend may obtain the access tokens available to that frontend. Do not describe this choice as “BFF security without proxying.”

During design review, separate the responsibilities: who requests scopes, who approves them, who refreshes credentials and who handles an API rejection? Require answers for concurrent tabs and expired sessions before choosing an SDK.

3. Browser-based OAuth client: put the OAuth client in the SPA

Browser -- authorization code + PKCE --> Authorization server
Browser -- access token --> API

The browser owns OAuth token handling and API calls. Malicious JavaScript may steal accessible tokens or, under suitable conditions, initiate a fresh authorization flow. Storage isolation alone does not resolve that threat. (rfc-editor.org)

Consider this when a backend is genuinely unavailable and the exposure is acceptable for the application. Record that acceptance rather than treating static hosting as an automatic security decision.

Use Authorization Code with PKCE, not the implicit grant. RFC 9700 requires PKCE for public clients and recommends it for confidential clients. When refresh tokens are issued to public clients, they must be sender-constrained or use rotation. Restrict token privileges to the required resources and scopes. These controls address particular OAuth threats; they do not make arbitrary frontend code trustworthy. (rfc-editor.org)

For a proposed server-side session, the browser cookie could contain an opaque identifier rather than an OAuth token. This illustrative header is not a complete session implementation:

Set-Cookie: __Host-session=<opaque-id>; Path=/; Secure; HttpOnly; SameSite=Strict

Secure restricts transmission to secure connections. HttpOnly prevents script access to the cookie value. A __Host- cookie requires Secure, Path=/ and no Domain attribute. Session expiration, identifier rotation and server-side invalidation still need implementation. (cheatsheetseries.owasp.org)

Protect state-changing cookie-authenticated endpoints with the framework’s supported CSRF mechanism. A synchronizer token is one option. Origin validation can add protection; SameSite is defense in depth rather than a universal substitute. Do not use GET for state changes. (cheatsheetseries.owasp.org)

These controls target forged requests from outside the trusted application context. XSS runs inside that context and can defeat CSRF mitigations. An anti-CSRF token is not a barrier against a script that can read the page and issue its own requests. (cheatsheetseries.owasp.org)

Make the decision around one sensitive action

For a design review, trace an action such as changing a payout destination. Record:

  • Which component holds each credential?
  • Which component sends the final API request?
  • What could injected JavaScript read, obtain or invoke?
  • Which server-side checks must succeed before the change is accepted?
  • Who operates each component when authentication or upstream requests fail?

Then choose the architecture whose credential exposure you can accept and whose server responsibilities you can actually support. Treat approval of the sensitive action as a separate design decision from custody of its OAuth token.

THE CONVERSATION

Comments

Add your perspective ↓

Questions, useful examples and different approaches are welcome. Keep the discussion thoughtful and on topic.

Loading comments…

    Add your perspective

    All comments are reviewed before publication. Your name and comment will be public after approval; your email stays private and is used for account access or moderation, not a mailing list.

    Checking sign-in…