Authorization code with PKCE for a public client

This documentation was generated with the assistance of AI. Please report any inaccuracies.

spa-public is the registration a browser-only application (a single page application that talks to the authorization server itself, with no backend) would use. It is a public client: it has no secret, authenticates with none, and PKCE is the only thing that binds the authorization code to the application that asked for it. The repository ships the registration only: there is no single page application for spa-public in this repository (the SPA it ships is served by the bff and uses the confidential bff client, see Authorization code with PKCE through the BFF). The dev profile seeds spa-public with the redirect URI http://localhost:3000/callback, where nothing of this repository runs. The flow below is what Spring Authorization Server does for such a client, with the settings this repository gives it.

At a glance

Item Value

Actors

User, browser running a third-party SPA (not part of this repository), auth-server, resource-server.

Client

spa-public (DefaultClientDefinitions.spaPublic): public, none, grants authorization_code and refresh_token, PKCE required, no consent, scopes openid, profile, documents.read, documents.delete, audiences resource-server and auth-server, access token 10 minutes, self-contained (JWT).

Preconditions

ClientSeeder seeds the client only when authserver.clients.spa-public.redirect-uris (AUTHSERVER_SPA_REDIRECT_URIS) is set; authserver.clients.spa-public.post-logout-redirect-uris (AUTHSERVER_SPA_POST_LOGOUT_REDIRECT_URIS) is optional. The dev profile (application-dev.yml) sets http://localhost:3000/callback and http://localhost:3000/.

Endpoints

GET /oauth2/authorize, /login, /login/second-factor when owed, POST /oauth2/token, GET /oauth2/jwks, GET /userinfo, GET /.well-known/openid-configuration; then GET /api/documents on the resource server.

The domain model enforces PKCE for public clients: ClientRegistration refuses a non-confidential client with requireProofKey false (a public client must require PKCE), except a client whose only grant type is implicit, which only the legacy demo seeder creates (see Implicit flow (legacy demo) and PAR).

    /**
     * Defines the {@code bff} client: confidential, {@code client_secret_basic}, authorization code with PKCE and
     * refresh tokens, with refresh token rotation on. It requires the end-user's consent to the requested scopes.
     *
     * @param redirectUris redirect URIs of the deployment.
     * @param apiAudience  audience of the authorization server's own API.
     * @return the client.
     */
    public static ClientRegistration bff(final List<String> redirectUris, final String apiAudience) {
        return bff(redirectUris, apiAudience, List.of());
    }

    /**
     * Defines the {@code bff} client with the URIs it may be sent to after an OpenID Connect RP-initiated logout.
     *
     * @param redirectUris           redirect URIs of the deployment.
     * @param apiAudience            audience of the authorization server's own API.
     * @param postLogoutRedirectUris post-logout redirect URIs of the deployment; none when empty.
     * @return the client.
     */
    public static ClientRegistration bff(final List<String> redirectUris, final String apiAudience,
                                         final List<String> postLogoutRedirectUris) {
        return new ClientRegistration(BFF_CLIENT_ID, "Backend for Frontend", true,
                List.of("client_secret_basic"), List.of("authorization_code", "refresh_token"),
                redirectUris, SCOPES, true, List.of(RESOURCE_SERVER_AUDIENCE, apiAudience), ACCESS_TOKEN_TTL,
                REFRESH_TOKEN_TTL, ClientRegistration.FORMAT_SELF_CONTAINED, false,
                postLogoutRedirectUris, true, false, null, null, null, null, false);
    }

    /**
     * Defines the {@code spa-public} client: public, authorization code with PKCE, no client authentication.
     *
     * @param redirectUris redirect URIs of the deployment.
     * @param apiAudience  audience of the authorization server's own API.
     * @return the client.
     */
    public static ClientRegistration spaPublic(final List<String> redirectUris, final String apiAudience) {
        return spaPublic(redirectUris, apiAudience, List.of());
    }

    /**
     * Defines the {@code spa-public} client with the URIs it may be sent to after an OpenID Connect RP-initiated
     * logout.
     *
     * @param redirectUris           redirect URIs of the deployment.
     * @param apiAudience            audience of the authorization server's own API.
     * @param postLogoutRedirectUris post-logout redirect URIs of the deployment; none when empty.
     * @return the client.
     */
    public static ClientRegistration spaPublic(final List<String> redirectUris, final String apiAudience,
                                               final List<String> postLogoutRedirectUris) {
        return new ClientRegistration(SPA_PUBLIC_CLIENT_ID, "Single-page application", false,
                List.of("none"), List.of("authorization_code", "refresh_token"),
                redirectUris, SCOPES, true, List.of(RESOURCE_SERVER_AUDIENCE, apiAudience), ACCESS_TOKEN_TTL,
                REFRESH_TOKEN_TTL, ClientRegistration.FORMAT_SELF_CONTAINED, false,
                postLogoutRedirectUris, false, false, null, null, null, null, false);
    }

Steps

  1. The SPA generates a random code_verifier, derives code_challenge with S256, and keeps the verifier, a state and a nonce in the browser.

  2. It navigates to GET /oauth2/authorize?response_type=code&client_id=spa-public&redirect_uri=…​&scope=openid documents.read&state=…​&nonce=…​&code_challenge=…​&code_challenge_method=S256. The redirect URI must match a registered one exactly. A request without code_challenge is refused, because the client has requireProofKey true.

  3. The protocol chain sends the browser to /login; the user signs in with any offered method and, if the MFA policy asks for it, completes /login/second-factor (see Authentication methods inside the authorization code flow).

  4. spa-public has requireConsent false, so there is no consent page: the server redirects to the redirect URI with code and state.

  5. The SPA checks state and posts grant_type=authorization_code, code, redirect_uri, client_id=spa-public and code_verifier to /oauth2/token. Spring Authorization Server authenticates the public client through the PKCE verifier and answers with an access token and an ID token.

  6. The SPA validates the ID token (signature from /oauth2/jwks, iss, aud, nonce) and calls the resource server with the access token as a bearer token.

  7. When the access token expires the SPA runs the flow again: the server still has the user’s session, so the user normally does not see the login page again.

sequenceDiagram autonumber participant B as Browser SPA participant A as auth-server participant R as resource-server Note over B: code_verifier, state and nonce generated in the browser B->>A: GET /oauth2/authorize client_id=spa-public with code_challenge S256 A-->>B: 302 to /login B->>A: POST /login A-->>B: 302 back to /oauth2/authorize B->>A: GET /oauth2/authorize Note over A: requireConsent is false, no consent page A-->>B: 302 to redirect_uri with code and state B->>A: POST /oauth2/token with code, client_id and code_verifier A-->>B: access_token and id_token, no refresh_token B->>R: GET /api/documents with Bearer access token R-->>B: 200 documents
flowchart TD Start(["GET /oauth2/authorize client_id=spa-public"]) --> Pkce{"code_challenge present?"} Pkce -- "no" --> Error["Error, no code issued"] Pkce -- "yes" --> Uri{"redirect_uri registered?"} Uri -- "no" --> Error Uri -- "yes" --> Signed{"Signed in and MFA satisfied?"} Signed -- "no" --> Login["/login and /login/second-factor"] Login --> Code Signed -- "yes" --> Code["302 with code, no consent page"] Code --> Token{"code_verifier matches the challenge?"} Token -- "yes" --> Tokens["access_token and id_token"] Token -- "no" --> Grant["400 invalid_grant"]

Tokens and claims

  • Access token: the same JWT as for bff (sub, aud resource-server and auth-server, scope, roles, tenant, amr, acr, auth_time), 10 minutes.

  • ID token: sub, aud spa-public, azp, nonce, amr, acr (see OpenID Connect sign-in).

  • No refresh token. The registration lists the refresh_token grant, but Spring Authorization Server’s OAuth2RefreshTokenGenerator does not issue a refresh token to a public client in the authorization code grant, and this repository does not replace that generator. Refresh token rotation (Refresh token rotation and reuse detection) therefore applies to bff, not to spa-public.

Limits of this repository

  • No application uses spa-public, and no test runs the full code flow with it; the seeding is tested, and the public client path of the authorization endpoint is tested with clients registered by the tests.

  • No CORS configuration exists in auth-server, so a script served from another origin (such as http://localhost:3000) cannot read the answers of /oauth2/token or /userinfo from the browser. A real SPA on another origin would need CORS on those endpoints.

  • Tokens held by browser JavaScript are exposed to any script injected into the page. The Browser-Based Apps BCP recommends the BFF pattern instead, which is why the shipped SPA uses the bff.

How it is tested

  • auth-server/boot ConsentPageIT: a public client (none, PKCE required) goes through the authorization endpoint with an S256 challenge up to the code (consent is on for that test client).

  • auth-server/boot DevDataInitializerIT: the dev profile seeds spa-public.

  • auth-server/api/web ClientSeederTest and auth-server/application SeedRegisteredClientsUseCaseTest, DefaultClientDefinitionsTest: the client is seeded only when configured, with the expected settings.

  • e2e-tests LegacyImplicitEndToEndIT: an implicit (response_type=token) request made as spa-public is refused.

  • auth-server/boot ImplicitRejectedIT: the default server answers response_type=token with unsupported_response_type.