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

AWS STS: Obtain an AWS-Signed JWT for an External Service

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.

Set audience to the intended external service. Requests RS256 with a five-minute lifetime from a regional STS endpoint; outbound federation must be enabled for the account. Optional subject_mode is AWSIdentity or OriginIdentity. OriginIdentity requires the caller's trusted identity propagation context. Optional scope is permitted only for OriginIdentity and this single audience, and requires sts:SetWebIdentityTokenScope in addition to sts:GetWebIdentityToken. Validates scope spacing, characters and duplicate values.

The returned token is an external identity JWT, not an AWS access key/session-token credential set. The recipient must validate its signature using the trusted issuer's JWKS, issuer, audience and lifetime; merely decoding it is insufficient. The request cannot extend the calling session's lifetime.

AWS references: GetWebIdentityToken

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_StringCompare
oleobject loo_Credentials
long li_StringMatch1
long li_StringMatch2
oleobject loo_Expires
oleobject loo_Now
string ls_Region
long li_StringMatch3
long li_StringMatch4
long li_StringMatch5
string ls_SubjectMode
long li_StringMatch6
long li_StringMatch7
long li_StringMatch8
long li_StringMatch9
long li_StringMatch10
oleobject loo_ScopeText
oleobject loo_AllowedScope
long li_ScopeIndex
oleobject loo_ScopeValues
oleobject loo_SeenScopes
string ls_ScopeValue
oleobject loo_Endpoint
oleobject loo_Aws
oleobject loo_Rest
oleobject loo_Connection
oleobject loo_Xml
string ls_Value
string ls_SessionToken
string ls_Expiration
string ls_ResponseXml
long li_StringMatch11
long li_StringMatch12
long li_StringMatch13
long li_StringMatch14

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
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Keep this file private and outside public web content/source control.
loo_Credentials = create oleobject
li_rc = loo_Credentials.ConnectToNewObject("Chilkat.JsonObject")

loo_Credentials.Strict = 1
li_Success = loo_Credentials.LoadFile("qa_data/aws/sts.json")
if li_Success <> 1 then
    Write-Debug "Could not load private STS configuration."
    destroy loo_StringCompare
    destroy loo_Credentials
    return
end if

loo_StringCompare.SetString(loo_Credentials.StringOf("access_key"))
li_StringMatch1 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (li_StringMatch1 = 1) then
    Write-Debug "Missing or invalid access_key."
    destroy loo_StringCompare
    destroy loo_Credentials
    return
end if

loo_StringCompare.SetString(loo_Credentials.StringOf("secret_key"))
li_StringMatch2 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (li_StringMatch2 = 1) then
    Write-Debug "Missing or invalid secret_key."
    destroy loo_StringCompare
    destroy loo_Credentials
    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")

ls_Region = "us-west-2"
if loo_Credentials.HasMember("session_token") = 1 then
    loo_StringCompare.SetString(loo_Credentials.StringOf("session_token"))
    li_StringMatch3 = loo_StringCompare.ContentsEqual("",1)
    if (loo_Credentials.JsonTypeOf("session_token") <> 1) OR (li_StringMatch3 = 1) then
        Write-Debug "Missing or invalid session_token."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Expires
        destroy loo_Now
        return
    end if

    loo_StringCompare.SetString(loo_Credentials.StringOf("expiration"))
    li_StringMatch4 = loo_StringCompare.ContentsEqual("",1)
    if (loo_Credentials.JsonTypeOf("expiration") <> 1) OR (li_StringMatch4 = 1) then
        Write-Debug "Missing or invalid expiration."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Expires
        destroy loo_Now
        return
    end if

    li_Success = loo_Expires.SetFromTimestamp(loo_Credentials.StringOf("expiration"))
    if li_Success <> 1 then
        Write-Debug "Source expiration is invalid."
        destroy loo_StringCompare
        destroy loo_Credentials
        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_Credentials
        destroy loo_Expires
        destroy loo_Now
        return
    end if

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

end if

loo_StringCompare.SetString(loo_Credentials.StringOf("audience"))
li_StringMatch5 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("audience") <> 1) OR (li_StringMatch5 = 1) then
    Write-Debug "Missing or invalid audience."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Expires
    destroy loo_Now
    return
end if

