Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.6.0+

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loStringCompare
LOCAL loCredentials
LOCAL loExpires
LOCAL loNow
LOCAL lcRegion
LOCAL lnStringMatch1
LOCAL lnStringMatch2
LOCAL lnStringMatch3
LOCAL loEndpoint
LOCAL loRest
LOCAL loConnection
LOCAL loXml
LOCAL lcValue
LOCAL lcAccessKey
LOCAL lcSecretKey
LOCAL lcSessionToken
LOCAL lcExpiration
LOCAL lcResponseXml
LOCAL lnStringMatch4
LOCAL lnStringMatch5
LOCAL lnStringMatch6
LOCAL lnStringMatch7
LOCAL lnStringMatch8
LOCAL lnStringMatch9
LOCAL lnStringMatch10
LOCAL lnStringMatch11
LOCAL loSession

lnSuccess = 0

loStringCompare = CreateObject('Chilkat.StringBuilder')
*  Assumes Chilkat is unlocked. See Global Unlock Sample.
*  Keep this file private and outside public web content/source control.
loCredentials = CreateObject('Chilkat.JsonObject')
loCredentials.Strict = 1
lnSuccess = loCredentials.LoadFile("qa_data/aws/sts.json")
IF (lnSuccess <> 1) THEN
    ? "Could not load private STS configuration."
    RELEASE loStringCompare
    RELEASE loCredentials
    CANCEL
ENDIF

loExpires = CreateObject('Chilkat.CkDateTime')
loNow = CreateObject('Chilkat.CkDateTime')
lcRegion = "us-west-2"
loStringCompare.SetString(loCredentials.StringOf("role_arn"))
lnStringMatch1 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("role_arn") <> 1) OR (lnStringMatch1 = 1)) THEN
    ? "Missing or invalid role_arn."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    CANCEL
ENDIF

loStringCompare.SetString(loCredentials.StringOf("principal_arn"))
lnStringMatch2 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("principal_arn") <> 1) OR (lnStringMatch2 = 1)) THEN
    ? "Missing or invalid principal_arn."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    CANCEL
ENDIF

loStringCompare.SetString(loCredentials.StringOf("saml_assertion"))
lnStringMatch3 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("saml_assertion") <> 1) OR (lnStringMatch3 = 1)) THEN
    ? "Missing or invalid saml_assertion."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    CANCEL
ENDIF

loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("sts.")
loEndpoint.Append(lcRegion)
loEndpoint.Append(".amazonaws.com")
loRest = CreateObject('Chilkat.Rest')
loRest.ConnectTimeoutMs = 20000
loRest.IdleTimeoutMs = 60000
loRest.VerboseLogging = 0
*  Keep this verified Socket alive while Rest uses its connection.
loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
loConnection.MaxReadIdleMs = 60000
loConnection.MaxSendIdleMs = 60000
lnSuccess = loConnection.Connect(loEndpoint.GetAsString(),443,1,20000)
IF (lnSuccess <> 1) THEN
    ? "TLS connection failed; inspect diagnostics privately."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    CANCEL
ENDIF

lnSuccess = loRest.UseConnection(loConnection,1)
IF (lnSuccess <> 1) THEN
    ? "Could not attach TLS connection."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    CANCEL
ENDIF

loXml = CreateObject('Chilkat.Xml')
loXml.Strict = 1

lcResponseXml = ""
lnSuccess = loRest.ClearAllQueryParams()
IF (lnSuccess <> 1) THEN
    ? "Could not reset request parameters."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loRest.AddQueryParam("Action","AssumeRoleWithSAML")
IF (lnSuccess <> 1) THEN
    ? "Could not set Action."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loRest.AddQueryParam("Version","2011-06-15")
IF (lnSuccess <> 1) THEN
    ? "Could not set Version."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loRest.AddQueryParam("RoleArn",loCredentials.StringOf("role_arn"))
IF (lnSuccess <> 1) THEN
    ? "Could not set RoleArn."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loRest.AddQueryParam("PrincipalArn",loCredentials.StringOf("principal_arn"))
IF (lnSuccess <> 1) THEN
    ? "Could not set PrincipalArn."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loRest.AddQueryParam("SAMLAssertion",loCredentials.StringOf("saml_assertion"))
IF (lnSuccess <> 1) THEN
    ? "Could not set SAMLAssertion."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loRest.AddQueryParam("DurationSeconds","3600")
IF (lnSuccess <> 1) THEN
    ? "Could not set DurationSeconds."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcResponseXml = loRest.FullRequestFormUrlEncoded("POST","/")
IF (loRest.LastMethodSuccess <> 1) THEN
    ? "STS transport failed; inspect diagnostics privately."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

