Sample code for 30+ languages & platforms
VBScript 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 VBScript Downloads

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

set stringCompare = CreateObject("Chilkat.StringBuilder")
'  Load a privately stored STS XML response; never print its contents.
set tokenXml = CreateObject("Chilkat.Xml")
tokenXml.Strict = 1
success = tokenXml.LoadXmlFile("qa_data/tokens/aws_session_token.xml")
If (success <> 1) Then
    outFile.WriteLine("Could not load private STS XML.")
    WScript.Quit
End If

credentialPath = ""
success = stringCompare.SetString(tokenXml.Tag)
stringMatch1 = stringCompare.ContentsEqual("GetSessionTokenResponse",1)
If (stringMatch1 = 1) Then
    credentialPath = "GetSessionTokenResult|Credentials"
End If

success = stringCompare.SetString(tokenXml.Tag)
stringMatch2 = stringCompare.ContentsEqual("AssumeRoleResponse",1)
If (stringMatch2 = 1) Then
    credentialPath = "AssumeRoleResult|Credentials"
End If

success = stringCompare.SetString(tokenXml.Tag)
stringMatch3 = stringCompare.ContentsEqual("AssumeRoleWithWebIdentityResponse",1)
If (stringMatch3 = 1) Then
    credentialPath = "AssumeRoleWithWebIdentityResult|Credentials"
End If

success = stringCompare.SetString(tokenXml.Tag)
stringMatch4 = stringCompare.ContentsEqual("AssumeRoleWithSAMLResponse",1)
If (stringMatch4 = 1) Then
    credentialPath = "AssumeRoleWithSAMLResult|Credentials"
End If

success = stringCompare.SetString(tokenXml.Tag)
stringMatch5 = stringCompare.ContentsEqual("GetFederationTokenResponse",1)
If (stringMatch5 = 1) Then
    credentialPath = "GetFederationTokenResult|Credentials"
End If

success = stringCompare.SetString(tokenXml.Tag)
stringMatch6 = stringCompare.ContentsEqual("GetDelegatedAccessTokenResponse",1)
If (stringMatch6 = 1) Then
    credentialPath = "GetDelegatedAccessTokenResult|Credentials"
End If

success = stringCompare.SetString(tokenXml.Tag)
stringMatch7 = stringCompare.ContentsEqual("AssumeRootResponse",1)
If (stringMatch7 = 1) Then
    credentialPath = "AssumeRootResult|Credentials"
End If

success = stringCompare.SetString(credentialPath)
stringMatch8 = stringCompare.ContentsEqual("",1)
If (stringMatch8 = 1) Then
    outFile.WriteLine("Unsupported STS credential response.")
    WScript.Quit
End If

set path = CreateObject("Chilkat.StringBuilder")
success = path.SetString(credentialPath)
If (success <> 1) Then
    outFile.WriteLine("Could not construct credential path.")
    WScript.Quit
End If

success = path.Append("|AccessKeyId")
If (success <> 1) Then
    outFile.WriteLine("Could not construct credential path.")
    WScript.Quit
End If

accessKey = tokenXml.GetChildContent(path.GetAsString())
success = stringCompare.SetString(accessKey)
stringMatch9 = stringCompare.ContentsEqual("",1)
If ((tokenXml.LastMethodSuccess <> 1) Or (stringMatch9 = 1)) Then
    outFile.WriteLine("Missing credential field AccessKeyId.")
    WScript.Quit
End If

success = path.SetString(credentialPath)
If (success <> 1) Then
    outFile.WriteLine("Could not construct credential path.")
    WScript.Quit
End If

success = path.Append("|SecretAccessKey")
If (success <> 1) Then
    outFile.WriteLine("Could not construct credential path.")
    WScript.Quit
End If

secretKey = tokenXml.GetChildContent(path.GetAsString())
success = stringCompare.SetString(secretKey)
stringMatch10 = stringCompare.ContentsEqual("",1)
If ((tokenXml.LastMethodSuccess <> 1) Or (stringMatch10 = 1)) Then
    outFile.WriteLine("Missing credential field SecretAccessKey.")
    WScript.Quit
End If

success = path.SetString(credentialPath)
If (success <> 1) Then
    outFile.WriteLine("Could not construct credential path.")
    WScript.Quit
End If

success = path.Append("|SessionToken")
If (success <> 1) Then
    outFile.WriteLine("Could not construct credential path.")
    WScript.Quit
End If

sessionToken = tokenXml.GetChildContent(path.GetAsString())
success = stringCompare.SetString(sessionToken)
stringMatch11 = stringCompare.ContentsEqual("",1)
If ((tokenXml.LastMethodSuccess <> 1) Or (stringMatch11 = 1)) Then
    outFile.WriteLine("Missing credential field SessionToken.")
    WScript.Quit
End If

success = path.SetString(credentialPath)
If (success <> 1) Then
    outFile.WriteLine("Could not construct credential path.")
    WScript.Quit
End If

success = path.Append("|Expiration")
If (success <> 1) Then
    outFile.WriteLine("Could not construct credential path.")
    WScript.Quit
End If

expiration = tokenXml.GetChildContent(path.GetAsString())
success = stringCompare.SetString(expiration)
stringMatch12 = stringCompare.ContentsEqual("",1)
If ((tokenXml.LastMethodSuccess <> 1) Or (stringMatch12 = 1)) Then
    outFile.WriteLine("Missing credential field Expiration.")
    WScript.Quit
End If

set expires = CreateObject("Chilkat.CkDateTime")
set now = CreateObject("Chilkat.CkDateTime")
success = expires.SetFromTimestamp(expiration)
If (success <> 1) Then
    outFile.WriteLine("Stored expiration is invalid.")
    WScript.Quit
End If

success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
    outFile.WriteLine("Could not read current time.")
    WScript.Quit
End If

If (expires.DiffSeconds(now) <= 60) Then
    outFile.WriteLine("Stored credentials are expired or expire within 60 seconds; obtain fresh credentials.")
    WScript.Quit
End If

set session = CreateObject("Chilkat.JsonObject")
success = session.UpdateString("access_key",accessKey)
If (success <> 1) Then
    outFile.WriteLine("Could not build normalized session.")
    WScript.Quit
End If

success = session.UpdateString("secret_key",secretKey)
If (success <> 1) Then
    outFile.WriteLine("Could not build normalized session.")
    WScript.Quit
End If

success = session.UpdateString("session_token",sessionToken)
If (success <> 1) Then
    outFile.WriteLine("Could not build normalized session.")
    WScript.Quit
End If

success = session.UpdateString("expiration",expiration)
If (success <> 1) Then
    outFile.WriteLine("Could not build normalized session.")
    WScript.Quit
End If

region = "us-west-2"
set endpoint = CreateObject("Chilkat.StringBuilder")
success = endpoint.Append("sts.")
success = endpoint.Append(region)
success = endpoint.Append(".amazonaws.com")
set aws = CreateObject("Chilkat.AuthAws")
aws.AccessKey = session.StringOf("access_key")
aws.SecretKey = session.StringOf("secret_key")
aws.Region = region
aws.ServiceName = "sts"
aws.SignatureVersion = 4
set rest = CreateObject("Chilkat.Rest")
rest.ConnectTimeoutMs = 20000
rest.IdleTimeoutMs = 60000
rest.VerboseLogging = 0
success = rest.SetAuthAws(aws)
If (success <> 1) Then
    outFile.WriteLine("Could not configure request signing.")
    WScript.Quit
End If

'  Keep this verified Socket alive while Rest uses its connection.
set connection = CreateObject("Chilkat.Socket")
connection.RequireSslCertVerify = 1
connection.MaxReadIdleMs = 60000
connection.MaxSendIdleMs = 60000
success = connection.Connect(endpoint.GetAsString(),443,1,20000)
If (success <> 1) Then
    outFile.WriteLine("TLS connection failed; inspect diagnostics privately.")
    WScript.Quit
End If

success = rest.UseConnection(connection,1)
If (success <> 1) Then
    outFile.WriteLine("Could not attach TLS connection.")
    WScript.Quit
End If

set xml = CreateObject("Chilkat.Xml")
xml.Strict = 1

success = rest.ClearAllQueryParams()
If (success <> 1) Then
    outFile.WriteLine("Could not reset request parameters.")
    WScript.Quit
End If

success = rest.AddQueryParam("Action","GetCallerIdentity")
If (success <> 1) Then
    outFile.WriteLine("Could not set Action.")
    WScript.Quit
End If

success = rest.AddQueryParam("Version","2011-06-15")
If (success <> 1) Then
    outFile.WriteLine("Could not set Version.")
    WScript.Quit
End If

success = rest.AddQueryParam("X-Amz-Security-Token",session.StringOf("session_token"))
If (success <> 1) Then
    outFile.WriteLine("Could not set X-Amz-Security-Token.")
    WScript.Quit
End If

responseXml = rest.FullRequestFormUrlEncoded("POST","/")
If (rest.LastMethodSuccess <> 1) Then
    outFile.WriteLine("STS transport failed; inspect diagnostics privately.")
    WScript.Quit
End If

If (rest.ResponseStatusCode <> 200) Then
    outFile.WriteLine("STS HTTP status: " & rest.ResponseStatusCode)
    success = xml.LoadXml(responseXml)
    If (success = 1) Then
        value = xml.GetChildContent("Error|Code")
        If (xml.LastMethodSuccess = 1) Then
            outFile.WriteLine("AWS error code: " & value)
        End If

        value = xml.GetChildContent("RequestId")
        If (xml.LastMethodSuccess = 1) Then
            outFile.WriteLine("AWS request ID: " & value)
        End If

    End If

    WScript.Quit
End If

success = xml.LoadXml(responseXml)
If (success <> 1) Then
    outFile.WriteLine("Invalid STS XML response.")
    WScript.Quit
End If

success = stringCompare.SetString(xml.Tag)
stringMatch13 = stringCompare.ContentsEqual("GetCallerIdentityResponse",1)
If (stringMatch13 <> 1) Then
    outFile.WriteLine("Unexpected STS response root.")
    WScript.Quit
End If

value = xml.GetChildContent("ResponseMetadata|RequestId")
success = stringCompare.SetString(value)
stringMatch14 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch14 = 1)) Then
    outFile.WriteLine("Missing or empty ResponseMetadata|RequestId.")
    WScript.Quit
End If

If (xml.HasChildWithTag("GetCallerIdentityResult") <> 1) Then
    outFile.WriteLine("Missing STS result.")
    WScript.Quit
End If

value = xml.GetChildContent("GetCallerIdentityResult|Arn")
success = stringCompare.SetString(value)
stringMatch15 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch15 = 1)) Then
    outFile.WriteLine("Missing or empty GetCallerIdentityResult|Arn.")
    WScript.Quit
End If

outFile.WriteLine("Authenticated principal: " & value)
outFile.WriteLine("Example completed.")

outFile.Close