Skip to content

Commit 3f6104d

Browse files
committed
Use WEBSITE_AUTH_V2_CONFIG_JSON for auth settings if available
The function now checks for the WEBSITE_AUTH_V2_CONFIG_JSON environment variable and uses it to obtain authentication settings if present, reducing the need for an additional REST call. Falls back to the previous method if the variable is not set.
1 parent 2394d85 commit 3f6104d

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

Modules/CIPPCore/Public/Authentication/Get-CippApiAuth.ps1

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@ function Get-CippApiAuth {
44
[string]$FunctionAppName
55
)
66

7-
if ($env:MSI_SECRET) {
8-
Disable-AzContextAutosave -Scope Process | Out-Null
9-
$null = Connect-AzAccount -Identity
10-
$SubscriptionId = $env:WEBSITE_OWNER_NAME -split '\+' | Select-Object -First 1
11-
$Context = Set-AzContext -SubscriptionId $SubscriptionId
12-
} else {
13-
$Context = Get-AzContext
14-
$SubscriptionId = $Context.Subscription.Id
7+
if ($env:WEBSITE_AUTH_V2_CONFIG_JSON) {
8+
$AuthSettings = $env:WEBSITE_AUTH_V2_CONFIG_JSON | ConvertFrom-Json -ErrorAction SilentlyContinue
159
}
1610

17-
# Get auth settings
18-
$AuthSettings = Invoke-AzRestMethod -Uri "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$RGName/providers/Microsoft.Web/sites/$($FunctionAppName)/config/authsettingsV2/list?api-version=2020-06-01" -ErrorAction Stop | Select-Object -ExpandProperty Content | ConvertFrom-Json
11+
if (-not $AuthSettings) {
12+
if ($env:MSI_SECRET) {
13+
Disable-AzContextAutosave -Scope Process | Out-Null
14+
$null = Connect-AzAccount -Identity
15+
$SubscriptionId = $env:WEBSITE_OWNER_NAME -split '\+' | Select-Object -First 1
16+
$Context = Set-AzContext -SubscriptionId $SubscriptionId
17+
} else {
18+
$Context = Get-AzContext
19+
$SubscriptionId = $Context.Subscription.Id
20+
}
21+
22+
# Get auth settings
23+
$AuthSettings = Invoke-AzRestMethod -Uri "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$RGName/providers/Microsoft.Web/sites/$($FunctionAppName)/config/authsettingsV2/list?api-version=2020-06-01" -ErrorAction Stop | Select-Object -ExpandProperty Content | ConvertFrom-Json
24+
}
1925

2026
if ($AuthSettings.properties) {
2127
[PSCustomObject]@{

0 commit comments

Comments
 (0)