Duo API Bash Call

Viewed 2073

I'm trying to use Curl to perform a call with the DUO API.

I tried reviewed their docs here: https://duo.com/docs/adminapi#authentication

The docs says to pass the creds as HMAC key for the request but now sure how to get that going.

This is what I got so far:

curl --request GET \
     --header 'Authorization: Basic 'Integration key:Secret key'' \
     --header "Content-Type: application/x-www-form-urlencoded" \
     "https://api-12345678.duosecurity.com/auth/v2/check"

Which returns

{"code": 40101, "message": "Missing request credentials", "stat": "FAIL"}

Can one point me to the right direction for an example in Bash. If not in Python.

2 Answers

First, your request format does not seem correct, because Integration key:Secret key'' is outside the header (look at the way the syntax is highlighted in the question).

Try:

curl --request GET \
     --header 'Authorization: Basic' \
     --header 'Integration key: Secret key' \
     --header 'Date: Tue, 21 Aug 2012 17:29:18 -0000' \
     --header "Content-Type: application/x-www-form-urlencoded" \
     "https://api-12345678.duosecurity.com/auth/v2/check"

It's somewhat uncommon to have header names with a space and a lowercase like Integration key, so you may need to experiment with variants, like Integration-Key.

Second, the 401xx series errors mean:

401 The “Authorization”, “Date”, and/or “Content-Type” headers were missing or invalid.

You'll need to add the missing the Date header, required by the authenticator.

In case anyone else stumbles on this, here's what I came up with:

#!/bin/bash -u

FORM="Content-Type: application/x-www-form-urlencoded"
NOW=$(date -R)

#get these from the Duo Admin interface
INT="<integration key>"
KEY="<secret passcode>"
API="<api host>.duosecurity.com"

URL="/auth/v2/check"
REQ="$NOW\nGET\n$API\n$URL\n"

#could also use awk here, or the --binary mode as suggested elsewhere
HMAC=$(echo -n "$REQ" | openssl sha1 -hmac "$KEY" | cut -d" " -f 2)

AUTH=$(echo -n "$INT:$HMAC" | base64 -w0)

curl -s -H "Date: $NOW" -H $FORM -H "Authorization: Basic $AUTH" https://$API$URL

Running this yields:

{"response": {"time": 1539726254}, "stat": "OK"}

Reference: Duo Api docs section on authentication

Related