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

AWS STS: Refresh an AssumeRole Session Before Expiration

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.

Configure source credentials, role_arn and role_session_name for an unattended role that does not require interactive MFA. Optional qa_data/tokens/sts_session.json holds a previous normalized session for exactly that role, session configuration and workload; keep this cache private and never reuse it across roles. A complete cached set with more than five minutes remaining is reused. Otherwise obtains a new one-hour session, validates it, and publishes one serialized in-memory snapshot. No cache file is overwritten. Existing credentials cannot be extended.

Retries throttling and selected transient HTTP errors at most three times with one- and two-second waits, rebuilding the signed form and checking source expiration on each attempt. Transport, authorization and invalid-response failures stop without publication. Add jitter, synchronization and a credential provider appropriate to your runtime in a concurrent application. This single-thread sample does not implement shared-cache locking or reuse expired sources/MFA codes. Repeat the refresh decision before each unit of work.

AWS references: AssumeRole

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loStringCompare
LOCAL loCredentials
LOCAL lnStringMatch1
LOCAL lnStringMatch2
LOCAL loExpires
LOCAL loNow
LOCAL lcRegion
LOCAL lnStringMatch3
LOCAL lnStringMatch4
LOCAL lnStringMatch5
LOCAL lnStringMatch6
LOCAL loIdentifier
LOCAL loAllowed
LOCAL lnCharIndex
LOCAL loFiles
LOCAL loCached
LOCAL lcActiveSnapshot
LOCAL lnRefreshRequired
LOCAL lnStringMatch7
LOCAL lnStringMatch8
LOCAL lnStringMatch9
LOCAL lnStringMatch10
LOCAL loEndpoint
LOCAL loAws
LOCAL loRest
LOCAL loConnection
LOCAL loXml
LOCAL lcValue
LOCAL lcAccessKey
LOCAL lcSecretKey
LOCAL lcSessionToken
LOCAL lcExpiration
LOCAL lcResponseXml
LOCAL lnAttempt
LOCAL lnDelayMs
LOCAL lnReceived
LOCAL lnRetryable
LOCAL lnStringMatch11
LOCAL lnStringMatch12
LOCAL lnStringMatch13
LOCAL lnStringMatch14
LOCAL lnStringMatch15
LOCAL lnStringMatch16
LOCAL lnStringMatch17
LOCAL lnStringMatch18
LOCAL lnStringMatch19
LOCAL lnStringMatch20
LOCAL lnStringMatch21
LOCAL lnStringMatch22
LOCAL loSession
LOCAL loUsable

lnSuccess = 0

loStringCompare = CreateObject('Chilkat.StringBuilder')
*  Assumes Chilkat is unlocked. See Global Unlock Sample.
*  Keep this file private and outside public web content/source control.
loCredentials = CreateObject('Chilkat.JsonObject')
loCredentials.Strict = 1
lnSuccess = loCredentials.LoadFile("qa_data/aws/sts.json")
IF (lnSuccess <> 1) THEN
    ? "Could not load private STS configuration."
    RELEASE loStringCompare
    RELEASE loCredentials
    CANCEL
ENDIF

loStringCompare.SetString(loCredentials.StringOf("access_key"))
lnStringMatch1 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("access_key") <> 1) OR (lnStringMatch1 = 1)) THEN
    ? "Missing or invalid access_key."
    RELEASE loStringCompare
    RELEASE loCredentials
    CANCEL
ENDIF

loStringCompare.SetString(loCredentials.StringOf("secret_key"))
lnStringMatch2 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("secret_key") <> 1) OR (lnStringMatch2 = 1)) THEN
    ? "Missing or invalid secret_key."
    RELEASE loStringCompare
    RELEASE loCredentials
    CANCEL
ENDIF

loExpires = CreateObject('Chilkat.CkDateTime')
loNow = CreateObject('Chilkat.CkDateTime')
lcRegion = "us-west-2"
IF (loCredentials.HasMember("session_token") = 1) THEN
    loStringCompare.SetString(loCredentials.StringOf("session_token"))
    lnStringMatch3 = loStringCompare.ContentsEqual("",1)
    IF ((loCredentials.JsonTypeOf("session_token") <> 1) OR (lnStringMatch3 = 1)) THEN
        ? "Missing or invalid session_token."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        CANCEL
    ENDIF

    loStringCompare.SetString(loCredentials.StringOf("expiration"))
    lnStringMatch4 = loStringCompare.ContentsEqual("",1)
    IF ((loCredentials.JsonTypeOf("expiration") <> 1) OR (lnStringMatch4 = 1)) THEN
        ? "Missing or invalid expiration."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        CANCEL
    ENDIF

    lnSuccess = loExpires.SetFromTimestamp(loCredentials.StringOf("expiration"))
    IF (lnSuccess <> 1) THEN
        ? "Source expiration is invalid."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        CANCEL
    ENDIF

    lnSuccess = loNow.SetFromCurrentSystemTime()
    IF (lnSuccess <> 1) THEN
        ? "Could not read current time."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        CANCEL
    ENDIF

    IF (loExpires.DiffSeconds(loNow) <= 60) THEN
        ? "Source credentials are expired or expire within 60 seconds; obtain fresh credentials."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        CANCEL
    ENDIF

ENDIF

loStringCompare.SetString(loCredentials.StringOf("role_arn"))
lnStringMatch5 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("role_arn") <> 1) OR (lnStringMatch5 = 1)) THEN
    ? "Missing or invalid role_arn."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    CANCEL
ENDIF

loStringCompare.SetString(loCredentials.StringOf("role_session_name"))
lnStringMatch6 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("role_session_name") <> 1) OR (lnStringMatch6 = 1)) THEN
    ? "Missing or invalid role_session_name."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    CANCEL
ENDIF

loIdentifier = CreateObject('Chilkat.StringBuilder')
loIdentifier.Append(loCredentials.StringOf("role_session_name"))
IF ((loIdentifier.Length < 2) OR (loIdentifier.Length > 64)) THEN
    ? "role_session_name must have 2-64 characters."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    CANCEL
ENDIF

loAllowed = CreateObject('Chilkat.StringBuilder')
loAllowed.Append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+=,.@-")
lnCharIndex = 0
DO WHILE (lnCharIndex < loIdentifier.Length)
    IF (loAllowed.Contains(loIdentifier.GetRange(lnCharIndex,1,0),1) <> 1) THEN
        ? "Invalid character in role_session_name."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        CANCEL
    ENDIF

    lnCharIndex = lnCharIndex + 1
ENDDO
IF ((loCredentials.HasMember("mfa_code") = 1) OR (loCredentials.HasMember("mfa_serial") = 1)) THEN
    ? "This unattended refresh example does not reuse MFA codes."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    CANCEL
ENDIF

