Delphi SAML Single Sign-On

Plug your Delphi web application into the corporate identity provider. Users sign in once with Microsoft Entra ID, Okta, AD FS, Google Workspace or Keycloak, and your server receives a signed, fully validated identity.

TsgcSAMLServiceProvider

Implements the service provider side of the SAML 2.0 Web Browser SSO profile: it builds the AuthnRequest, publishes the SP metadata and validates the signed response that the browser posts back. Host the URLs in any HTTP server, such as TsgcWebSocketHTTPServer or TsgcHTTPServer.

Component class

TsgcSAMLServiceProvider (unit sgcAuth_SAML_SP)

Protocol

SAML 2.0 (OASIS)

Platforms

Windows, macOS, Linux, iOS, Android

Edition

Enterprise and All-Access, plus the sgcAuth pack. Also available in sgcWebSockets .NET.

Three URLs and one validation call

Set EntityID and AssertionConsumerServiceURL, load the IdP metadata, then serve the metadata, the login redirect and the Assertion Consumer Service from your HTTP handler.

uses
  sgcAuth_SAML_SP;

// SAML is a form field: SAML: TsgcSAMLServiceProvider;
procedure TForm1.ConfigureSAML(const aIdPMetadataXML: string);
begin
  SAML := TsgcSAMLServiceProvider.Create(nil);
  SAML.EntityID := 'https://app.example.com/saml/metadata';
  SAML.AssertionConsumerServiceURL := 'https://app.example.com/saml/acs';
  // entity ID, SSO URL, binding and signing certificates of the IdP
  SAML.LoadIdPMetadata(aIdPMetadataXML);
end;

// GET /saml/metadata: return SAML.GetMetadata and register it in the IdP

// GET /saml/login: redirect the browser to the IdP
function TForm1.LoginURL(const aRelayState: string;
  out aRequestID: string): string;
begin
  // keep aRequestID for this RelayState, the ACS needs it
  Result := SAML.GetAuthnRequestRedirectURL(aRelayState, aRequestID);
end;

// POST /saml/acs: validate the signed response posted by the browser
function TForm1.ValidateResponse(const aSAMLResponse, aRelayState,
  aRequestID: string): string;
var
  oResult: TsgcSAMLResult;
begin
  oResult := TsgcSAMLResult.Create;
  try
    if SAML.ProcessResponse(aSAMLResponse, aRelayState, aRequestID, oResult) then
      Result := oResult.NameID // plus oResult.Attributes and SessionIndex
    else
      raise Exception.Create(oResult.ErrorMessage);
  finally
    oResult.Free;
  end;
end;
// uses: sgcAuth_SAML_SP
TsgcSAMLServiceProvider *SAML = new TsgcSAMLServiceProvider(this);
SAML->EntityID = "https://app.example.com/saml/metadata";
SAML->AssertionConsumerServiceURL = "https://app.example.com/saml/acs";
SAML->LoadIdPMetadata(IdPMetadataXML);

// GET /saml/login
String RequestID;
String URL = SAML->GetAuthnRequestRedirectURL(RelayState, RequestID);

// POST /saml/acs
TsgcSAMLResult *SAMLResult = new TsgcSAMLResult();
if (SAML->ProcessResponse(SAMLResponse, RelayState, RequestID, SAMLResult))
  ShowMessage(SAMLResult->NameID);
delete SAMLResult;

What's inside

A service provider that is easy to wire up and strict about what it accepts, because a SAML bug is a login bypass.

Metadata both ways

GetMetadata produces the SP metadata to register in the identity provider. LoadIdPMetadata reads the IdP metadata and fills IdPEntityID, IdPSSOURL, IdPSSOBinding and IdPCertificates.

Redirect and POST bindings

GetAuthnRequestRedirectURL returns the HTTP-Redirect URL with the deflated AuthnRequest, GetAuthnRequestPostForm an auto-submitted HTTP-POST form. SignAuthnRequests signs them with SPCertificate and SPPrivateKey.

Trusted keys only

Signatures are checked only against IdPCertificates, a certificate embedded in the message is never trusted. RSA-SHA256, RSA-SHA384 and RSA-SHA512 with exclusive canonicalization. SHA-1 is rejected unless AllowSHA1 is set.

Signature wrapping protection

The response must hold exactly one assertion as a direct child, and the signature must reference that element, which defeats the XML signature wrapping attacks that broke many SAML libraries.

Full assertion checks

ProcessResponse validates issuer, audience, destination, InResponseTo and the validity window with ClockSkew and MaxAssertionAge, and keeps a replay cache of assertion IDs. IdP-initiated sign in stays off until you set AllowIdPInitiated.

Clean result object

TsgcSAMLResult returns NameID, NameIDFormat, SessionIndex, AuthnInstant, the Attributes with their friendly names, and a readable ErrorMessage when something is wrong.

Specifications & references

Authoritative sources for the standards this component implements.

Documentation & Demos

Deep-link to the component reference, grab the ready-to-run demo project, and download the trial.

Online Help: TsgcSAMLServiceProvider Full property, method and event reference for this component.
Demo Project: Demos\26.Authentication\03.SAML_ServiceProvider A complete service provider with login, ACS and metadata URLs on a TsgcWebSocketHTTPServer. Ships inside the sgcWebSockets package, download the trial below.
Technical Document (PDF) Features, quick start, code samples for Delphi & C++ Builder and primary-source references for this component only.
User Manual (PDF) Comprehensive manual covering every component in the library.
Blog: SAML Single Sign-On in Delphi With Entra ID, Okta and AD FS Step-by-step setup with the three most common identity providers.
Best value: All-AccessEvery eSeGeCe product, Premium Support included, from €1,059/year.
See All-Access pricing

Ready to Add SAML Single Sign-On?

Download the free trial and connect your Delphi web application to the identity provider your customers already use.