DUO API - Code 40101 Missing Request Credentials - Powershell


#1

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