Last active
November 28, 2017 14:58
-
-
Save chelnak/124d921ea5ad7e66bf7ca4ed0e57fd13 to your computer and use it in GitHub Desktop.
Generate a CosmosDB REST API Auth token with PowerShell See here for more information https://docs.microsoft.com/en-us/rest/api/documentdb/access-control-on-documentdb-resources
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
function New-CosmosDBRESTAuthToken { | |
[CmdletBinding()] | |
Param( | |
[Parameter()] | |
[String]$Verb, | |
[Parameter()] | |
[String]$ResourceType, | |
[Parameter()] | |
[String]$ResourceId, | |
[Parameter()] | |
[String]$Date = [System.DateTime]::UtcNow.ToString("R"), | |
[Parameter()] | |
[String]$Key, | |
[Parameter()] | |
[String]$KeyType, | |
[Parameter()] | |
[String]$TokenVersion | |
) | |
$HMACSHA = [System.Security.Cryptography.HMACSHA256]::new() | |
$HMACSHA.Key = [Convert]::FromBase64String($Key) #[Text.Encoding]::ASCII.GetBytes($secret) | |
$Payload = [String]::Format([System.Globalization.CultureInfo]::InstalledUICulture, "{0}\n{1}\n{2}\n{3}\n{4}\n", | |
` $verb.ToLowerInvariant(), | |
` $resourceType.ToLowerInvariant(), | |
` $resourceId, | |
` $date.ToLowerInvariant(),"" | |
) | |
[Byte[]]$HashPayLoad = $HMACSHA.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($Payload)) | |
$Signature = [Convert]::ToBase64String($hashPayLoad) | |
[System.Web.HttpUtility]::UrlEncode([String]::Format([System.Globalization.CultureInfo]::InstalledUICulture,"type={0}&ver={1}&sig={2}", | |
` $KeyType, | |
` $TokenVersion, | |
` $Signature | |
)) | |
} | |
# --- Load the function and use like this | |
$Key = "dsZQi3KtZmCv1ljt3VNWNm7sQUF1y5rJfC6kv5JiwvW0EndXdDku/dkKBp8/ufDToSxLzR4y+O/0H/t4bQtVNw==" | |
New-CosmosDBRESTAuthToken -Verb POST -ResourceType "dbs" -ResourceId "dbs/ToDoList" -KeyType "master" -TokenVersion "1.0" -Key $Key |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment