The Authorization Code with PKCE (Proof Key for Code Exchange) grant extends the standard Authorization Code flow with an additional security layer. It is designed for native applications, mobile applications, and single-page applications (SPAs) where the client secret cannot be stored securely. Instead of relying on a client secret, the client generates a cryptographic code verifier and challenge that prove the entity exchanging the authorization code is the same entity that initiated the flow.
code_challenge and code_challenge_method=S256 with the authorization request.code_verifier with the token exchange request.SHA256(code_verifier) == code_challenge before issuing the token.This prevents authorization code interception attacks because an attacker who intercepts the code cannot exchange it without the original code_verifier.
| Property | Description |
|---|---|
OAuth2Options.GrantType |
Set to auth2CodePKCE. |
OAuth2Options.ClientId |
The client identifier issued by the authorization server. |
OAuth2Options.ClientSecret |
Optional. Some providers require it even with PKCE; others do not. |
AuthorizationServerOptions.AuthURL |
The authorization endpoint URL where the user is redirected to authenticate. |
AuthorizationServerOptions.TokenURL |
The token endpoint URL where the authorization code is exchanged for an access token. |
AuthorizationServerOptions.Scope |
The scope of the access request. |
LocalServerOptions.IP |
The IP address of the local redirect server (e.g., 127.0.0.1). |
LocalServerOptions.Port |
The port of the local redirect server. Set to 0 for a random available port. |
OAuth2.OAuth2Options.GrantType := auth2CodePKCE;
OAuth2.OAuth2Options.ClientId := 'your-client-id';
OAuth2.AuthorizationServerOptions.AuthURL := 'https://provider.com/oauth2/authorize';
OAuth2.AuthorizationServerOptions.TokenURL := 'https://provider.com/oauth2/token';
OAuth2.AuthorizationServerOptions.Scope.Text := 'openid profile';
OAuth2.LocalServerOptions.IP := '127.0.0.1';
OAuth2.LocalServerOptions.Port := 0;
OAuth2.Start;
When LocalServerOptions.Port is set to 0, the component automatically selects a random available port. This is recommended for desktop and mobile applications because it avoids port conflicts. The selected port is included in the redirect URI sent to the authorization server.
PKCE flows typically return a refresh token. Use the Refresh method to obtain a new access token:
OAuth2.Refresh('your-refresh-token');
Use the Authorization Code with PKCE grant for: