AWS STS: Normalize STS Credentials and Use Chilkat.Rest
See more AWS Security Token Service Examples
Uses the AWS STS Query API version 2011-06-15 and a verified TLS connection to the commercial-partition regional endpoint sts.us-west-2.amazonaws.com. Change the endpoint and signing region together for your deployment. Assumes Chilkat is unlocked.
Keep qa_data/aws/sts.json private and outside web roots/source control. Signed examples require nonempty access_key and secret_key strings; temporary source credentials also require session_token and ISO 8601 expiration. Federation exchanges load their specific fields without AWS keys. The examples do not print credentials, bearer tokens, full responses or diagnostic dumps. Credential-producing examples build a complete in-memory session object with access_key, secret_key, session_token and expiration; integrate that object into your application before it goes out of scope.
Reads only private qa_data/tokens/aws_session_token.xml; no sts.json is needed. Accepts all seven credential-producing STS response roots, including the existing GetSessionToken/AssumeRole handoff. Builds a complete normalized in-memory session and uses it to sign GetCallerIdentity. The normalized fields are access_key, secret_key, session_token and expiration. Checks expiration with a 60-second margin before connecting. JWT-only GetWebIdentityToken responses are intentionally not credential sets.
AWS references: GetCallerIdentity
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoStringCompare
Handle hoTokenXml
String sCredentialPath
Boolean iStringMatch1
Boolean iStringMatch2
Boolean iStringMatch3
Boolean iStringMatch4
Boolean iStringMatch5
Boolean iStringMatch6
Boolean iStringMatch7
Boolean iStringMatch8
String sAccessKey
String sSecretKey
String sSessionToken
String sExpiration
Handle hoPath
Boolean iStringMatch9
Boolean iStringMatch10
Boolean iStringMatch11
Boolean iStringMatch12
Handle hoExpires
Variant vNow
Handle hoNow
Handle hoSession
String sRegion
Handle hoEndpoint
Variant vAws
Handle hoAws
Handle hoRest
Variant vConnection
Handle hoConnection
Handle hoXml
String sValue
String sResponseXml
Boolean iStringMatch13
Boolean iStringMatch14
Boolean iStringMatch15
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoStringCompare
If (Not(IsComObjectCreated(hoStringCompare))) Begin
Send CreateComObject of hoStringCompare
End
// Load a privately stored STS XML response; never print its contents.
Get Create (RefClass(cComChilkatXml)) To hoTokenXml
If (Not(IsComObjectCreated(hoTokenXml))) Begin
Send CreateComObject of hoTokenXml
End
Set ComStrict Of hoTokenXml To True
Get ComLoadXmlFile Of hoTokenXml "qa_data/tokens/aws_session_token.xml" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load private STS XML."
Procedure_Return
End
Move "" To sCredentialPath
Get ComTag Of hoTokenXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "GetSessionTokenResponse" True To iStringMatch1
If (iStringMatch1 = True) Begin
Move "GetSessionTokenResult|Credentials" To sCredentialPath
End
Get ComTag Of hoTokenXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "AssumeRoleResponse" True To iStringMatch2
If (iStringMatch2 = True) Begin
Move "AssumeRoleResult|Credentials" To sCredentialPath
End
Get ComTag Of hoTokenXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "AssumeRoleWithWebIdentityResponse" True To iStringMatch3
If (iStringMatch3 = True) Begin
Move "AssumeRoleWithWebIdentityResult|Credentials" To sCredentialPath
End
Get ComTag Of hoTokenXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "AssumeRoleWithSAMLResponse" True To iStringMatch4
If (iStringMatch4 = True) Begin
Move "AssumeRoleWithSAMLResult|Credentials" To sCredentialPath
End
Get ComTag Of hoTokenXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "GetFederationTokenResponse" True To iStringMatch5
If (iStringMatch5 = True) Begin
Move "GetFederationTokenResult|Credentials" To sCredentialPath
End
Get ComTag Of hoTokenXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "GetDelegatedAccessTokenResponse" True To iStringMatch6
If (iStringMatch6 = True) Begin
Move "GetDelegatedAccessTokenResult|Credentials" To sCredentialPath
End
Get ComTag Of hoTokenXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "AssumeRootResponse" True To iStringMatch7
If (iStringMatch7 = True) Begin
Move "AssumeRootResult|Credentials" To sCredentialPath
End
Get ComSetString Of hoStringCompare sCredentialPath To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch8
If (iStringMatch8 = True) Begin
Showln "Unsupported STS credential response."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoPath
If (Not(IsComObjectCreated(hoPath))) Begin
Send CreateComObject of hoPath
End
Get ComSetString Of hoPath sCredentialPath To iSuccess
If (iSuccess <> True) Begin
Showln "Could not construct credential path."
Procedure_Return
End
Get ComAppend Of hoPath "|AccessKeyId" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not construct credential path."
Procedure_Return
End
Get ComGetAsString Of hoPath To sTemp1
Get ComGetChildContent Of hoTokenXml sTemp1 To sAccessKey
Get ComSetString Of hoStringCompare sAccessKey To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch9
Get ComLastMethodSuccess Of hoTokenXml To bTemp1
If ((bTemp1 <> True) Or (iStringMatch9 = True)) Begin
Showln "Missing credential field AccessKeyId."
Procedure_Return
End
Get ComSetString Of hoPath sCredentialPath To iSuccess
If (iSuccess <> True) Begin
Showln "Could not construct credential path."
Procedure_Return
End
Get ComAppend Of hoPath "|SecretAccessKey" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not construct credential path."
Procedure_Return
End
Get ComGetAsString Of hoPath To sTemp1
Get ComGetChildContent Of hoTokenXml sTemp1 To sSecretKey
Get ComSetString Of hoStringCompare sSecretKey To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch10
Get ComLastMethodSuccess Of hoTokenXml To bTemp1
If ((bTemp1 <> True) Or (iStringMatch10 = True)) Begin
Showln "Missing credential field SecretAccessKey."
Procedure_Return
End
Get ComSetString Of hoPath sCredentialPath To iSuccess
If (iSuccess <> True) Begin
Showln "Could not construct credential path."
Procedure_Return
End
Get ComAppend Of hoPath "|SessionToken" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not construct credential path."
Procedure_Return
End
Get ComGetAsString Of hoPath To sTemp1
Get ComGetChildContent Of hoTokenXml sTemp1 To sSessionToken
Get ComSetString Of hoStringCompare sSessionToken To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch11
Get ComLastMethodSuccess Of hoTokenXml To bTemp1
If ((bTemp1 <> True) Or (iStringMatch11 = True)) Begin
Showln "Missing credential field SessionToken."
Procedure_Return
End
Get ComSetString Of hoPath sCredentialPath To iSuccess
If (iSuccess <> True) Begin
Showln "Could not construct credential path."
Procedure_Return
End
Get ComAppend Of hoPath "|Expiration" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not construct credential path."
Procedure_Return
End
Get ComGetAsString Of hoPath To sTemp1
Get ComGetChildContent Of hoTokenXml sTemp1 To sExpiration
Get ComSetString Of hoStringCompare sExpiration To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch12
Get ComLastMethodSuccess Of hoTokenXml To bTemp1
If ((bTemp1 <> True) Or (iStringMatch12 = True)) Begin
Showln "Missing credential field Expiration."
Procedure_Return
End
Get Create (RefClass(cComCkDateTime)) To hoExpires
If (Not(IsComObjectCreated(hoExpires))) Begin
Send CreateComObject of hoExpires
End
Get Create (RefClass(cComCkDateTime)) To hoNow
If (Not(IsComObjectCreated(hoNow))) Begin
Send CreateComObject of hoNow
End
Get ComSetFromTimestamp Of hoExpires sExpiration To iSuccess
If (iSuccess <> True) Begin
Showln "Stored expiration is invalid."
Procedure_Return
End
Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read current time."
Procedure_Return
End
Get ComDiffSeconds Of hoExpires Get pvComObject of hoNow to vNow
vNow To iTemp1
If (iTemp1 <= 60) Begin
Showln "Stored credentials are expired or expire within 60 seconds; obtain fresh credentials."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoSession
If (Not(IsComObjectCreated(hoSession))) Begin
Send CreateComObject of hoSession
End
Get ComUpdateString Of hoSession "access_key" sAccessKey To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build normalized session."
Procedure_Return
End
Get ComUpdateString Of hoSession "secret_key" sSecretKey To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build normalized session."
Procedure_Return
End
Get ComUpdateString Of hoSession "session_token" sSessionToken To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build normalized session."
Procedure_Return
End
Get ComUpdateString Of hoSession "expiration" sExpiration To iSuccess
If (iSuccess <> True) Begin
Showln "Could not build normalized session."
Procedure_Return
End
Move "us-west-2" To sRegion
Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
If (Not(IsComObjectCreated(hoEndpoint))) Begin
Send CreateComObject of hoEndpoint
End
Get ComAppend Of hoEndpoint "sts." To iSuccess
Get ComAppend Of hoEndpoint sRegion To iSuccess
Get ComAppend Of hoEndpoint ".amazonaws.com" To iSuccess
Get Create (RefClass(cComChilkatAuthAws)) To hoAws
If (Not(IsComObjectCreated(hoAws))) Begin
Send CreateComObject of hoAws
End
Get ComStringOf Of hoSession "access_key" To sTemp1
Set ComAccessKey Of hoAws To sTemp1
Get ComStringOf Of hoSession "secret_key" To sTemp1
Set ComSecretKey Of hoAws To sTemp1
Set ComRegion Of hoAws To sRegion
Set ComServiceName Of hoAws To "sts"
Set ComSignatureVersion Of hoAws To 4
Get Create (RefClass(cComChilkatRest)) To hoRest
If (Not(IsComObjectCreated(hoRest))) Begin
Send CreateComObject of hoRest
End
Set ComConnectTimeoutMs Of hoRest To 20000
Set ComIdleTimeoutMs Of hoRest To 60000
Set ComVerboseLogging Of hoRest To False
Get pvComObject of hoAws to vAws
Get ComSetAuthAws Of hoRest vAws To iSuccess
If (iSuccess <> True) Begin
Showln "Could not configure request signing."
Procedure_Return
End
// Keep this verified Socket alive while Rest uses its connection.
Get Create (RefClass(cComChilkatSocket)) To hoConnection
If (Not(IsComObjectCreated(hoConnection))) Begin
Send CreateComObject of hoConnection
End
Set ComRequireSslCertVerify Of hoConnection To True
Set ComMaxReadIdleMs Of hoConnection To 60000
Set ComMaxSendIdleMs Of hoConnection To 60000
Get ComGetAsString Of hoEndpoint To sTemp1
Get ComConnect Of hoConnection sTemp1 443 True 20000 To iSuccess
If (iSuccess <> True) Begin
Showln "TLS connection failed; inspect diagnostics privately."
Procedure_Return
End
Get pvComObject of hoConnection to vConnection
Get ComUseConnection Of hoRest vConnection True To iSuccess
If (iSuccess <> True) Begin
Showln "Could not attach TLS connection."
Procedure_Return
End
Get Create (RefClass(cComChilkatXml)) To hoXml
If (Not(IsComObjectCreated(hoXml))) Begin
Send CreateComObject of hoXml
End
Set ComStrict Of hoXml To True
Get ComClearAllQueryParams Of hoRest To iSuccess
If (iSuccess <> True) Begin
Showln "Could not reset request parameters."
Procedure_Return
End
Get ComAddQueryParam Of hoRest "Action" "GetCallerIdentity" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set Action."
Procedure_Return
End
Get ComAddQueryParam Of hoRest "Version" "2011-06-15" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set Version."
Procedure_Return
End
Get ComStringOf Of hoSession "session_token" To sTemp1
Get ComAddQueryParam Of hoRest "X-Amz-Security-Token" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set X-Amz-Security-Token."
Procedure_Return
End
Get ComFullRequestFormUrlEncoded Of hoRest "POST" "/" To sResponseXml
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Showln "STS transport failed; inspect diagnostics privately."
Procedure_Return
End
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 200) Begin
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "STS HTTP status: " iTemp1
Get ComLoadXml Of hoXml sResponseXml To iSuccess
If (iSuccess = True) Begin
Get ComGetChildContent Of hoXml "Error|Code" To sValue
Get ComLastMethodSuccess Of hoXml To bTemp1
If (bTemp1 = True) Begin
Showln "AWS error code: " sValue
End
Get ComGetChildContent Of hoXml "RequestId" To sValue
Get ComLastMethodSuccess Of hoXml To bTemp1
If (bTemp1 = True) Begin
Showln "AWS request ID: " sValue
End
End
Procedure_Return
End
Get ComLoadXml Of hoXml sResponseXml To iSuccess
If (iSuccess <> True) Begin
Showln "Invalid STS XML response."
Procedure_Return
End
Get ComTag Of hoXml To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "GetCallerIdentityResponse" True To iStringMatch13
If (iStringMatch13 <> True) Begin
Showln "Unexpected STS response root."
Procedure_Return
End
Get ComGetChildContent Of hoXml "ResponseMetadata|RequestId" To sValue
Get ComSetString Of hoStringCompare sValue To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch14
Get ComLastMethodSuccess Of hoXml To bTemp1
If ((bTemp1 <> True) Or (iStringMatch14 = True)) Begin
Showln "Missing or empty ResponseMetadata|RequestId."
Procedure_Return
End
Get ComHasChildWithTag Of hoXml "GetCallerIdentityResult" To bTemp1
If (bTemp1 <> True) Begin
Showln "Missing STS result."
Procedure_Return
End
Get ComGetChildContent Of hoXml "GetCallerIdentityResult|Arn" To sValue
Get ComSetString Of hoStringCompare sValue To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch15
Get ComLastMethodSuccess Of hoXml To bTemp1
If ((bTemp1 <> True) Or (iStringMatch15 = True)) Begin
Showln "Missing or empty GetCallerIdentityResult|Arn."
Procedure_Return
End
Showln "Authenticated principal: " sValue
Showln "Example completed."
End_Procedure