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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_StringCompare
oleobject loo_TokenXml
string ls_CredentialPath
long li_StringMatch1
long li_StringMatch2
long li_StringMatch3
long li_StringMatch4
long li_StringMatch5
long li_StringMatch6
long li_StringMatch7
long li_StringMatch8
string ls_AccessKey
string ls_SecretKey
string ls_SessionToken
string ls_Expiration
oleobject loo_Path
long li_StringMatch9
long li_StringMatch10
long li_StringMatch11
long li_StringMatch12
oleobject loo_Expires
oleobject loo_Now
oleobject loo_Session
string ls_Region
oleobject loo_Endpoint
oleobject loo_Aws
oleobject loo_Rest
oleobject loo_Connection
oleobject loo_Xml
string ls_Value
string ls_ResponseXml
long li_StringMatch13
long li_StringMatch14
long li_StringMatch15

li_Success = 0

loo_StringCompare = create oleobject
li_rc = loo_StringCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_StringCompare
    MessageBox("Error","Connecting to COM object failed")
    return
end if
//  Load a privately stored STS XML response; never print its contents.
loo_TokenXml = create oleobject
li_rc = loo_TokenXml.ConnectToNewObject("Chilkat.Xml")

loo_TokenXml.Strict = 1
li_Success = loo_TokenXml.LoadXmlFile("qa_data/tokens/aws_session_token.xml")
if li_Success <> 1 then
    Write-Debug "Could not load private STS XML."
    destroy loo_StringCompare
    destroy loo_TokenXml
    return
end if

ls_CredentialPath = ""
loo_StringCompare.SetString(loo_TokenXml.Tag)
li_StringMatch1 = loo_StringCompare.ContentsEqual("GetSessionTokenResponse",1)
if li_StringMatch1 = 1 then
    ls_CredentialPath = "GetSessionTokenResult|Credentials"
end if

loo_StringCompare.SetString(loo_TokenXml.Tag)
li_StringMatch2 = loo_StringCompare.ContentsEqual("AssumeRoleResponse",1)
if li_StringMatch2 = 1 then
    ls_CredentialPath = "AssumeRoleResult|Credentials"
end if

loo_StringCompare.SetString(loo_TokenXml.Tag)
li_StringMatch3 = loo_StringCompare.ContentsEqual("AssumeRoleWithWebIdentityResponse",1)
if li_StringMatch3 = 1 then
    ls_CredentialPath = "AssumeRoleWithWebIdentityResult|Credentials"
end if

loo_StringCompare.SetString(loo_TokenXml.Tag)
li_StringMatch4 = loo_StringCompare.ContentsEqual("AssumeRoleWithSAMLResponse",1)
if li_StringMatch4 = 1 then
    ls_CredentialPath = "AssumeRoleWithSAMLResult|Credentials"
end if

loo_StringCompare.SetString(loo_TokenXml.Tag)
li_StringMatch5 = loo_StringCompare.ContentsEqual("GetFederationTokenResponse",1)
if li_StringMatch5 = 1 then
    ls_CredentialPath = "GetFederationTokenResult|Credentials"
end if

loo_StringCompare.SetString(loo_TokenXml.Tag)
li_StringMatch6 = loo_StringCompare.ContentsEqual("GetDelegatedAccessTokenResponse",1)
if li_StringMatch6 = 1 then
    ls_CredentialPath = "GetDelegatedAccessTokenResult|Credentials"
end if

loo_StringCompare.SetString(loo_TokenXml.Tag)
li_StringMatch7 = loo_StringCompare.ContentsEqual("AssumeRootResponse",1)
if li_StringMatch7 = 1 then
    ls_CredentialPath = "AssumeRootResult|Credentials"
end if

loo_StringCompare.SetString(ls_CredentialPath)
li_StringMatch8 = loo_StringCompare.ContentsEqual("",1)
if li_StringMatch8 = 1 then
    Write-Debug "Unsupported STS credential response."
    destroy loo_StringCompare
    destroy loo_TokenXml
    return
end if

loo_Path = create oleobject
li_rc = loo_Path.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Path.SetString(ls_CredentialPath)
if li_Success <> 1 then
    Write-Debug "Could not construct credential path."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    return
end if

li_Success = loo_Path.Append("|AccessKeyId")
if li_Success <> 1 then
    Write-Debug "Could not construct credential path."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    return
end if

