<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Powershell API Authorization Encoding in APIs</title>
    <link>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877876#M170</link>
    <description>&lt;DIV class="duo-migrated-content"&gt;&lt;P&gt;Confirmed…  if I put in my own ikey, skey and Host (&lt;A href="http://api-xxxxxx-duosecurity.com" rel="noopener nofollow ugc"&gt;■■■■■■■■■■■■■■■■■■■■■■■■■■&lt;/A&gt;)  the function returns my user ID, or at least I’m assuming it is a user ID; (20 character string uppercase letters and numbers)  not sure where to verify it.&lt;/P&gt;&lt;/DIV&gt;</description>
    <pubDate>Wed, 31 Mar 2021 20:11:22 GMT</pubDate>
    <dc:creator>lkeyes1</dc:creator>
    <dc:date>2021-03-31T20:11:22Z</dc:date>
    <item>
      <title>Powershell API Authorization Encoding</title>
      <link>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877870#M164</link>
      <description>&lt;DIV class="duo-migrated-content"&gt;&lt;P&gt;I am working in an environment where I am unable to load custom modules, so the github solutions really won’t work for me.&lt;/P&gt;
&lt;P&gt;The authentication encoding method is a horribly twisted process.&lt;/P&gt;
&lt;P&gt;The latest attempt is (Keys and data are from the Authentication example):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class="lang-auto"&gt;$intKey = "■■■■■■■■■■■■■■■■■■■■"
$secretkey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
$hostname = "■■■■■■■■■■■■■■■■■■■■■■■■■■■■"
$path = "/accounts/v1/account/list"
$params = 'realname=First%20Last&amp;amp;username=root'
$method = "POST"
$date="Tue, 21 Aug 2012 17:29:18 -0000"

$lines = @($date,$method,$hostname,$path,$params)
$jlines  = [string]::Join("`n", $lines)

$hmacsha1 = New-Object System.Security.Cryptography.HMACSHA1
$hmacsha1.Key = [Text.Encoding]::ASCII.GetBytes($secretkey)
$signature = $hmacsha1.ComputeHash([Text.Encoding]::ASCII.GetBytes($jlines))
$hash_hex = [System.BitConverter]::ToString($signature) -replace '-', ''
$auth = $integration + ":" + $hash_hex
[byte[]]$plainText_bytes = [System.Text.Encoding]::ASCII.GetBytes($auth)
$return = [System.Convert]::ToBase64String($plainText_bytes)
$authorize = "Authorization : Basic " + $return
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The Result in the example is:&lt;BR /&gt;
Authorization: Basic RElXSjhYNkFFWU9SNU9NQzZUUTE6MmQ5N2Q2MTY2MzE5NzgxYjVhM2EwN2FmMzlkMzY2ZjQ5MTIzNGVkYw==&lt;/P&gt;
&lt;P&gt;However the result I am getting is:&lt;BR /&gt;
Authorization: Basic OjBGNzBCRTUzQTE1QkYxMzY3MkIwMkNCQ0EyOTFGODFCREUzREU5RDQ=&lt;/P&gt;
&lt;P&gt;Which is definitely not the expected result. An idea why it appears to be encoding differently?&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 12 Feb 2021 23:56:20 GMT</pubDate>
      <guid>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877870#M164</guid>
      <dc:creator>awheeler3</dc:creator>
      <dc:date>2021-02-12T23:56:20Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell API Authorization Encoding</title>
      <link>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877871#M165</link>
      <description>&lt;DIV class="duo-migrated-content"&gt;&lt;P&gt;Phew I just noticed you said the secrets in your post are from the authentication examples provided in the Duo docs! FYI I edited your post to remove the secret key before I saw that, because &lt;A href="https://help.duo.com/s/article/application-credentials"&gt;you should never store or transmit your secrets in an insecure system that can be accessed by the public&lt;/A&gt;. This is to protect the integrity and security of your Duo integration (for the folks reading this at home &lt;IMG width="20" height="20" src="https://community.cisco.com/legacyfs/online/ciscoduo/cdn_emojis/twitter/wink.png" style="display : inline;" /&gt; )&lt;/P&gt;
