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

AWS STS: Exchange an OIDC Token for Role Credentials

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.

Provide role_arn, role_session_name and the complete web_identity_token from your OIDC provider. The exchange is unsigned: no AWS access key or SigV4 header is sent. Configure the IAM provider and role trust for the expected issuer, audience and subject. STS validates the token; this sample does not treat decoding a JWT as verification. Refresh expired input through your identity provider.

AWS references: AssumeRoleWithWebIdentity

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

$oStringCompare = ObjCreate("Chilkat.StringBuilder")
;  Assumes Chilkat is unlocked. See Global Unlock Sample.
;  Keep this file private and outside public web content/source control.
$oCredentials = ObjCreate("Chilkat.JsonObject")
$oCredentials.Strict = True
$bSuccess = $oCredentials.LoadFile("qa_data/aws/sts.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not load private STS configuration." & @CRLF)
    Exit
EndIf

$oExpires = ObjCreate("Chilkat.CkDateTime")
$oNow = ObjCreate("Chilkat.CkDateTime")
Local $sRegion = "us-west-2"
$oStringCompare.SetString($oCredentials.StringOf("role_arn"))
Local $bStringMatch1 = $oStringCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("role_arn") <> 1) Or ($bStringMatch1 = True)) Then
    ConsoleWrite("Missing or invalid role_arn." & @CRLF)
    Exit
EndIf

$oStringCompare.SetString($oCredentials.StringOf("role_session_name"))
Local $bStringMatch2 = $oStringCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("role_session_name") <> 1) Or ($bStringMatch2 = True)) Then
    ConsoleWrite("Missing or invalid role_session_name." & @CRLF)
    Exit
EndIf

$oIdentifier = ObjCreate("Chilkat.StringBuilder")
$oIdentifier.Append($oCredentials.StringOf("role_session_name"))
If (($oIdentifier.Length < 2) Or ($oIdentifier.Length > 64)) Then
    ConsoleWrite("role_session_name must have 2-64 characters." & @CRLF)
    Exit
EndIf

$oAllowed = ObjCreate("Chilkat.StringBuilder")
$oAllowed.Append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+=,.@-")
Local $iCharIndex = 0
While ($iCharIndex < $oIdentifier.Length)
    If ($oAllowed.Contains($oIdentifier.GetRange($iCharIndex,1,False),True) <> True) Then
        ConsoleWrite("Invalid character in role_session_name." & @CRLF)
        Exit
    EndIf

    $iCharIndex = $iCharIndex + 1
Wend
$oStringCompare.SetString($oCredentials.StringOf("web_identity_token"))
Local $bStringMatch3 = $oStringCompare.ContentsEqual("",True)
If (($oCredentials.JsonTypeOf("web_identity_token") <> 1) Or ($bStringMatch3 = True)) Then
    ConsoleWrite("Missing or invalid web_identity_token." & @CRLF)
    Exit
EndIf

$oEndpoint = ObjCreate("Chilkat.StringBuilder")
$oEndpoint.Append("sts.")
$oEndpoint.Append($sRegion)
$oEndpoint.Append(".amazonaws.com")
$oRest = ObjCreate("Chilkat.Rest")
$oRest.ConnectTimeoutMs = 20000
$oRest.IdleTimeoutMs = 60000
$oRest.VerboseLogging = False
;  Keep this verified Socket alive while Rest uses its connection.
$oConnection = ObjCreate("Chilkat.Socket")
$oConnection.RequireSslCertVerify = True
$oConnection.MaxReadIdleMs = 60000
$oConnection.MaxSendIdleMs = 60000
$bSuccess = $oConnection.Connect($oEndpoint.GetAsString(),443,True,20000)
If ($bSuccess <> True) Then
    ConsoleWrite("TLS connection failed; inspect diagnostics privately." & @CRLF)
    Exit
EndIf

