Knowledge Base

Support Policies

Generate IdP API Authorization Header via Powershel)

Use the following cmdlet to generate the Authentication header to invoke the IdP API.

 

function Convert-HexToByte
{
param
(
[string]$HexString
)
$numberChars=$HexString.Length;

[byte[]]$keyBytes=New-Object byte[]32
for([int]$i=0;$i-lt$numberChars;$i=$i+2)
{
$keyBytes[$i/2]=[Convert]::ToByte($HexString.Substring($i,2),16);
}
return$keyBytes;
}



function New-SecureAuthAuthorizationHeader
{
param
(
[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][string]$AppId,
[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][string]$AppKey,
[Parameter(Mandatory=$true)][Uri]$ResourceUri,
[Parameter(Mandatory=$true)][System.DateTimeOffset]$RequestDate,
[Parameter(Mandatory=$true)][ValidateSet('GET','POST', 'PUT', 'DELETE')]$Method,
[Parameter(Mandatory=$false)]$RequestBody
)

begin
{
if([string]::IsNullOrEmpty($RequestBody))
{
$hasBody=$false;
}
else
{
$hasBody=$true;
}

$appKeyByte=(Convert-HexToByte-HexString $AppKey);

$dateString=$RequestDate.UtcDateTime.ToString('ddd, dd MMM yyyy HH:mm:ss.fff')+" GMT";
}


process
{
$pathstr=$ResourceUri.AbsolutePath;
#Need to figure out a cleaner way of doing this. Joining on a linebreak doesn't seem to work.
if($hasBody)
{
#$message = [String]::Join( [System.Environment]::NewLine, $Method, $dateString, $AppId, $ResourceUri.AbsolutePath, $RequestBody);

$message="$Method
$dateString
$AppId
$pathstr
$RequestBody"
 

}
else
{
 
#$message = [String]::Join( [System.Environment]::NewLine, $Method, $dateString, $AppId, $ResourceUri.AbsolutePath)
 
$message="$Method
$dateString
$AppId
$pathstr"
 

}

$messageByte=[System.Text.Encoding]::UTF8.GetBytes($message);

$hmacProvider=New-Object-TypeName 'System.Security.Cryptography.HMACSHA256'-ArgumentList @(,$appKeyByte);

$hmac=[System.Convert]::ToBase64String($hmacProvider.ComputeHash($messageByte));

$headerValue="Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(("{0}:{1}"-f$AppId,$hmac)));

return$headerValue;
}
}
 
 
$authHeaderParams= @{
"AppId"=$AppId;
"AppKey"=$AppKey;
"Method"="GET";
"ResourceUri"=$ResourceUri;
"RequestDate"=$date;
 
}

$authHeader=New-SecureAuthAuthorizationHeader @authHeaderParams;
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.