Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

Duo Auth API - Async Auth

See more Duo Auth MFA Examples

If you enable async, then your application will be able to retrieve real-time status updates from the authentication process, rather than receiving no information until the process is complete.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL cIntegrationKey
LOCAL cSecretKey
LOCAL oHttp
LOCAL cUrl
LOCAL oReq
LOCAL oResp
LOCAL oJson
LOCAL cTxid
LOCAL oSbUrl
LOCAL cUrl
LOCAL oSbResult
LOCAL cResponseStatus
LOCAL cResponseStatus_msg
LOCAL i
LOCAL nMaxWaitIterations

nSuccess := 0

//  This example assumes the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

cIntegrationKey := "DIMS3V5QDVG9J9ABRXC4"
cSecretKey := "HWVQ46nubLBxhnRlKddTltWIi3hL0fIQF2qTvLab"

oHttp := CreateObject("Chilkat.Http")

oHttp:Accept := "application/json"

//  Use your own hostname here:
cUrl := "https://api-a03782e1.duosecurity.com/auth/v2/auth"

//  This example requires Chilkat v9.5.0.89 or greater because Chilkat will automatically
//  generate and send the HMAC signature for the requires based on the integration key and secret key.
oHttp:Login := cIntegrationKey
oHttp:Password := cSecretKey

oReq := CreateObject("Chilkat.HttpRequest")
oReq:AddParam("username", "matt")
oReq:AddParam("factor", "push")
//  The device ID can be obtained from the preauth response.  See Duo Preauth Example
oReq:AddParam("device", "DP6GYVTQ5NK82BMR851F")
//  Add the async param to get an immediate response, then periodically check for updates to find out when the MFA authentication completes for fails.
oReq:AddParam("async", "1")

oReq:HttpVerb := "POST"
oReq:ContentType := "application/x-www-form-urlencoded"

oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpReq(cUrl, oReq, oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oReq:destroy()
    oResp:destroy()
    RETURN
ENDIF

? "status code = " + Str(oResp:StatusCode)

oJson := CreateObject("Chilkat.JsonObject")
nSuccess := oJson:Load(oResp:BodyStr)
oJson:EmitCompact := 0
? oJson:Emit()

IF (oResp:StatusCode != 200)
    oHttp:destroy()
    oReq:destroy()
    oResp:destroy()
    oJson:destroy()
    RETURN
ENDIF

//  Sample successful output:

//  status code = 200

//  {
//    "stat": "OK",
//    "response": {
//      "txid": "45f7c92b-f45f-4862-8545-e0f58e78075a"
//    }
//  }

cTxid := oJson:StringOf("response.txid")

//  Use your own hostname here:
oSbUrl := CreateObject("Chilkat.StringBuilder")
oSbUrl:Append("https://api-a03782e1.duosecurity.com/auth/v2/auth_status?txid=")
oSbUrl:Append(cTxid)
cUrl := oSbUrl:GetAsString()

? "Auth status URL: " + cUrl

oSbResult := CreateObject("Chilkat.StringBuilder")

//  Wait for a response...
i := 0
nMaxWaitIterations := 100
DO WHILE i < nMaxWaitIterations
    //  Wait 3 seconds.
    oHttp:SleepMs(3000)

    ? "Polling..."

    nSuccess := oHttp:HttpNoBody("GET", cUrl, oResp)
    IF (nSuccess == 0)
        ? oHttp:LastErrorText
        oHttp:destroy()
        oReq:destroy()
        oResp:destroy()
        oJson:destroy()
        oSbUrl:destroy()
        oSbResult:destroy()
        RETURN
    ENDIF

    IF (oResp:StatusCode != 200)
        ? "error status code = " + Str(oResp:StatusCode)
        ? oResp:BodyStr
        ? "Failed."
        oHttp:destroy()
        oReq:destroy()
        oResp:destroy()
        oJson:destroy()
        oSbUrl:destroy()
        oSbResult:destroy()
        RETURN
    ENDIF

    //  Sample response:

    //  	{
    //  	  "stat": "OK",
    //  	  "response": {
    //  	    "result": "waiting",
    //  	    "status": "pushed",
    //  	    "status_msg": "Pushed a login request to your phone..."
    //  	  }
    //  	}

    oJson:Load(oResp:BodyStr)

    //  The responseResult can be "allow", "deny", or "waiting"
    oSbResult:Clear()
    oJson:StringOfSb("response.result", oSbResult)
    cResponseStatus := oJson:StringOf("response.status")
    cResponseStatus_msg := oJson:StringOf("response.status_msg")

    ? oSbResult:GetAsString()
    ? cResponseStatus
    ? cResponseStatus_msg
    ? ""

    IF (oSbResult:ContentsEqual("waiting", 1) == 1)
        i := i + 1
    ELSE
        //  Force loop exit..
        i := nMaxWaitIterations
    ENDIF

ENDDO

? "Finished."

oHttp:destroy()
oReq:destroy()
oResp:destroy()
oJson:destroy()
oSbUrl:destroy()
oSbResult:destroy()