Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loStringCompare
LOCAL loCredentials
LOCAL loExpires
LOCAL loNow
LOCAL lcRegion
LOCAL lnStringMatch1
LOCAL lnStringMatch2
LOCAL loIdentifier
LOCAL loAllowed
LOCAL lnCharIndex
LOCAL lnStringMatch3
LOCAL loEndpoint
LOCAL loRest
LOCAL loConnection
LOCAL loXml
LOCAL lcValue
LOCAL lcAccessKey
LOCAL lcSecretKey
LOCAL lcSessionToken
LOCAL lcExpiration
LOCAL lcResponseXml
LOCAL lnStringMatch4
LOCAL lnStringMatch5
LOCAL lnStringMatch6
LOCAL lnStringMatch7
LOCAL lnStringMatch8
LOCAL lnStringMatch9
LOCAL lnStringMatch10
LOCAL lnStringMatch11
LOCAL loSession

lnSuccess = 0

loStringCompare = CreateObject('Chilkat.StringBuilder')
*  Assumes Chilkat is unlocked. See Global Unlock Sample.
*  Keep this file private and outside public web content/source control.
loCredentials = CreateObject('Chilkat.JsonObject')
loCredentials.Strict = 1
lnSuccess = loCredentials.LoadFile("qa_data/aws/sts.json")
IF (lnSuccess <> 1) THEN
    ? "Could not load private STS configuration."
    RELEASE loStringCompare
    RELEASE loCredentials
    CANCEL
ENDIF

loExpires = CreateObject('Chilkat.CkDateTime')
loNow = CreateObject('Chilkat.CkDateTime')
lcRegion = "us-west-2"
loStringCompare.SetString(loCredentials.StringOf("role_arn"))
lnStringMatch1 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("role_arn") <> 1) OR (lnStringMatch1 = 1)) THEN
    ? "Missing or invalid role_arn."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    CANCEL
ENDIF

loStringCompare.SetString(loCredentials.StringOf("role_session_name"))
lnStringMatch2 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("role_session_name") <> 1) OR (lnStringMatch2 = 1)) THEN
    ? "Missing or invalid role_session_name."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    CANCEL
ENDIF

loIdentifier = CreateObject('Chilkat.StringBuilder')
loIdentifier.Append(loCredentials.StringOf("role_session_name"))
IF ((loIdentifier.Length < 2) OR (loIdentifier.Length > 64)) THEN
    ? "role_session_name must have 2-64 characters."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    CANCEL
ENDIF

loAllowed = CreateObject('Chilkat.StringBuilder')
loAllowed.Append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+=,.@-")
lnCharIndex = 0
DO WHILE (lnCharIndex < loIdentifier.Length)
    IF (loAllowed.Contains(loIdentifier.GetRange(lnCharIndex,1,0),1) <> 1) THEN
        ? "Invalid character in role_session_name."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loIdentifier
        RELEASE loAllowed
        CANCEL
    ENDIF

    lnCharIndex = lnCharIndex + 1
ENDDO
loStringCompare.SetString(loCredentials.StringOf("web_identity_token"))
lnStringMatch3 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("web_identity_token") <> 1) OR (lnStringMatch3 = 1)) THEN
    ? "Missing or invalid web_identity_token."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    CANCEL
ENDIF

loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("sts.")
loEndpoint.Append(lcRegion)
loEndpoint.Append(".amazonaws.com")
loRest = CreateObject('Chilkat.Rest')
loRest.ConnectTimeoutMs = 20000
loRest.IdleTimeoutMs = 60000
loRest.VerboseLogging = 0
*  Keep this verified Socket alive while Rest uses its connection.
loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
loConnection.MaxReadIdleMs = 60000
loConnection.MaxSendIdleMs = 60000
lnSuccess = loConnection.Connect(loEndpoint.GetAsString(),443,1,20000)
IF (lnSuccess <> 1) THEN
    ? "TLS connection failed; inspect diagnostics privately."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    CANCEL
ENDIF

lnSuccess = loRest.UseConnection(loConnection,1)
IF (lnSuccess <> 1) THEN
    ? "Could not attach TLS connection."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    CANCEL
ENDIF

loXml = CreateObject('Chilkat.Xml')
loXml.Strict = 1

lcResponseXml = ""
lnSuccess = loRest.ClearAllQueryParams()
IF (lnSuccess <> 1) THEN
    ? "Could not reset request parameters."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loRest.AddQueryParam("Action","AssumeRoleWithWebIdentity")
IF (lnSuccess <> 1) THEN
    ? "Could not set Action."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loRest.AddQueryParam("Version","2011-06-15")
IF (lnSuccess <> 1) THEN
    ? "Could not set Version."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loRest.AddQueryParam("RoleArn",loCredentials.StringOf("role_arn"))
IF (lnSuccess <> 1) THEN
    ? "Could not set RoleArn."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loRest.AddQueryParam("RoleSessionName",loCredentials.StringOf("role_session_name"))
IF (lnSuccess <> 1) THEN
    ? "Could not set RoleSessionName."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loRest.AddQueryParam("DurationSeconds","3600")
IF (lnSuccess <> 1) THEN
    ? "Could not set DurationSeconds."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loRest.AddQueryParam("WebIdentityToken",loCredentials.StringOf("web_identity_token"))