&lt;P&gt;Unfortunately I cannot help much with your API question as this is outside the realm of my personal expertise. I’ll take a look at our docs and support cases though and follow up here if I find anything that is helpful for you!&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 15 Feb 2021 13:12:41 GMT</pubDate>
      <guid>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877871#M165</guid>
      <dc:creator>Amy2</dc:creator>
      <dc:date>2021-02-15T13:12:41Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell API Authorization Encoding</title>
      <link>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877872#M166</link>
      <description>&lt;DIV class="duo-migrated-content"&gt;&lt;P&gt;I have tried a variation of the encryption documented in the duo-psmodule documented on github.&lt;BR /&gt;
&lt;A href="https://github.com/mbegan/Duo-PSModule/blob/master/Duo.psm1" class="inline-onebox" rel="noopener nofollow ugc"&gt;Duo-PSModule/Duo.psm1 at master · mbegan/Duo-PSModule · GitHub&lt;/A&gt;&lt;BR /&gt;
Again all the keys and information are from the Duo Documentation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class="lang-auto"&gt;$intKey = "■■■■■■■■■■■■■■■■■■■■"
$secretkey = "Zh5eGmUq9zpfQnyUIu5OL9iWoMMv5ZNmk3zLJ4Ep"
$hostname = "■■■■■■■■■■■■■■■■■■■■■■■■■■■■"
$path = "/accounts/v1/account/list"
$params = 'realname=First%20Last&amp;amp;username=root'
$method = "POST"
$date='Tue, 21 Aug 2012 17:29:18 -0000'
$lines = @($date,$method,$hostname,$path,$params)
$jlines  = [string]::Join("`n", $lines)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output of $jlines at this point is:&lt;/P&gt;
&lt;P&gt;Tue, 21 Aug 2012 17:29:18 -0000&lt;BR /&gt;
POST&lt;BR /&gt;
&lt;A href="http://api-XXXXXXXX.duosecurity.com" class="onebox" target="_blank" rel="noopener nofollow ugc"&gt;■■■■■■■■■■■■■■■■■■■■■■■■■■■■&lt;/A&gt;&lt;BR /&gt;
/accounts/v1/account/list&lt;BR /&gt;
realname=First%20Last&amp;amp;username=root&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class="lang-auto"&gt;[byte[]]$key_bytes = [System.Text.Encoding]::UTF8.GetBytes($secretkey)
[byte[]]$data_bytes = [System.Text.Encoding]::UTF8.GetBytes($jlines)
$hmacsha1 = New-Object System.Security.Cryptography.HMACSHA1
$hmacsha1.Key = $key_bytes
$hash_bytes = $hmacsha1.ComputeHash($data_bytes)
$hash_hex = [System.BitConverter]::ToString($hmacsha1.Hash)
$return = $hash_hex.Replace("-","").ToLower()
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output of $return at this point is:&lt;BR /&gt;
0f70be53a15bf13672b02cbca291f81bde3de9d4&lt;/P&gt;
&lt;P&gt;Which of course does not meet the expected output of RElXSjhYNkFFWU9SNU9NQzZUUTE6MmQ5N2Q2MTY2MzE5NzgxYjVhM2EwN2FmMzlkMzY2ZjQ5MTIzNGVkYw==&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 15 Feb 2021 18:01:03 GMT</pubDate>
      <guid>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877872#M166</guid>
      <dc:creator>awheeler3</dc:creator>
      <dc:date>2021-02-15T18:01:03Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell API Authorization Encoding</title>
      <link>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877873#M167</link>
      <description>&lt;DIV class="duo-migrated-content"&gt;&lt;P&gt;Its not nice to work with I agree.&lt;/P&gt;
&lt;P&gt;Here is a reusable PowerShell function you can use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;function New-DuoRequest(){
    param(
        [Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
            $apiHost,
        
        [Parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
            [ValidateNotNull()]
            $apiEndpoint,
        
        [Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
            $apiKey,
        
        [Parameter(Mandatory=$true,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
            [ValidateNotNull()]
            $apiSecret,
        
        [Parameter(Mandatory=$false,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
            [ValidateNotNull()]
            $requestMethod = 'GET',
        
        [Parameter(Mandatory=$false,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
            [ValidateNotNull()]
            [System.Collections.Hashtable]$requestParams
    )
    $date = (Get-Date).ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss -0000")
    $formattedParams = ($requestParams.Keys | Sort-Object | ForEach-Object {$_ + "=" + [uri]::EscapeDataString($requestParams.$_)}) -join "&amp;amp;"
    
    #DUO Params formatted and stored as bytes with StringAPIParams
    $requestToSign = (@(
        $Date.Trim(),
        $requestMethod.ToUpper().Trim(),
        $apiHost.ToLower().Trim(),
        $apiEndpoint.Trim(),
        $formattedParams
    ).trim() -join "`n").ToCharArray().ToByte([System.IFormatProvider]$UTF8)
 
    $hmacsha1 = [System.Security.Cryptography.HMACSHA1]::new($apiSecret.ToCharArray().ToByte([System.IFormatProvider]$UTF8))
    $hmacsha1.ComputeHash($requestToSign) | Out-Null
    $authSignature = [System.BitConverter]::ToString($hmacsha1.Hash).Replace("-", "").ToLower()

    $authHeader = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(('{0}:{1}' -f $apiKey, $authSignature)))

    $httpRequest = @{
        URI         = ('https://{0}{1}' -f $apiHost, $apiEndpoint)
        Headers     = @{
            "X-Duo-Date"    = $Date
            "Authorization" = "Basic $authHeader"
        }
        Body = $requestParams
        Method      = $requestMethod
        ContentType = 'application/x-www-form-urlencoded'
    }
    
    $httpRequest
}

# Calling the function

$values = @{
    
    apiHost = 'myapi.duo$ecurity.com'
    apiEndpoint     = '/admin/v1/users'
    requestMethod   = 'GET'
    requestParams   = @{username="$env:Username"}
    apiSecret       = '*************'
    apiKey          = '*************'
}
$contructWebRequest = New-DuoRequest @values

# Send the request
$wr = Invoke-WebRequest @contructWebRequest
Write-host "Your User ID is $((($wr.Content | ConvertFrom-Json).response).user_id)"&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 18 Feb 2021 21:46:19 GMT</pubDate>
      <guid>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877873#M167</guid>
      <dc:creator>Michael_Maher</dc:creator>
      <dc:date>2021-02-18T21:46:19Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell API Authorization Encoding</title>
      <link>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877874#M168</link>
      <description>&lt;DIV class="duo-migrated-content"&gt;&lt;P&gt;When I use the values from the example&lt;/P&gt;
&lt;P&gt;$values = @{&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■'
apiEndpoint     = '/accounts/v1/account/list'
requestMethod   = 'POST'
requestParams   = @{realname="First%20Last";username="root"}
apiSecret       = 'Zh5eGmUq9zpfQnyUIu5OL9iWoMMv5ZNmk3zLJ4Ep'
apiKey          = '■■■■■■■■■■■■■■■■■■■■'
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;And I force $date to “Tue, 21 Aug 2012 17:29:18 -0000” in the function, I get a value of RElXSjhYNkFFWU9SNU9NQzZUUTE6ZmVmNWZjOTU0MjdhMjhlMDk5ODQyNDYzZWMwOGRmYmE5ZjFlYTJkNA== which does not appear to agree with the Examples value of RElXSjhYNkFFWU9SNU9NQzZUUTE6MmQ5N2Q2MTY2MzE5NzgxYjVhM2EwN2FmMzlkMzY2ZjQ5MTIzNGVkYw==&lt;/P&gt;
&lt;P&gt;But it is a lot closer.&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 19 Feb 2021 14:50:02 GMT</pubDate>
      <guid>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877874#M168</guid>
      <dc:creator>awheeler3</dc:creator>
      <dc:date>2021-02-19T14:50:02Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell API Authorization Encoding</title>
      <link>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877875#M169</link>
      <description>&lt;DIV class="duo-migrated-content"&gt;&lt;P&gt;I was able to get a successful  /auth/v2/check using that function.&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 19 Feb 2021 22:21:37 GMT</pubDate>
      <guid>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877875#M169</guid>
      <dc:creator>awheeler3</dc:creator>
      <dc:date>2021-02-19T22:21:37Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell API Authorization Encoding</title>
      <link>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877876#M170</link>
      <description>&lt;DIV class="duo-migrated-content"&gt;&lt;P&gt;Confirmed…  if I put in my own ikey, skey and Host (&lt;A href="http://api-xxxxxx-duosecurity.com" rel="noopener nofollow ugc"&gt;■■■■■■■■■■■■■■■■■■■■■■■■■■&lt;/A&gt;)  the function returns my user ID, or at least I’m assuming it is a user ID; (20 character string uppercase letters and numbers)  not sure where to verify it.&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 31 Mar 2021 20:11:22 GMT</pubDate>
      <guid>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877876#M170</guid>
      <dc:creator>lkeyes1</dc:creator>
      <dc:date>2021-03-31T20:11:22Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell API Authorization Encoding</title>
      <link>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877877#M171</link>
      <description>&lt;DIV class="duo-migrated-content"&gt;&lt;P&gt;Using that function I was able to generate a working Powershell Lambda for AWS Client VPN Endpoint Client Connection Handler to use Duo for MFA.&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 31 Mar 2021 20:35:41 GMT</pubDate>
      <guid>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877877#M171</guid>
      <dc:creator>awheeler3</dc:creator>
      <dc:date>2021-03-31T20:35:41Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell API Authorization Encoding</title>
      <link>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877878#M172</link>
      <description>&lt;DIV class="duo-migrated-content"&gt;&lt;P&gt;Hi Michael, I just wanted to thank you for your post. The script you provided was incredibly helpful in resolving an issue with my company’s environment.&lt;/P&gt;
&lt;P&gt;In case it can prove useful to others, here was our problem. When first setting up a directory sync for Duo, I imported the mail attribute from our Active Directory to the alias1 attribute and I imported UserPrincipalName for our username attribute. At the time, these were different values. However we recently began a project to update our UPNs to use a new domain suffix. In the end this caused the UPN to match the values in our mail attributes. In our testing phase we flipped the domain suffix in our UPNs from the old value to the new value, which led to my username (UPN) and alias1 (mail) value being identical. Duo doesn’t permit this, so two things happened when I synced my account from AD to Duo: 1) Duo failed to update my username to the new UPN value and 2) my Duo account was moved to the trash because my previous username was no longer present in AD.&lt;/P&gt;
&lt;P&gt;By removing alias1 from our directory sync and customizing the script Michael provided to remove the now read-writable alias1 field, I was able to automate the removal of the no longer needed alias1 attribute from all Duo accounts in our environment.&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Thu, 27 May 2021 17:53:45 GMT</pubDate>
      <guid>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877878#M172</guid>
      <dc:creator>david.lowery</dc:creator>
      <dc:date>2021-05-27T17:53:45Z</dc:date>
    </item>
    <item>
      <title>Re: Powershell API Authorization Encoding</title>
      <link>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877879#M173</link>
      <description>&lt;DIV class="duo-migrated-content"&gt;&lt;P&gt;Thanks &lt;A class="mention" href="https://community.duo.com/u/michael_maher"&gt;@Michael_Maher&lt;/A&gt; !&lt;/P&gt;
&lt;P&gt;If anyone else also is using PowerShell with a different Culture/language:&lt;BR /&gt;
Delete the $date variable and add these two lines:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class="lang-auto"&gt;    $culture = [CultureInfo]'en-us'
    $date = (Get-Date).ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss -0000", $culture)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and then add -UseBasicParsing to the WebRequest:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class="lang-auto"&gt;$wr = Invoke-WebRequest @contructWebRequest -UseBasicParsing
&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 18 May 2022 14:45:03 GMT</pubDate>
      <guid>https://community.cisco.com/t5/apis/powershell-api-authorization-encoding/m-p/4877879#M173</guid>
      <dc:creator>Thomas_Powell</dc:creator>
      <dc:date>2022-05-18T14:45:03Z</dc:date>
    </item>
  </channel>
</rss>