ls_AccessKey = loo_TokenXml.GetChildContent(loo_Path.GetAsString())
loo_StringCompare.SetString(ls_AccessKey)
li_StringMatch9 = loo_StringCompare.ContentsEqual("",1)
if (loo_TokenXml.LastMethodSuccess <> 1) OR (li_StringMatch9 = 1) then
    Write-Debug "Missing credential field AccessKeyId."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    return
end if

li_Success = loo_Path.SetString(ls_CredentialPath)
if li_Success <> 1 then
    Write-Debug "Could not construct credential path."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    return
end if

li_Success = loo_Path.Append("|SecretAccessKey")
if li_Success <> 1 then
    Write-Debug "Could not construct credential path."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    return
end if

ls_SecretKey = loo_TokenXml.GetChildContent(loo_Path.GetAsString())
loo_StringCompare.SetString(ls_SecretKey)
li_StringMatch10 = loo_StringCompare.ContentsEqual("",1)
if (loo_TokenXml.LastMethodSuccess <> 1) OR (li_StringMatch10 = 1) then
    Write-Debug "Missing credential field SecretAccessKey."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    return
end if

li_Success = loo_Path.SetString(ls_CredentialPath)
if li_Success <> 1 then
    Write-Debug "Could not construct credential path."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    return
end if

li_Success = loo_Path.Append("|SessionToken")
if li_Success <> 1 then
    Write-Debug "Could not construct credential path."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    return
end if

ls_SessionToken = loo_TokenXml.GetChildContent(loo_Path.GetAsString())
loo_StringCompare.SetString(ls_SessionToken)
li_StringMatch11 = loo_StringCompare.ContentsEqual("",1)
if (loo_TokenXml.LastMethodSuccess <> 1) OR (li_StringMatch11 = 1) then
    Write-Debug "Missing credential field SessionToken."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    return
end if

li_Success = loo_Path.SetString(ls_CredentialPath)
if li_Success <> 1 then
    Write-Debug "Could not construct credential path."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    return
end if

li_Success = loo_Path.Append("|Expiration")
if li_Success <> 1 then
    Write-Debug "Could not construct credential path."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    return
end if

ls_Expiration = loo_TokenXml.GetChildContent(loo_Path.GetAsString())
loo_StringCompare.SetString(ls_Expiration)
li_StringMatch12 = loo_StringCompare.ContentsEqual("",1)
if (loo_TokenXml.LastMethodSuccess <> 1) OR (li_StringMatch12 = 1) then
    Write-Debug "Missing credential field Expiration."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    return
end if

loo_Expires = create oleobject
li_rc = loo_Expires.ConnectToNewObject("Chilkat.CkDateTime")

loo_Now = create oleobject
li_rc = loo_Now.ConnectToNewObject("Chilkat.CkDateTime")

li_Success = loo_Expires.SetFromTimestamp(ls_Expiration)
if li_Success <> 1 then
    Write-Debug "Stored expiration is invalid."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    return
end if

li_Success = loo_Now.SetFromCurrentSystemTime()
if li_Success <> 1 then
    Write-Debug "Could not read current time."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    return
end if

if loo_Expires.DiffSeconds(loo_Now) <= 60 then
    Write-Debug "Stored credentials are expired or expire within 60 seconds; obtain fresh credentials."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    return
end if

loo_Session = create oleobject
li_rc = loo_Session.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Session.UpdateString("access_key",ls_AccessKey)
if li_Success <> 1 then
    Write-Debug "Could not build normalized session."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    return
end if

li_Success = loo_Session.UpdateString("secret_key",ls_SecretKey)
if li_Success <> 1 then
    Write-Debug "Could not build normalized session."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    return
end if

li_Success = loo_Session.UpdateString("session_token",ls_SessionToken)
if li_Success <> 1 then
    Write-Debug "Could not build normalized session."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    return
end if

li_Success = loo_Session.UpdateString("expiration",ls_Expiration)
if li_Success <> 1 then
    Write-Debug "Could not build normalized session."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    return
end if

ls_Region = "us-west-2"
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")

loo_Endpoint.Append("sts.")
loo_Endpoint.Append(ls_Region)
loo_Endpoint.Append(".amazonaws.com")
loo_Aws = create oleobject
li_rc = loo_Aws.ConnectToNewObject("Chilkat.AuthAws")

loo_Aws.AccessKey = loo_Session.StringOf("access_key")
loo_Aws.SecretKey = loo_Session.StringOf("secret_key")
loo_Aws.Region = ls_Region
loo_Aws.ServiceName = "sts"
loo_Aws.SignatureVersion = 4
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")

