01-22-2019 07:02 AM
Would anybody be able to help me out here? Having problems connecting to the DUO api in powershell, i think it is to do with the headers but cannot figure out what is wrong???
function ConvertTo-Base64($string)
{
$bytes = [System.Text.Encoding]::UTF8.GetBytes($string);
$encoded = [System.Convert]::ToBase64String($bytes);
return $encoded;
}
$intKey = “integrationkey”
$sKey = “secretkey”
$hostname = “hostname”
$path = “/auth/v2/check”
$params = $null
$method = “GET”
$contentType = “application/x-www-form-urlencoded”
$day = (get-date).day
$dayAbr = (get-date).DayOfWeek.ToString().Substring(0,3)
$month = (get-date).Month
$monthAbr = (Get-Culture).DateTimeFormat.GetAbbreviatedMonthName($month)
$year = (get-date).Year
$time = (get-date).TimeOfDay
[string]$date = $dayAbr + “, " + $day + " " + $monthAbr + " " + $year + " " + $time.Hours + “:” + $time.Minutes + “:” + $time.Seconds + " -0000”
message = ($date + “n" + $method + "
n” + $hostname + “n" + $path + "
n”)
$secret = $sKey
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Text.Encoding]::ASCII.GetBytes($secret)
$signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($message))
$signature = [Convert]::ToBase64String($signature)
$b64 = ConvertTo-Base64($intKey + “:” + $signature);
$auth = "Basic " + $signature;
$headers = @{}
$headers.Add(“Date”, $date)
$headers.Add(“Authorization”, $auth)
$headers.Add(“Host”, $hostname)
$headers.Add(“Content-Length”, “35”)
$headers.Add(“Content-Type”, $contentType)
Invoke-WebRequest -Uri $(“https://” + $hostname + $command) -Headers $headers
04-18-2019 02:50 PM
Check out this function I created that will create a properly formatted request
https://github.com/StarkCaptain/duo/blob/master/scripts/new-duoRequest.ps1
$Request = New-DuoRequest -apiHost ‘■■■■’ -apiEndpoint ‘/admin/v1/users’ -apiKey ‘nnnnn’ -apiSecret ‘nnnnn’
Invoke-RestMethod @Request
Its forked from the below module, however I didn’t prefer how the author assumes certain things about how you store the api secret so I re-wrote it a bit.
03-13-2020 12:54 PM
I tried the module but i get this error. Verified the integration Key and secret key. Is there something I am missing?
Invoke-RestMethod : {“code”: 40102, “message”: “Invalid integration key in request credentials”, “stat”: “FAIL”}
03-13-2020 07:21 PM
I am still using that powershell function as is today. Do you have the admin api application setup with the necessary API permissions based on the api endpoints you want to use?
03-16-2020 09:26 AM
I believe I do but I am new to DUO so is there anything I should look for?
07-16-2020 01:37 AM
Hi This is Yuri
do you know how to call api with powershell?
BR
07-29-2020 05:43 PM
what is message = ($date + “n" + $method + “n” + $hostname + “n” + $path + "n”)
$secret = $sKey
$hmacsha = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha.key = [Text.Encoding]::ASCII.GetBytes($secret)
$signature = $hmacsha.ComputeHash([Text.Encoding]::ASCII.GetBytes($message))
$signature = [Convert]::ToBase64String($signature)
$b64 = ConvertTo-Base64($intKey + “:” + $signature);
$auth = "Basic " + $signature;?? do we still need?
04-14-2021 09:59 AM
Just want to verify that this function worked for me. You can test it by calling the function as described in the comments at the top. Thanks! — L
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide