Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.6.0+

Azure Key Vault: Exchange a Workload Identity Token

See more Azure Key Vault Examples

Requires Chilkat 11.6.0 or later. Keep qa_data/azure/keyvault.json and token/PFX files private, outside web content and source control, with access restricted to the application identity. Use placeholders replaced by your configuration, never commit credentials. Create output directories before running file-writing examples. These examples target Azure public-cloud vaults, not Managed HSM or sovereign-cloud endpoints.

Configure an Entra federated identity credential with the exact external issuer, subject, and audience (commonly api://AzureADTokenExchange). Provide client_id and tenant_id. Have the trusted workload runtime supply a fresh projected JWT at qa_data/azure/federated-token.jwt before each exchange; adapt this path to your runtime-provided token path. The application forwards this assertion to the fixed tenant endpoint; Entra validates the federation trust. This does not mint or locally trust an arbitrary external token.

Microsoft reference

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
stringCompare: HCkStringBuilder;
config: HCkJsonObject;
stringMatch1: Boolean;
stringMatch2: Boolean;
validate5: HCkStringBuilder;
alphabet5: HCkStringBuilder;
charIndex5: Integer;
http: HCkHttp;
request: HCkHttpRequest;
response: HCkHttpResponse;
result: HCkJsonObject;
tokenUrl: HCkStringBuilder;
projected: HCkStringBuilder;
assertion: PWideChar;
tokenExpires: HCkDateTime;
stringMatch3: Boolean;
stringMatch4: Boolean;
validate8: HCkStringBuilder;
alphabet8: HCkStringBuilder;
charIndex8: Integer;
tokenLifetime: Integer;
tokenNow: HCkDateTime;
saved: HCkJsonObject;

begin
success := False;

stringCompare := CkStringBuilder_Create();
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Keep this configuration private, outside public web content and source control.
config := CkJsonObject_Create();
CkJsonObject_putStrict(config,True);
success := CkJsonObject_LoadFile(config,'qa_data/azure/keyvault.json');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not load private Key Vault configuration.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'client_id'));
stringMatch1 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'client_id') <> 1) or (stringMatch1 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid client_id.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(config,'tenant_id'));
stringMatch2 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(config,'tenant_id') <> 1) or (stringMatch2 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid tenant_id.');
    Exit;
  end;
validate5 := CkStringBuilder_Create();
alphabet5 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate5,CkJsonObject__stringOf(config,'tenant_id'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not validate tenant GUID.');
    Exit;
  end;
success := CkStringBuilder_Append(alphabet5,'0123456789abcdefABCDEF-');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not initialize validation.');
    Exit;
  end;
if ((CkStringBuilder_getLength(validate5) < 36) or (CkStringBuilder_getLength(validate5) > 36)) then
  begin
    Memo1.Lines.Add('Invalid tenant GUID length.');
    Exit;
  end;
charIndex5 := 0;
while (charIndex5 < CkStringBuilder_getLength(validate5)) do
  begin
    if (CkStringBuilder_Contains(alphabet5,CkStringBuilder__getRange(validate5,charIndex5,1,False),True) <> True) then
      begin
        Memo1.Lines.Add('Invalid character in tenant GUID.');
        Exit;
      end;
    charIndex5 := charIndex5 + 1;
  end;

http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putFollowRedirects(http,False);
CkHttp_putConnectTimeout(http,20);
CkHttp_putReadTimeout(http,60);
CkHttp_putVerboseLogging(http,False);
request := CkHttpRequest_Create();
CkHttpRequest_putHttpVerb(request,'GET');
response := CkHttpResponse_Create();
result := CkJsonObject_Create();
CkJsonObject_putStrict(result,True);
tokenUrl := CkStringBuilder_Create();
success := CkStringBuilder_Append(tokenUrl,'https://login.microsoftonline.com/');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(tokenUrl,CkJsonObject__stringOf(config,'tenant_id'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
success := CkStringBuilder_Append(tokenUrl,'/oauth2/v2.0/token');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not construct URL or identifier.');
    Exit;
  end;
//  Your trusted workload runtime must refresh this projected token file.
projected := CkStringBuilder_Create();
success := CkStringBuilder_LoadFile(projected,'qa_data/azure/federated-token.jwt','utf-8');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not read projected workload token.');
    Exit;
  end;
success := CkStringBuilder_Trim(projected);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not trim token.');
    Exit;
  end;
if (CkStringBuilder_getLength(projected) = 0) then
  begin
    Memo1.Lines.Add('Projected token is empty.');
    Exit;
  end;
assertion := CkStringBuilder__getAsString(projected);
CkHttpRequest_putHttpVerb(request,'POST');
CkHttpRequest_putContentType(request,'application/x-www-form-urlencoded');
CkHttpRequest_AddParam(request,'client_id',CkJsonObject__stringOf(config,'client_id'));
CkHttpRequest_AddParam(request,'scope','https://vault.azure.net/.default');
CkHttpRequest_AddParam(request,'grant_type','client_credentials');
CkHttpRequest_AddParam(request,'client_assertion_type','urn:ietf:params:oauth:client-assertion-type:jwt-bearer');
CkHttpRequest_AddParam(request,'client_assertion',assertion);
tokenExpires := CkDateTime_Create();
success := CkDateTime_SetFromCurrentSystemTime(tokenExpires);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not read time.');
    Exit;
  end;
success := CkHttp_HttpReq(http,CkStringBuilder__getAsString(tokenUrl),request,response);
if (success <> True) then
  begin
    Memo1.Lines.Add('Token exchange failed; inspect diagnostics privately.');
    Exit;
  end;
if (CkHttpResponse_getStatusCode(response) <> 200) then
  begin
    Memo1.Lines.Add('Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.');
    Exit;
  end;
success := CkJsonObject_Load(result,CkHttpResponse__bodyStr(response));
if (success <> True) then
  begin
    Memo1.Lines.Add('Invalid JSON response.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(result,'access_token'));
stringMatch3 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(result,'access_token') <> 1) or (stringMatch3 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid access_token.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(result,'token_type'));
stringMatch4 := CkStringBuilder_ContentsEqual(stringCompare,'Bearer',True);
if (stringMatch4 <> True) then
  begin
    Memo1.Lines.Add('Expected Bearer token.');
    Exit;
  end;
if ((CkJsonObject_JsonTypeOf(result,'expires_in') <> 2)) then
  begin
    Memo1.Lines.Add('Missing token lifetime.');
    Exit;
  end;
validate8 := CkStringBuilder_Create();
alphabet8 := CkStringBuilder_Create();
success := CkStringBuilder_Append(validate8,CkJsonObject__stringOf(result,'expires_in'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not validate token lifetime.');
    Exit;
  end;
success := CkStringBuilder_Append(alphabet8,'0123456789');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not initialize validation.');
    Exit;
  end;
if ((CkStringBuilder_getLength(validate8) < 1) or (CkStringBuilder_getLength(validate8) > 8)) then
  begin
    Memo1.Lines.Add('Invalid token lifetime length.');
    Exit;
  end;
charIndex8 := 0;
while (charIndex8 < CkStringBuilder_getLength(validate8)) do
  begin
    if (CkStringBuilder_Contains(alphabet8,CkStringBuilder__getRange(validate8,charIndex8,1,False),True) <> True) then
      begin
        Memo1.Lines.Add('Invalid character in token lifetime.');
        Exit;
      end;
    charIndex8 := charIndex8 + 1;
  end;

tokenLifetime := CkJsonObject_IntOf(result,'expires_in');
if ((tokenLifetime <= 60) or (tokenLifetime > 604800)) then
  begin
    Memo1.Lines.Add('Unexpected token lifetime.');
    Exit;
  end;
success := CkDateTime_AddSeconds(tokenExpires,tokenLifetime);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not calculate expiration.');
    Exit;
  end;
tokenNow := CkDateTime_Create();
success := CkDateTime_SetFromCurrentSystemTime(tokenNow);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not read time.');
    Exit;
  end;
if (CkDateTime_DiffSeconds(tokenExpires,tokenNow) <= 60) then
  begin
    Memo1.Lines.Add('Token expired before it could be saved.');
    Exit;
  end;
saved := CkJsonObject_Create();
success := CkJsonObject_UpdateString(saved,'access_token',CkJsonObject__stringOf(result,'access_token'));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not set access_token.');
    Exit;
  end;
success := CkJsonObject_UpdateString(saved,'expires_at',CkDateTime__getAsTimestamp(tokenExpires,False));
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not set expires_at.');
    Exit;
  end;
success := CkJsonObject_WriteFile(saved,'qa_data/tokens/azureKeyVaultToken.json');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not save the private token file.');
    Exit;
  end;
Memo1.Lines.Add('Example completed.');

CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(config);
CkStringBuilder_Dispose(validate5);
CkStringBuilder_Dispose(alphabet5);
CkHttp_Dispose(http);
CkHttpRequest_Dispose(request);
CkHttpResponse_Dispose(response);
CkJsonObject_Dispose(result);
CkStringBuilder_Dispose(tokenUrl);
CkStringBuilder_Dispose(projected);
CkDateTime_Dispose(tokenExpires);
CkStringBuilder_Dispose(validate8);
CkStringBuilder_Dispose(alphabet8);
CkDateTime_Dispose(tokenNow);
CkJsonObject_Dispose(saved);