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

Go
    success := false

    stringCompare := chilkat.NewStringBuilder()
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    //  Keep this file private and outside public web content/source control.
    credentials := chilkat.NewJsonObject()
    credentials.SetStrict(true)
    success = credentials.LoadFile("qa_data/aws/sts.json")
    if success != true {
        fmt.Println("Could not load private STS configuration.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        return
    }

    stringCompare.SetString(*credentials.StringOf("access_key"))
    stringMatch1 := stringCompare.ContentsEqual("",true)
    if (credentials.JsonTypeOf("access_key") != 1) || (stringMatch1 == true) {
        fmt.Println("Missing or invalid access_key.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        return
    }

    stringCompare.SetString(*credentials.StringOf("secret_key"))
    stringMatch2 := stringCompare.ContentsEqual("",true)
    if (credentials.JsonTypeOf("secret_key") != 1) || (stringMatch2 == true) {
        fmt.Println("Missing or invalid secret_key.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        return
    }

    expires := chilkat.NewCkDateTime()
    now := chilkat.NewCkDateTime()
    region := "us-west-2"
    if credentials.HasMember("session_token") == true {
        stringCompare.SetString(*credentials.StringOf("session_token"))
        stringMatch3 := stringCompare.ContentsEqual("",true)
        if (credentials.JsonTypeOf("session_token") != 1) || (stringMatch3 == true) {
            fmt.Println("Missing or invalid session_token.")
            stringCompare.DisposeStringBuilder()
            credentials.DisposeJsonObject()
            expires.DisposeCkDateTime()
            now.DisposeCkDateTime()
            return
        }

        stringCompare.SetString(*credentials.StringOf("expiration"))
        stringMatch4 := stringCompare.ContentsEqual("",true)
        if (credentials.JsonTypeOf("expiration") != 1) || (stringMatch4 == true) {
            fmt.Println("Missing or invalid expiration.")
            stringCompare.DisposeStringBuilder()
            credentials.DisposeJsonObject()
            expires.DisposeCkDateTime()
            now.DisposeCkDateTime()
            return
        }

        success = expires.SetFromTimestamp(*credentials.StringOf("expiration"))
        if success != true {
            fmt.Println("Source expiration is invalid.")
            stringCompare.DisposeStringBuilder()
            credentials.DisposeJsonObject()
            expires.DisposeCkDateTime()
            now.DisposeCkDateTime()
            return
        }

        success = now.SetFromCurrentSystemTime()
        if success != true {
            fmt.Println("Could not read current time.")
            stringCompare.DisposeStringBuilder()
            credentials.DisposeJsonObject()
            expires.DisposeCkDateTime()
            now.DisposeCkDateTime()
            return
        }

        if expires.DiffSeconds(now) <= 60 {
            fmt.Println("Source credentials are expired or expire within 60 seconds; obtain fresh credentials.")
            stringCompare.DisposeStringBuilder()
            credentials.DisposeJsonObject()
            expires.DisposeCkDateTime()
            now.DisposeCkDateTime()
            return
        }

    }

    stringCompare.SetString(*credentials.StringOf("audience"))
    stringMatch5 := stringCompare.ContentsEqual("",true)
    if (credentials.JsonTypeOf("audience") != 1) || (stringMatch5 == true) {
        fmt.Println("Missing or invalid audience.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        return
    }

    //  Use AWSIdentity by default. OriginIdentity requires trusted propagation context.
    var subjectMode *string = new(string)
    *subjectMode = "AWSIdentity"
    if credentials.HasMember("subject_mode") == true {
        stringCompare.SetString(*credentials.StringOf("subject_mode"))
        stringMatch6 := stringCompare.ContentsEqual("",true)
        if (credentials.JsonTypeOf("subject_mode") != 1) || (stringMatch6 == true) {
            fmt.Println("Missing or invalid subject_mode.")
            stringCompare.DisposeStringBuilder()
            credentials.DisposeJsonObject()
            expires.DisposeCkDateTime()
            now.DisposeCkDateTime()
            return
        }

        subjectMode = credentials.StringOf("subject_mode")
    }

    stringCompare.SetString(*subjectMode)
    stringMatch7 := stringCompare.ContentsEqual("AWSIdentity",true)
    stringCompare.SetString(*subjectMode)
    stringMatch8 := stringCompare.ContentsEqual("OriginIdentity",true)
    if (stringMatch7 != true) && (stringMatch8 != true) {
        fmt.Println("Invalid subject_mode.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        return
    }

    if credentials.HasMember("scope") == true {
        stringCompare.SetString(*credentials.StringOf("scope"))
        stringMatch9 := stringCompare.ContentsEqual("",true)
        if (credentials.JsonTypeOf("scope") != 1) || (stringMatch9 == true) {
            fmt.Println("Missing or invalid scope.")
            stringCompare.DisposeStringBuilder()
            credentials.DisposeJsonObject()
            expires.DisposeCkDateTime()
            now.DisposeCkDateTime()
            return
        }

        stringCompare.SetString(*subjectMode)
        stringMatch10 := stringCompare.ContentsEqual("OriginIdentity",true)
        if stringMatch10 != true {
            fmt.Println("scope requires OriginIdentity.")
            stringCompare.DisposeStringBuilder()
            credentials.DisposeJsonObject()
            expires.DisposeCkDateTime()
            now.DisposeCkDateTime()
            return
        }

        scopeText := chilkat.NewStringBuilder()
        scopeText.Append(*credentials.StringOf("scope"))
        if (scopeText.Length() > 256) || (scopeText.StartsWith(" ",true) == true) || (scopeText.EndsWith(" ",true) == true) || (scopeText.Contains("  ",true) == true) {
            fmt.Println("Invalid scope length or spacing.")
            stringCompare.DisposeStringBuilder()
            credentials.DisposeJsonObject()
            expires.DisposeCkDateTime()
            now.DisposeCkDateTime()
            scopeText.DisposeStringBuilder()
            return
        }

        allowedScope := chilkat.NewStringBuilder()
        allowedScope.Append("!#$%&'()*+-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~ ")
        scopeIndex := 0
        for (scopeIndex < scopeText.Length()) {
            if allowedScope.Contains(*scopeText.GetRange(scopeIndex,1,false),true) != true {
                fmt.Println("Invalid scope character.")
                stringCompare.DisposeStringBuilder()
                credentials.DisposeJsonObject()
                expires.DisposeCkDateTime()
                now.DisposeCkDateTime()
                scopeText.DisposeStringBuilder()
                allowedScope.DisposeStringBuilder()
                return
            }

            scopeIndex = scopeIndex + 1
        }

        scopeValues := chilkat.NewStringTable()
        success = scopeValues.SplitAndAppend(*scopeText.GetAsString()," ",false,false)
        if success != true {
            fmt.Println("Could not split scope values.")
            stringCompare.DisposeStringBuilder()
            credentials.DisposeJsonObject()
            expires.DisposeCkDateTime()
            now.DisposeCkDateTime()
            scopeText.DisposeStringBuilder()
            allowedScope.DisposeStringBuilder()
            scopeValues.DisposeStringTable()
            return
        }

        seenScopes := chilkat.NewHashtable()
        scopeIndex = 0
        var scopeValue *string = new(string)
        for (scopeIndex < scopeValues.Count()) {
            scopeValue = scopeValues.StringAt(scopeIndex)
            if seenScopes.Contains(*scopeValue) == true {
                fmt.Println("Duplicate scope value.")
                stringCompare.DisposeStringBuilder()
                credentials.DisposeJsonObject()
                expires.DisposeCkDateTime()
                now.DisposeCkDateTime()
                scopeText.DisposeStringBuilder()
                allowedScope.DisposeStringBuilder()
                scopeValues.DisposeStringTable()
                seenScopes.DisposeHashtable()
                return
            }

            success = seenScopes.AddStr(*scopeValue,"seen")
            if success != true {
                fmt.Println("Could not track scope values.")
                stringCompare.DisposeStringBuilder()
                credentials.DisposeJsonObject()
                expires.DisposeCkDateTime()
                now.DisposeCkDateTime()
                scopeText.DisposeStringBuilder()
                allowedScope.DisposeStringBuilder()
                scopeValues.DisposeStringTable()
                seenScopes.DisposeHashtable()
                return
            }

            scopeIndex = scopeIndex + 1
        }

    }

    endpoint := chilkat.NewStringBuilder()
    endpoint.Append("sts.")
    endpoint.Append(region)
    endpoint.Append(".amazonaws.com")
    aws := chilkat.NewAuthAws()
    aws.SetAccessKey(credentials.StringOf("access_key"))
    aws.SetSecretKey(credentials.StringOf("secret_key"))
    aws.SetRegion(region)
    aws.SetServiceName("sts")
    aws.SetSignatureVersion(4)
    rest := chilkat.NewRest()
    rest.SetConnectTimeoutMs(20000)
    rest.SetIdleTimeoutMs(60000)
    rest.SetVerboseLogging(false)
    success = rest.SetAuthAws(aws)
    if success != true {
        fmt.Println("Could not configure request signing.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        return
    }

    //  Keep this verified Socket alive while Rest uses its connection.
    connection := chilkat.NewSocket()
    connection.SetRequireSslCertVerify(true)
    connection.SetMaxReadIdleMs(60000)
    connection.SetMaxSendIdleMs(60000)
    success = connection.Connect(*endpoint.GetAsString(),443,true,20000)
    if success != true {
        fmt.Println("TLS connection failed; inspect diagnostics privately.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        return
    }

    success = rest.UseConnection(connection,true)
    if success != true {
        fmt.Println("Could not attach TLS connection.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        return
    }

    xml := chilkat.NewXml()
    xml.SetStrict(true)
    var value *string = new(string)
    var sessionToken *string = new(string)
    var expiration *string = new(string)
    var responseXml *string = new(string)
    *responseXml = ""
    success = rest.ClearAllQueryParams()
    if success != true {
        fmt.Println("Could not reset request parameters.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    success = rest.AddQueryParam("Action","GetWebIdentityToken")
    if success != true {
        fmt.Println("Could not set Action.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    success = rest.AddQueryParam("Version","2011-06-15")
    if success != true {
        fmt.Println("Could not set Version.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    if credentials.HasMember("session_token") == true {
        success = rest.AddQueryParam("X-Amz-Security-Token",*credentials.StringOf("session_token"))
        if success != true {
            fmt.Println("Could not set X-Amz-Security-Token.")
            stringCompare.DisposeStringBuilder()
            credentials.DisposeJsonObject()
            expires.DisposeCkDateTime()
            now.DisposeCkDateTime()
            scopeText.DisposeStringBuilder()
            allowedScope.DisposeStringBuilder()
            scopeValues.DisposeStringTable()
            seenScopes.DisposeHashtable()
            endpoint.DisposeStringBuilder()
            aws.DisposeAuthAws()
            rest.DisposeRest()
            connection.DisposeSocket()
            xml.DisposeXml()
            return
        }

    }

    success = rest.AddQueryParam("Audience.member.1",*credentials.StringOf("audience"))
    if success != true {
        fmt.Println("Could not set Audience.member.1.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    success = rest.AddQueryParam("SigningAlgorithm","RS256")
    if success != true {
        fmt.Println("Could not set SigningAlgorithm.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    success = rest.AddQueryParam("DurationSeconds","300")
    if success != true {
        fmt.Println("Could not set DurationSeconds.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    success = rest.AddQueryParam("SubjectMode",*subjectMode)
    if success != true {
        fmt.Println("Could not set SubjectMode.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    if credentials.HasMember("scope") == true {
        success = rest.AddQueryParam("Scope",*credentials.StringOf("scope"))
        if success != true {
            fmt.Println("Could not set Scope.")
            stringCompare.DisposeStringBuilder()
            credentials.DisposeJsonObject()
            expires.DisposeCkDateTime()
            now.DisposeCkDateTime()
            scopeText.DisposeStringBuilder()
            allowedScope.DisposeStringBuilder()
            scopeValues.DisposeStringTable()
            seenScopes.DisposeHashtable()
            endpoint.DisposeStringBuilder()
            aws.DisposeAuthAws()
            rest.DisposeRest()
            connection.DisposeSocket()
            xml.DisposeXml()
            return
        }

    }

    responseXml = rest.FullRequestFormUrlEncoded("POST","/")
    if rest.LastMethodSuccess() != true {
        fmt.Println("STS transport failed; inspect diagnostics privately.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    if rest.ResponseStatusCode() != 200 {
        fmt.Println("STS HTTP status: ", rest.ResponseStatusCode())
        success = xml.LoadXml(*responseXml)
        if success == true {
            value = xml.GetChildContent("Error|Code")
            if xml.LastMethodSuccess() == true {
                fmt.Println("AWS error code: ", *value)
            }

            value = xml.GetChildContent("RequestId")
            if xml.LastMethodSuccess() == true {
                fmt.Println("AWS request ID: ", *value)
            }

        }

        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    success = xml.LoadXml(*responseXml)
    if success != true {
        fmt.Println("Invalid STS XML response.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    stringCompare.SetString(xml.Tag())
    stringMatch11 := stringCompare.ContentsEqual("GetWebIdentityTokenResponse",true)
    if stringMatch11 != true {
        fmt.Println("Unexpected STS response root.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    value = xml.GetChildContent("ResponseMetadata|RequestId")
    stringCompare.SetString(*value)
    stringMatch12 := stringCompare.ContentsEqual("",true)
    if (xml.LastMethodSuccess() != true) || (stringMatch12 == true) {
        fmt.Println("Missing or empty ResponseMetadata|RequestId.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    if xml.HasChildWithTag("GetWebIdentityTokenResult") != true {
        fmt.Println("Missing STS result.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    sessionToken = xml.GetChildContent("GetWebIdentityTokenResult|WebIdentityToken")
    stringCompare.SetString(*sessionToken)
    stringMatch13 := stringCompare.ContentsEqual("",true)
    if (xml.LastMethodSuccess() != true) || (stringMatch13 == true) {
        fmt.Println("Missing or empty GetWebIdentityTokenResult|WebIdentityToken.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    expiration = xml.GetChildContent("GetWebIdentityTokenResult|Expiration")
    stringCompare.SetString(*expiration)
    stringMatch14 := stringCompare.ContentsEqual("",true)
    if (xml.LastMethodSuccess() != true) || (stringMatch14 == true) {
        fmt.Println("Missing or empty GetWebIdentityTokenResult|Expiration.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    success = expires.SetFromTimestamp(*expiration)
    if success != true {
        fmt.Println("JWT expiration is invalid.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    success = now.SetFromCurrentSystemTime()
    if success != true {
        fmt.Println("Could not read current time.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    if expires.DiffSeconds(now) <= 60 {
        fmt.Println("JWT credentials are expired or expire within 60 seconds; obtain fresh credentials.")
        stringCompare.DisposeStringBuilder()
        credentials.DisposeJsonObject()
        expires.DisposeCkDateTime()
        now.DisposeCkDateTime()
        scopeText.DisposeStringBuilder()
        allowedScope.DisposeStringBuilder()
        scopeValues.DisposeStringTable()
        seenScopes.DisposeHashtable()
        endpoint.DisposeStringBuilder()
        aws.DisposeAuthAws()
        rest.DisposeRest()
        connection.DisposeSocket()
        xml.DisposeXml()
        return
    }

    //  Hand sessionToken to the intended external service over TLS; never log it.
    fmt.Println("Web identity token obtained; expiration: ", *expiration)
    fmt.Println("Example completed.")

    stringCompare.DisposeStringBuilder()
    credentials.DisposeJsonObject()
    expires.DisposeCkDateTime()
    now.DisposeCkDateTime()
    scopeText.DisposeStringBuilder()
    allowedScope.DisposeStringBuilder()
    scopeValues.DisposeStringTable()
    seenScopes.DisposeHashtable()
    endpoint.DisposeStringBuilder()
    aws.DisposeAuthAws()
    rest.DisposeRest()
    connection.DisposeSocket()
    xml.DisposeXml()