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.
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.
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.
TsgcSAMLServiceProvider (unit sgcAuth_SAML_SP)
Windows, macOS, Linux, iOS, Android
Enterprise and All-Access, plus the sgcAuth pack. Also available in sgcWebSockets .NET.
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;
A service provider that is easy to wire up and strict about what it accepts, because a SAML bug is a login bypass.
GetMetadata produces the SP metadata to register in the identity provider. LoadIdPMetadata reads the IdP metadata and fills IdPEntityID, IdPSSOURL, IdPSSOBinding and IdPCertificates.
GetAuthnRequestRedirectURL returns the HTTP-Redirect URL with the deflated AuthnRequest, GetAuthnRequestPostForm an auto-submitted HTTP-POST form. SignAuthnRequests signs them with SPCertificate and SPPrivateKey.
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.
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.
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.
TsgcSAMLResult returns NameID, NameIDFormat, SessionIndex, AuthnInstant, the Attributes with their friendly names, and a readable ErrorMessage when something is wrong.
Authoritative sources for the standards this component implements.
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. | Open | |
| 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. | Open | |
| Technical Document (PDF) Features, quick start, code samples for Delphi & C++ Builder and primary-source references for this component only. | Open | |
| User Manual (PDF) Comprehensive manual covering every component in the library. | Open | |
| 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. | Open |