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

AWS STS: Find the Account for an Access Key ID

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 lookup_access_key_id to the access key ID being investigated. Uses the caller's credentials to sign the request. The returned account does not establish key activity, validity of a secret, or effective permissions. Do not supply a secret access key as the lookup value.

AWS references: GetAccessKeyInfo

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loStringCompare
LOCAL loCredentials
LOCAL lnStringMatch1
LOCAL lnStringMatch2
LOCAL loExpires
LOCAL loNow
LOCAL lcRegion
LOCAL lnStringMatch3
LOCAL lnStringMatch4
LOCAL lnStringMatch5
LOCAL loEndpoint
LOCAL loAws
LOCAL loRest
LOCAL loConnection
LOCAL loXml
LOCAL lcValue
LOCAL lcResponseXml
LOCAL lnStringMatch6
LOCAL lnStringMatch7
LOCAL lnStringMatch8

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

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

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

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

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

    lnSuccess = loExpires.SetFromTimestamp(loCredentials.StringOf("expiration"))
    IF (lnSuccess <> 1) THEN
        ? "Source expiration is invalid."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        CANCEL
    ENDIF

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

    IF (loExpires.DiffSeconds(loNow) <= 60) THEN
        ? "Source credentials are expired or expire within 60 seconds; obtain fresh credentials."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        CANCEL
    ENDIF

ENDIF

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

loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("sts.")
loEndpoint.Append(lcRegion)
loEndpoint.Append(".amazonaws.com")
loAws = CreateObject('Chilkat.AuthAws')
loAws.AccessKey = loCredentials.StringOf("access_key")
loAws.SecretKey = loCredentials.StringOf("secret_key")
loAws.Region = lcRegion
loAws.ServiceName = "sts"
loAws.SignatureVersion = 4
loRest = CreateObject('Chilkat.Rest')
loRest.ConnectTimeoutMs = 20000
loRest.IdleTimeoutMs = 60000
loRest.VerboseLogging = 0
lnSuccess = loRest.SetAuthAws(loAws)
IF (lnSuccess <> 1) THEN
    ? "Could not configure request signing."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    CANCEL
ENDIF

*  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 loEndpoint
    RELEASE loAws
    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 loEndpoint
    RELEASE loAws
    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 loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

lnSuccess = loRest.AddQueryParam("Action","GetAccessKeyInfo")
IF (lnSuccess <> 1) THEN
    ? "Could not set Action."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loAws
    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 loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

IF (loCredentials.HasMember("session_token") = 1) THEN
    lnSuccess = loRest.AddQueryParam("X-Amz-Security-Token",loCredentials.StringOf("session_token"))
    IF (lnSuccess <> 1) THEN
        ? "Could not set X-Amz-Security-Token."
        RELEASE loStringCompare
        RELEASE loCredentials
        RELEASE loExpires
        RELEASE loNow
        RELEASE loEndpoint
        RELEASE loAws
        RELEASE loRest
        RELEASE loConnection
        RELEASE loXml
        CANCEL
    ENDIF

ENDIF

lnSuccess = loRest.AddQueryParam("AccessKeyId",loCredentials.StringOf("lookup_access_key_id"))
IF (lnSuccess <> 1) THEN
    ? "Could not set AccessKeyId."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loAws
    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 loEndpoint
    RELEASE loAws
    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 loEndpoint
    RELEASE loAws
    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 loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

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

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

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

lcValue = loXml.GetChildContent("GetAccessKeyInfoResult|Account")
loStringCompare.SetString(lcValue)
lnStringMatch8 = loStringCompare.ContentsEqual("",1)
IF ((loXml.LastMethodSuccess <> 1) OR (lnStringMatch8 = 1)) THEN
    ? "Missing or empty GetAccessKeyInfoResult|Account."
    RELEASE loStringCompare
    RELEASE loCredentials
    RELEASE loExpires
    RELEASE loNow
    RELEASE loEndpoint
    RELEASE loAws
    RELEASE loRest
    RELEASE loConnection
    RELEASE loXml
    CANCEL
ENDIF

? "Account: " + lcValue
? "Example completed."

RELEASE loStringCompare
RELEASE loCredentials
RELEASE loExpires
RELEASE loNow
RELEASE loEndpoint
RELEASE loAws
RELEASE loRest
RELEASE loConnection
RELEASE loXml