From sgcWebSockets 2024.5.0 PKCE, which stands for "Proof of Key Code Exchange" is an extension of the OAuth 2.0 protocol that helps prevent code interception attacks.
PKCE (Proof Key for Code Exchange) is an OAuth 2.0 security enhancement designed to protect against authorization code interception attacks in public or native applications. It is detailed in RFC 7636 and serves as a mitigation technique against the "authorization code interception" vulnerability, particularly in environments where client secrets cannot be reliably kept confidential, such as in mobile or client-side applications.
In a typical OAuth 2.0 Authorization Code Grant flow, a client application obtains an authorization code by redirecting the user to an authorization server, and then exchanges the code for an access token. However, if an attacker intercepts the authorization code, they could potentially use it to obtain the access token and access protected resources.
PKCE addresses this risk by introducing a proof key mechanism. The key components of PKCE are:
The PKCE flow works as follows:
This mechanism ensures that only the client with the original code verifier can successfully exchange the authorization code for an access token, providing a robust layer of security in OAuth flows.
The TsgcHTTP_OAuth2_Client component supports the Authorization Code + PKCE Flow, in order to use this authorization type, set the property GrantType to the value auth2CodePKCE.
oAuth2 := TsgcHTTP2_OAuth2.Create(nil); oAuth2.LocalServerOptions.Host := '127.0.0.1'; oAuth2.LocalServerOptions.Port := 8080; oAuth2.AuthorizationServerOptions.AuthURL := 'https://accounts.google.com/o/oauth2/auth'; oAuth2.AuthorizationServerOptions.TokenURL := 'https://accounts.google.com/o/oauth2/token'; oAuth2.AuthorizationServerOptions.Scope.Add('https://mail.google.com/'); oAuth2.OAuth2Options.ClientId := '180803918357-eqjtn20gqfhcs6gjkebbrrenh022mqqc.apps.googleusercontent.com'; oAuth2.OAuth2Options.ClientSecret := '_by0iYYrvVHxC2Z8TbtNEYQN'; oAuth2.OAuth2Options.GrantType := auth2CodePKCE; procedure OnOAuth2AfterAccessToken(Sender: TObject; const Access_Token, Token_Type, Expires_In, Refresh_Token, Scope, RawParams: string; var Handled: Boolean); begin <...> <...> end; oAuth2.OnAfterAccessToken := OnOAuth2AfterAccessToken; oAuth2.Start;
The TsgcHTTP_OAuth2_Server component supports PKCE by default (although can be disabled in the property OAuth2Options.PKCE). When the server detects the OAuth2 Client is using PKCE, it will validate the PKCE values are valid, if not, the response will return an error.
Find below a link about how to use the Delphi OAuth2 Server.
When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.