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

Duo Auth API - Preauth

See more Duo Auth MFA Examples

The /preauth endpoint determines whether a user is authorized to log in, and (if so) returns the user's available authentication factors.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

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

    const integration_key = "DIMS3V5QDVG9J9ABRXC4";
    const secret_key = "HWVQ46nubLBxhnRlKddTltWIi3hL0fIQF2qTvLab";

    const http = try chilkat.Http.init();
    defer http.deinit();

    http.setAccept("application/json");

    // Use your own hostname here:
    const url = "https://api-a03782e1.duosecurity.com/auth/v2/preauth";

    http.setLogin(integration_key);
    http.setPassword(secret_key);

    const req = try chilkat.HttpRequest.init();
    defer req.deinit();
    req.addParam("username", "matt");

    req.setHttpVerb("POST");
    req.setContentType("application/x-www-form-urlencoded");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpReq(url, req, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("status code = {d}\n", .{resp.getStatusCode()});

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    try json.load(try resp.getBodyStr(alloc));
    json.setEmitCompact(false);
    std.debug.print("{s}\n", .{try json.emit(alloc)});

    // Sample successful output:

    // status code = 200

    // {
    //   "response": {
    //     "devices": [
    //       {
    //         "capabilities": [
    //           "auto",
    //           "push",
    //           "sms",
    //           "mobile_otp"
    //         ],
    //         "device": "DP6GYVTQ5NK82BMR851F",
    //         "display_name": "iOS (XXX-XXX-1871)",
    //         "name": "",
    //         "number": "XXX-XXX-1871",
    //         "type": "phone"
    //       }
    //     ],
    //     "result": "auth",
    //     "status_msg": "Account is active"
    //   },
    //   "stat": "OK"
    // }

    // Sample code to parse the above JSON response..

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var device: [:0]const u8 = "";
    var display_name: [:0]const u8 = "";
    var name: [:0]const u8 = "";
    var number: [:0]const u8 = "";
    var v_type: [:0]const u8 = "";
    var j: i32 = 0;
    var count_j: i32 = 0;
    var str_val: [:0]const u8 = "";

    _ = try json.stringOf(alloc, "response.result");
    _ = try json.stringOf(alloc, "response.status_msg");
    _ = try json.stringOf(alloc, "stat");
    var i: i32 = 0;
    const count_i = json.sizeOfArray("response.devices");
    while (i < count_i) {
        json.setI(i);
        device = try json.stringOf(alloc, "response.devices[i].device");
        display_name = try json.stringOf(alloc, "response.devices[i].display_name");
        name = try json.stringOf(alloc, "response.devices[i].name");
        number = try json.stringOf(alloc, "response.devices[i].number");
        v_type = try json.stringOf(alloc, "response.devices[i].type");
        j = 0;
        count_j = json.sizeOfArray("response.devices[i].capabilities");
        while (j < count_j) {
            json.setJ(j);
            str_val = try json.stringOf(alloc, "response.devices[i].capabilities[j]");
            j = j + 1;
        }

        i = i + 1;
    }
}