Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
string ls_IntegrationKey
string ls_SecretKey
oleobject loo_Http
string ls_Url
oleobject loo_Req
oleobject loo_Resp
oleobject loo_Json
string ls_Txid
oleobject loo_SbUrl
string ls_Url
oleobject loo_SbResult
string ls_ResponseStatus
string ls_ResponseStatus_msg
integer i
integer li_MaxWaitIterations

li_Success = 0

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

ls_IntegrationKey = "DIMS3V5QDVG9J9ABRXC4"
ls_SecretKey = "HWVQ46nubLBxhnRlKddTltWIi3hL0fIQF2qTvLab"

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Http.Accept = "application/json"

// Use your own hostname here:
ls_Url = "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.
loo_Http.Login = ls_IntegrationKey
loo_Http.Password = ls_SecretKey

loo_Req = create oleobject
li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")

loo_Req.AddParam("username","matt")
loo_Req.AddParam("factor","push")
// The device ID can be obtained from the preauth response.  See Duo Preauth Example
loo_Req.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.
loo_Req.AddParam("async","1")

loo_Req.HttpVerb = "POST"
loo_Req.ContentType = "application/x-www-form-urlencoded"

loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpReq(ls_Url,loo_Req,loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Req
    destroy loo_Resp
    return
end if

Write-Debug "status code = " + string(loo_Resp.StatusCode)

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Json.Load(loo_Resp.BodyStr)
loo_Json.EmitCompact = 0
Write-Debug loo_Json.Emit()

if loo_Resp.StatusCode <> 200 then
    destroy loo_Http
    destroy loo_Req
    destroy loo_Resp
    destroy loo_Json
    return
end if

// Sample successful output:

// status code = 200

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

ls_Txid = loo_Json.StringOf("response.txid")

// Use your own hostname here:
loo_SbUrl = create oleobject
li_rc = loo_SbUrl.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbUrl.Append("https://api-a03782e1.duosecurity.com/auth/v2/auth_status?txid=")
loo_SbUrl.Append(ls_Txid)
ls_Url = loo_SbUrl.GetAsString()

Write-Debug "Auth status URL: " + ls_Url

loo_SbResult = create oleobject
li_rc = loo_SbResult.ConnectToNewObject("Chilkat.StringBuilder")

// Wait for a response...
i = 0
li_MaxWaitIterations = 100
do while i < li_MaxWaitIterations
    // Wait 3 seconds.
    loo_Http.SleepMs(3000)

    Write-Debug "Polling..."

    li_Success = loo_Http.HttpNoBody("GET",ls_Url,loo_Resp)
    if li_Success = 0 then
        Write-Debug loo_Http.LastErrorText
        destroy loo_Http
        destroy loo_Req
        destroy loo_Resp
        destroy loo_Json
        destroy loo_SbUrl
        destroy loo_SbResult
        return
    end if

    if loo_Resp.StatusCode <> 200 then
        Write-Debug "error status code = " + string(loo_Resp.StatusCode)
        Write-Debug loo_Resp.BodyStr
        Write-Debug "Failed."
        destroy loo_Http
        destroy loo_Req
        destroy loo_Resp
        destroy loo_Json
        destroy loo_SbUrl
        destroy loo_SbResult
        return
    end if

    // Sample response:

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

    loo_Json.Load(loo_Resp.BodyStr)

    // The responseResult can be "allow", "deny", or "waiting"
    loo_SbResult.Clear()
    loo_Json.StringOfSb("response.result",loo_SbResult)
    ls_ResponseStatus = loo_Json.StringOf("response.status")
    ls_ResponseStatus_msg = loo_Json.StringOf("response.status_msg")

    Write-Debug loo_SbResult.GetAsString()
    Write-Debug ls_ResponseStatus
    Write-Debug ls_ResponseStatus_msg
    Write-Debug ""

    if loo_SbResult.ContentsEqual("waiting",1) = 1 then
        i = i + 1
    else
        // Force loop exit..
        i = li_MaxWaitIterations
    end if

loop

Write-Debug "Finished."


destroy loo_Http
destroy loo_Req
destroy loo_Resp
destroy loo_Json
destroy loo_SbUrl
destroy loo_SbResult