$bSuccess = $oRest.UseConnection($oConnection,True)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not attach TLS connection." & @CRLF)
    Exit
EndIf

$oXml = ObjCreate("Chilkat.Xml")
$oXml.Strict = True
Local $sValue
Local $sAccessKey
Local $secretKey
Local $sessionToken
Local $sExpiration
Local $sResponseXml = ""
$bSuccess = $oRest.ClearAllQueryParams()
If ($bSuccess <> True) Then
    ConsoleWrite("Could not reset request parameters." & @CRLF)
    Exit
EndIf

$bSuccess = $oRest.AddQueryParam("Action","AssumeRoleWithWebIdentity")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set Action." & @CRLF)
    Exit
EndIf

$bSuccess = $oRest.AddQueryParam("Version","2011-06-15")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set Version." & @CRLF)
    Exit
EndIf

$bSuccess = $oRest.AddQueryParam("RoleArn",$oCredentials.StringOf("role_arn"))
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set RoleArn." & @CRLF)
    Exit
EndIf

$bSuccess = $oRest.AddQueryParam("RoleSessionName",$oCredentials.StringOf("role_session_name"))
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set RoleSessionName." & @CRLF)
    Exit
EndIf

$bSuccess = $oRest.AddQueryParam("DurationSeconds","3600")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set DurationSeconds." & @CRLF)
    Exit
EndIf

$bSuccess = $oRest.AddQueryParam("WebIdentityToken",$oCredentials.StringOf("web_identity_token"))
If ($bSuccess <> True) Then
    ConsoleWrite("Could not set WebIdentityToken." & @CRLF)
    Exit
EndIf

$sResponseXml = $oRest.FullRequestFormUrlEncoded("POST","/")
If ($oRest.LastMethodSuccess <> True) Then
    ConsoleWrite("STS transport failed; inspect diagnostics privately." & @CRLF)
    Exit
EndIf

If ($oRest.ResponseStatusCode <> 200) Then
    ConsoleWrite("STS HTTP status: " & $oRest.ResponseStatusCode & @CRLF)
    $bSuccess = $oXml.LoadXml($sResponseXml)
    If ($bSuccess = True) Then
        $sValue = $oXml.GetChildContent("Error|Code")
        If ($oXml.LastMethodSuccess = True) Then
            ConsoleWrite("AWS error code: " & $sValue & @CRLF)
        EndIf

        $sValue = $oXml.GetChildContent("RequestId")
        If ($oXml.LastMethodSuccess = True) Then
            ConsoleWrite("AWS request ID: " & $sValue & @CRLF)
        EndIf

    EndIf

    Exit
EndIf

$bSuccess = $oXml.LoadXml($sResponseXml)
If ($bSuccess <> True) Then
    ConsoleWrite("Invalid STS XML response." & @CRLF)
    Exit
EndIf

$oStringCompare.SetString($oXml.Tag)
Local $bStringMatch4 = $oStringCompare.ContentsEqual("AssumeRoleWithWebIdentityResponse",True)
If ($bStringMatch4 <> True) Then
    ConsoleWrite("Unexpected STS response root." & @CRLF)
    Exit
EndIf

$sValue = $oXml.GetChildContent("ResponseMetadata|RequestId")
$oStringCompare.SetString($sValue)
Local $bStringMatch5 = $oStringCompare.ContentsEqual("",True)
If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch5 = True)) Then
    ConsoleWrite("Missing or empty ResponseMetadata|RequestId." & @CRLF)
    Exit
EndIf

If ($oXml.HasChildWithTag("AssumeRoleWithWebIdentityResult") <> True) Then
    ConsoleWrite("Missing STS result." & @CRLF)
    Exit
EndIf

$sAccessKey = $oXml.GetChildContent("AssumeRoleWithWebIdentityResult|Credentials|AccessKeyId")
$oStringCompare.SetString($sAccessKey)
Local $bStringMatch6 = $oStringCompare.ContentsEqual("",True)
If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch6 = True)) Then
    ConsoleWrite("Missing or empty AssumeRoleWithWebIdentityResult|Credentials|AccessKeyId." & @CRLF)
    Exit
