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 Unicode C++ Downloads
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkDateTimeW.h>
#include <CkRestW.h>
#include <CkSocketW.h>
#include <CkXmlW.h>
void ChilkatSample(void)
{
bool success = false;
CkStringBuilderW stringCompare;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this file private and outside public web content/source control.
CkJsonObjectW credentials;
credentials.put_Strict(true);
success = credentials.LoadFile(L"qa_data/aws/sts.json");
if (success != true) {
wprintf(L"Could not load private STS configuration.\n");
return;
}
CkDateTimeW expires;
CkDateTimeW now;
const wchar_t *region = L"us-west-2";
stringCompare.SetString(credentials.stringOf(L"role_arn"));
bool stringMatch1 = stringCompare.ContentsEqual(L"",true);
if ((credentials.JsonTypeOf(L"role_arn") != 1) || (stringMatch1 == true)) {
wprintf(L"Missing or invalid role_arn.\n");
return;
}
stringCompare.SetString(credentials.stringOf(L"principal_arn"));
bool stringMatch2 = stringCompare.ContentsEqual(L"",true);
if ((credentials.JsonTypeOf(L"principal_arn") != 1) || (stringMatch2 == true)) {
wprintf(L"Missing or invalid principal_arn.\n");
return;
}
stringCompare.SetString(credentials.stringOf(L"saml_assertion"));
bool stringMatch3 = stringCompare.ContentsEqual(L"",true);
if ((credentials.JsonTypeOf(L"saml_assertion") != 1) || (stringMatch3 == true)) {
wprintf(L"Missing or invalid saml_assertion.\n");
return;
}
CkStringBuilderW endpoint;
endpoint.Append(L"sts.");
endpoint.Append(region);
endpoint.Append(L".amazonaws.com");
CkRestW rest;
rest.put_ConnectTimeoutMs(20000);
rest.put_IdleTimeoutMs(60000);
rest.put_VerboseLogging(false);
// Keep this verified Socket alive while Rest uses its connection.
CkSocketW connection;
connection.put_RequireSslCertVerify(true);
connection.put_MaxReadIdleMs(60000);
connection.put_MaxSendIdleMs(60000);
success = connection.Connect(endpoint.getAsString(),443,true,20000);
if (success != true) {
wprintf(L"TLS connection failed; inspect diagnostics privately.\n");
return;
}
success = rest.UseConnection(connection,true);
if (success != true) {
wprintf(L"Could not attach TLS connection.\n");
return;
}
CkXmlW xml;
xml.put_Strict(true);
const wchar_t *value = 0;
const wchar_t *accessKey = 0;
const wchar_t *secretKey = 0;
const wchar_t *sessionToken = 0;
const wchar_t *expiration = 0;
const wchar_t *responseXml = L"";
success = rest.ClearAllQueryParams();
if (success != true) {
wprintf(L"Could not reset request parameters.\n");
return;
}
success = rest.AddQueryParam(L"Action",L"AssumeRoleWithSAML");
if (success != true) {
wprintf(L"Could not set Action.\n");
return;
}
success = rest.AddQueryParam(L"Version",L"2011-06-15");
if (success != true) {
wprintf(L"Could not set Version.\n");
return;
}
success = rest.AddQueryParam(L"RoleArn",credentials.stringOf(L"role_arn"));
if (success != true) {
wprintf(L"Could not set RoleArn.\n");
return;
}
success = rest.AddQueryParam(L"PrincipalArn",credentials.stringOf(L"principal_arn"));
if (success != true) {
wprintf(L"Could not set PrincipalArn.\n");
return;
}
success = rest.AddQueryParam(L"SAMLAssertion",credentials.stringOf(L"saml_assertion"));
if (success != true) {
wprintf(L"Could not set SAMLAssertion.\n");
return;
}
success = rest.AddQueryParam(L"DurationSeconds",L"3600");
if (success != true) {
wprintf(L"Could not set DurationSeconds.\n");
return;
}
responseXml = rest.fullRequestFormUrlEncoded(L"POST",L"/");
if (rest.get_LastMethodSuccess() != true) {
wprintf(L"STS transport failed; inspect diagnostics privately.\n");
return;
}
if (rest.get_ResponseStatusCode() != 200) {
wprintf(L"STS HTTP status: %d\n",rest.get_ResponseStatusCode());
success = xml.LoadXml(responseXml);
if (success == true) {
value = xml.getChildContent(L"Error|Code");
if (xml.get_LastMethodSuccess() == true) {
wprintf(L"AWS error code: %s\n",value);
}
value = xml.getChildContent(L"RequestId");
if (xml.get_LastMethodSuccess() == true) {
wprintf(L"AWS request ID: %s\n",value);
}
}
return;
}
success = xml.LoadXml(responseXml);
if (success != true) {
wprintf(L"Invalid STS XML response.\n");
return;
}
stringCompare.SetString(xml.tag());
bool stringMatch4 = stringCompare.ContentsEqual(L"AssumeRoleWithSAMLResponse",true);
if (stringMatch4 != true) {
wprintf(L"Unexpected STS response root.\n");
return;
}
value = xml.getChildContent(L"ResponseMetadata|RequestId");
stringCompare.SetString(value);
bool stringMatch5 = stringCompare.ContentsEqual(L"",true);
if ((xml.get_LastMethodSuccess() != true) || (stringMatch5 == true)) {
wprintf(L"Missing or empty ResponseMetadata|RequestId.\n");
return;
}
if (xml.HasChildWithTag(L"AssumeRoleWithSAMLResult") != true) {
wprintf(L"Missing STS result.\n");
return;
}
accessKey = xml.getChildContent(L"AssumeRoleWithSAMLResult|Credentials|AccessKeyId");
stringCompare.SetString(accessKey);
bool stringMatch6 = stringCompare.ContentsEqual(L"",true);
if ((xml.get_LastMethodSuccess() != true) || (stringMatch6 == true)) {
wprintf(L"Missing or empty AssumeRoleWithSAMLResult|Credentials|AccessKeyId.\n");
return;
}
secretKey = xml.getChildContent(L"AssumeRoleWithSAMLResult|Credentials|SecretAccessKey");
stringCompare.SetString(secretKey);
bool stringMatch7 = stringCompare.ContentsEqual(L"",true);
if ((xml.get_LastMethodSuccess() != true) || (stringMatch7 == true)) {
wprintf(L"Missing or empty AssumeRoleWithSAMLResult|Credentials|SecretAccessKey.\n");
return;
}
sessionToken = xml.getChildContent(L"AssumeRoleWithSAMLResult|Credentials|SessionToken");
stringCompare.SetString(sessionToken);
bool stringMatch8 = stringCompare.ContentsEqual(L"",true);
if ((xml.get_LastMethodSuccess() != true) || (stringMatch8 == true)) {
wprintf(L"Missing or empty AssumeRoleWithSAMLResult|Credentials|SessionToken.\n");
return;
}
expiration = xml.getChildContent(L"AssumeRoleWithSAMLResult|Credentials|Expiration");
stringCompare.SetString(expiration);
bool stringMatch9 = stringCompare.ContentsEqual(L"",true);
if ((xml.get_LastMethodSuccess() != true) || (stringMatch9 == true)) {
wprintf(L"Missing or empty AssumeRoleWithSAMLResult|Credentials|Expiration.\n");
return;
}
success = expires.SetFromTimestamp(expiration);
if (success != true) {
wprintf(L"Returned expiration is invalid.\n");
return;
}
success = now.SetFromCurrentSystemTime();
if (success != true) {
wprintf(L"Could not read current time.\n");
return;
}
if (expires.DiffSeconds(now) <= 60) {
wprintf(L"Returned credentials are expired or expire within 60 seconds; obtain fresh credentials.\n");
return;
}
value = xml.getChildContent(L"AssumeRoleWithSAMLResult|AssumedRoleUser|Arn");
stringCompare.SetString(value);
bool stringMatch10 = stringCompare.ContentsEqual(L"",true);
if ((xml.get_LastMethodSuccess() != true) || (stringMatch10 == true)) {
wprintf(L"Missing or empty AssumeRoleWithSAMLResult|AssumedRoleUser|Arn.\n");
return;
}
value = xml.getChildContent(L"AssumeRoleWithSAMLResult|AssumedRoleUser|AssumedRoleId");
stringCompare.SetString(value);
bool stringMatch11 = stringCompare.ContentsEqual(L"",true);
if ((xml.get_LastMethodSuccess() != true) || (stringMatch11 == true)) {
wprintf(L"Missing or empty AssumeRoleWithSAMLResult|AssumedRoleUser|AssumedRoleId.\n");
return;
}
CkJsonObjectW session;
success = session.UpdateString(L"access_key",accessKey);
if (success != true) {
wprintf(L"Could not build complete session.\n");
return;
}
success = session.UpdateString(L"secret_key",secretKey);
if (success != true) {
wprintf(L"Could not build complete session.\n");
return;
}
success = session.UpdateString(L"session_token",sessionToken);
if (success != true) {
wprintf(L"Could not build complete session.\n");
return;
}
success = session.UpdateString(L"expiration",expiration);
if (success != true) {
wprintf(L"Could not build complete session.\n");
return;
}
// Use session in memory; do not print or log session.Emit().
wprintf(L"Session validated; expiration: %s\n",expiration);
wprintf(L"Example completed.\n");
}