//  Use AWSIdentity by default. OriginIdentity requires trusted propagation context.
ls_SubjectMode = "AWSIdentity"
if loo_Credentials.HasMember("subject_mode") = 1 then
    loo_StringCompare.SetString(loo_Credentials.StringOf("subject_mode"))
    li_StringMatch6 = loo_StringCompare.ContentsEqual("",1)
    if (loo_Credentials.JsonTypeOf("subject_mode") <> 1) OR (li_StringMatch6 = 1) then
        Write-Debug "Missing or invalid subject_mode."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Expires
        destroy loo_Now
        return
    end if

    ls_SubjectMode = loo_Credentials.StringOf("subject_mode")
end if

loo_StringCompare.SetString(ls_SubjectMode)
li_StringMatch7 = loo_StringCompare.ContentsEqual("AWSIdentity",1)
loo_StringCompare.SetString(ls_SubjectMode)
li_StringMatch8 = loo_StringCompare.ContentsEqual("OriginIdentity",1)
if (li_StringMatch7 <> 1) AND (li_StringMatch8 <> 1) then
    Write-Debug "Invalid subject_mode."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Expires
    destroy loo_Now
    return
end if

if loo_Credentials.HasMember("scope") = 1 then
    loo_StringCompare.SetString(loo_Credentials.StringOf("scope"))
    li_StringMatch9 = loo_StringCompare.ContentsEqual("",1)
    if (loo_Credentials.JsonTypeOf("scope") <> 1) OR (li_StringMatch9 = 1) then
        Write-Debug "Missing or invalid scope."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Expires
        destroy loo_Now
        return
    end if

    loo_StringCompare.SetString(ls_SubjectMode)
    li_StringMatch10 = loo_StringCompare.ContentsEqual("OriginIdentity",1)
    if li_StringMatch10 <> 1 then
        Write-Debug "scope requires OriginIdentity."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Expires
        destroy loo_Now
        return
    end if

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

    loo_ScopeText.Append(loo_Credentials.StringOf("scope"))
    if (loo_ScopeText.Length > 256) OR (loo_ScopeText.StartsWith(" ",1) = 1) OR (loo_ScopeText.EndsWith(" ",1) = 1) OR (loo_ScopeText.Contains("  ",1) = 1) then
        Write-Debug "Invalid scope length or spacing."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Expires
        destroy loo_Now
        destroy loo_ScopeText
        return
    end if

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

    loo_AllowedScope.Append("!#$%&'()*+-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~~ ")
    li_ScopeIndex = 0
    do while (li_ScopeIndex < loo_ScopeText.Length)
        if loo_AllowedScope.Contains(loo_ScopeText.GetRange(li_ScopeIndex,1,0),1) <> 1 then
            Write-Debug "Invalid scope character."
            destroy loo_StringCompare
            destroy loo_Credentials
            destroy loo_Expires
            destroy loo_Now
            destroy loo_ScopeText
            destroy loo_AllowedScope
            return
        end if

        li_ScopeIndex = li_ScopeIndex + 1
    loop
    loo_ScopeValues = create oleobject
    li_rc = loo_ScopeValues.ConnectToNewObject("Chilkat.StringTable")

    li_Success = loo_ScopeValues.SplitAndAppend(loo_ScopeText.GetAsString()," ",0,0)
    if li_Success <> 1 then
        Write-Debug "Could not split scope values."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Expires
        destroy loo_Now
        destroy loo_ScopeText
        destroy loo_AllowedScope
        destroy loo_ScopeValues
        return
    end if

    loo_SeenScopes = create oleobject
    li_rc = loo_SeenScopes.ConnectToNewObject("Chilkat.Hashtable")

    li_ScopeIndex = 0

    do while (li_ScopeIndex < loo_ScopeValues.Count)
        ls_ScopeValue = loo_ScopeValues.StringAt(li_ScopeIndex)
        if loo_SeenScopes.Contains(ls_ScopeValue) = 1 then
            Write-Debug "Duplicate scope value."
            destroy loo_StringCompare
            destroy loo_Credentials
            destroy loo_Expires
            destroy loo_Now
            destroy loo_ScopeText
            destroy loo_AllowedScope
            destroy loo_ScopeValues
            destroy loo_SeenScopes
            return
        end if

        li_Success = loo_SeenScopes.AddStr(ls_ScopeValue,"seen")
        if li_Success <> 1 then
            Write-Debug "Could not track scope values."
            destroy loo_StringCompare
            destroy loo_Credentials
            destroy loo_Expires
            destroy loo_Now
            destroy loo_ScopeText
            destroy loo_AllowedScope
            destroy loo_ScopeValues
            destroy loo_SeenScopes
            return
        end if

        li_ScopeIndex = li_ScopeIndex + 1
    loop
