Sample code for 30+ languages & platforms
VBScript 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 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")
'  Assumes Chilkat is unlocked. See Global Unlock Sample.
'  Keep this file private and outside public web content/source control.
set credentials = CreateObject("Chilkat.JsonObject")
credentials.Strict = 1
success = credentials.LoadFile("qa_data/aws/sts.json")
If (success <> 1) Then
    outFile.WriteLine("Could not load private STS configuration.")
    WScript.Quit
End If

set expires = CreateObject("Chilkat.CkDateTime")
set now = CreateObject("Chilkat.CkDateTime")
region = "us-west-2"
success = stringCompare.SetString(credentials.StringOf("role_arn"))
stringMatch1 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("role_arn") <> 1) Or (stringMatch1 = 1)) Then
    outFile.WriteLine("Missing or invalid role_arn.")
    WScript.Quit
End If

success = stringCompare.SetString(credentials.StringOf("principal_arn"))
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("principal_arn") <> 1) Or (stringMatch2 = 1)) Then
    outFile.WriteLine("Missing or invalid principal_arn.")
    WScript.Quit
End If

success = stringCompare.SetString(credentials.StringOf("saml_assertion"))
stringMatch3 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("saml_assertion") <> 1) Or (stringMatch3 = 1)) Then
    outFile.WriteLine("Missing or invalid saml_assertion.")
    WScript.Quit
End If

set endpoint = CreateObject("Chilkat.StringBuilder")
success = endpoint.Append("sts.")
success = endpoint.Append(region)
success = endpoint.Append(".amazonaws.com")
set rest = CreateObject("Chilkat.Rest")
rest.ConnectTimeoutMs = 20000
rest.IdleTimeoutMs = 60000
rest.VerboseLogging = 0
'  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

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

success = rest.AddQueryParam("Action","AssumeRoleWithSAML")
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("RoleArn",credentials.StringOf("role_arn"))
If (success <> 1) Then
    outFile.WriteLine("Could not set RoleArn.")
    WScript.Quit
End If

success = rest.AddQueryParam("PrincipalArn",credentials.StringOf("principal_arn"))
If (success <> 1) Then
    outFile.WriteLine("Could not set PrincipalArn.")
    WScript.Quit
End If

success = rest.AddQueryParam("SAMLAssertion",credentials.StringOf("saml_assertion"))
If (success <> 1) Then
    outFile.WriteLine("Could not set SAMLAssertion.")
    WScript.Quit
End If

success = rest.AddQueryParam("DurationSeconds","3600")
If (success <> 1) Then
    outFile.WriteLine("Could not set DurationSeconds.")
    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)
stringMatch4 = stringCompare.ContentsEqual("AssumeRoleWithSAMLResponse",1)
If (stringMatch4 <> 1) Then
    outFile.WriteLine("Unexpected STS response root.")
    WScript.Quit
End If

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

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

accessKey = xml.GetChildContent("AssumeRoleWithSAMLResult|Credentials|AccessKeyId")
success = stringCompare.SetString(accessKey)
stringMatch6 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch6 = 1)) Then
    outFile.WriteLine("Missing or empty AssumeRoleWithSAMLResult|Credentials|AccessKeyId.")
    WScript.Quit
End If

secretKey = xml.GetChildContent("AssumeRoleWithSAMLResult|Credentials|SecretAccessKey")
success = stringCompare.SetString(secretKey)
stringMatch7 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch7 = 1)) Then
    outFile.WriteLine("Missing or empty AssumeRoleWithSAMLResult|Credentials|SecretAccessKey.")
    WScript.Quit
End If

sessionToken = xml.GetChildContent("AssumeRoleWithSAMLResult|Credentials|SessionToken")
success = stringCompare.SetString(sessionToken)
stringMatch8 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch8 = 1)) Then
    outFile.WriteLine("Missing or empty AssumeRoleWithSAMLResult|Credentials|SessionToken.")
    WScript.Quit
End If

expiration = xml.GetChildContent("AssumeRoleWithSAMLResult|Credentials|Expiration")
success = stringCompare.SetString(expiration)
stringMatch9 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch9 = 1)) Then
    outFile.WriteLine("Missing or empty AssumeRoleWithSAMLResult|Credentials|Expiration.")
    WScript.Quit
End If

success = expires.SetFromTimestamp(expiration)
If (success <> 1) Then
    outFile.WriteLine("Returned 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("Returned credentials are expired or expire within 60 seconds; obtain fresh credentials.")
    WScript.Quit
End If

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

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

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

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

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

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

'  Use session in memory; do not print or log session.Emit().
outFile.WriteLine("Session validated; expiration: " & expiration)
outFile.WriteLine("Example completed.")

outFile.Close