cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
445
Views
0
Helpful
3
Replies

Duo enroll user Rest Api Call java

I want to enroll the user for duo authentication while I create a new user in my application. Is there any java client available which can be used for enrolling the user to DUO

3 Replies 3

DuoKristina
Cisco Employee
Cisco Employee

You can use the Admin API examples in https://github.com/duosecurity/duo_client_java to figure out how to call the Auth API enroll endpoint https://duo.com/docs/authapi#/enroll or make use of the enroll response received by the preauth endpoint https://duo.com/docs/authapi#/preauth.

If your application is able to render a web page for authentication consider using our WebSDK instead, which will handle user enrollment for you.

https://duo.com/docs/duoweb

https://github.com/duosecurity/duo_universal_java 

Duo, not DUO.

How to do http authentication in java? It is throwing Access forbidden error

enamul-haque
Level 1
Level 1

I have use below like which is working fine:

1. Add below dependency to pom.xml'

<dependency>
    <groupId>com.duosecurity</groupId>
    <artifactId>duo-client</artifactId>
    <version>0.6.0</version>
</dependency>

2. User below code:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Base64;
import java.util.SortedMap;
import java.util.TreeMap;

public class AdminAPIExample {

private static SortedMap<String, Object> params = new TreeMap<String, Object>();

public static void main(String[] args) throws IOException {
String ikey = "x";
String skey = "x";
String host = "x";
String httpMethod = "POST";
String requestPath = "/auth/v2/auth";

String timestamp = OffsetDateTime.now().format(DateTimeFormatter.RFC_1123_DATE_TIME);

params.put("username","20230612");
params.put("factor","push");
params.put("device","auto");

String queryString = canonQueryString();
System.out.println("queryString = " + queryString);
// System.out.println("queryString = " +params.size());


String canonicalRequest = timestamp +"\n" + httpMethod.toUpperCase() +"\n" + host.toLowerCase() +"\n"+requestPath +"\n" +queryString;
System.out.println("canonicalRequest = " + canonicalRequest);

String signature = sign2(canonicalRequest, skey);
System.out.println("signature = " + signature);

String url = "https://" + host.toLowerCase() + requestPath+"?"+queryString;
System.out.println("url = " + url);

String authString ="Basic "+ Base64.getEncoder().encodeToString((ikey + ":" + signature).getBytes());
System.out.println("authString = " + authString);
System.out.println("authString length= " + authString.length());


HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Date", timestamp);
httpPost.setHeader("Authorization", authString);
httpPost.setHeader("Host", host.toLowerCase());
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");


// Make the request
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseContent = entity != null ? EntityUtils.toString(entity) : "";

String rs = "Response Status Code: " + response.getStatusLine().getStatusCode() + "\nResponse Content:\n" + responseContent;
System.out.println("rs = " + rs);
}

public static String canonQueryString()
throws UnsupportedEncodingException {
ArrayList<String> args = new ArrayList<String>();

for (String key : params.keySet()) {
String name = URLEncoder
.encode(key, "UTF-8")
.replace("+", "%20")
.replace("*", "%2A")
.replace("%7E", "~");
String value = URLEncoder
.encode(params.get(key).toString(), "UTF-8")
.replace("+", "%20")
.replace("*", "%2A")
.replace("%7E", "~");
args.add(name + "=" + value);
}

return com.duosecurity.client.Util.join(args.toArray(), "&");
}

private static String bytesToHex(byte[] bytes) {
StringBuilder hexStringBuilder = new StringBuilder();
for (byte b : bytes) {
hexStringBuilder.append(String.format("%02x", b));
}
return hexStringBuilder.toString();
}

static String sign2(String data, String secretKey){
try {

byte[] secretKeyBytes = secretKey.getBytes("UTF-8");
byte[] messageBytes = data.getBytes("UTF-8");

// Create a secret key specification
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKeyBytes, "HmacSHA1"); // Use a suitable HMAC algorithm

// Initialize the HMAC with the secret key
Mac mac = Mac.getInstance("HmacSHA1"); // Use the same HMAC algorithm
mac.init(secretKeySpec);

// Calculate the HMAC
byte[] hmacBytes = mac.doFinal(messageBytes);

// Convert the HMAC to a hexadecimal string
StringBuilder hexStringBuilder = new StringBuilder();
for (byte b : hmacBytes) {
hexStringBuilder.append(String.format("%02x", b));
}
String hexHMAC = hexStringBuilder.toString();

// Print the hex HMAC
System.out.println(hexHMAC);



return hexHMAC;
} catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}

}

Note: All Other API are similar. Just change your intregation key, Secrete key, host, end point and parameters.

Check here: Admin API 

 

Quick Links