oauth-2.0oauthpkce

Why is PKCE `code_verifier` calculated server-side in BFF pattern?


The draft RFC OAuth 2.0 for Browser-Based Applications (published: 17 January 2025) introduces BFF pattern in 6.1. Backend For Frontend (BFF).

Here's the architecture:

enter image description here

The draft says

the JavaScript application triggers a navigation to the BFF (C) to initiate the Authorization Code flow with the PKCE extension (described in Section 6.1.3.1), to which the BFF responds by redirecting the browser to the authorization endpoint (D).

When the user is redirected back, the browser delivers the authorization code to the BFF (E), where the BFF can then exchange it for tokens at the token endpoint (F) using its client credentials and PKCE code verifier.

This implies PKCE code_verifier, code_challenge and code_challenge_method are all calculated and stored server-side, but why?

I have three questions:


Solution

  • From an OpenID-Connect point of view, the BFF module becomes the client in relation to the Authorization Server. The PKCE is calculated server side because the entire point of the BFF is to shift the authentication logic from the browser to the backend. It is also important to remember that the BFF is usually located in a separate "server/service" from your authorization Server.

    Also, PKCE is only applicable when you use the authorization code flow and this is all about authentiating a user and in asecure way exhange the code for the token.

    To answer your question.

    1. The BFF is registered as a client in the authorization server and each "BFF authenication attempt gets a different code_verifier generated and its typically stored in a a separate browser cookie through-out the authentication process.

    2. Its often implemented as a temporary HTTP Only cookie.

    3. The big idea with all of this is to avoid any authentication logic in the browser. The browser and mobile applications are public clients. Meaning, they can't handle or keep secrets.

    You can find more info about PKCE and OpenID-Connect in: OpenID Connect for Developers