*  Optional private cache contains only the four normalized session fields.
*  In an application, keep activeSnapshot in memory between units of work.
loFiles = CreateObject('Chilkat.FileAccess')
loCached = CreateObject('Chilkat.JsonObject')
loCached.Strict = 1
lcActiveSnapshot = ""
lnRefreshRequired = 1
IF (loFiles.FileExists("qa_data/tokens/sts_session.json") = 1) THEN
    lnSuccess = loCached.LoadFile("qa_data/tokens/sts_session.json")
    IF (lnSuccess <> 1) THEN
        ? "Invalid private cache."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        CANCEL
    ENDIF

    loStringCompare.SetString(loCached.StringOf("access_key"))
    lnStringMatch7 = loStringCompare.ContentsEqual("",1)
    IF ((loCached.JsonTypeOf("access_key") <> 1) OR (lnStringMatch7 = 1)) THEN
        ? "Incomplete private cache."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        CANCEL
    ENDIF

    loStringCompare.SetString(loCached.StringOf("secret_key"))
    lnStringMatch8 = loStringCompare.ContentsEqual("",1)
    IF ((loCached.JsonTypeOf("secret_key") <> 1) OR (lnStringMatch8 = 1)) THEN
        ? "Incomplete private cache."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        CANCEL
    ENDIF

    loStringCompare.SetString(loCached.StringOf("session_token"))
    lnStringMatch9 = loStringCompare.ContentsEqual("",1)
    IF ((loCached.JsonTypeOf("session_token") <> 1) OR (lnStringMatch9 = 1)) THEN
        ? "Incomplete private cache."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        CANCEL
    ENDIF

    loStringCompare.SetString(loCached.StringOf("expiration"))
    lnStringMatch10 = loStringCompare.ContentsEqual("",1)
    IF ((loCached.JsonTypeOf("expiration") <> 1) OR (lnStringMatch10 = 1)) THEN
        ? "Incomplete private cache."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        CANCEL
    ENDIF

    lnSuccess = loExpires.SetFromTimestamp(loCached.StringOf("expiration"))
    IF (lnSuccess <> 1) THEN
        ? "Invalid cached expiration."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        CANCEL
    ENDIF

    lnSuccess = loNow.SetFromCurrentSystemTime()
    IF (lnSuccess <> 1) THEN
        ? "Could not read current time."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        CANCEL
    ENDIF

    IF (loExpires.DiffSeconds(loNow) > 300) THEN
        lcActiveSnapshot = loCached.Emit()
        lnRefreshRequired = 0
    ENDIF

ENDIF

