Cliente OAuth2 para Delphi

Componente cliente OAuth 2.0 / OAuth 2.1 — Authorization Code con PKCE, Client Credentials, Device Code, rotación de refresh tokens y flujo de navegador integrado.

TsgcHTTP_OAuth2_Client

Implementa el lado cliente de OAuth 2.0 / 2.1 — cubre los flujos Authorization Code (con PKCE), Client Credentials, Device Code y refresh token. Incluye un pequeño listener HTTP embebido para la URI de redirección.

Clase del componente

TsgcHTTP_OAuth2_Client

Plataformas

Windows, macOS, Linux, iOS, Android

Edición

Standard / Professional / Enterprise

Configura el proveedor e inicia el flujo

Configura ClientId / ClientSecret / Scope / Endpoints (o usa un preset de Provider para Google / Microsoft / GitHub), llama a Start y usa Token en tus llamadas HTTP.

uses
  sgcHTTP, sgcHTTP_OAuth_Types;

// OAuth2 is a form field: OAuth2: TsgcHTTP_OAuth2_Client;
procedure TForm1.Authorize;
begin
  OAuth2 := TsgcHTTP_OAuth2_Client.Create(nil);
  OAuth2.OnAfterAccessToken := OnAfterAccessTokenEvent;

  OAuth2.OAuth2Options.GrantType    := auth2CodePKCE;
  OAuth2.OAuth2Options.ClientId     := 'your-client-id';
  OAuth2.OAuth2Options.ClientSecret := 'your-client-secret';

  OAuth2.AuthorizationServerOptions.AuthURL  := 'https://auth.example.com/authorize';
  OAuth2.AuthorizationServerOptions.TokenURL := 'https://auth.example.com/token';
  OAuth2.AuthorizationServerOptions.Scope.Clear;
  OAuth2.AuthorizationServerOptions.Scope.Add('profile');
  OAuth2.AuthorizationServerOptions.Scope.Add('email');

  OAuth2.LocalServerOptions.IP          := '127.0.0.1';
  OAuth2.LocalServerOptions.Port        := 5555;
  OAuth2.LocalServerOptions.RedirectURL := 'http://localhost:5555/callback';

  // Spawns the user browser at the auth URL and returns immediately
  OAuth2.Start;
end;

// ... the user logs in, the browser is redirected to the callback URL,
// ... the component captures the code and exchanges it for tokens
procedure TForm1.OnAfterAccessTokenEvent(Sender: TObject; const Access_Token,
  Token_Type, Expires_In, Refresh_Token, Scope, RawParams: String;
  var Handled: Boolean);
begin
  ShowMessage(Access_Token);
end;
// uses: sgcHTTP, sgcHTTP_OAuth_Types
TsgcHTTP_OAuth2_Client *OAuth2 = new TsgcHTTP_OAuth2_Client(this);
OAuth2->OnAfterAccessToken = OnAfterAccessTokenEvent;

OAuth2->OAuth2Options->GrantType    = auth2CodePKCE;
OAuth2->OAuth2Options->ClientId     = "your-client-id";
OAuth2->OAuth2Options->ClientSecret = "your-client-secret";

OAuth2->AuthorizationServerOptions->AuthURL  = "https://auth.example.com/authorize";
OAuth2->AuthorizationServerOptions->TokenURL = "https://auth.example.com/token";
OAuth2->AuthorizationServerOptions->Scope->Clear();
OAuth2->AuthorizationServerOptions->Scope->Add("profile");
OAuth2->AuthorizationServerOptions->Scope->Add("email");

OAuth2->LocalServerOptions->IP          = "127.0.0.1";
OAuth2->LocalServerOptions->Port        = 5555;
OAuth2->LocalServerOptions->RedirectURL = "http://localhost:5555/callback";

OAuth2->Start();

Qué incluye

Implementa la superficie cliente de OAuth 2.0 con PKCE, rotación de refresh tokens y un listener de redirección loopback embebido.

Flows

Authorization Code (con PKCE opcional según RFC 7636), Client Credentials (RFC 6749 §4.4), Device Code (RFC 8628) y Refresh Token (RFC 6749 §6) están soportados como métodos tipados.

PKCE por defecto

Asigne auth2CodePKCE a OAuth2Options.GrantType, el componente genera el par code_verifier / code_challenge y los incluye según la RFC 7636 (obligatorio en OAuth 2.1).

Preajustes de proveedor

Los componentes hermanos TsgcHTTP_OAuth2_Client_Google y TsgcHTTP_OAuth2_Client_Microsoft incluyen los endpoints y scopes correctos ya rellenados. Usa la clase base para el resto de proveedores.

Listener de redirección loopback

El componente levanta un pequeño listener HTTP embebido en la RedirectURL configurada, captura el authorization code y después cierra el listener — no hace falta ningún servidor web externo.

Persistencia del token

Lea CurrentRefreshToken tras la autorización y guárdelo usted mismo, luego llame a Refresh en el siguiente arranque para que no se vuelva a pedir la autenticación al usuario.

Drop-in to HTTP / WebSocket

Combínalo con TsgcHTTPClient, TsgcHTTP2Client o TsgcWebSocketClient mediante Authentication.Token.OAuth — el bearer token se inyecta automáticamente en cada petición.

Especificaciones y referencias

Fuentes autorizadas de los estándares que implementa este componente.

Documentación y Demos

Enlace directo a la referencia del componente, descarga el proyecto demo listo para ejecutar y la prueba gratuita.

Online Help — TsgcHTTP_OAuth2_Client Referencia completa de propiedades, métodos y eventos de este componente.
Demo Project — Demos\20.HTTP_Protocol\02.OAuth2_Authentication Proyecto de ejemplo listo para ejecutar. Se incluye en el paquete sgcWebSockets — descarga la prueba gratuita más abajo.
Documento técnico (PDF) Características, inicio rápido, ejemplos de código para Delphi y C++ Builder y referencias de fuentes primarias — solo este componente.
Manual de usuario (PDF) Manual completo que cubre todos los componentes de la biblioteca.
La mejor opción: All-AccessTodos los productos de eSeGeCe, con Premium Support incluido, desde €1,059 al año.
Ver precios de All-Access

Ready to Add OAuth 2.0 to Delphi?

Descarga la prueba gratuita e integra la autenticación OAuth 2.0 / 2.1 en tus aplicaciones Delphi.