Rust Requires Chilkat v11.0.0+
Rust
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 Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let integration_key = "DIMS3V5QDVG9J9ABRXC4".to_string();
let secret_key = "HWVQ46nubLBxhnRlKddTltWIi3hL0fIQF2qTvLab".to_string();
let http = chilkat::Http::new();
http.set_accept("application/json");
// Use your own hostname here:
let url = "https://api-a03782e1.duosecurity.com/auth/v2/auth".to_string();
// 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.
http.set_login(&integration_key);
http.set_password(&secret_key);
let req = chilkat::HttpRequest::new();
req.add_param("username", "matt");
req.add_param("factor", "push");
// The device ID can be obtained from the preauth response. See Duo Preauth Example
req.add_param("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.
req.add_param("async", "1");
req.set_http_verb("POST");
req.set_content_type("application/x-www-form-urlencoded");
let resp = chilkat::HttpResponse::new();
if http.http_req(&url, &req, &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
println!("status code = {}", resp.status_code());
let json = chilkat::JsonObject::new();
let _ = json.load(&resp.body_str()).is_ok();
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());
if resp.status_code() != 200 {
return;
}
// Sample successful output:
// status code = 200
// {
// "stat": "OK",
// "response": {
// "txid": "45f7c92b-f45f-4862-8545-e0f58e78075a"
// }
// }
let txid = json.string_of("response.txid").unwrap_or_default();
// Use your own hostname here:
let sb_url = chilkat::StringBuilder::new();
let _ = sb_url.append("https://api-a03782e1.duosecurity.com/auth/v2/auth_status?txid=");
let _ = sb_url.append(&txid);
let url = sb_url.get_as_string().unwrap_or_default();
println!("Auth status URL: {}", url);
let sb_result = chilkat::StringBuilder::new();
let mut response_status = String::new();
let mut response_status_msg = String::new();
// Wait for a response...
let mut i = 0;
let max_wait_iterations = 100;
while i < max_wait_iterations {
// Wait 3 seconds.
http.sleep_ms(3000);
println!("Polling...");
if http.http_no_body("GET", &url, &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
if resp.status_code() != 200 {
println!("error status code = {}", resp.status_code());
println!("{}", resp.body_str());
println!("Failed.");
return;
}
// Sample response:
// {
// "stat": "OK",
// "response": {
// "result": "waiting",
// "status": "pushed",
// "status_msg": "Pushed a login request to your phone..."
// }
// }
let _ = json.load(&resp.body_str());
// The responseResult can be "allow", "deny", or "waiting"
sb_result.clear();
let _ = json.string_of_sb("response.result", &sb_result);
response_status = json.string_of("response.status").unwrap_or_default();
response_status_msg = json.string_of("response.status_msg").unwrap_or_default();
println!("{}", sb_result.get_as_string().unwrap_or_default());
println!("{}", response_status);
println!("{}", response_status_msg);
println!("");
if sb_result.contents_equal("waiting", true) {
i = i + 1;
} else {
// Force loop exit..
i = max_wait_iterations;
}
}
println!("Finished.");