Dart Requires Chilkat v11.0.0+
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
final integrationKey = 'DIMS3V5QDVG9J9ABRXC4';
final secretKey = 'HWVQ46nubLBxhnRlKddTltWIi3hL0fIQF2qTvLab';
final http = CkHttp();
http.accept = 'application/json';
// Use your own hostname here:
var 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.
http.login = integrationKey;
http.password = secretKey;
final req = CkHttpRequest();
req.addParam('username', 'matt');
req.addParam('factor', 'push');
// The device ID can be obtained from the preauth response. See Duo Preauth Example
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.
req.addParam('async', '1');
req.httpVerb = 'POST';
req.contentType = 'application/x-www-form-urlencoded';
final resp = CkHttpResponse();
try {
http.httpReq(url, req, resp);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
print('status code = ${resp.statusCode}');
final json = CkJsonObject();
json.load(resp.bodyStr);
json.emitCompact = false;
print(json.emit());
if (resp.statusCode != 200) {
return;
}
// Sample successful output:
// status code = 200
// {
// "stat": "OK",
// "response": {
// "txid": "45f7c92b-f45f-4862-8545-e0f58e78075a"
// }
// }
final txid = json.stringOf('response.txid');
// Use your own hostname here:
final sbUrl = CkStringBuilder();
sbUrl.append('https://api-a03782e1.duosecurity.com/auth/v2/auth_status?txid=');
sbUrl.append(txid);
url = sbUrl.getAsString();
print('Auth status URL: $url');
final sbResult = CkStringBuilder();
var responseStatus = '';
var responseStatusMsg = '';
// Wait for a response...
var i = 0;
final maxWaitIterations = 100;
while (i < maxWaitIterations) {
// Wait 3 seconds.
http.sleepMs(3000);
print('Polling...');
try {
http.httpNoBody('GET', url, resp);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
if (resp.statusCode != 200) {
print('error status code = ${resp.statusCode}');
print(resp.bodyStr);
print('Failed.');
return;
}
// Sample response:
// {
// "stat": "OK",
// "response": {
// "result": "waiting",
// "status": "pushed",
// "status_msg": "Pushed a login request to your phone..."
// }
// }
json.load(resp.bodyStr);
// The responseResult can be "allow", "deny", or "waiting"
sbResult.clear();
json.stringOfSb('response.result', sbResult);
responseStatus = json.stringOf('response.status');
responseStatusMsg = json.stringOf('response.status_msg');
print(sbResult.getAsString());
print(responseStatus);
print(responseStatusMsg);
print('');
if (sbResult.contentsEqual('waiting', true)) {
i++;
} else {
// Force loop exit..
i = maxWaitIterations;
}
}
print('Finished.');
}