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 PowerBuilder Downloads
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
long li_StringMatch6
oleobject loo_Identifier
oleobject loo_Allowed
long li_CharIndex
oleobject loo_Files
oleobject loo_Cached
string ls_ActiveSnapshot
long li_RefreshRequired
long li_StringMatch7
long li_StringMatch8
long li_StringMatch9
long li_StringMatch10
oleobject loo_Endpoint
oleobject loo_Aws
oleobject loo_Rest
oleobject loo_Connection
oleobject loo_Xml
string ls_Value
string ls_AccessKey
string ls_SecretKey
string ls_SessionToken
string ls_Expiration
string ls_ResponseXml
long li_Attempt
long li_DelayMs
long li_Received
long li_Retryable
long li_StringMatch11
long li_StringMatch12
long li_StringMatch13
long li_StringMatch14
long li_StringMatch15
long li_StringMatch16
long li_StringMatch17
long li_StringMatch18
long li_StringMatch19
long li_StringMatch20
long li_StringMatch21
long li_StringMatch22
oleobject loo_Session
oleobject loo_Usable
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("role_arn"))
li_StringMatch5 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("role_arn") <> 1) OR (li_StringMatch5 = 1) then
Write-Debug "Missing or invalid role_arn."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
return
end if
loo_StringCompare.SetString(loo_Credentials.StringOf("role_session_name"))
li_StringMatch6 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("role_session_name") <> 1) OR (li_StringMatch6 = 1) then
Write-Debug "Missing or invalid role_session_name."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
return
end if
loo_Identifier = create oleobject
li_rc = loo_Identifier.ConnectToNewObject("Chilkat.StringBuilder")
loo_Identifier.Append(loo_Credentials.StringOf("role_session_name"))
if (loo_Identifier.Length < 2) OR (loo_Identifier.Length > 64) then
Write-Debug "role_session_name must have 2-64 characters."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
return
end if
loo_Allowed = create oleobject
li_rc = loo_Allowed.ConnectToNewObject("Chilkat.StringBuilder")
loo_Allowed.Append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+=,.@-")
li_CharIndex = 0
do while (li_CharIndex < loo_Identifier.Length)
if loo_Allowed.Contains(loo_Identifier.GetRange(li_CharIndex,1,0),1) <> 1 then
Write-Debug "Invalid character in role_session_name."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
return
end if
li_CharIndex = li_CharIndex + 1
loop
if (loo_Credentials.HasMember("mfa_code") = 1) OR (loo_Credentials.HasMember("mfa_serial") = 1) then
Write-Debug "This unattended refresh example does not reuse MFA codes."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
return
end if
// Optional private cache contains only the four normalized session fields.
// In an application, keep activeSnapshot in memory between units of work.
loo_Files = create oleobject
li_rc = loo_Files.ConnectToNewObject("Chilkat.FileAccess")
loo_Cached = create oleobject
li_rc = loo_Cached.ConnectToNewObject("Chilkat.JsonObject")
loo_Cached.Strict = 1
ls_ActiveSnapshot = ""
li_RefreshRequired = 1
if loo_Files.FileExists("qa_data/tokens/sts_session.json") = 1 then
li_Success = loo_Cached.LoadFile("qa_data/tokens/sts_session.json")
if li_Success <> 1 then
Write-Debug "Invalid private cache."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
return
end if
loo_StringCompare.SetString(loo_Cached.StringOf("access_key"))
li_StringMatch7 = loo_StringCompare.ContentsEqual("",1)
if (loo_Cached.JsonTypeOf("access_key") <> 1) OR (li_StringMatch7 = 1) then
Write-Debug "Incomplete private cache."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
return
end if
loo_StringCompare.SetString(loo_Cached.StringOf("secret_key"))
li_StringMatch8 = loo_StringCompare.ContentsEqual("",1)
if (loo_Cached.JsonTypeOf("secret_key") <> 1) OR (li_StringMatch8 = 1) then
Write-Debug "Incomplete private cache."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
return
end if
loo_StringCompare.SetString(loo_Cached.StringOf("session_token"))
li_StringMatch9 = loo_StringCompare.ContentsEqual("",1)
if (loo_Cached.JsonTypeOf("session_token") <> 1) OR (li_StringMatch9 = 1) then
Write-Debug "Incomplete private cache."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
return
end if
loo_StringCompare.SetString(loo_Cached.StringOf("expiration"))
li_StringMatch10 = loo_StringCompare.ContentsEqual("",1)
if (loo_Cached.JsonTypeOf("expiration") <> 1) OR (li_StringMatch10 = 1) then
Write-Debug "Incomplete private cache."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
return
end if
li_Success = loo_Expires.SetFromTimestamp(loo_Cached.StringOf("expiration"))
if li_Success <> 1 then
Write-Debug "Invalid cached expiration."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
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_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
return
end if
if loo_Expires.DiffSeconds(loo_Now) > 300 then
ls_ActiveSnapshot = loo_Cached.Emit()
li_RefreshRequired = 0
end if
end if
if li_RefreshRequired = 1 then
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_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
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_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
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_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
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_Attempt = 0
li_DelayMs = 1000
li_Received = 0
do while ((li_Attempt < 3) AND (li_Received = 0))
if loo_Credentials.HasMember("session_token") = 1 then
loo_StringCompare.SetString(loo_Credentials.StringOf("session_token"))
li_StringMatch11 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("session_token") <> 1) OR (li_StringMatch11 = 1) then
Write-Debug "Missing or invalid session_token."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
loo_StringCompare.SetString(loo_Credentials.StringOf("expiration"))
li_StringMatch12 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("expiration") <> 1) OR (li_StringMatch12 = 1) then
Write-Debug "Missing or invalid expiration."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
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
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
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_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
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 "Source 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_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
end if
li_Attempt = li_Attempt + 1
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_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
li_Success = loo_Rest.AddQueryParam("Action","AssumeRole")
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_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
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_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
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_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
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("RoleArn",loo_Credentials.StringOf("role_arn"))
if li_Success <> 1 then
Write-Debug "Could not set RoleArn."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
li_Success = loo_Rest.AddQueryParam("RoleSessionName",loo_Credentials.StringOf("role_session_name"))
if li_Success <> 1 then
Write-Debug "Could not set RoleSessionName."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
li_Success = loo_Rest.AddQueryParam("DurationSeconds","3600")
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_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
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 "Transport failed; credential refresh was not published."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
if loo_Rest.ResponseStatusCode = 200 then
li_Received = 1
end if
if loo_Rest.ResponseStatusCode <> 200 then
li_Retryable = 0
ls_Value = ""
li_Success = loo_Xml.LoadXml(ls_ResponseXml)
if li_Success = 1 then
ls_Value = loo_Xml.GetChildContent("Error|Code")
end if
loo_StringCompare.SetString(ls_Value)
li_StringMatch13 = loo_StringCompare.ContentsEqual("Throttling",1)
loo_StringCompare.SetString(ls_Value)
li_StringMatch14 = loo_StringCompare.ContentsEqual("ThrottlingException",1)
if (loo_Rest.ResponseStatusCode = 429) OR (loo_Rest.ResponseStatusCode = 500) OR (loo_Rest.ResponseStatusCode = 502) OR (loo_Rest.ResponseStatusCode = 503) OR (loo_Rest.ResponseStatusCode = 504) OR (li_StringMatch13 = 1) OR (li_StringMatch14 = 1) then
li_Retryable = 1
end if
if (li_Retryable <> 1) OR (li_Attempt = 3) then
Write-Debug "Refresh failed. HTTP status: " + string(loo_Rest.ResponseStatusCode)
Write-Debug "AWS error code: " + ls_Value
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
loo_Connection.SleepMs(li_DelayMs)
li_DelayMs = li_DelayMs * 2
end if
loop
if li_Received <> 1 then
Write-Debug "Refresh attempts exhausted."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
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_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
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_StringMatch15 = loo_StringCompare.ContentsEqual("AssumeRoleResponse",1)
if li_StringMatch15 <> 1 then
Write-Debug "Unexpected STS response root."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
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_StringMatch16 = loo_StringCompare.ContentsEqual("",1)
if (loo_Xml.LastMethodSuccess <> 1) OR (li_StringMatch16 = 1) then
Write-Debug "Missing or empty ResponseMetadata|RequestId."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
if loo_Xml.HasChildWithTag("AssumeRoleResult") <> 1 then
Write-Debug "Missing STS result."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
ls_AccessKey = loo_Xml.GetChildContent("AssumeRoleResult|Credentials|AccessKeyId")
loo_StringCompare.SetString(ls_AccessKey)
li_StringMatch17 = loo_StringCompare.ContentsEqual("",1)
if (loo_Xml.LastMethodSuccess <> 1) OR (li_StringMatch17 = 1) then
Write-Debug "Missing or empty AssumeRoleResult|Credentials|AccessKeyId."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
ls_SecretKey = loo_Xml.GetChildContent("AssumeRoleResult|Credentials|SecretAccessKey")
loo_StringCompare.SetString(ls_SecretKey)
li_StringMatch18 = loo_StringCompare.ContentsEqual("",1)
if (loo_Xml.LastMethodSuccess <> 1) OR (li_StringMatch18 = 1) then
Write-Debug "Missing or empty AssumeRoleResult|Credentials|SecretAccessKey."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
ls_SessionToken = loo_Xml.GetChildContent("AssumeRoleResult|Credentials|SessionToken")
loo_StringCompare.SetString(ls_SessionToken)
li_StringMatch19 = loo_StringCompare.ContentsEqual("",1)
if (loo_Xml.LastMethodSuccess <> 1) OR (li_StringMatch19 = 1) then
Write-Debug "Missing or empty AssumeRoleResult|Credentials|SessionToken."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
ls_Expiration = loo_Xml.GetChildContent("AssumeRoleResult|Credentials|Expiration")
loo_StringCompare.SetString(ls_Expiration)
li_StringMatch20 = loo_StringCompare.ContentsEqual("",1)
if (loo_Xml.LastMethodSuccess <> 1) OR (li_StringMatch20 = 1) then
Write-Debug "Missing or empty AssumeRoleResult|Credentials|Expiration."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
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 "Returned expiration is invalid."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
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_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
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 "Returned 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_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
ls_Value = loo_Xml.GetChildContent("AssumeRoleResult|AssumedRoleUser|Arn")
loo_StringCompare.SetString(ls_Value)
li_StringMatch21 = loo_StringCompare.ContentsEqual("",1)
if (loo_Xml.LastMethodSuccess <> 1) OR (li_StringMatch21 = 1) then
Write-Debug "Missing or empty AssumeRoleResult|AssumedRoleUser|Arn."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
ls_Value = loo_Xml.GetChildContent("AssumeRoleResult|AssumedRoleUser|AssumedRoleId")
loo_StringCompare.SetString(ls_Value)
li_StringMatch22 = loo_StringCompare.ContentsEqual("",1)
if (loo_Xml.LastMethodSuccess <> 1) OR (li_StringMatch22 = 1) then
Write-Debug "Missing or empty AssumeRoleResult|AssumedRoleUser|AssumedRoleId."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
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 complete session."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
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 complete session."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
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 complete session."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_Session
return
end if
li_Success = loo_Session.UpdateString("expiration",ls_Expiration)
if li_Success <> 1 then
Write-Debug "Could not build complete session."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_Session
return
end if
// Use session in memory; do not print or log session.Emit().
Write-Debug "Session validated; expiration: " + ls_Expiration
// Publish one complete snapshot only after validating every returned field.
ls_ActiveSnapshot = loo_Session.Emit()
end if
// Rehydrate a complete set for the next unit of work, never four partial updates.
loo_Usable = create oleobject
li_rc = loo_Usable.ConnectToNewObject("Chilkat.JsonObject")
loo_Usable.Strict = 1
li_Success = loo_Usable.Load(ls_ActiveSnapshot)
if li_Success <> 1 then
Write-Debug "Could not load the complete session snapshot."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_Session
destroy loo_Usable
return
end if
// Pass usable to your service client here. Do not log activeSnapshot.
Write-Debug "Session ready for the next unit of work; expiration: " + loo_Usable.StringOf("expiration")
Write-Debug "Example completed."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Expires
destroy loo_Now
destroy loo_Identifier
destroy loo_Allowed
destroy loo_Files
destroy loo_Cached
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_Session
destroy loo_Usable