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 Xojo Plugin Downloads
Dim success As Boolean
success = False
Dim stringCompare As New Chilkat.StringBuilder
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this file private and outside public web content/source control.
Dim credentials As New Chilkat.JsonObject
credentials.Strict = True
success = credentials.LoadFile("qa_data/aws/sts.json")
If (success <> True) Then
System.DebugLog("Could not load private STS configuration.")
Return
End If
success = stringCompare.SetString(credentials.StringOf("access_key"))
Dim stringMatch1 As Boolean
stringMatch1 = stringCompare.ContentsEqual("",True)
If ((credentials.JsonTypeOf("access_key") <> 1) Or (stringMatch1 = True)) Then
System.DebugLog("Missing or invalid access_key.")
Return
End If
success = stringCompare.SetString(credentials.StringOf("secret_key"))
Dim stringMatch2 As Boolean
stringMatch2 = stringCompare.ContentsEqual("",True)
If ((credentials.JsonTypeOf("secret_key") <> 1) Or (stringMatch2 = True)) Then
System.DebugLog("Missing or invalid secret_key.")
Return
End If
Dim expires As New Chilkat.CkDateTime
Dim now As New Chilkat.CkDateTime
Dim region As String
region = "us-west-2"
If (credentials.HasMember("session_token") = True) Then
success = stringCompare.SetString(credentials.StringOf("session_token"))
Dim stringMatch3 As Boolean
stringMatch3 = stringCompare.ContentsEqual("",True)
If ((credentials.JsonTypeOf("session_token") <> 1) Or (stringMatch3 = True)) Then
System.DebugLog("Missing or invalid session_token.")
Return
End If
success = stringCompare.SetString(credentials.StringOf("expiration"))
Dim stringMatch4 As Boolean
stringMatch4 = stringCompare.ContentsEqual("",True)
If ((credentials.JsonTypeOf("expiration") <> 1) Or (stringMatch4 = True)) Then
System.DebugLog("Missing or invalid expiration.")
Return
End If
success = expires.SetFromTimestamp(credentials.StringOf("expiration"))
If (success <> True) Then
System.DebugLog("Source expiration is invalid.")
Return
End If
success = now.SetFromCurrentSystemTime()
If (success <> True) Then
System.DebugLog("Could not read current time.")
Return
End If
If (expires.DiffSeconds(now) <= 60) Then
System.DebugLog("Source credentials are expired or expire within 60 seconds; obtain fresh credentials.")
Return
End If
End If
success = stringCompare.SetString(credentials.StringOf("encoded_message"))
Dim stringMatch5 As Boolean
stringMatch5 = stringCompare.ContentsEqual("",True)
If ((credentials.JsonTypeOf("encoded_message") <> 1) Or (stringMatch5 = True)) Then
System.DebugLog("Missing or invalid encoded_message.")
Return
End If
Dim endpoint As New Chilkat.StringBuilder
success = endpoint.Append("sts.")
success = endpoint.Append(region)
success = endpoint.Append(".amazonaws.com")
Dim aws As New Chilkat.AuthAws
aws.AccessKey = credentials.StringOf("access_key")
aws.SecretKey = credentials.StringOf("secret_key")
aws.Region = region
aws.ServiceName = "sts"
aws.SignatureVersion = 4
Dim rest As New Chilkat.Rest
rest.ConnectTimeoutMs = 20000
rest.IdleTimeoutMs = 60000
rest.VerboseLogging = False
success = rest.SetAuthAws(aws)
If (success <> True) Then
System.DebugLog("Could not configure request signing.")
Return
End If
// Keep this verified Socket alive while Rest uses its connection.
Dim connection As New Chilkat.Socket
connection.RequireSslCertVerify = True
connection.MaxReadIdleMs = 60000
connection.MaxSendIdleMs = 60000
success = connection.Connect(endpoint.GetAsString(),443,True,20000)
If (success <> True) Then
System.DebugLog("TLS connection failed; inspect diagnostics privately.")
Return
End If
success = rest.UseConnection(connection,True)
If (success <> True) Then
System.DebugLog("Could not attach TLS connection.")
Return
End If
Dim xml As New Chilkat.Xml
xml.Strict = True
Dim value As String
Dim responseXml As String
responseXml = ""
success = rest.ClearAllQueryParams()
If (success <> True) Then
System.DebugLog("Could not reset request parameters.")
Return
End If
success = rest.AddQueryParam("Action","DecodeAuthorizationMessage")
If (success <> True) Then
System.DebugLog("Could not set Action.")
Return
End If
success = rest.AddQueryParam("Version","2011-06-15")
If (success <> True) Then
System.DebugLog("Could not set Version.")
Return
End If
If (credentials.HasMember("session_token") = True) Then
success = rest.AddQueryParam("X-Amz-Security-Token",credentials.StringOf("session_token"))
If (success <> True) Then
System.DebugLog("Could not set X-Amz-Security-Token.")
Return
End If
End If
success = rest.AddQueryParam("EncodedMessage",credentials.StringOf("encoded_message"))
If (success <> True) Then
System.DebugLog("Could not set EncodedMessage.")
Return
End If
responseXml = rest.FullRequestFormUrlEncoded("POST","/")
If (rest.LastMethodSuccess <> True) Then
System.DebugLog("STS transport failed; inspect diagnostics privately.")
Return
End If
If (rest.ResponseStatusCode <> 200) Then
System.DebugLog("STS HTTP status: " + Str(rest.ResponseStatusCode))
success = xml.LoadXml(responseXml)
If (success = True) Then
value = xml.GetChildContent("Error|Code")
If (xml.LastMethodSuccess = True) Then
System.DebugLog("AWS error code: " + value)
End If
value = xml.GetChildContent("RequestId")
If (xml.LastMethodSuccess = True) Then
System.DebugLog("AWS request ID: " + value)
End If
End If
Return
End If
success = xml.LoadXml(responseXml)
If (success <> True) Then
System.DebugLog("Invalid STS XML response.")
Return
End If
success = stringCompare.SetString(xml.Tag)
Dim stringMatch6 As Boolean
stringMatch6 = stringCompare.ContentsEqual("DecodeAuthorizationMessageResponse",True)
If (stringMatch6 <> True) Then
System.DebugLog("Unexpected STS response root.")
Return
End If
If (xml.HasChildWithTag("DecodeAuthorizationMessageResult") = True) Then
value = xml.GetChildContent("ResponseMetadata|RequestId")
success = stringCompare.SetString(value)
Dim stringMatch7 As Boolean
stringMatch7 = stringCompare.ContentsEqual("",True)
If ((xml.LastMethodSuccess <> True) Or (stringMatch7 = True)) Then
System.DebugLog("Missing or empty ResponseMetadata|RequestId.")
Return
End If
Else
value = xml.GetChildContent("requestId")
success = stringCompare.SetString(value)
Dim stringMatch8 As Boolean
stringMatch8 = stringCompare.ContentsEqual("",True)
If ((xml.LastMethodSuccess <> True) Or (stringMatch8 = True)) Then
System.DebugLog("Missing or empty requestId.")
Return
End If
End If
Dim decodedPath As String
decodedPath = "DecodeAuthorizationMessageResult|DecodedMessage"
// Also accept the top-level form shown in the AWS reference's example response.
If (xml.HasChildWithTag("DecodeAuthorizationMessageResult") <> True) Then
decodedPath = "DecodedMessage"
End If
value = xml.GetChildContent(decodedPath)
success = stringCompare.SetString(value)
Dim stringMatch9 As Boolean
stringMatch9 = stringCompare.ContentsEqual("",True)
If ((xml.LastMethodSuccess <> True) Or (stringMatch9 = True)) Then
System.DebugLog("Missing decoded authorization details.")
Return
End If
Dim decoded As New Chilkat.JsonObject
decoded.Strict = True
success = decoded.Load(value)
If (success <> True) Then
System.DebugLog("Decoded authorization details are not valid JSON.")
Return
End If
Dim allowed As Boolean
allowed = False
If ((decoded.JsonTypeOf("allowed") <> 5) And (decoded.JsonTypeOf("allowed") <> 1)) Then
System.DebugLog("Invalid authorization flag type.")
Return
End If
If (decoded.JsonTypeOf("allowed") = 5) Then
allowed = decoded.BoolOf("allowed")
End If
If (decoded.JsonTypeOf("allowed") = 1) Then
value = decoded.StringOf("allowed")
success = stringCompare.SetString(value)
Dim stringMatch10 As Boolean
stringMatch10 = stringCompare.ContentsEqual("true",True)
success = stringCompare.SetString(value)
Dim stringMatch11 As Boolean
stringMatch11 = stringCompare.ContentsEqual("false",True)
If ((stringMatch10 <> True) And (stringMatch11 <> True)) Then
System.DebugLog("Invalid string authorization flag.")
Return
End If
success = stringCompare.SetString(value)
Dim stringMatch12 As Boolean
stringMatch12 = stringCompare.ContentsEqual("true",True)
If (stringMatch12 = True) Then
allowed = True
End If
End If
System.DebugLog("allowed: " + Str(allowed))
Dim explicitDeny As Boolean
explicitDeny = False
If ((decoded.JsonTypeOf("explicitDeny") <> 5) And (decoded.JsonTypeOf("explicitDeny") <> 1)) Then
System.DebugLog("Invalid authorization flag type.")
Return
End If
If (decoded.JsonTypeOf("explicitDeny") = 5) Then
explicitDeny = decoded.BoolOf("explicitDeny")
End If
If (decoded.JsonTypeOf("explicitDeny") = 1) Then
value = decoded.StringOf("explicitDeny")
success = stringCompare.SetString(value)
Dim stringMatch13 As Boolean
stringMatch13 = stringCompare.ContentsEqual("true",True)
success = stringCompare.SetString(value)
Dim stringMatch14 As Boolean
stringMatch14 = stringCompare.ContentsEqual("false",True)
If ((stringMatch13 <> True) And (stringMatch14 <> True)) Then
System.DebugLog("Invalid string authorization flag.")
Return
End If
success = stringCompare.SetString(value)
Dim stringMatch15 As Boolean
stringMatch15 = stringCompare.ContentsEqual("true",True)
If (stringMatch15 = True) Then
explicitDeny = True
End If
End If
System.DebugLog("explicitDeny: " + Str(explicitDeny))
// Inspect other decoded fields privately; they can contain privileged details.
System.DebugLog("Example completed.")