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

AWS STS: Normalize STS Credentials and Use Chilkat.Rest

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.

Reads only private qa_data/tokens/aws_session_token.xml; no sts.json is needed. Accepts all seven credential-producing STS response roots, including the existing GetSessionToken/AssumeRole handoff. Builds a complete normalized in-memory session and uses it to sign GetCallerIdentity. The normalized fields are access_key, secret_key, session_token and expiration. Checks expiration with a 60-second margin before connecting. JWT-only GetWebIdentityToken responses are intentionally not credential sets.

AWS references: GetCallerIdentity

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loStringCompare
LOCAL loTokenXml
LOCAL lcCredentialPath
LOCAL lnStringMatch1
LOCAL lnStringMatch2
LOCAL lnStringMatch3
LOCAL lnStringMatch4
LOCAL lnStringMatch5
LOCAL lnStringMatch6
LOCAL lnStringMatch7
LOCAL lnStringMatch8
LOCAL lcAccessKey
LOCAL lcSecretKey
LOCAL lcSessionToken
LOCAL lcExpiration
LOCAL loPath
LOCAL lnStringMatch9
LOCAL lnStringMatch10
LOCAL lnStringMatch11
LOCAL lnStringMatch12
LOCAL loExpires
LOCAL loNow
LOCAL loSession
LOCAL lcRegion
LOCAL loEndpoint
LOCAL loAws
LOCAL loRest
LOCAL loConnection
LOCAL loXml
LOCAL lcValue
LOCAL lcResponseXml
LOCAL lnStringMatch13
LOCAL lnStringMatch14
LOCAL lnStringMatch15

lnSuccess = 0

loStringCompare = CreateObject('Chilkat.StringBuilder')
*  Load a privately stored STS XML response; never print its contents.
loTokenXml = CreateObject('Chilkat.Xml')
loTokenXml.Strict = 1
lnSuccess = loTokenXml.LoadXmlFile("qa_data/tokens/aws_session_token.xml")
IF (lnSuccess <> 1) THEN
    ? "Could not load private STS XML."
    RELEASE loStringCompare
    RELEASE loTokenXml
    CANCEL
ENDIF

lcCredentialPath = ""
loStringCompare.SetString(loTokenXml.Tag)
lnStringMatch1 = loStringCompare.ContentsEqual("GetSessionTokenResponse",1)
IF (lnStringMatch1 = 1) THEN
    lcCredentialPath = "GetSessionTokenResult|Credentials"
ENDIF

loStringCompare.SetString(loTokenXml.Tag)
lnStringMatch2 = loStringCompare.ContentsEqual("AssumeRoleResponse",1)
IF (lnStringMatch2 = 1) THEN
    lcCredentialPath = "AssumeRoleResult|Credentials"
ENDIF

loStringCompare.SetString(loTokenXml.Tag)
lnStringMatch3 = loStringCompare.ContentsEqual("AssumeRoleWithWebIdentityResponse",1)
IF (lnStringMatch3 = 1) THEN
    lcCredentialPath = "AssumeRoleWithWebIdentityResult|Credentials"
ENDIF

loStringCompare.SetString(loTokenXml.Tag)
lnStringMatch4 = loStringCompare.ContentsEqual("AssumeRoleWithSAMLResponse",1)
IF (lnStringMatch4 = 1) THEN
    lcCredentialPath = "AssumeRoleWithSAMLResult|Credentials"
ENDIF

loStringCompare.SetString(loTokenXml.Tag)
lnStringMatch5 = loStringCompare.ContentsEqual("GetFederationTokenResponse",1)
IF (lnStringMatch5 = 1) THEN
    lcCredentialPath = "GetFederationTokenResult|Credentials"
ENDIF

loStringCompare.SetString(loTokenXml.Tag)
lnStringMatch6 = loStringCompare.ContentsEqual("GetDelegatedAccessTokenResponse",1)
IF (lnStringMatch6 = 1) THEN
    lcCredentialPath = "GetDelegatedAccessTokenResult|Credentials"