EndIf

$secretKey = $oXml.GetChildContent("AssumeRoleWithWebIdentityResult|Credentials|SecretAccessKey")
$oStringCompare.SetString($secretKey)
Local $bStringMatch7 = $oStringCompare.ContentsEqual("",True)
If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch7 = True)) Then
    ConsoleWrite("Missing or empty AssumeRoleWithWebIdentityResult|Credentials|SecretAccessKey." & @CRLF)
    Exit
EndIf

$sessionToken = $oXml.GetChildContent("AssumeRoleWithWebIdentityResult|Credentials|SessionToken")
$oStringCompare.SetString($sessionToken)
Local $bStringMatch8 = $oStringCompare.ContentsEqual("",True)
If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch8 = True)) Then
    ConsoleWrite("Missing or empty AssumeRoleWithWebIdentityResult|Credentials|SessionToken." & @CRLF)
    Exit
EndIf

$sExpiration = $oXml.GetChildContent("AssumeRoleWithWebIdentityResult|Credentials|Expiration")
$oStringCompare.SetString($sExpiration)
Local $bStringMatch9 = $oStringCompare.ContentsEqual("",True)
If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch9 = True)) Then
    ConsoleWrite("Missing or empty AssumeRoleWithWebIdentityResult|Credentials|Expiration." & @CRLF)
    Exit
EndIf

$bSuccess = $oExpires.SetFromTimestamp($sExpiration)
If ($bSuccess <> True) Then
    ConsoleWrite("Returned expiration is invalid." & @CRLF)
    Exit
EndIf

$bSuccess = $oNow.SetFromCurrentSystemTime()
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read current time." & @CRLF)
    Exit
EndIf

If ($oExpires.DiffSeconds($oNow) <= 60) Then
    ConsoleWrite("Returned credentials are expired or expire within 60 seconds; obtain fresh credentials." & @CRLF)
    Exit
EndIf

$sValue = $oXml.GetChildContent("AssumeRoleWithWebIdentityResult|AssumedRoleUser|Arn")
$oStringCompare.SetString($sValue)
Local $bStringMatch10 = $oStringCompare.ContentsEqual("",True)
If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch10 = True)) Then
    ConsoleWrite("Missing or empty AssumeRoleWithWebIdentityResult|AssumedRoleUser|Arn." & @CRLF)
    Exit
EndIf

$sValue = $oXml.GetChildContent("AssumeRoleWithWebIdentityResult|AssumedRoleUser|AssumedRoleId")
$oStringCompare.SetString($sValue)
Local $bStringMatch11 = $oStringCompare.ContentsEqual("",True)
If (($oXml.LastMethodSuccess <> True) Or ($bStringMatch11 = True)) Then
    ConsoleWrite("Missing or empty AssumeRoleWithWebIdentityResult|AssumedRoleUser|AssumedRoleId." & @CRLF)
    Exit
EndIf

$oSession = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oSession.UpdateString("access_key",$sAccessKey)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build complete session." & @CRLF)
    Exit
EndIf

$bSuccess = $oSession.UpdateString("secret_key",$secretKey)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build complete session." & @CRLF)
    Exit
EndIf

$bSuccess = $oSession.UpdateString("session_token",$sessionToken)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build complete session." & @CRLF)
    Exit
EndIf

$bSuccess = $oSession.UpdateString("expiration",$sExpiration)
If ($bSuccess <> True) Then
    ConsoleWrite("Could not build complete session." & @CRLF)
    Exit
EndIf

;  Use session in memory; do not print or log session.Emit().
ConsoleWrite("Session validated; expiration: " & $sExpiration & @CRLF)
ConsoleWrite("Example completed." & @CRLF)