Sample code for 30+ languages & platforms
Classic ASP Requires Chilkat v11.6.0+

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 Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

set stringCompare = Server.CreateObject("Chilkat.StringBuilder")
'  Assumes Chilkat is unlocked. See Global Unlock Sample.
'  Keep this file private and outside public web content/source control.
set credentials = Server.CreateObject("Chilkat.JsonObject")
credentials.Strict = 1
success = credentials.LoadFile("qa_data/aws/sts.json")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not load private STS configuration.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(credentials.StringOf("access_key"))
stringMatch1 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("access_key") <> 1) Or (stringMatch1 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid access_key.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(credentials.StringOf("secret_key"))
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("secret_key") <> 1) Or (stringMatch2 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid secret_key.") & "</pre>"
    Response.End
End If

set expires = Server.CreateObject("Chilkat.CkDateTime")
set now = Server.CreateObject("Chilkat.CkDateTime")
region = "us-west-2"
If (credentials.HasMember("session_token") = 1) Then
    success = stringCompare.SetString(credentials.StringOf("session_token"))
    stringMatch3 = stringCompare.ContentsEqual("",1)
    If ((credentials.JsonTypeOf("session_token") <> 1) Or (stringMatch3 = 1)) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid session_token.") & "</pre>"
        Response.End
    End If

    success = stringCompare.SetString(credentials.StringOf("expiration"))
    stringMatch4 = stringCompare.ContentsEqual("",1)
    If ((credentials.JsonTypeOf("expiration") <> 1) Or (stringMatch4 = 1)) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid expiration.") & "</pre>"
        Response.End
    End If

    success = expires.SetFromTimestamp(credentials.StringOf("expiration"))
    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Source expiration is invalid.") & "</pre>"
        Response.End
    End If

    success = now.SetFromCurrentSystemTime()
    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Could not read current time.") & "</pre>"
        Response.End
    End If

    If (expires.DiffSeconds(now) <= 60) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Source credentials are expired or expire within 60 seconds; obtain fresh credentials.") & "</pre>"
        Response.End
    End If

End If

success = stringCompare.SetString(credentials.StringOf("encoded_message"))
stringMatch5 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("encoded_message") <> 1) Or (stringMatch5 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid encoded_message.") & "</pre>"
    Response.End
End If

set endpoint = Server.CreateObject("Chilkat.StringBuilder")
success = endpoint.Append("sts.")
success = endpoint.Append(region)
success = endpoint.Append(".amazonaws.com")
set aws = Server.CreateObject("Chilkat.AuthAws")
aws.AccessKey = credentials.StringOf("access_key")
aws.SecretKey = credentials.StringOf("secret_key")
aws.Region = region
aws.ServiceName = "sts"
aws.SignatureVersion = 4
set rest = Server.CreateObject("Chilkat.Rest")
rest.ConnectTimeoutMs = 20000
rest.IdleTimeoutMs = 60000
rest.VerboseLogging = 0
success = rest.SetAuthAws(aws)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not configure request signing.") & "</pre>"
    Response.End
End If

'  Keep this verified Socket alive while Rest uses its connection.
set connection = Server.CreateObject("Chilkat.Socket")
connection.RequireSslCertVerify = 1
connection.MaxReadIdleMs = 60000
connection.MaxSendIdleMs = 60000
success = connection.Connect(endpoint.GetAsString(),443,1,20000)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "TLS connection failed; inspect diagnostics privately.") & "</pre>"
    Response.End
End If

success = rest.UseConnection(connection,1)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not attach TLS connection.") & "</pre>"
    Response.End
End If

set xml = Server.CreateObject("Chilkat.Xml")
xml.Strict = 1

responseXml = ""
success = rest.ClearAllQueryParams()
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not reset request parameters.") & "</pre>"
    Response.End
End If

success = rest.AddQueryParam("Action","DecodeAuthorizationMessage")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set Action.") & "</pre>"
    Response.End
End If

success = rest.AddQueryParam("Version","2011-06-15")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set Version.") & "</pre>"
    Response.End
End If

If (credentials.HasMember("session_token") = 1) Then
    success = rest.AddQueryParam("X-Amz-Security-Token",credentials.StringOf("session_token"))
    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Could not set X-Amz-Security-Token.") & "</pre>"
        Response.End
    End If

End If

success = rest.AddQueryParam("EncodedMessage",credentials.StringOf("encoded_message"))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set EncodedMessage.") & "</pre>"
    Response.End
End If

responseXml = rest.FullRequestFormUrlEncoded("POST","/")
If (rest.LastMethodSuccess <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "STS transport failed; inspect diagnostics privately.") & "</pre>"
    Response.End
End If

If (rest.ResponseStatusCode <> 200) Then
    Response.Write "<pre>" & Server.HTMLEncode( "STS HTTP status: " & rest.ResponseStatusCode) & "</pre>"
    success = xml.LoadXml(responseXml)
    If (success = 1) Then
        value = xml.GetChildContent("Error|Code")
        If (xml.LastMethodSuccess = 1) Then
            Response.Write "<pre>" & Server.HTMLEncode( "AWS error code: " & value) & "</pre>"
        End If

        value = xml.GetChildContent("RequestId")
        If (xml.LastMethodSuccess = 1) Then
            Response.Write "<pre>" & Server.HTMLEncode( "AWS request ID: " & value) & "</pre>"
        End If

    End If

    Response.End
End If

success = xml.LoadXml(responseXml)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Invalid STS XML response.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(xml.Tag)
stringMatch6 = stringCompare.ContentsEqual("DecodeAuthorizationMessageResponse",1)
If (stringMatch6 <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Unexpected STS response root.") & "</pre>"
    Response.End
End If

If (xml.HasChildWithTag("DecodeAuthorizationMessageResult") = 1) Then
    value = xml.GetChildContent("ResponseMetadata|RequestId")
    success = stringCompare.SetString(value)
    stringMatch7 = stringCompare.ContentsEqual("",1)
    If ((xml.LastMethodSuccess <> 1) Or (stringMatch7 = 1)) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Missing or empty ResponseMetadata|RequestId.") & "</pre>"
        Response.End
    End If

Else
    value = xml.GetChildContent("requestId")
    success = stringCompare.SetString(value)
    stringMatch8 = stringCompare.ContentsEqual("",1)
    If ((xml.LastMethodSuccess <> 1) Or (stringMatch8 = 1)) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Missing or empty requestId.") & "</pre>"
        Response.End
    End If

End If

decodedPath = "DecodeAuthorizationMessageResult|DecodedMessage"
'  Also accept the top-level form shown in the AWS reference's example response.
If (xml.HasChildWithTag("DecodeAuthorizationMessageResult") <> 1) Then
    decodedPath = "DecodedMessage"
End If

value = xml.GetChildContent(decodedPath)
success = stringCompare.SetString(value)
stringMatch9 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch9 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing decoded authorization details.") & "</pre>"
    Response.End
End If

set decoded = Server.CreateObject("Chilkat.JsonObject")
decoded.Strict = 1
success = decoded.Load(value)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Decoded authorization details are not valid JSON.") & "</pre>"
    Response.End
End If

allowed = 0
If ((decoded.JsonTypeOf("allowed") <> 5) And (decoded.JsonTypeOf("allowed") <> 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Invalid authorization flag type.") & "</pre>"
    Response.End
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)
    stringMatch10 = stringCompare.ContentsEqual("true",1)
    success = stringCompare.SetString(value)
    stringMatch11 = stringCompare.ContentsEqual("false",1)
    If ((stringMatch10 <> 1) And (stringMatch11 <> 1)) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Invalid string authorization flag.") & "</pre>"
        Response.End
    End If

    success = stringCompare.SetString(value)
    stringMatch12 = stringCompare.ContentsEqual("true",1)
    If (stringMatch12 = 1) Then
        allowed = 1
    End If

End If

Response.Write "<pre>" & Server.HTMLEncode( "allowed: " & allowed) & "</pre>"
explicitDeny = 0
If ((decoded.JsonTypeOf("explicitDeny") <> 5) And (decoded.JsonTypeOf("explicitDeny") <> 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Invalid authorization flag type.") & "</pre>"
    Response.End
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)
    stringMatch13 = stringCompare.ContentsEqual("true",1)
    success = stringCompare.SetString(value)
    stringMatch14 = stringCompare.ContentsEqual("false",1)
    If ((stringMatch13 <> 1) And (stringMatch14 <> 1)) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Invalid string authorization flag.") & "</pre>"
        Response.End
    End If

    success = stringCompare.SetString(value)
    stringMatch15 = stringCompare.ContentsEqual("true",1)
    If (stringMatch15 = 1) Then
        explicitDeny = 1
    End If

End If

Response.Write "<pre>" & Server.HTMLEncode( "explicitDeny: " & explicitDeny) & "</pre>"
'  Inspect other decoded fields privately; they can contain privileged details.
Response.Write "<pre>" & Server.HTMLEncode( "Example completed.") & "</pre>"

%>
</body>
</html>