By Admin on Wednesday, 24 April 2024
Category: All

Delphi PKCE OAuth2

​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 is supported on OAuth2 Server and Client Components.

What is PKCE

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:

  1. The client application generates a code verifier and derives the code challenge from it.
  2. The client application initiates the OAuth authorization request, including the code challenge and the code challenge method (plain or S256).
  3. The authorization server sends the authorization code to the client after the user grants access.
  4. When the client application sends the authorization code to the token endpoint to exchange it for an access token, it also includes the code verifier.
  5. The authorization server verifies the code verifier by applying the same transformation method used to create the code challenge and checks whether it matches the stored code challenge.
  6. If the verification is successful, the authorization server issues the access token; otherwise, the request is rejected.

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.

Delphi OAuth2 Client

​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.

Delphi OAuth2 Server

​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.

https://www.esegece.com/help/sgcWebSockets/#t=Components%2FHTTP%2FAuthorization%2FOAuth2%2Fserver%2FQuickStart%2FOAuth2_Server_Example.htm

Related Posts