ENDIF

loStringCompare.SetString(loTokenXml.Tag)
lnStringMatch7 = loStringCompare.ContentsEqual("AssumeRootResponse",1)
IF (lnStringMatch7 = 1) THEN
    lcCredentialPath = "AssumeRootResult|Credentials"
ENDIF

loStringCompare.SetString(lcCredentialPath)
lnStringMatch8 = loStringCompare.ContentsEqual("",1)
IF (lnStringMatch8 = 1) THEN
    ? "Unsupported STS credential response."
    RELEASE loStringCompare
    RELEASE loTokenXml
    CANCEL
ENDIF

loPath = CreateObject('Chilkat.StringBuilder')
lnSuccess = loPath.SetString(lcCredentialPath)
IF (lnSuccess <> 1) THEN
    ? "Could not construct credential path."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    CANCEL
ENDIF

lnSuccess = loPath.Append("|AccessKeyId")
IF (lnSuccess <> 1) THEN
    ? "Could not construct credential path."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    CANCEL
ENDIF

lcAccessKey = loTokenXml.GetChildContent(loPath.GetAsString())
loStringCompare.SetString(lcAccessKey)
lnStringMatch9 = loStringCompare.ContentsEqual("",1)
IF ((loTokenXml.LastMethodSuccess <> 1) OR (lnStringMatch9 = 1)) THEN
    ? "Missing credential field AccessKeyId."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    CANCEL
ENDIF

lnSuccess = loPath.SetString(lcCredentialPath)
IF (lnSuccess <> 1) THEN
    ? "Could not construct credential path."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    CANCEL
ENDIF

lnSuccess = loPath.Append("|SecretAccessKey")
IF (lnSuccess <> 1) THEN
    ? "Could not construct credential path."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    CANCEL
ENDIF

lcSecretKey = loTokenXml.GetChildContent(loPath.GetAsString())
loStringCompare.SetString(lcSecretKey)
lnStringMatch10 = loStringCompare.ContentsEqual("",1)
IF ((loTokenXml.LastMethodSuccess <> 1) OR (lnStringMatch10 = 1)) THEN
    ? "Missing credential field SecretAccessKey."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    CANCEL
ENDIF

lnSuccess = loPath.SetString(lcCredentialPath)
IF (lnSuccess <> 1) THEN
    ? "Could not construct credential path."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    CANCEL
ENDIF

lnSuccess = loPath.Append("|SessionToken")
IF (lnSuccess <> 1) THEN
    ? "Could not construct credential path."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    CANCEL
ENDIF

lcSessionToken = loTokenXml.GetChildContent(loPath.GetAsString())
loStringCompare.SetString(lcSessionToken)
lnStringMatch11 = loStringCompare.ContentsEqual("",1)
IF ((loTokenXml.LastMethodSuccess <> 1) OR (lnStringMatch11 = 1)) THEN
    ? "Missing credential field SessionToken."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    CANCEL
ENDIF

lnSuccess = loPath.SetString(lcCredentialPath)
IF (lnSuccess <> 1) THEN
    ? "Could not construct credential path."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    CANCEL
ENDIF

lnSuccess = loPath.Append("|Expiration")
IF (lnSuccess <> 1) THEN
    ? "Could not construct credential path."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    CANCEL
ENDIF

lcExpiration = loTokenXml.GetChildContent(loPath.GetAsString())
loStringCompare.SetString(lcExpiration)
lnStringMatch12 = loStringCompare.ContentsEqual("",1)
IF ((loTokenXml.LastMethodSuccess <> 1) OR (lnStringMatch12 = 1)) THEN
    ? "Missing credential field Expiration."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    CANCEL
ENDIF

loExpires = CreateObject('Chilkat.CkDateTime')
loNow = CreateObject('Chilkat.CkDateTime')
lnSuccess = loExpires.SetFromTimestamp(lcExpiration)
IF (lnSuccess <> 1) THEN
    ? "Stored expiration is invalid."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    CANCEL
ENDIF

lnSuccess = loNow.SetFromCurrentSystemTime()
IF (lnSuccess <> 1) THEN
    ? "Could not read current time."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    CANCEL
ENDIF

IF (loExpires.DiffSeconds(loNow) <= 60) THEN
    ? "Stored credentials are expired or expire within 60 seconds; obtain fresh credentials."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    CANCEL
ENDIF

loSession = CreateObject('Chilkat.JsonObject')
lnSuccess = loSession.UpdateString("access_key",lcAccessKey)
IF (lnSuccess <> 1) THEN
    ? "Could not build normalized session."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    RELEASE loSession
    CANCEL
ENDIF

lnSuccess = loSession.UpdateString("secret_key",lcSecretKey)
IF (lnSuccess <> 1) THEN
    ? "Could not build normalized session."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    RELEASE loSession
    CANCEL
ENDIF

lnSuccess = loSession.UpdateString("session_token",lcSessionToken)
IF (lnSuccess <> 1) THEN
    ? "Could not build normalized session."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    RELEASE loSession
    CANCEL
ENDIF

lnSuccess = loSession.UpdateString("expiration",lcExpiration)
IF (lnSuccess <> 1) THEN
    ? "Could not build normalized session."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    RELEASE loSession
    CANCEL
ENDIF

lcRegion = "us-west-2"
loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("sts.")
loEndpoint.Append(lcRegion)
loEndpoint.Append(".amazonaws.com")
loAws = CreateObject('Chilkat.AuthAws')
loAws.AccessKey = loSession.StringOf("access_key")
loAws.SecretKey = loSession.StringOf("secret_key")
loAws.Region = lcRegion
loAws.ServiceName = "sts"
loAws.SignatureVersion = 4
loRest = CreateObject('Chilkat.Rest')
loRest.ConnectTimeoutMs = 20000
loRest.IdleTimeoutMs = 60000
loRest.VerboseLogging = 0
lnSuccess = loRest.SetAuthAws(loAws)
IF (lnSuccess <> 1) THEN
    ? "Could not configure request signing."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    RELEASE loSession
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    CANCEL
ENDIF

*  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 loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    RELEASE loSession
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    CANCEL
ENDIF

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

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

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

lnSuccess = loRest.AddQueryParam("Action","GetCallerIdentity")
IF (lnSuccess <> 1) THEN
    ? "Could not set Action."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    RELEASE loSession
    RELEASE loEndpoint
    RELEASE loAws
    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 loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    RELEASE loSession
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loRest.AddQueryParam("X-Amz-Security-Token",loSession.StringOf("session_token"))
IF (lnSuccess <> 1) THEN
    ? "Could not set X-Amz-Security-Token."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    RELEASE loSession
    RELEASE loEndpoint
    RELEASE loAws
    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 loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    RELEASE loSession
    RELEASE loEndpoint
    RELEASE loAws
    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 loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    RELEASE loSession
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

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

loStringCompare.SetString(loXml.Tag)
lnStringMatch13 = loStringCompare.ContentsEqual("GetCallerIdentityResponse",1)
IF (lnStringMatch13 <> 1) THEN
    ? "Unexpected STS response root."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    RELEASE loSession
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

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

IF (loXml.HasChildWithTag("GetCallerIdentityResult") <> 1) THEN
    ? "Missing STS result."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    RELEASE loSession
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcValue = loXml.GetChildContent("GetCallerIdentityResult|Arn")
loStringCompare.SetString(lcValue)
lnStringMatch15 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch15 = 1)) THEN
    ? "Missing or empty GetCallerIdentityResult|Arn."
    RELEASE loStringCompare
    RELEASE loTokenXml
    RELEASE loPath
    RELEASE loExpires
    RELEASE loNow
    RELEASE loSession
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

? "Authenticated principal: " + lcValue
? "Example completed."

RELEASE loStringCompare
RELEASE loTokenXml
RELEASE loPath
RELEASE loExpires
RELEASE loNow
RELEASE loSession
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml