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), |
Client |
|
Preconditions |
|
Endpoints |
|
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
-
The SPA generates a random
code_verifier, derivescode_challengewith S256, and keeps the verifier, astateand anoncein the browser. -
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 withoutcode_challengeis refused, because the client hasrequireProofKeytrue. -
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). -
spa-publichasrequireConsentfalse, so there is no consent page: the server redirects to the redirect URI withcodeandstate. -
The SPA checks
stateand postsgrant_type=authorization_code,code,redirect_uri,client_id=spa-publicandcode_verifierto/oauth2/token. Spring Authorization Server authenticates the public client through the PKCE verifier and answers with an access token and an ID token. -
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. -
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.
Tokens and claims
-
Access token: the same JWT as for
bff(sub,audresource-serverandauth-server,scope,roles,tenant,amr,acr,auth_time), 10 minutes. -
ID token:
sub,audspa-public,azp,nonce,amr,acr(see OpenID Connect sign-in). -
No refresh token. The registration lists the
refresh_tokengrant, but Spring Authorization Server’sOAuth2RefreshTokenGeneratordoes 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 tobff, not tospa-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 ashttp://localhost:3000) cannot read the answers of/oauth2/tokenor/userinfofrom 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/bootConsentPageIT: 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/bootDevDataInitializerIT: thedevprofile seedsspa-public. -
auth-server/api/webClientSeederTestandauth-server/applicationSeedRegisteredClientsUseCaseTest,DefaultClientDefinitionsTest: the client is seeded only when configured, with the expected settings. -
e2e-testsLegacyImplicitEndToEndIT: an implicit (response_type=token) request made asspa-publicis refused. -
auth-server/bootImplicitRejectedIT: the default server answersresponse_type=tokenwithunsupported_response_type.