AWS STS: Decode an Authorization Failure
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 encoded_message to the complete encoded message from a supported AWS failure. Requires sts:DecodeAuthorizationMessage. Parses the JSON contained inside the XML response and displays only its boolean decision flags. Not every access-denied response has an encoded message; do not pass an ordinary error message.
AWS references: DecodeAuthorizationMessage
Chilkat Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loStringCompare
LOCAL loCredentials
LOCAL lnStringMatch1
LOCAL lnStringMatch2
LOCAL loExpires
LOCAL loNow
LOCAL lcRegion
LOCAL lnStringMatch3
LOCAL lnStringMatch4
LOCAL lnStringMatch5
LOCAL loEndpoint
LOCAL loAws
LOCAL loRest
LOCAL loConnection
LOCAL loXml
LOCAL lcValue
LOCAL lcResponseXml
LOCAL lnStringMatch6
LOCAL lnStringMatch7
LOCAL lnStringMatch8
LOCAL lcDecodedPath
LOCAL lnStringMatch9
LOCAL loDecoded
LOCAL lnAllowed
LOCAL lnStringMatch10
LOCAL lnStringMatch11
LOCAL lnStringMatch12
LOCAL lnExplicitDeny
LOCAL lnStringMatch13
LOCAL lnStringMatch14
LOCAL lnStringMatch15
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("encoded_message"))
lnStringMatch5 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("encoded_message") <> 1) OR (lnStringMatch5 = 1)) THEN
? "Missing or invalid encoded_message."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
CANCEL
ENDIF
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 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 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 loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
CANCEL
ENDIF
loXml = CreateObject('Chilkat.Xml')
loXml.Strict = 1
lcResponseXml = ""
lnSuccess = loRest.ClearAllQueryParams()
IF (lnSuccess <> 1) THEN
? "Could not reset request parameters."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
CANCEL
ENDIF
lnSuccess = loRest.AddQueryParam("Action","DecodeAuthorizationMessage")
IF (lnSuccess <> 1) THEN
? "Could not set Action."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
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 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 loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
CANCEL
ENDIF
ENDIF
lnSuccess = loRest.AddQueryParam("EncodedMessage",loCredentials.StringOf("encoded_message"))
IF (lnSuccess <> 1) THEN
? "Could not set EncodedMessage."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
CANCEL
ENDIF
lcResponseXml = loRest.FullRequestFormUrlEncoded("POST","/")
IF (loRest.LastMethodSuccess <> 1) THEN
? "STS transport failed; inspect diagnostics privately."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
CANCEL
ENDIF
IF (loRest.ResponseStatusCode <> 200) THEN
? "STS HTTP status: " + STR(loRest.ResponseStatusCode)
lnSuccess = loXml.LoadXml(lcResponseXml)
IF (lnSuccess = 1) THEN
lcValue = loXml.GetChildContent("Error|Code")
IF (loXml.LastMethodSuccess = 1) THEN
? "AWS error code: " + lcValue
ENDIF
lcValue = loXml.GetChildContent("RequestId")
IF (loXml.LastMethodSuccess = 1) THEN
? "AWS request ID: " + lcValue
ENDIF
ENDIF
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
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 loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
CANCEL
ENDIF
loStringCompare.SetString(loXml.Tag)
lnStringMatch6 = loStringCompare.ContentsEqual("DecodeAuthorizationMessageResponse",1)
IF (lnStringMatch6 <> 1) THEN
? "Unexpected STS response root."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
CANCEL
ENDIF
IF (loXml.HasChildWithTag("DecodeAuthorizationMessageResult") = 1) THEN
lcValue = loXml.GetChildContent("ResponseMetadata|RequestId")
loStringCompare.SetString(lcValue)
lnStringMatch7 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch7 = 1)) THEN
? "Missing or empty ResponseMetadata|RequestId."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
CANCEL
ENDIF
ELSE
lcValue = loXml.GetChildContent("requestId")
loStringCompare.SetString(lcValue)
lnStringMatch8 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch8 = 1)) THEN
? "Missing or empty requestId."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
CANCEL
ENDIF
ENDIF
lcDecodedPath = "DecodeAuthorizationMessageResult|DecodedMessage"
* Also accept the top-level form shown in the AWS reference's example response.
IF (loXml.HasChildWithTag("DecodeAuthorizationMessageResult") <> 1) THEN
lcDecodedPath = "DecodedMessage"
ENDIF
lcValue = loXml.GetChildContent(lcDecodedPath)
loStringCompare.SetString(lcValue)
lnStringMatch9 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch9 = 1)) THEN
? "Missing decoded authorization details."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
CANCEL
ENDIF
loDecoded = CreateObject('Chilkat.JsonObject')
loDecoded.Strict = 1
lnSuccess = loDecoded.Load(lcValue)
IF (lnSuccess <> 1) THEN
? "Decoded authorization details are not valid JSON."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loDecoded
CANCEL
ENDIF
lnAllowed = 0
IF ((loDecoded.JsonTypeOf("allowed") <> 5) AND (loDecoded.JsonTypeOf("allowed") <> 1)) THEN
? "Invalid authorization flag type."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loDecoded
CANCEL
ENDIF
IF (loDecoded.JsonTypeOf("allowed") = 5) THEN
lnAllowed = loDecoded.BoolOf("allowed")
ENDIF
IF (loDecoded.JsonTypeOf("allowed") = 1) THEN
lcValue = loDecoded.StringOf("allowed")
loStringCompare.SetString(lcValue)
lnStringMatch10 = loStringCompare.ContentsEqual("true",1)
loStringCompare.SetString(lcValue)
lnStringMatch11 = loStringCompare.ContentsEqual("false",1)
IF ((lnStringMatch10 <> 1) AND (lnStringMatch11 <> 1)) THEN
? "Invalid string authorization flag."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loDecoded
CANCEL
ENDIF
loStringCompare.SetString(lcValue)
lnStringMatch12 = loStringCompare.ContentsEqual("true",1)
IF (lnStringMatch12 = 1) THEN
lnAllowed = 1
ENDIF
ENDIF
? "allowed: " + STR(lnAllowed)
lnExplicitDeny = 0
IF ((loDecoded.JsonTypeOf("explicitDeny") <> 5) AND (loDecoded.JsonTypeOf("explicitDeny") <> 1)) THEN
? "Invalid authorization flag type."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loDecoded
CANCEL
ENDIF
IF (loDecoded.JsonTypeOf("explicitDeny") = 5) THEN
lnExplicitDeny = loDecoded.BoolOf("explicitDeny")
ENDIF
IF (loDecoded.JsonTypeOf("explicitDeny") = 1) THEN
lcValue = loDecoded.StringOf("explicitDeny")
loStringCompare.SetString(lcValue)
lnStringMatch13 = loStringCompare.ContentsEqual("true",1)
loStringCompare.SetString(lcValue)
lnStringMatch14 = loStringCompare.ContentsEqual("false",1)
IF ((lnStringMatch13 <> 1) AND (lnStringMatch14 <> 1)) THEN
? "Invalid string authorization flag."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loDecoded
CANCEL
ENDIF
loStringCompare.SetString(lcValue)
lnStringMatch15 = loStringCompare.ContentsEqual("true",1)
IF (lnStringMatch15 = 1) THEN
lnExplicitDeny = 1
ENDIF
ENDIF
? "explicitDeny: " + STR(lnExplicitDeny)
* Inspect other decoded fields privately; they can contain privileged details.
? "Example completed."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loDecoded