IF (lnSuccess <> 1) THEN
    ? "Could not set WebIdentityToken."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcResponseXml = loRest.FullRequestFormUrlEncoded("POST","/")
IF (loRest.LastMethodSuccess <> 1) THEN
    ? "STS transport failed; inspect diagnostics privately."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

IF (loRest.ResponseStatusCode <> 200) THEN
    ? "STS HTTP status: " + STR(loRest.ResponseStatusCode)
    lnSuccess = loXml.LoadXml(lcResponseXml)
    IF (lnSuccess = 1) THEN
        lcValue = loXml.GetChildContent("Error|Code")
        IF (loXml.LastMethodSuccess = 1) THEN
            ? "AWS error code: " + lcValue
        ENDIF

        lcValue = loXml.GetChildContent("RequestId")
        IF (loXml.LastMethodSuccess = 1) THEN
            ? "AWS request ID: " + lcValue
        ENDIF

    ENDIF

    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loXml.LoadXml(lcResponseXml)
IF (lnSuccess <> 1) THEN
    ? "Invalid STS XML response."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

loStringCompare.SetString(loXml.Tag)
lnStringMatch4 = loStringCompare.ContentsEqual("AssumeRoleWithWebIdentityResponse",1)
IF (lnStringMatch4 <> 1) THEN
    ? "Unexpected STS response root."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcValue = loXml.GetChildContent("ResponseMetadata|RequestId")
loStringCompare.SetString(lcValue)
lnStringMatch5 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch5 = 1)) THEN
    ? "Missing or empty ResponseMetadata|RequestId."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

IF (loXml.HasChildWithTag("AssumeRoleWithWebIdentityResult") <> 1) THEN
    ? "Missing STS result."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcAccessKey = loXml.GetChildContent("AssumeRoleWithWebIdentityResult|Credentials|AccessKeyId")
loStringCompare.SetString(lcAccessKey)
lnStringMatch6 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch6 = 1)) THEN
    ? "Missing or empty AssumeRoleWithWebIdentityResult|Credentials|AccessKeyId."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcSecretKey = loXml.GetChildContent("AssumeRoleWithWebIdentityResult|Credentials|SecretAccessKey")
loStringCompare.SetString(lcSecretKey)
lnStringMatch7 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch7 = 1)) THEN
    ? "Missing or empty AssumeRoleWithWebIdentityResult|Credentials|SecretAccessKey."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcSessionToken = loXml.GetChildContent("AssumeRoleWithWebIdentityResult|Credentials|SessionToken")
loStringCompare.SetString(lcSessionToken)
lnStringMatch8 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch8 = 1)) THEN
    ? "Missing or empty AssumeRoleWithWebIdentityResult|Credentials|SessionToken."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcExpiration = loXml.GetChildContent("AssumeRoleWithWebIdentityResult|Credentials|Expiration")
loStringCompare.SetString(lcExpiration)
lnStringMatch9 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch9 = 1)) THEN
    ? "Missing or empty AssumeRoleWithWebIdentityResult|Credentials|Expiration."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loExpires.SetFromTimestamp(lcExpiration)
IF (lnSuccess <> 1) THEN
    ? "Returned expiration is invalid."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loNow.SetFromCurrentSystemTime()
IF (lnSuccess <> 1) THEN
    ? "Could not read current time."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

IF (loExpires.DiffSeconds(loNow) <= 60) THEN
    ? "Returned credentials are expired or expire within 60 seconds; obtain fresh credentials."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcValue = loXml.GetChildContent("AssumeRoleWithWebIdentityResult|AssumedRoleUser|Arn")
loStringCompare.SetString(lcValue)
lnStringMatch10 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch10 = 1)) THEN
    ? "Missing or empty AssumeRoleWithWebIdentityResult|AssumedRoleUser|Arn."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lcValue = loXml.GetChildContent("AssumeRoleWithWebIdentityResult|AssumedRoleUser|AssumedRoleId")
loStringCompare.SetString(lcValue)
lnStringMatch11 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch11 = 1)) THEN
    ? "Missing or empty AssumeRoleWithWebIdentityResult|AssumedRoleUser|AssumedRoleId."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

loSession = CreateObject('Chilkat.JsonObject')
lnSuccess = loSession.UpdateString("access_key",lcAccessKey)
IF (lnSuccess <> 1) THEN
    ? "Could not build complete session."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loSession
    CANCEL
ENDIF

lnSuccess = loSession.UpdateString("secret_key",lcSecretKey)
IF (lnSuccess <> 1) THEN
    ? "Could not build complete session."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loSession
    CANCEL
ENDIF

lnSuccess = loSession.UpdateString("session_token",lcSessionToken)
IF (lnSuccess <> 1) THEN
    ? "Could not build complete session."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loSession
    CANCEL
ENDIF

lnSuccess = loSession.UpdateString("expiration",lcExpiration)
IF (lnSuccess <> 1) THEN
    ? "Could not build complete session."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loIdentifier
    RELEASE loAllowed
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    RELEASE loSession
    CANCEL
ENDIF

*  Use session in memory; do not print or log session.Emit().
? "Session validated; expiration: " + lcExpiration
? "Example completed."

RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loIdentifier
RELEASE loAllowed
RELEASE loEndpoint
RELEASE loRest
RELEASE loConnection
RELEASE loXml
RELEASE loSession