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 Go Downloads
success := false
stringCompare := chilkat.NewStringBuilder()
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this file private and outside public web content/source control.
credentials := chilkat.NewJsonObject()
credentials.SetStrict(true)
success = credentials.LoadFile("qa_data/aws/sts.json")
if success != true {
fmt.Println("Could not load private STS configuration.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
return
}
stringCompare.SetString(*credentials.StringOf("access_key"))
stringMatch1 := stringCompare.ContentsEqual("",true)
if (credentials.JsonTypeOf("access_key") != 1) || (stringMatch1 == true) {
fmt.Println("Missing or invalid access_key.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
return
}
stringCompare.SetString(*credentials.StringOf("secret_key"))
stringMatch2 := stringCompare.ContentsEqual("",true)
if (credentials.JsonTypeOf("secret_key") != 1) || (stringMatch2 == true) {
fmt.Println("Missing or invalid secret_key.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
return
}
expires := chilkat.NewCkDateTime()
now := chilkat.NewCkDateTime()
region := "us-west-2"
if credentials.HasMember("session_token") == true {
stringCompare.SetString(*credentials.StringOf("session_token"))
stringMatch3 := stringCompare.ContentsEqual("",true)
if (credentials.JsonTypeOf("session_token") != 1) || (stringMatch3 == true) {
fmt.Println("Missing or invalid session_token.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
return
}
stringCompare.SetString(*credentials.StringOf("expiration"))
stringMatch4 := stringCompare.ContentsEqual("",true)
if (credentials.JsonTypeOf("expiration") != 1) || (stringMatch4 == true) {
fmt.Println("Missing or invalid expiration.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
return
}
success = expires.SetFromTimestamp(*credentials.StringOf("expiration"))
if success != true {
fmt.Println("Source expiration is invalid.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
return
}
success = now.SetFromCurrentSystemTime()
if success != true {
fmt.Println("Could not read current time.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
return
}
if expires.DiffSeconds(now) <= 60 {
fmt.Println("Source credentials are expired or expire within 60 seconds; obtain fresh credentials.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
return
}
}
stringCompare.SetString(*credentials.StringOf("encoded_message"))
stringMatch5 := stringCompare.ContentsEqual("",true)
if (credentials.JsonTypeOf("encoded_message") != 1) || (stringMatch5 == true) {
fmt.Println("Missing or invalid encoded_message.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
return
}
endpoint := chilkat.NewStringBuilder()
endpoint.Append("sts.")
endpoint.Append(region)
endpoint.Append(".amazonaws.com")
aws := chilkat.NewAuthAws()
aws.SetAccessKey(credentials.StringOf("access_key"))
aws.SetSecretKey(credentials.StringOf("secret_key"))
aws.SetRegion(region)
aws.SetServiceName("sts")
aws.SetSignatureVersion(4)
rest := chilkat.NewRest()
rest.SetConnectTimeoutMs(20000)
rest.SetIdleTimeoutMs(60000)
rest.SetVerboseLogging(false)
success = rest.SetAuthAws(aws)
if success != true {
fmt.Println("Could not configure request signing.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
return
}
// Keep this verified Socket alive while Rest uses its connection.
connection := chilkat.NewSocket()
connection.SetRequireSslCertVerify(true)
connection.SetMaxReadIdleMs(60000)
connection.SetMaxSendIdleMs(60000)
success = connection.Connect(*endpoint.GetAsString(),443,true,20000)
if success != true {
fmt.Println("TLS connection failed; inspect diagnostics privately.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
return
}
success = rest.UseConnection(connection,true)
if success != true {
fmt.Println("Could not attach TLS connection.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
return
}
xml := chilkat.NewXml()
xml.SetStrict(true)
var value *string = new(string)
var responseXml *string = new(string)
*responseXml = ""
success = rest.ClearAllQueryParams()
if success != true {
fmt.Println("Could not reset request parameters.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
return
}
success = rest.AddQueryParam("Action","DecodeAuthorizationMessage")
if success != true {
fmt.Println("Could not set Action.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
return
}
success = rest.AddQueryParam("Version","2011-06-15")
if success != true {
fmt.Println("Could not set Version.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
return
}
if credentials.HasMember("session_token") == true {
success = rest.AddQueryParam("X-Amz-Security-Token",*credentials.StringOf("session_token"))
if success != true {
fmt.Println("Could not set X-Amz-Security-Token.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
return
}
}
success = rest.AddQueryParam("EncodedMessage",*credentials.StringOf("encoded_message"))
if success != true {
fmt.Println("Could not set EncodedMessage.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
return
}
responseXml = rest.FullRequestFormUrlEncoded("POST","/")
if rest.LastMethodSuccess() != true {
fmt.Println("STS transport failed; inspect diagnostics privately.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
return
}
if rest.ResponseStatusCode() != 200 {
fmt.Println("STS HTTP status: ", rest.ResponseStatusCode())
success = xml.LoadXml(*responseXml)
if success == true {
value = xml.GetChildContent("Error|Code")
if xml.LastMethodSuccess() == true {
fmt.Println("AWS error code: ", *value)
}
value = xml.GetChildContent("RequestId")
if xml.LastMethodSuccess() == true {
fmt.Println("AWS request ID: ", *value)
}
}
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
return
}
success = xml.LoadXml(*responseXml)
if success != true {
fmt.Println("Invalid STS XML response.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
return
}
stringCompare.SetString(xml.Tag())
stringMatch6 := stringCompare.ContentsEqual("DecodeAuthorizationMessageResponse",true)
if stringMatch6 != true {
fmt.Println("Unexpected STS response root.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
return
}
if xml.HasChildWithTag("DecodeAuthorizationMessageResult") == true {
value = xml.GetChildContent("ResponseMetadata|RequestId")
stringCompare.SetString(*value)
stringMatch7 := stringCompare.ContentsEqual("",true)
if (xml.LastMethodSuccess() != true) || (stringMatch7 == true) {
fmt.Println("Missing or empty ResponseMetadata|RequestId.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
return
}
} else {
value = xml.GetChildContent("requestId")
stringCompare.SetString(*value)
stringMatch8 := stringCompare.ContentsEqual("",true)
if (xml.LastMethodSuccess() != true) || (stringMatch8 == true) {
fmt.Println("Missing or empty requestId.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
return
}
}
decodedPath := "DecodeAuthorizationMessageResult|DecodedMessage"
// Also accept the top-level form shown in the AWS reference's example response.
if xml.HasChildWithTag("DecodeAuthorizationMessageResult") != true {
decodedPath = "DecodedMessage"
}
value = xml.GetChildContent(decodedPath)
stringCompare.SetString(*value)
stringMatch9 := stringCompare.ContentsEqual("",true)
if (xml.LastMethodSuccess() != true) || (stringMatch9 == true) {
fmt.Println("Missing decoded authorization details.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
return
}
decoded := chilkat.NewJsonObject()
decoded.SetStrict(true)
success = decoded.Load(*value)
if success != true {
fmt.Println("Decoded authorization details are not valid JSON.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
decoded.DisposeJsonObject()
return
}
allowed := false
if (decoded.JsonTypeOf("allowed") != 5) && (decoded.JsonTypeOf("allowed") != 1) {
fmt.Println("Invalid authorization flag type.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
decoded.DisposeJsonObject()
return
}
if decoded.JsonTypeOf("allowed") == 5 {
allowed = decoded.BoolOf("allowed")
}
if decoded.JsonTypeOf("allowed") == 1 {
value = decoded.StringOf("allowed")
stringCompare.SetString(*value)
stringMatch10 := stringCompare.ContentsEqual("true",true)
stringCompare.SetString(*value)
stringMatch11 := stringCompare.ContentsEqual("false",true)
if (stringMatch10 != true) && (stringMatch11 != true) {
fmt.Println("Invalid string authorization flag.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
decoded.DisposeJsonObject()
return
}
stringCompare.SetString(*value)
stringMatch12 := stringCompare.ContentsEqual("true",true)
if stringMatch12 == true {
allowed = true
}
}
fmt.Println("allowed: ", allowed)
explicitDeny := false
if (decoded.JsonTypeOf("explicitDeny") != 5) && (decoded.JsonTypeOf("explicitDeny") != 1) {
fmt.Println("Invalid authorization flag type.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
decoded.DisposeJsonObject()
return
}
if decoded.JsonTypeOf("explicitDeny") == 5 {
explicitDeny = decoded.BoolOf("explicitDeny")
}
if decoded.JsonTypeOf("explicitDeny") == 1 {
value = decoded.StringOf("explicitDeny")
stringCompare.SetString(*value)
stringMatch13 := stringCompare.ContentsEqual("true",true)
stringCompare.SetString(*value)
stringMatch14 := stringCompare.ContentsEqual("false",true)
if (stringMatch13 != true) && (stringMatch14 != true) {
fmt.Println("Invalid string authorization flag.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
decoded.DisposeJsonObject()
return
}
stringCompare.SetString(*value)
stringMatch15 := stringCompare.ContentsEqual("true",true)
if stringMatch15 == true {
explicitDeny = true
}
}
fmt.Println("explicitDeny: ", explicitDeny)
// Inspect other decoded fields privately; they can contain privileged details.
fmt.Println("Example completed.")
stringCompare.DisposeStringBuilder()
credentials.DisposeJsonObject()
expires.DisposeCkDateTime()
now.DisposeCkDateTime()
endpoint.DisposeStringBuilder()
aws.DisposeAuthAws()
rest.DisposeRest()
connection.DisposeSocket()
xml.DisposeXml()
decoded.DisposeJsonObject()