loo_Rest.ConnectTimeoutMs = 20000
loo_Rest.IdleTimeoutMs = 60000
loo_Rest.VerboseLogging = 0
li_Success = loo_Rest.SetAuthAws(loo_Aws)
if li_Success <> 1 then
    Write-Debug "Could not configure request signing."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    return
end if

//  Keep this verified Socket alive while Rest uses its connection.
loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")

loo_Connection.RequireSslCertVerify = 1
loo_Connection.MaxReadIdleMs = 60000
loo_Connection.MaxSendIdleMs = 60000
li_Success = loo_Connection.Connect(loo_Endpoint.GetAsString(),443,1,20000)
if li_Success <> 1 then
    Write-Debug "TLS connection failed; inspect diagnostics privately."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    return
end if

li_Success = loo_Rest.UseConnection(loo_Connection,1)
if li_Success <> 1 then
    Write-Debug "Could not attach TLS connection."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    return
end if

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")

loo_Xml.Strict = 1

li_Success = loo_Rest.ClearAllQueryParams()
if li_Success <> 1 then
    Write-Debug "Could not reset request parameters."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

li_Success = loo_Rest.AddQueryParam("Action","GetCallerIdentity")
if li_Success <> 1 then
    Write-Debug "Could not set Action."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

li_Success = loo_Rest.AddQueryParam("Version","2011-06-15")
if li_Success <> 1 then
    Write-Debug "Could not set Version."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

li_Success = loo_Rest.AddQueryParam("X-Amz-Security-Token",loo_Session.StringOf("session_token"))
if li_Success <> 1 then
    Write-Debug "Could not set X-Amz-Security-Token."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

ls_ResponseXml = loo_Rest.FullRequestFormUrlEncoded("POST","/")
if loo_Rest.LastMethodSuccess <> 1 then
    Write-Debug "STS transport failed; inspect diagnostics privately."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

if loo_Rest.ResponseStatusCode <> 200 then
    Write-Debug "STS HTTP status: " + string(loo_Rest.ResponseStatusCode)
    li_Success = loo_Xml.LoadXml(ls_ResponseXml)
    if li_Success = 1 then
        ls_Value = loo_Xml.GetChildContent("Error|Code")
        if loo_Xml.LastMethodSuccess = 1 then
            Write-Debug "AWS error code: " + ls_Value
        end if

        ls_Value = loo_Xml.GetChildContent("RequestId")
        if loo_Xml.LastMethodSuccess = 1 then
            Write-Debug "AWS request ID: " + ls_Value
        end if

    end if

    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

li_Success = loo_Xml.LoadXml(ls_ResponseXml)
if li_Success <> 1 then
    Write-Debug "Invalid STS XML response."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

loo_StringCompare.SetString(loo_Xml.Tag)
li_StringMatch13 = loo_StringCompare.ContentsEqual("GetCallerIdentityResponse",1)
if li_StringMatch13 <> 1 then
    Write-Debug "Unexpected STS response root."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

ls_Value = loo_Xml.GetChildContent("ResponseMetadata|RequestId")
loo_StringCompare.SetString(ls_Value)
li_StringMatch14 = loo_StringCompare.ContentsEqual("",1)
if (loo_Xml.LastMethodSuccess <> 1) OR (li_StringMatch14 = 1) then
    Write-Debug "Missing or empty ResponseMetadata|RequestId."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

if loo_Xml.HasChildWithTag("GetCallerIdentityResult") <> 1 then
    Write-Debug "Missing STS result."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

ls_Value = loo_Xml.GetChildContent("GetCallerIdentityResult|Arn")
loo_StringCompare.SetString(ls_Value)
li_StringMatch15 = loo_StringCompare.ContentsEqual("",1)
if (loo_Xml.LastMethodSuccess <> 1) OR (li_StringMatch15 = 1) then
    Write-Debug "Missing or empty GetCallerIdentityResult|Arn."
    destroy loo_StringCompare
    destroy loo_TokenXml
    destroy loo_Path
    destroy loo_Expires
    destroy loo_Now
    destroy loo_Session
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

Write-Debug "Authenticated principal: " + ls_Value
Write-Debug "Example completed."


destroy loo_StringCompare
destroy loo_TokenXml
destroy loo_Path
destroy loo_Expires
destroy loo_Now
destroy loo_Session
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml