AWS STS: Exchange a SAML Assertion for Role Credentials
See more AWS Security Token Service Examples
Uses the AWS STS Query API version 2011-06-15 and a verified TLS connection to the commercial-partition regional endpoint sts.us-west-2.amazonaws.com. Change the endpoint and signing region together for your deployment. Assumes Chilkat is unlocked.
Keep qa_data/aws/sts.json private and outside web roots/source control. Signed examples require nonempty access_key and secret_key strings; temporary source credentials also require session_token and ISO 8601 expiration. Federation exchanges load their specific fields without AWS keys. The examples do not print credentials, bearer tokens, full responses or diagnostic dumps. Credential-producing examples build a complete in-memory session object with access_key, secret_key, session_token and expiration; integrate that object into your application before it goes out of scope.
Provide role_arn, principal_arn for the IAM SAML provider, and saml_assertion containing the complete base64-encoded SAML response supplied by your IdP. Do not base64-encode it a second time. Uses unsigned POST with no AWS keys. The IdP metadata, role claims and trust must already be configured. STS verifies the assertion. The session can end sooner than the requested hour due to SessionNotOnOrAfter. IAM Identity Center AWSReservedSSO_ roles are not supported by this operation.
AWS references: AssumeRoleWithSAML
Chilkat Delphi DLL Downloads
var
success: Boolean;
stringCompare: HCkStringBuilder;
credentials: HCkJsonObject;
expires: HCkDateTime;
now: HCkDateTime;
region: PWideChar;
stringMatch1: Boolean;
stringMatch2: Boolean;
stringMatch3: Boolean;
endpoint: HCkStringBuilder;
rest: HCkRest;
connection: HCkSocket;
xml: HCkXml;
value: PWideChar;
accessKey: PWideChar;
secretKey: PWideChar;
sessionToken: PWideChar;
expiration: PWideChar;
responseXml: PWideChar;
stringMatch4: Boolean;
stringMatch5: Boolean;
stringMatch6: Boolean;
stringMatch7: Boolean;
stringMatch8: Boolean;
stringMatch9: Boolean;
stringMatch10: Boolean;
stringMatch11: Boolean;
session: HCkJsonObject;
begin
success := False;
stringCompare := CkStringBuilder_Create();
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this file private and outside public web content/source control.
credentials := CkJsonObject_Create();
CkJsonObject_putStrict(credentials,True);
success := CkJsonObject_LoadFile(credentials,'qa_data/aws/sts.json');
if (success <> True) then
begin
Memo1.Lines.Add('Could not load private STS configuration.');
Exit;
end;
expires := CkDateTime_Create();
now := CkDateTime_Create();
region := 'us-west-2';
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(credentials,'role_arn'));
stringMatch1 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'role_arn') <> 1) or (stringMatch1 = True)) then
begin
Memo1.Lines.Add('Missing or invalid role_arn.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(credentials,'principal_arn'));
stringMatch2 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'principal_arn') <> 1) or (stringMatch2 = True)) then
begin
Memo1.Lines.Add('Missing or invalid principal_arn.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(credentials,'saml_assertion'));
stringMatch3 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'saml_assertion') <> 1) or (stringMatch3 = True)) then
begin
Memo1.Lines.Add('Missing or invalid saml_assertion.');
Exit;
end;
endpoint := CkStringBuilder_Create();
CkStringBuilder_Append(endpoint,'sts.');
CkStringBuilder_Append(endpoint,region);
CkStringBuilder_Append(endpoint,'.amazonaws.com');
rest := CkRest_Create();
CkRest_putConnectTimeoutMs(rest,20000);
CkRest_putIdleTimeoutMs(rest,60000);
CkRest_putVerboseLogging(rest,False);
// Keep this verified Socket alive while Rest uses its connection.
connection := CkSocket_Create();
CkSocket_putRequireSslCertVerify(connection,True);
CkSocket_putMaxReadIdleMs(connection,60000);
CkSocket_putMaxSendIdleMs(connection,60000);
success := CkSocket_Connect(connection,CkStringBuilder__getAsString(endpoint),443,True,20000);
if (success <> True) then
begin
Memo1.Lines.Add('TLS connection failed; inspect diagnostics privately.');
Exit;
end;
success := CkRest_UseConnection(rest,connection,True);
if (success <> True) then
begin
Memo1.Lines.Add('Could not attach TLS connection.');
Exit;
end;
xml := CkXml_Create();
CkXml_putStrict(xml,True);
responseXml := '';
success := CkRest_ClearAllQueryParams(rest);
if (success <> True) then
begin
Memo1.Lines.Add('Could not reset request parameters.');
Exit;
end;
success := CkRest_AddQueryParam(rest,'Action','AssumeRoleWithSAML');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set Action.');
Exit;
end;
success := CkRest_AddQueryParam(rest,'Version','2011-06-15');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set Version.');
Exit;
end;
success := CkRest_AddQueryParam(rest,'RoleArn',CkJsonObject__stringOf(credentials,'role_arn'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not set RoleArn.');
Exit;
end;
success := CkRest_AddQueryParam(rest,'PrincipalArn',CkJsonObject__stringOf(credentials,'principal_arn'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not set PrincipalArn.');
Exit;
end;
success := CkRest_AddQueryParam(rest,'SAMLAssertion',CkJsonObject__stringOf(credentials,'saml_assertion'));
if (success <> True) then
begin
Memo1.Lines.Add('Could not set SAMLAssertion.');
Exit;
end;
success := CkRest_AddQueryParam(rest,'DurationSeconds','3600');
if (success <> True) then
begin
Memo1.Lines.Add('Could not set DurationSeconds.');
Exit;
end;
responseXml := CkRest__fullRequestFormUrlEncoded(rest,'POST','/');
if (CkRest_getLastMethodSuccess(rest) <> True) then
begin
Memo1.Lines.Add('STS transport failed; inspect diagnostics privately.');
Exit;
end;
if (CkRest_getResponseStatusCode(rest) <> 200) then
begin
Memo1.Lines.Add('STS HTTP status: ' + IntToStr(CkRest_getResponseStatusCode(rest)));
success := CkXml_LoadXml(xml,responseXml);
if (success = True) then
begin
value := CkXml__getChildContent(xml,'Error|Code');
if (CkXml_getLastMethodSuccess(xml) = True) then
begin
Memo1.Lines.Add('AWS error code: ' + value);
end;
value := CkXml__getChildContent(xml,'RequestId');
if (CkXml_getLastMethodSuccess(xml) = True) then
begin
Memo1.Lines.Add('AWS request ID: ' + value);
end;
end;
Exit;
end;
success := CkXml_LoadXml(xml,responseXml);
if (success <> True) then
begin
Memo1.Lines.Add('Invalid STS XML response.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkXml__tag(xml));
stringMatch4 := CkStringBuilder_ContentsEqual(stringCompare,'AssumeRoleWithSAMLResponse',True);
if (stringMatch4 <> True) then
begin
Memo1.Lines.Add('Unexpected STS response root.');
Exit;
end;
value := CkXml__getChildContent(xml,'ResponseMetadata|RequestId');
CkStringBuilder_SetString(stringCompare,value);
stringMatch5 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkXml_getLastMethodSuccess(xml) <> True) or (stringMatch5 = True)) then
begin
Memo1.Lines.Add('Missing or empty ResponseMetadata|RequestId.');
Exit;
end;
if (CkXml_HasChildWithTag(xml,'AssumeRoleWithSAMLResult') <> True) then
begin
Memo1.Lines.Add('Missing STS result.');
Exit;
end;
accessKey := CkXml__getChildContent(xml,'AssumeRoleWithSAMLResult|Credentials|AccessKeyId');
CkStringBuilder_SetString(stringCompare,accessKey);
stringMatch6 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkXml_getLastMethodSuccess(xml) <> True) or (stringMatch6 = True)) then
begin
Memo1.Lines.Add('Missing or empty AssumeRoleWithSAMLResult|Credentials|AccessKeyId.');
Exit;
end;
secretKey := CkXml__getChildContent(xml,'AssumeRoleWithSAMLResult|Credentials|SecretAccessKey');
CkStringBuilder_SetString(stringCompare,secretKey);
stringMatch7 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkXml_getLastMethodSuccess(xml) <> True) or (stringMatch7 = True)) then
begin
Memo1.Lines.Add('Missing or empty AssumeRoleWithSAMLResult|Credentials|SecretAccessKey.');
Exit;
end;
sessionToken := CkXml__getChildContent(xml,'AssumeRoleWithSAMLResult|Credentials|SessionToken');
CkStringBuilder_SetString(stringCompare,sessionToken);
stringMatch8 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkXml_getLastMethodSuccess(xml) <> True) or (stringMatch8 = True)) then
begin
Memo1.Lines.Add('Missing or empty AssumeRoleWithSAMLResult|Credentials|SessionToken.');
Exit;
end;
expiration := CkXml__getChildContent(xml,'AssumeRoleWithSAMLResult|Credentials|Expiration');
CkStringBuilder_SetString(stringCompare,expiration);
stringMatch9 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkXml_getLastMethodSuccess(xml) <> True) or (stringMatch9 = True)) then
begin
Memo1.Lines.Add('Missing or empty AssumeRoleWithSAMLResult|Credentials|Expiration.');
Exit;
end;
success := CkDateTime_SetFromTimestamp(expires,expiration);
if (success <> True) then
begin
Memo1.Lines.Add('Returned expiration is invalid.');
Exit;
end;
success := CkDateTime_SetFromCurrentSystemTime(now);
if (success <> True) then
begin
Memo1.Lines.Add('Could not read current time.');
Exit;
end;
if (CkDateTime_DiffSeconds(expires,now) <= 60) then
begin
Memo1.Lines.Add('Returned credentials are expired or expire within 60 seconds; obtain fresh credentials.');
Exit;
end;
value := CkXml__getChildContent(xml,'AssumeRoleWithSAMLResult|AssumedRoleUser|Arn');
CkStringBuilder_SetString(stringCompare,value);
stringMatch10 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkXml_getLastMethodSuccess(xml) <> True) or (stringMatch10 = True)) then
begin
Memo1.Lines.Add('Missing or empty AssumeRoleWithSAMLResult|AssumedRoleUser|Arn.');
Exit;
end;
value := CkXml__getChildContent(xml,'AssumeRoleWithSAMLResult|AssumedRoleUser|AssumedRoleId');
CkStringBuilder_SetString(stringCompare,value);
stringMatch11 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkXml_getLastMethodSuccess(xml) <> True) or (stringMatch11 = True)) then
begin
Memo1.Lines.Add('Missing or empty AssumeRoleWithSAMLResult|AssumedRoleUser|AssumedRoleId.');
Exit;
end;
session := CkJsonObject_Create();
success := CkJsonObject_UpdateString(session,'access_key',accessKey);
if (success <> True) then
begin
Memo1.Lines.Add('Could not build complete session.');
Exit;
end;
success := CkJsonObject_UpdateString(session,'secret_key',secretKey);
if (success <> True) then
begin
Memo1.Lines.Add('Could not build complete session.');
Exit;
end;
success := CkJsonObject_UpdateString(session,'session_token',sessionToken);
if (success <> True) then
begin
Memo1.Lines.Add('Could not build complete session.');
Exit;
end;
success := CkJsonObject_UpdateString(session,'expiration',expiration);
if (success <> True) then
begin
Memo1.Lines.Add('Could not build complete session.');
Exit;
end;
// Use session in memory; do not print or log session.Emit().
Memo1.Lines.Add('Session validated; expiration: ' + expiration);
Memo1.Lines.Add('Example completed.');
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkDateTime_Dispose(expires);
CkDateTime_Dispose(now);
CkStringBuilder_Dispose(endpoint);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
CkXml_Dispose(xml);
CkJsonObject_Dispose(session);