IF (loRest.ResponseStatusCode <> 200) THEN
    ? "STS HTTP status: " + STR(loRest.ResponseStatusCode)
    lnSuccess = loXml.LoadXml(lcResponseXml)
    IF (lnSuccess = 1) THEN
        lcValue = loXml.GetChildContent("Error|Code")
        IF (loXml.LastMethodSuccess = 1) THEN
            ? "AWS error code: " + lcValue
        ENDIF

        lcValue = loXml.GetChildContent("RequestId")
        IF (loXml.LastMethodSuccess = 1) THEN
            ? "AWS request ID: " + lcValue
        ENDIF

    ENDIF

    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loXml.LoadXml(lcResponseXml)
IF (lnSuccess <> 1) THEN
    ? "Invalid STS XML response."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

loStringCompare.SetString(loXml.Tag)
lnStringMatch4 = loStringCompare.ContentsEqual("AssumeRoleWithSAMLResponse",1)
IF (lnStringMatch4 <> 1) THEN
    ? "Unexpected STS response root."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcValue = loXml.GetChildContent("ResponseMetadata|RequestId")
loStringCompare.SetString(lcValue)
lnStringMatch5 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch5 = 1)) THEN
    ? "Missing or empty ResponseMetadata|RequestId."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

IF (loXml.HasChildWithTag("AssumeRoleWithSAMLResult") <> 1) THEN
    ? "Missing STS result."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcAccessKey = loXml.GetChildContent("AssumeRoleWithSAMLResult|Credentials|AccessKeyId")
loStringCompare.SetString(lcAccessKey)
lnStringMatch6 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch6 = 1)) THEN
    ? "Missing or empty AssumeRoleWithSAMLResult|Credentials|AccessKeyId."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcSecretKey = loXml.GetChildContent("AssumeRoleWithSAMLResult|Credentials|SecretAccessKey")
loStringCompare.SetString(lcSecretKey)
lnStringMatch7 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch7 = 1)) THEN
    ? "Missing or empty AssumeRoleWithSAMLResult|Credentials|SecretAccessKey."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcSessionToken = loXml.GetChildContent("AssumeRoleWithSAMLResult|Credentials|SessionToken")
loStringCompare.SetString(lcSessionToken)
lnStringMatch8 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch8 = 1)) THEN
    ? "Missing or empty AssumeRoleWithSAMLResult|Credentials|SessionToken."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcExpiration = loXml.GetChildContent("AssumeRoleWithSAMLResult|Credentials|Expiration")
loStringCompare.SetString(lcExpiration)
lnStringMatch9 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch9 = 1)) THEN
    ? "Missing or empty AssumeRoleWithSAMLResult|Credentials|Expiration."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loExpires.SetFromTimestamp(lcExpiration)
IF (lnSuccess <> 1) THEN
    ? "Returned expiration is invalid."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loNow.SetFromCurrentSystemTime()
IF (lnSuccess <> 1) THEN
    ? "Could not read current time."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

IF (loExpires.DiffSeconds(loNow) <= 60) THEN
    ? "Returned credentials are expired or expire within 60 seconds; obtain fresh credentials."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcValue = loXml.GetChildContent("AssumeRoleWithSAMLResult|AssumedRoleUser|Arn")
loStringCompare.SetString(lcValue)
lnStringMatch10 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch10 = 1)) THEN
    ? "Missing or empty AssumeRoleWithSAMLResult|AssumedRoleUser|Arn."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcValue = loXml.GetChildContent("AssumeRoleWithSAMLResult|AssumedRoleUser|AssumedRoleId")
loStringCompare.SetString(lcValue)
lnStringMatch11 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch11 = 1)) THEN
    ? "Missing or empty AssumeRoleWithSAMLResult|AssumedRoleUser|AssumedRoleId."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

loSession = CreateObject('Chilkat.JsonObject')
lnSuccess = loSession.UpdateString("access_key",lcAccessKey)
IF (lnSuccess <> 1) THEN
    ? "Could not build complete session."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loSession
    CANCEL
ENDIF

lnSuccess = loSession.UpdateString("secret_key",lcSecretKey)
IF (lnSuccess <> 1) THEN
    ? "Could not build complete session."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loSession
    CANCEL
ENDIF

lnSuccess = loSession.UpdateString("session_token",lcSessionToken)
IF (lnSuccess <> 1) THEN
    ? "Could not build complete session."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loSession
    CANCEL
ENDIF

lnSuccess = loSession.UpdateString("expiration",lcExpiration)
IF (lnSuccess <> 1) THEN
    ? "Could not build complete session."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loSession
    CANCEL
ENDIF

*  Use session in memory; do not print or log session.Emit().
? "Session validated; expiration: " + lcExpiration
? "Example completed."

RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSession