Last active
June 1, 2021 17:01
-
-
Save shurab/fa9cef457a48786add9742fa7291c5c4 to your computer and use it in GitHub Desktop.
The Ruby way to interact with Duo Admin API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'base64' | |
require 'openssl' | |
require 'json' | |
require 'time' | |
# See Duo Admin API details here: https://duo.com/docs/adminapi#authentication | |
# TODO: replace URL/user/ikey/skey by your values | |
user = '502814310' | |
ikey = 'DIWJ8X6AEYOR5OMC6TQ1' | |
skey = 'Zh5eGmUq9zpfQnyUIu5OL9iWoMMv5ZNmk3zLJ4Ep' | |
now = Time.now.rfc2822 | |
# GET requests use this five-line format: | |
# | |
# Sun, 13 Oct 2019 18:12:05 -0000 | |
# GET | |
# api-XXXXXXXX.duosecurity.com | |
# /admin/v1/users | |
# username=502814310 | |
canon = [ | |
now, | |
"GET", | |
"api-XXXXXXXX.duosecurity.com", | |
"/admin/v1/users", | |
"username=#{user}" | |
].join("\n") | |
auth = "#{ikey}:#{OpenSSL::HMAC.hexdigest('sha1', skey, canon)}" | |
query = %Q{curl -s -X GET -H "Date: #{now}" \ | |
-H "Content-Type: application/x-www-form-urlencoded" \ | |
-H "Authorization: Basic #{Base64.strict_encode64(auth)}" \ | |
https://api-XXXXXXXX.duosecurity.com/admin/v1/users?username=#{user}} | |
puts "Query: #{query}\n" | |
response = `#{query}` | |
jobj = JSON.parse(response) | |
if jobj['stat'] == "FAIL" | |
puts jobj['message'] | |
else | |
puts JSON.pretty_generate(jobj['response']) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment