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

AWS STS: Get Caller Identity

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.

Shows the account, ARN and principal ID of the credentials actually used. No sts:GetCallerIdentity permission grant is needed. Successful identity lookup does not prove permission to access another service.

AWS references: GetCallerIdentity

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkAuthAws.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkDateTime.pb"
IncludeFile "CkSocket.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"

Procedure ChilkatExample()

    success.i = 0

    stringCompare.i = CkStringBuilder::ckCreate()
    If stringCompare.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.
    ;  Keep this file private and outside public web content/source control.
    credentials.i = CkJsonObject::ckCreate()
    If credentials.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::setCkStrict(credentials, 1)
    success = CkJsonObject::ckLoadFile(credentials,"qa_data/aws/sts.json")
    If success <> 1
        Debug "Could not load private STS configuration."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        ProcedureReturn
    EndIf

    CkStringBuilder::ckSetString(stringCompare,CkJsonObject::ckStringOf(credentials,"access_key"))
    stringMatch1.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If (CkJsonObject::ckJsonTypeOf(credentials,"access_key") <> 1) OR (stringMatch1 = 1)
        Debug "Missing or invalid access_key."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        ProcedureReturn
    EndIf

    CkStringBuilder::ckSetString(stringCompare,CkJsonObject::ckStringOf(credentials,"secret_key"))
    stringMatch2.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If (CkJsonObject::ckJsonTypeOf(credentials,"secret_key") <> 1) OR (stringMatch2 = 1)
        Debug "Missing or invalid secret_key."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        ProcedureReturn
    EndIf

    expires.i = CkDateTime::ckCreate()
    If expires.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    now.i = CkDateTime::ckCreate()
    If now.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    region.s = "us-west-2"
    If CkJsonObject::ckHasMember(credentials,"session_token") = 1
        CkStringBuilder::ckSetString(stringCompare,CkJsonObject::ckStringOf(credentials,"session_token"))
        stringMatch3.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
        If (CkJsonObject::ckJsonTypeOf(credentials,"session_token") <> 1) OR (stringMatch3 = 1)
            Debug "Missing or invalid session_token."
            CkStringBuilder::ckDispose(stringCompare)
            CkJsonObject::ckDispose(credentials)
            CkDateTime::ckDispose(expires)
            CkDateTime::ckDispose(now)
            ProcedureReturn
        EndIf

        CkStringBuilder::ckSetString(stringCompare,CkJsonObject::ckStringOf(credentials,"expiration"))
        stringMatch4.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
        If (CkJsonObject::ckJsonTypeOf(credentials,"expiration") <> 1) OR (stringMatch4 = 1)
            Debug "Missing or invalid expiration."
            CkStringBuilder::ckDispose(stringCompare)
            CkJsonObject::ckDispose(credentials)
            CkDateTime::ckDispose(expires)
            CkDateTime::ckDispose(now)
            ProcedureReturn
        EndIf

        success = CkDateTime::ckSetFromTimestamp(expires,CkJsonObject::ckStringOf(credentials,"expiration"))
        If success <> 1
            Debug "Source expiration is invalid."
            CkStringBuilder::ckDispose(stringCompare)
            CkJsonObject::ckDispose(credentials)
            CkDateTime::ckDispose(expires)
            CkDateTime::ckDispose(now)
            ProcedureReturn
        EndIf

        success = CkDateTime::ckSetFromCurrentSystemTime(now)
        If success <> 1
            Debug "Could not read current time."
            CkStringBuilder::ckDispose(stringCompare)
            CkJsonObject::ckDispose(credentials)
            CkDateTime::ckDispose(expires)
            CkDateTime::ckDispose(now)
            ProcedureReturn
        EndIf

        If CkDateTime::ckDiffSeconds(expires,now) <= 60
            Debug "Source credentials are expired or expire within 60 seconds; obtain fresh credentials."
            CkStringBuilder::ckDispose(stringCompare)
            CkJsonObject::ckDispose(credentials)
            CkDateTime::ckDispose(expires)
            CkDateTime::ckDispose(now)
            ProcedureReturn
        EndIf

    EndIf

    endpoint.i = CkStringBuilder::ckCreate()
    If endpoint.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(endpoint,"sts.")
    CkStringBuilder::ckAppend(endpoint,region)
    CkStringBuilder::ckAppend(endpoint,".amazonaws.com")
    aws.i = CkAuthAws::ckCreate()
    If aws.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkAuthAws::setCkAccessKey(aws, CkJsonObject::ckStringOf(credentials,"access_key"))
    CkAuthAws::setCkSecretKey(aws, CkJsonObject::ckStringOf(credentials,"secret_key"))
    CkAuthAws::setCkRegion(aws, region)
    CkAuthAws::setCkServiceName(aws, "sts")
    CkAuthAws::setCkSignatureVersion(aws, 4)
    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkRest::setCkConnectTimeoutMs(rest, 20000)
    CkRest::setCkIdleTimeoutMs(rest, 60000)
    CkRest::setCkVerboseLogging(rest, 0)
    success = CkRest::ckSetAuthAws(rest,aws)
    If success <> 1
        Debug "Could not configure request signing."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ;  Keep this verified Socket alive while Rest uses its connection.
    connection.i = CkSocket::ckCreate()
    If connection.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkSocket::setCkRequireSslCertVerify(connection, 1)
    CkSocket::setCkMaxReadIdleMs(connection, 60000)
    CkSocket::setCkMaxSendIdleMs(connection, 60000)
    success = CkSocket::ckConnect(connection,CkStringBuilder::ckGetAsString(endpoint),443,1,20000)
    If success <> 1
        Debug "TLS connection failed; inspect diagnostics privately."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        ProcedureReturn
    EndIf

    success = CkRest::ckUseConnection(rest,connection,1)
    If success <> 1
        Debug "Could not attach TLS connection."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        ProcedureReturn
    EndIf

    xml.i = CkXml::ckCreate()
    If xml.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkXml::setCkStrict(xml, 1)
    value.s
    responseXml.s = ""
    success = CkRest::ckClearAllQueryParams(rest)
    If success <> 1
        Debug "Could not reset request parameters."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

    success = CkRest::ckAddQueryParam(rest,"Action","GetCallerIdentity")
    If success <> 1
        Debug "Could not set Action."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

    success = CkRest::ckAddQueryParam(rest,"Version","2011-06-15")
    If success <> 1
        Debug "Could not set Version."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

    If CkJsonObject::ckHasMember(credentials,"session_token") = 1
        success = CkRest::ckAddQueryParam(rest,"X-Amz-Security-Token",CkJsonObject::ckStringOf(credentials,"session_token"))
        If success <> 1
            Debug "Could not set X-Amz-Security-Token."
            CkStringBuilder::ckDispose(stringCompare)
            CkJsonObject::ckDispose(credentials)
            CkDateTime::ckDispose(expires)
            CkDateTime::ckDispose(now)
            CkStringBuilder::ckDispose(endpoint)
            CkAuthAws::ckDispose(aws)
            CkRest::ckDispose(rest)
            CkSocket::ckDispose(connection)
            CkXml::ckDispose(xml)
            ProcedureReturn
        EndIf

    EndIf

    responseXml = CkRest::ckFullRequestFormUrlEncoded(rest,"POST","/")
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug "STS transport failed; inspect diagnostics privately."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

    If CkRest::ckResponseStatusCode(rest) <> 200
        Debug "STS HTTP status: " + Str(CkRest::ckResponseStatusCode(rest))
        success = CkXml::ckLoadXml(xml,responseXml)
        If success = 1
            value = CkXml::ckGetChildContent(xml,"Error|Code")
            If CkXml::ckLastMethodSuccess(xml) = 1
                Debug "AWS error code: " + value
            EndIf

            value = CkXml::ckGetChildContent(xml,"RequestId")
            If CkXml::ckLastMethodSuccess(xml) = 1
                Debug "AWS request ID: " + value
            EndIf

        EndIf

        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

    success = CkXml::ckLoadXml(xml,responseXml)
    If success <> 1
        Debug "Invalid STS XML response."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

    CkStringBuilder::ckSetString(stringCompare,CkXml::ckTag(xml))
    stringMatch5.i = CkStringBuilder::ckContentsEqual(stringCompare,"GetCallerIdentityResponse",1)
    If stringMatch5 <> 1
        Debug "Unexpected STS response root."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

    value = CkXml::ckGetChildContent(xml,"ResponseMetadata|RequestId")
    CkStringBuilder::ckSetString(stringCompare,value)
    stringMatch6.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If (CkXml::ckLastMethodSuccess(xml) <> 1) OR (stringMatch6 = 1)
        Debug "Missing or empty ResponseMetadata|RequestId."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

    If CkXml::ckHasChildWithTag(xml,"GetCallerIdentityResult") <> 1
        Debug "Missing STS result."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

    value = CkXml::ckGetChildContent(xml,"GetCallerIdentityResult|Account")
    CkStringBuilder::ckSetString(stringCompare,value)
    stringMatch7.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If (CkXml::ckLastMethodSuccess(xml) <> 1) OR (stringMatch7 = 1)
        Debug "Missing or empty GetCallerIdentityResult|Account."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

    Debug "Account: " + value
    value = CkXml::ckGetChildContent(xml,"GetCallerIdentityResult|Arn")
    CkStringBuilder::ckSetString(stringCompare,value)
    stringMatch8.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If (CkXml::ckLastMethodSuccess(xml) <> 1) OR (stringMatch8 = 1)
        Debug "Missing or empty GetCallerIdentityResult|Arn."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

    Debug "Arn: " + value
    value = CkXml::ckGetChildContent(xml,"GetCallerIdentityResult|UserId")
    CkStringBuilder::ckSetString(stringCompare,value)
    stringMatch9.i = CkStringBuilder::ckContentsEqual(stringCompare,"",1)
    If (CkXml::ckLastMethodSuccess(xml) <> 1) OR (stringMatch9 = 1)
        Debug "Missing or empty GetCallerIdentityResult|UserId."
        CkStringBuilder::ckDispose(stringCompare)
        CkJsonObject::ckDispose(credentials)
        CkDateTime::ckDispose(expires)
        CkDateTime::ckDispose(now)
        CkStringBuilder::ckDispose(endpoint)
        CkAuthAws::ckDispose(aws)
        CkRest::ckDispose(rest)
        CkSocket::ckDispose(connection)
        CkXml::ckDispose(xml)
        ProcedureReturn
    EndIf

    Debug "UserId: " + value
    Debug "Example completed."


    CkStringBuilder::ckDispose(stringCompare)
    CkJsonObject::ckDispose(credentials)
    CkDateTime::ckDispose(expires)
    CkDateTime::ckDispose(now)
    CkStringBuilder::ckDispose(endpoint)
    CkAuthAws::ckDispose(aws)
    CkRest::ckDispose(rest)
    CkSocket::ckDispose(connection)
    CkXml::ckDispose(xml)


    ProcedureReturn
EndProcedure