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

AWS STS: Chain Roles with SourceIdentity

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.

Supply fresh temporary credentials from the first role (including expiration), role_arn for the next role, role_session_name and source_identity. If source identity was already set, use that same immutable value; otherwise set a value allowed by policy. Both sides must authorize sts:SetSourceIdentity as applicable, in addition to AssumeRole. Requests one hour and checks the returned identity. A source identity cannot be changed during a chain.

Source identity propagation

AWS references: AssumeRole

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("role_arn"))
stringMatch5 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("role_arn") <> 1) Or (stringMatch5 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid role_arn.") & "</pre>"
    Response.End
End If

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

set identifier = Server.CreateObject("Chilkat.StringBuilder")
success = identifier.Append(credentials.StringOf("role_session_name"))
If ((identifier.Length < 2) Or (identifier.Length > 64)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "role_session_name must have 2-64 characters.") & "</pre>"
    Response.End
End If

set allowed = Server.CreateObject("Chilkat.StringBuilder")
success = allowed.Append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+=,.@-")
charIndex = 0
Do While (charIndex < identifier.Length)
    If (allowed.Contains(identifier.GetRange(charIndex,1,0),1) <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in role_session_name.") & "</pre>"
        Response.End
    End If

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

success = stringCompare.SetString(credentials.StringOf("source_identity"))
stringMatch8 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("source_identity") <> 1) Or (stringMatch8 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid source_identity.") & "</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","AssumeRole")
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("RoleArn",credentials.StringOf("role_arn"))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set RoleArn.") & "</pre>"
    Response.End
End If

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

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

success = rest.AddQueryParam("SourceIdentity",credentials.StringOf("source_identity"))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set SourceIdentity.") & "</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)
stringMatch9 = stringCompare.ContentsEqual("AssumeRoleResponse",1)
If (stringMatch9 <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Unexpected STS response root.") & "</pre>"
    Response.End
End If

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

If (xml.HasChildWithTag("AssumeRoleResult") <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing STS result.") & "</pre>"
    Response.End
End If

accessKey = xml.GetChildContent("AssumeRoleResult|Credentials|AccessKeyId")
success = stringCompare.SetString(accessKey)
stringMatch11 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch11 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or empty AssumeRoleResult|Credentials|AccessKeyId.") & "</pre>"
    Response.End
End If

secretKey = xml.GetChildContent("AssumeRoleResult|Credentials|SecretAccessKey")
success = stringCompare.SetString(secretKey)
stringMatch12 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch12 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or empty AssumeRoleResult|Credentials|SecretAccessKey.") & "</pre>"
    Response.End
End If

sessionToken = xml.GetChildContent("AssumeRoleResult|Credentials|SessionToken")
success = stringCompare.SetString(sessionToken)
stringMatch13 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch13 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or empty AssumeRoleResult|Credentials|SessionToken.") & "</pre>"
    Response.End
End If

expiration = xml.GetChildContent("AssumeRoleResult|Credentials|Expiration")
success = stringCompare.SetString(expiration)
stringMatch14 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch14 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or empty AssumeRoleResult|Credentials|Expiration.") & "</pre>"
    Response.End
End If

success = expires.SetFromTimestamp(expiration)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Returned 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( "Returned credentials are expired or expire within 60 seconds; obtain fresh credentials.") & "</pre>"
    Response.End
End If

value = xml.GetChildContent("AssumeRoleResult|AssumedRoleUser|Arn")
success = stringCompare.SetString(value)
stringMatch15 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch15 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or empty AssumeRoleResult|AssumedRoleUser|Arn.") & "</pre>"
    Response.End
End If

value = xml.GetChildContent("AssumeRoleResult|AssumedRoleUser|AssumedRoleId")
success = stringCompare.SetString(value)
stringMatch16 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch16 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or empty AssumeRoleResult|AssumedRoleUser|AssumedRoleId.") & "</pre>"
    Response.End
End If

set session = Server.CreateObject("Chilkat.JsonObject")
success = session.UpdateString("access_key",accessKey)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not build complete session.") & "</pre>"
    Response.End
End If

success = session.UpdateString("secret_key",secretKey)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not build complete session.") & "</pre>"
    Response.End
End If

success = session.UpdateString("session_token",sessionToken)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not build complete session.") & "</pre>"
    Response.End
End If

success = session.UpdateString("expiration",expiration)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not build complete session.") & "</pre>"
    Response.End
End If

'  Use session in memory; do not print or log session.Emit().
Response.Write "<pre>" & Server.HTMLEncode( "Session validated; expiration: " & expiration) & "</pre>"
value = xml.GetChildContent("AssumeRoleResult|SourceIdentity")
success = stringCompare.SetString(value)
stringMatch17 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch17 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or empty AssumeRoleResult|SourceIdentity.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(value)
stringMatch18 = stringCompare.ContentsEqual(credentials.StringOf("source_identity"),1)
If (stringMatch18 <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Unexpected returned source identity.") & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Example completed.") & "</pre>"

%>
</body>
</html>