end if

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_Credentials.StringOf("access_key")
loo_Aws.SecretKey = loo_Credentials.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_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    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_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    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_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    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

ls_ResponseXml = ""
li_Success = loo_Rest.ClearAllQueryParams()
if li_Success <> 1 then
    Write-Debug "Could not reset request parameters."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

li_Success = loo_Rest.AddQueryParam("Action","GetWebIdentityToken")
if li_Success <> 1 then
    Write-Debug "Could not set Action."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    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_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

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

end if

li_Success = loo_Rest.AddQueryParam("Audience.member.1",loo_Credentials.StringOf("audience"))
if li_Success <> 1 then
    Write-Debug "Could not set Audience.member.1."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

li_Success = loo_Rest.AddQueryParam("SigningAlgorithm","RS256")
if li_Success <> 1 then
    Write-Debug "Could not set SigningAlgorithm."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

li_Success = loo_Rest.AddQueryParam("DurationSeconds","300")
if li_Success <> 1 then
    Write-Debug "Could not set DurationSeconds."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

li_Success = loo_Rest.AddQueryParam("SubjectMode",ls_SubjectMode)
if li_Success <> 1 then
    Write-Debug "Could not set SubjectMode."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

if loo_Credentials.HasMember("scope") = 1 then
    li_Success = loo_Rest.AddQueryParam("Scope",loo_Credentials.StringOf("scope"))
    if li_Success <> 1 then
        Write-Debug "Could not set Scope."
        destroy loo_StringCompare
        destroy loo_Credentials
        destroy loo_Expires
        destroy loo_Now
        destroy loo_ScopeText
        destroy loo_AllowedScope
        destroy loo_ScopeValues
        destroy loo_SeenScopes
        destroy loo_Endpoint
        destroy loo_Aws
        destroy loo_Rest
        destroy loo_Connection
        destroy loo_Xml
        return
    end if

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_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    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_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    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_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    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_StringMatch11 = loo_StringCompare.ContentsEqual("GetWebIdentityTokenResponse",1)
if li_StringMatch11 <> 1 then
    Write-Debug "Unexpected STS response root."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    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_StringMatch12 = loo_StringCompare.ContentsEqual("",1)
if (loo_Xml.LastMethodSuccess <> 1) OR (li_StringMatch12 = 1) then
    Write-Debug "Missing or empty ResponseMetadata|RequestId."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

if loo_Xml.HasChildWithTag("GetWebIdentityTokenResult") <> 1 then
    Write-Debug "Missing STS result."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

ls_SessionToken = loo_Xml.GetChildContent("GetWebIdentityTokenResult|WebIdentityToken")
loo_StringCompare.SetString(ls_SessionToken)
li_StringMatch13 = loo_StringCompare.ContentsEqual("",1)
if (loo_Xml.LastMethodSuccess <> 1) OR (li_StringMatch13 = 1) then
    Write-Debug "Missing or empty GetWebIdentityTokenResult|WebIdentityToken."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

ls_Expiration = loo_Xml.GetChildContent("GetWebIdentityTokenResult|Expiration")
loo_StringCompare.SetString(ls_Expiration)
li_StringMatch14 = loo_StringCompare.ContentsEqual("",1)
if (loo_Xml.LastMethodSuccess <> 1) OR (li_StringMatch14 = 1) then
    Write-Debug "Missing or empty GetWebIdentityTokenResult|Expiration."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

li_Success = loo_Expires.SetFromTimestamp(ls_Expiration)
if li_Success <> 1 then
    Write-Debug "JWT expiration is invalid."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    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_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

if loo_Expires.DiffSeconds(loo_Now) <= 60 then
    Write-Debug "JWT credentials are expired or expire within 60 seconds; obtain fresh credentials."
    destroy loo_StringCompare
    destroy loo_Credentials
    destroy loo_Expires
    destroy loo_Now
    destroy loo_ScopeText
    destroy loo_AllowedScope
    destroy loo_ScopeValues
    destroy loo_SeenScopes
    destroy loo_Endpoint
    destroy loo_Aws
    destroy loo_Rest
    destroy loo_Connection
    destroy loo_Xml
    return
end if

//  Hand sessionToken to the intended external service over TLS; never log it.
Write-Debug "Web identity token obtained; expiration: " + ls_Expiration
Write-Debug "Example completed."


destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_ScopeText
destroy loo_AllowedScope
destroy loo_ScopeValues
destroy loo_SeenScopes
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml