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 AutoIt Downloads
Local $bSuccess = False
$oStringCompare = ObjCreate("Chilkat.StringBuilder")
; Load a privately stored STS XML response; never print its contents.
$oTokenXml = ObjCreate("Chilkat.Xml")
$oTokenXml.Strict = True
$bSuccess = $oTokenXml.LoadXmlFile("qa_data/tokens/aws_session_token.xml")
If ($bSuccess <> True) Then
ConsoleWrite("Could not load private STS XML." & @CRLF)
Exit
EndIf
Local $sCredentialPath = ""
$oStringCompare.SetString($oTokenXml.Tag)
Local $bStringMatch1 = $oStringCompare.ContentsEqual("GetSessionTokenResponse",True)
If ($bStringMatch1 = True) Then
$sCredentialPath = "GetSessionTokenResult|Credentials"
EndIf
$oStringCompare.SetString($oTokenXml.Tag)
Local $bStringMatch2 = $oStringCompare.ContentsEqual("AssumeRoleResponse",True)
If ($bStringMatch2 = True) Then
$sCredentialPath = "AssumeRoleResult|Credentials"
EndIf
$oStringCompare.SetString($oTokenXml.Tag)
Local $bStringMatch3 = $oStringCompare.ContentsEqual("AssumeRoleWithWebIdentityResponse",True)
If ($bStringMatch3 = True) Then
$sCredentialPath = "AssumeRoleWithWebIdentityResult|Credentials"
EndIf
$oStringCompare.SetString($oTokenXml.Tag)
Local $bStringMatch4 = $oStringCompare.ContentsEqual("AssumeRoleWithSAMLResponse",True)
If ($bStringMatch4 = True) Then
$sCredentialPath = "AssumeRoleWithSAMLResult|Credentials"
EndIf
$oStringCompare.SetString($oTokenXml.Tag)
Local $bStringMatch5 = $oStringCompare.ContentsEqual("GetFederationTokenResponse",True)
If ($bStringMatch5 = True) Then
$sCredentialPath = "GetFederationTokenResult|Credentials"
EndIf
$oStringCompare.SetString($oTokenXml.Tag)
Local $bStringMatch6 = $oStringCompare.ContentsEqual("GetDelegatedAccessTokenResponse",True)
If ($bStringMatch6 = True) Then
$sCredentialPath = "GetDelegatedAccessTokenResult|Credentials"
EndIf
$oStringCompare.SetString($oTokenXml.Tag)
Local $bStringMatch7 = $oStringCompare.ContentsEqual("AssumeRootResponse",True)
If ($bStringMatch7 = True) Then
$sCredentialPath = "AssumeRootResult|Credentials"
EndIf
$oStringCompare.SetString($sCredentialPath)
Local $bStringMatch8 = $oStringCompare.ContentsEqual("",True)
If ($bStringMatch8 = True) Then
ConsoleWrite("Unsupported STS credential response." & @CRLF)
Exit
EndIf
Local $sAccessKey
Local $secretKey
Local $sessionToken
Local $sExpiration
$oPath = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oPath.SetString($sCredentialPath)
If ($bSuccess <> True) Then
ConsoleWrite("Could not construct credential path." & @CRLF)
Exit
EndIf
$bSuccess = $oPath.Append("|AccessKeyId")
If ($bSuccess <> True) Then
ConsoleWrite("Could not construct credential path." & @CRLF)
Exit
EndIf
$sAccessKey = $oTokenXml.GetChildContent($oPath.GetAsString())
$oStringCompare.SetString($sAccessKey)
Local $bStringMatch9 = $oStringCompare.ContentsEqual("",True)
If (($oTokenXml.LastMethodSuccess <> True) Or ($bStringMatch9 = True)) Then
ConsoleWrite("Missing credential field AccessKeyId." & @CRLF)
Exit
EndIf
$bSuccess = $oPath.SetString($sCredentialPath)
If ($bSuccess <> True) Then
ConsoleWrite("Could not construct credential path." & @CRLF)
Exit
EndIf
$bSuccess = $oPath.Append("|SecretAccessKey")
If ($bSuccess <> True) Then
ConsoleWrite("Could not construct credential path." & @CRLF)
Exit
EndIf
$secretKey = $oTokenXml.GetChildContent($oPath.GetAsString())
$oStringCompare.SetString($secretKey)
Local $bStringMatch10 = $oStringCompare.ContentsEqual("",True)
If (($oTokenXml.LastMethodSuccess <> True) Or ($bStringMatch10 = True)) Then
ConsoleWrite("Missing credential field SecretAccessKey." & @CRLF)
Exit
EndIf
$bSuccess = $oPath.SetString($sCredentialPath)
If ($bSuccess <> True) Then
ConsoleWrite("Could not construct credential path." & @CRLF)
Exit
EndIf
$bSuccess = $oPath.Append("|SessionToken")
If ($bSuccess <> True) Then
ConsoleWrite("Could not construct credential path." & @CRLF)
Exit
EndIf
$sessionToken = $oTokenXml.GetChildContent($oPath.GetAsString())
$oStringCompare.SetString($sessionToken)
Local $bStringMatch11 = $oStringCompare.ContentsEqual("",True)
If (($oTokenXml.LastMethodSuccess <> True) Or ($bStringMatch11 = True)) Then
ConsoleWrite("Missing credential field SessionToken." & @CRLF)
Exit
EndIf
$bSuccess = $oPath.SetString($sCredentialPath)
If ($bSuccess <> True) Then
ConsoleWrite("Could not construct credential path." & @CRLF)
Exit
EndIf
$bSuccess = $oPath.Append("|Expiration")
If ($bSuccess <> True) Then
ConsoleWrite("Could not construct credential path." & @CRLF)
Exit
EndIf
$sExpiration = $oTokenXml.GetChildContent($oPath.GetAsString())
$oStringCompare.SetString($sExpiration)
Local $bStringMatch12 = $oStringCompare.ContentsEqual("",True)
If (($oTokenXml.LastMethodSuccess <> True) Or ($bStringMatch12 = True)) Then
ConsoleWrite("Missing credential field Expiration." & @CRLF)
Exit
EndIf
$oExpires = ObjCreate("Chilkat.CkDateTime")
$oNow = ObjCreate("Chilkat.CkDateTime")
$bSuccess = $oExpires.SetFromTimestamp($sExpiration)
If ($bSuccess <> True) Then
ConsoleWrite("Stored expiration is invalid." & @CRLF)
Exit
EndIf
$bSuccess = $oNow.SetFromCurrentSystemTime()
If ($bSuccess <> True) Then
ConsoleWrite("Could not read current time." & @CRLF)
Exit
EndIf
If ($oExpires.DiffSeconds($oNow) <= 60) Then
ConsoleWrite("Stored credentials are expired or expire within 60 seconds; obtain fresh credentials." & @CRLF)
Exit
EndIf
$oSession = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oSession.UpdateString("access_key",$sAccessKey)
If ($bSuccess <> True) Then
ConsoleWrite("Could not build normalized session." & @CRLF)
Exit
EndIf
$bSuccess = $oSession.UpdateString("secret_key",$secretKey)
If ($bSuccess <> True) Then
ConsoleWrite("Could not build normalized session." & @CRLF)
Exit
EndIf
$bSuccess = $oSession.UpdateString("session_token",$sessionToken)
If ($bSuccess <> True) Then
ConsoleWrite("Could not build normalized session." & @CRLF)
Exit
EndIf
$bSuccess = $oSession.UpdateString("expiration",$sExpiration)
If ($bSuccess <> True) Then
ConsoleWrite("Could not build normalized session." & @CRLF)
Exit
EndIf
Local $sRegion = "us-west-2"
$oEndpoint = ObjCreate("Chilkat.StringBuilder")
$oEndpoint.Append("sts.")
$oEndpoint.Append($sRegion)
$oEndpoint.Append(".amazonaws.com")
$oAws = ObjCreate("Chilkat.AuthAws")
$oAws.AccessKey = $oSession.StringOf("access_key")
$oAws.SecretKey = $oSession.StringOf("secret_key")
$oAws.Region = $sRegion
$oAws.ServiceName = "sts"
$oAws.SignatureVersion = 4
$oRest = ObjCreate("Chilkat.Rest")
$oRest.ConnectTimeoutMs = 20000
$oRest.IdleTimeoutMs = 60000
$oRest.VerboseLogging = False
$bSuccess = $oRest.SetAuthAws($oAws)
If ($bSuccess <> True) Then
ConsoleWrite("Could not configure request signing." & @CRLF)
Exit
EndIf
; Keep this verified Socket alive while Rest uses its connection.
$oConnection = ObjCreate("Chilkat.Socket")
$oConnection.RequireSslCertVerify = True
$oConnection.MaxReadIdleMs = 60000
$oConnection.MaxSendIdleMs = 60000
$bSuccess = $oConnection.Connect($oEndpoint.GetAsString(),443,True,20000)
If ($bSuccess <> True) Then
ConsoleWrite("TLS connection failed; inspect diagnostics privately." & @CRLF)
Exit
EndIf
$bSuccess = $oRest.UseConnection($oConnection,True)
If ($bSuccess <> True) Then
ConsoleWrite("Could not attach TLS connection." & @CRLF)
Exit
EndIf
$oXml = ObjCreate("Chilkat.Xml")
$oXml.Strict = True
Local $sValue
Local $sResponseXml
$bSuccess = $oRest.ClearAllQueryParams()
If ($bSuccess <> True) Then
ConsoleWrite("Could not reset request parameters." & @CRLF)
Exit
EndIf
$bSuccess = $oRest.AddQueryParam("Action","GetCallerIdentity")
If ($bSuccess <> True) Then
ConsoleWrite("Could not set Action." & @CRLF)
Exit
EndIf
$bSuccess = $oRest.AddQueryParam("Version","2011-06-15")
If ($bSuccess <> True) Then
ConsoleWrite("Could not set Version." & @CRLF)
Exit
EndIf
$bSuccess = $oRest.AddQueryParam("X-Amz-Security-Token",$oSession.StringOf("session_token"))
If ($bSuccess <> True) Then
ConsoleWrite("Could not set X-Amz-Security-Token." & @CRLF)
Exit
EndIf
$sResponseXml = $oRest.FullRequestFormUrlEncoded("POST","/")
If ($oRest.LastMethodSuccess <> True) Then
ConsoleWrite("STS transport failed; inspect diagnostics privately." & @CRLF)
Exit
EndIf
If ($oRest.ResponseStatusCode <> 200) Then
ConsoleWrite("STS HTTP status: " & $oRest.ResponseStatusCode & @CRLF)
$bSuccess = $oXml.LoadXml($sResponseXml)
If ($bSuccess = True) Then
$sValue = $oXml.GetChildContent("Error|Code")
If ($oXml.LastMethodSuccess = True) Then
ConsoleWrite("AWS error code: " & $sValue & @CRLF)
EndIf
$sValue = $oXml.GetChildContent("RequestId")
If ($oXml.LastMethodSuccess = True) Then
ConsoleWrite("AWS request ID: " & $sValue & @CRLF)
EndIf
EndIf
Exit
EndIf
$bSuccess = $oXml.LoadXml($sResponseXml)
If ($bSuccess <> True) Then
ConsoleWrite("Invalid STS XML response." & @CRLF)
Exit
EndIf
$oStringCompare.SetString($oXml.Tag)
Local $bStringMatch13 = $oStringCompare.ContentsEqual("GetCallerIdentityResponse",True)
If ($bStringMatch13 <> True) Then
ConsoleWrite("Unexpected STS response root." & @CRLF)
Exit
EndIf
$sValue = $oXml.GetChildContent("ResponseMetadata|RequestId")
$oStringCompare.SetString($sValue)
Local $bStringMatch14 = $oStringCompare.ContentsEqual("",True)
If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch14 = True)) Then
ConsoleWrite("Missing or empty ResponseMetadata|RequestId." & @CRLF)
Exit
EndIf
If ($oXml.HasChildWithTag("GetCallerIdentityResult") <> True) Then
ConsoleWrite("Missing STS result." & @CRLF)
Exit
EndIf
$sValue = $oXml.GetChildContent("GetCallerIdentityResult|Arn")
$oStringCompare.SetString($sValue)
Local $bStringMatch15 = $oStringCompare.ContentsEqual("",True)
If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch15 = True)) Then
ConsoleWrite("Missing or empty GetCallerIdentityResult|Arn." & @CRLF)
Exit
EndIf
ConsoleWrite("Authenticated principal: " & $sValue & @CRLF)
ConsoleWrite("Example completed." & @CRLF)