Skip to content
22 changes: 20 additions & 2 deletions src/Authentication/Authentication/custom/common/GraphUri.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,29 @@ function GraphUri_TokenizeIds {
)

$TokenizedUri = $Uri.GetComponents([System.UriComponents]::SchemeAndServer, [System.UriFormat]::SafeUnescaped)
$LastSegmentIndex = $Uri.Segments.length - 1
$LastSegment = $Uri.Segments[$LastSegmentIndex]
$UnescapedUri = $Uri.ToString()
for ($i = 0 ; $i -lt $Uri.Segments.length; $i++) {
# Segment contains an integer/id and is not API version.
if ($Uri.Segments[$i] -match "[^v1.0|beta]\d") {
# Substitute integers/ids with {id} tokens, e.g, /users/289ee2a5-9450-4837-aa87-6bd8d8e72891 -> users/{id}.
$TokenizedUri += "{id}/"
#For Uris whose last segments match the regex '(.*?)', all characters from the first '(' are substituted with '.*'
if ($i -eq $LastSegmentIndex) {
if ($UnescapedUri -match '(.*?)') {
try {
$UpdatedLastSegment = $LastSegment.Substring(0, $LastSegment.IndexOf("("))
$TokenizedUri += $UpdatedLastSegment + ".*"

}
catch {
$TokenizedUri += "{id}/"
}
}
}
else {
# Substitute integers/ids with {id} tokens, e.g, /users/289ee2a5-9450-4837-aa87-6bd8d8e72891 -> users/{id}.
$TokenizedUri += "{id}/"
}
}
else {
$TokenizedUri += $Uri.Segments[$i]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Describe "Find-MgGraphCommand Command" {
$MgCommand.Command | Should -Be "Get-MgEntitlementManagementAccessPackageAssignmentResourceRole"
} | Should -Not -Throw
}

It 'Should find command using URI with query parameters' {
{
$Uri = "beta/users?`$select=displayName&`$filter=identities/any(c:c/issuerAssignedId eq 'j.smith@yahoo.com')"
Expand Down Expand Up @@ -159,6 +160,20 @@ Describe "Find-MgGraphCommand Command" {
It 'Should throw error when command name is invalid' {
$ExpectedErrorMessage = "*'New-MgInvalid' is not a valid Microsoft Graph PowerShell command.*"
{ Find-MgGraphCommand -Command "New-MgInvalid" -ErrorAction Stop | Out-Null } | Should -Throw -ExpectedMessage $ExpectedErrorMessage }

It 'Should find command using actual id in key segments inside parenthesis' {
{
$ExpectedResourceUri = @("/reports/getSharePointActivityUserCounts(period='{period}')", "/reports/getSharePointActivityUserCounts(period='{period}')")
$Uri = "/reports/getSharePointActivityUserCounts(period='D3')"
$MgCommand = Find-MgGraphCommand -Uri $Uri
$MgCommand | Should -HaveCount 2
$MgCommand.Method | Should -Be @("GET", "GET")
$MgCommand.APIVersion | Should -BeIn @("v1.0", "beta")
$MgCommand.Variants | Should -Contain "Get"
$MgCommand.URI | Should -Be $ExpectedResourceUri
$MgCommand.Command | Should -Be @("Get-MgReportSharePointActivityUserCount", "Get-MgReportSharePointActivityUserCount")
} | Should -Not -Throw
}
}
}
}