IF (lnRefreshRequired = 1) THEN
    loEndpoint = CreateObject('Chilkat.StringBuilder')
    loEndpoint.Append("sts.")
    loEndpoint.Append(lcRegion)
    loEndpoint.Append(".amazonaws.com")
    loAws = CreateObject('Chilkat.AuthAws')
    loAws.AccessKey = loCredentials.StringOf("access_key")
    loAws.SecretKey = loCredentials.StringOf("secret_key")
    loAws.Region = lcRegion
    loAws.ServiceName = "sts"
    loAws.SignatureVersion = 4
    loRest = CreateObject('Chilkat.Rest')
    loRest.ConnectTimeoutMs = 20000
    loRest.IdleTimeoutMs = 60000
    loRest.VerboseLogging = 0
    lnSuccess = loRest.SetAuthAws(loAws)
    IF (lnSuccess <> 1) THEN
        ? "Could not configure request signing."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        CANCEL
    ENDIF

    *  Keep this verified Socket alive while Rest uses its connection.
    loConnection = CreateObject('Chilkat.Socket')
    loConnection.RequireSslCertVerify = 1
    loConnection.MaxReadIdleMs = 60000
    loConnection.MaxSendIdleMs = 60000
    lnSuccess = loConnection.Connect(loEndpoint.GetAsString(),443,1,20000)
    IF (lnSuccess <> 1) THEN
        ? "TLS connection failed; inspect diagnostics privately."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        CANCEL
    ENDIF

    lnSuccess = loRest.UseConnection(loConnection,1)
    IF (lnSuccess <> 1) THEN
        ? "Could not attach TLS connection."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        CANCEL
    ENDIF

    loXml = CreateObject('Chilkat.Xml')
    loXml.Strict = 1

    lcResponseXml = ""
    lnAttempt = 0
    lnDelayMs = 1000
    lnReceived = 0

    DO WHILE ((lnAttempt < 3) AND (lnReceived = 0))
        IF (loCredentials.HasMember("session_token") = 1) THEN
            loStringCompare.SetString(loCredentials.StringOf("session_token"))
            lnStringMatch11 = loStringCompare.ContentsEqual("",1)
            IF ((loCredentials.JsonTypeOf("session_token") <> 1) OR (lnStringMatch11 = 1)) THEN
                ? "Missing or invalid session_token."
                RELEASE loStringCompare
                RELEASE loCredentials
                RELEASE loExpires
                RELEASE loNow
                RELEASE loIdentifier
                RELEASE loAllowed
                RELEASE loFiles
                RELEASE loCached
                RELEASE loEndpoint
                RELEASE loAws
                RELEASE loRest
                RELEASE loConnection
                RELEASE loXml
                CANCEL
            ENDIF

            loStringCompare.SetString(loCredentials.StringOf("expiration"))
            lnStringMatch12 = loStringCompare.ContentsEqual("",1)
            IF ((loCredentials.JsonTypeOf("expiration") <> 1) OR (lnStringMatch12 = 1)) THEN
                ? "Missing or invalid expiration."
                RELEASE loStringCompare
                RELEASE loCredentials
                RELEASE loExpires
                RELEASE loNow
                RELEASE loIdentifier
                RELEASE loAllowed
                RELEASE loFiles
                RELEASE loCached
                RELEASE loEndpoint
                RELEASE loAws
                RELEASE loRest
                RELEASE loConnection
                RELEASE loXml
                CANCEL
            ENDIF

            lnSuccess = loExpires.SetFromTimestamp(loCredentials.StringOf("expiration"))
            IF (lnSuccess <> 1) THEN
                ? "Source expiration is invalid."
                RELEASE loStringCompare
                RELEASE loCredentials
                RELEASE loExpires
                RELEASE loNow
                RELEASE loIdentifier
                RELEASE loAllowed
                RELEASE loFiles
                RELEASE loCached
                RELEASE loEndpoint
                RELEASE loAws
                RELEASE loRest
                RELEASE loConnection
                RELEASE loXml
                CANCEL
            ENDIF

            lnSuccess = loNow.SetFromCurrentSystemTime()
            IF (lnSuccess <> 1) THEN
                ? "Could not read current time."
                RELEASE loStringCompare
                RELEASE loCredentials
                RELEASE loExpires
                RELEASE loNow
                RELEASE loIdentifier
                RELEASE loAllowed
                RELEASE loFiles
                RELEASE loCached
                RELEASE loEndpoint
                RELEASE loAws
                RELEASE loRest
                RELEASE loConnection
                RELEASE loXml
                CANCEL
            ENDIF

            IF (loExpires.DiffSeconds(loNow) <= 60) THEN
                ? "Source credentials are expired or expire within 60 seconds; obtain fresh credentials."
                RELEASE loStringCompare
                RELEASE loCredentials
                RELEASE loExpires
                RELEASE loNow
                RELEASE loIdentifier
                RELEASE loAllowed
                RELEASE loFiles
                RELEASE loCached
                RELEASE loEndpoint
                RELEASE loAws
                RELEASE loRest
                RELEASE loConnection
                RELEASE loXml
                CANCEL
            ENDIF

        ENDIF

        lnAttempt = lnAttempt + 1
        lnSuccess = loRest.ClearAllQueryParams()
        IF (lnSuccess <> 1) THEN
            ? "Could not reset request parameters."
            RELEASE loStringCompare
            RELEASE loCredentials
            RELEASE loExpires
            RELEASE loNow
            RELEASE loIdentifier
            RELEASE loAllowed
            RELEASE loFiles
            RELEASE loCached
            RELEASE loEndpoint
            RELEASE loAws
            RELEASE loRest
            RELEASE loConnection
            RELEASE loXml
            CANCEL
        ENDIF

        lnSuccess = loRest.AddQueryParam("Action","AssumeRole")
        IF (lnSuccess <> 1) THEN
            ? "Could not set Action."
            RELEASE loStringCompare
            RELEASE loCredentials
            RELEASE loExpires
            RELEASE loNow
            RELEASE loIdentifier
            RELEASE loAllowed
            RELEASE loFiles
            RELEASE loCached
            RELEASE loEndpoint
            RELEASE loAws
            RELEASE loRest
            RELEASE loConnection
            RELEASE loXml
            CANCEL
        ENDIF

        lnSuccess = loRest.AddQueryParam("Version","2011-06-15")
        IF (lnSuccess <> 1) THEN
            ? "Could not set Version."
            RELEASE loStringCompare
            RELEASE loCredentials
            RELEASE loExpires
            RELEASE loNow
            RELEASE loIdentifier
            RELEASE loAllowed
            RELEASE loFiles
            RELEASE loCached
            RELEASE loEndpoint
            RELEASE loAws
            RELEASE loRest
            RELEASE loConnection
            RELEASE loXml
            CANCEL
        ENDIF

        IF (loCredentials.HasMember("session_token") = 1) THEN
            lnSuccess = loRest.AddQueryParam("X-Amz-Security-Token",loCredentials.StringOf("session_token"))
            IF (lnSuccess <> 1) THEN
                ? "Could not set X-Amz-Security-Token."
                RELEASE loStringCompare
                RELEASE loCredentials
                RELEASE loExpires
                RELEASE loNow
                RELEASE loIdentifier
                RELEASE loAllowed
                RELEASE loFiles
                RELEASE loCached
                RELEASE loEndpoint
                RELEASE loAws
                RELEASE loRest
                RELEASE loConnection
                RELEASE loXml
                CANCEL
            ENDIF

        ENDIF

        lnSuccess = loRest.AddQueryParam("RoleArn",loCredentials.StringOf("role_arn"))
        IF (lnSuccess <> 1) THEN
            ? "Could not set RoleArn."
            RELEASE loStringCompare
            RELEASE loCredentials
            RELEASE loExpires
            RELEASE loNow
            RELEASE loIdentifier
            RELEASE loAllowed
            RELEASE loFiles
            RELEASE loCached
            RELEASE loEndpoint
            RELEASE loAws
            RELEASE loRest
            RELEASE loConnection
            RELEASE loXml
            CANCEL
        ENDIF

        lnSuccess = loRest.AddQueryParam("RoleSessionName",loCredentials.StringOf("role_session_name"))
        IF (lnSuccess <> 1) THEN
            ? "Could not set RoleSessionName."
            RELEASE loStringCompare
            RELEASE loCredentials
            RELEASE loExpires
            RELEASE loNow
            RELEASE loIdentifier
            RELEASE loAllowed
            RELEASE loFiles
            RELEASE loCached
            RELEASE loEndpoint
            RELEASE loAws
            RELEASE loRest
            RELEASE loConnection
            RELEASE loXml
            CANCEL
        ENDIF

        lnSuccess = loRest.AddQueryParam("DurationSeconds","3600")
        IF (lnSuccess <> 1) THEN
            ? "Could not set DurationSeconds."
            RELEASE loStringCompare
            RELEASE loCredentials
            RELEASE loExpires
            RELEASE loNow
            RELEASE loIdentifier
            RELEASE loAllowed
            RELEASE loFiles
            RELEASE loCached
            RELEASE loEndpoint
            RELEASE loAws
            RELEASE loRest
            RELEASE loConnection
            RELEASE loXml
            CANCEL
        ENDIF

        lcResponseXml = loRest.FullRequestFormUrlEncoded("POST","/")
        IF (loRest.LastMethodSuccess <> 1) THEN
            ? "Transport failed; credential refresh was not published."
            RELEASE loStringCompare
            RELEASE loCredentials
            RELEASE loExpires
            RELEASE loNow
            RELEASE loIdentifier
            RELEASE loAllowed
            RELEASE loFiles
            RELEASE loCached
            RELEASE loEndpoint
            RELEASE loAws
            RELEASE loRest
            RELEASE loConnection
            RELEASE loXml
            CANCEL
        ENDIF

        IF (loRest.ResponseStatusCode = 200) THEN
            lnReceived = 1
        ENDIF

        IF (loRest.ResponseStatusCode <> 200) THEN
            lnRetryable = 0
            lcValue = ""
            lnSuccess = loXml.LoadXml(lcResponseXml)
            IF (lnSuccess = 1) THEN
                lcValue = loXml.GetChildContent("Error|Code")
            ENDIF

            loStringCompare.SetString(lcValue)
            lnStringMatch13 = loStringCompare.ContentsEqual("Throttling",1)
            loStringCompare.SetString(lcValue)
            lnStringMatch14 = loStringCompare.ContentsEqual("ThrottlingException",1)
            IF ((loRest.ResponseStatusCode = 429) OR (loRest.ResponseStatusCode = 500) OR (loRest.ResponseStatusCode = 502) OR (loRest.ResponseStatusCode = 503) OR (loRest.ResponseStatusCode = 504) OR (lnStringMatch13 = 1) OR (lnStringMatch14 = 1)) THEN
                lnRetryable = 1
            ENDIF

            IF ((lnRetryable <> 1) OR (lnAttempt = 3)) THEN
                ? "Refresh failed. HTTP status: " + STR(loRest.ResponseStatusCode)
                ? "AWS error code: " + lcValue
                RELEASE loStringCompare
                RELEASE loCredentials
                RELEASE loExpires
                RELEASE loNow
                RELEASE loIdentifier
                RELEASE loAllowed
                RELEASE loFiles
                RELEASE loCached
                RELEASE loEndpoint
                RELEASE loAws
                RELEASE loRest
                RELEASE loConnection
                RELEASE loXml
                CANCEL
            ENDIF

            loConnection.SleepMs(lnDelayMs)
            lnDelayMs = lnDelayMs * 2
        ENDIF

    ENDDO
    IF (lnReceived <> 1) THEN
        ? "Refresh attempts exhausted."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        CANCEL
    ENDIF

    lnSuccess = loXml.LoadXml(lcResponseXml)
    IF (lnSuccess <> 1) THEN
        ? "Invalid STS XML response."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        CANCEL
    ENDIF

    loStringCompare.SetString(loXml.Tag)
    lnStringMatch15 = loStringCompare.ContentsEqual("AssumeRoleResponse",1)
    IF (lnStringMatch15 <> 1) THEN
        ? "Unexpected STS response root."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        CANCEL
    ENDIF

    lcValue = loXml.GetChildContent("ResponseMetadata|RequestId")
    loStringCompare.SetString(lcValue)
    lnStringMatch16 = loStringCompare.ContentsEqual("",1)
    IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch16 = 1)) THEN
        ? "Missing or empty ResponseMetadata|RequestId."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        CANCEL
    ENDIF

    IF (loXml.HasChildWithTag("AssumeRoleResult") <> 1) THEN
        ? "Missing STS result."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        CANCEL
    ENDIF

    lcAccessKey = loXml.GetChildContent("AssumeRoleResult|Credentials|AccessKeyId")
    loStringCompare.SetString(lcAccessKey)
    lnStringMatch17 = loStringCompare.ContentsEqual("",1)
    IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch17 = 1)) THEN
        ? "Missing or empty AssumeRoleResult|Credentials|AccessKeyId."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        CANCEL
    ENDIF

    lcSecretKey = loXml.GetChildContent("AssumeRoleResult|Credentials|SecretAccessKey")
    loStringCompare.SetString(lcSecretKey)
    lnStringMatch18 = loStringCompare.ContentsEqual("",1)
    IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch18 = 1)) THEN
        ? "Missing or empty AssumeRoleResult|Credentials|SecretAccessKey."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        CANCEL
    ENDIF

    lcSessionToken = loXml.GetChildContent("AssumeRoleResult|Credentials|SessionToken")
    loStringCompare.SetString(lcSessionToken)
    lnStringMatch19 = loStringCompare.ContentsEqual("",1)
    IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch19 = 1)) THEN
        ? "Missing or empty AssumeRoleResult|Credentials|SessionToken."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        CANCEL
    ENDIF

    lcExpiration = loXml.GetChildContent("AssumeRoleResult|Credentials|Expiration")
    loStringCompare.SetString(lcExpiration)
    lnStringMatch20 = loStringCompare.ContentsEqual("",1)
    IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch20 = 1)) THEN
        ? "Missing or empty AssumeRoleResult|Credentials|Expiration."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        CANCEL
    ENDIF

    lnSuccess = loExpires.SetFromTimestamp(lcExpiration)
    IF (lnSuccess <> 1) THEN
        ? "Returned expiration is invalid."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        CANCEL
    ENDIF

    lnSuccess = loNow.SetFromCurrentSystemTime()
    IF (lnSuccess <> 1) THEN
        ? "Could not read current time."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        CANCEL
    ENDIF

    IF (loExpires.DiffSeconds(loNow) <= 60) THEN
        ? "Returned credentials are expired or expire within 60 seconds; obtain fresh credentials."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        CANCEL
    ENDIF

    lcValue = loXml.GetChildContent("AssumeRoleResult|AssumedRoleUser|Arn")
    loStringCompare.SetString(lcValue)
    lnStringMatch21 = loStringCompare.ContentsEqual("",1)
    IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch21 = 1)) THEN
        ? "Missing or empty AssumeRoleResult|AssumedRoleUser|Arn."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        CANCEL
    ENDIF

    lcValue = loXml.GetChildContent("AssumeRoleResult|AssumedRoleUser|AssumedRoleId")
    loStringCompare.SetString(lcValue)
    lnStringMatch22 = loStringCompare.ContentsEqual("",1)
    IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch22 = 1)) THEN
        ? "Missing or empty AssumeRoleResult|AssumedRoleUser|AssumedRoleId."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        CANCEL
    ENDIF

    loSession = CreateObject('Chilkat.JsonObject')
    lnSuccess = loSession.UpdateString("access_key",lcAccessKey)
    IF (lnSuccess <> 1) THEN
        ? "Could not build complete session."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loSession
        CANCEL
    ENDIF

    lnSuccess = loSession.UpdateString("secret_key",lcSecretKey)
    IF (lnSuccess <> 1) THEN
        ? "Could not build complete session."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loSession
        CANCEL
    ENDIF

    lnSuccess = loSession.UpdateString("session_token",lcSessionToken)
    IF (lnSuccess <> 1) THEN
        ? "Could not build complete session."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loSession
        CANCEL
    ENDIF

    lnSuccess = loSession.UpdateString("expiration",lcExpiration)
    IF (lnSuccess <> 1) THEN
        ? "Could not build complete session."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        RELEASE loFiles
        RELEASE loCached
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        RELEASE loSession
        CANCEL
    ENDIF

    *  Use session in memory; do not print or log session.Emit().
    ? "Session validated; expiration: " + lcExpiration
    *  Publish one complete snapshot only after validating every returned field.
    lcActiveSnapshot = loSession.Emit()
ENDIF

*  Rehydrate a complete set for the next unit of work, never four partial updates.
loUsable = CreateObject('Chilkat.JsonObject')
loUsable.Strict = 1
lnSuccess = loUsable.Load(lcActiveSnapshot)
IF (lnSuccess <> 1) THEN
    ? "Could not load the complete session snapshot."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loFiles
    RELEASE loCached
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loSession
    RELEASE loUsable
    CANCEL
ENDIF

*  Pass usable to your service client here. Do not log activeSnapshot.
? "Session ready for the next unit of work; expiration: " + loUsable.StringOf("expiration")
? "Example completed."

RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loIdentifier
RELEASE loAllowed
RELEASE loFiles
RELEASE loCached
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSession
RELEASE loUsable