Skip to content

Commit

Permalink
allow disabling api authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnDuprey committed Feb 7, 2025
1 parent e93b097 commit 653b356
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions Modules/CIPPCore/Public/Authentication/Set-CippApiAuth.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,33 @@ function Set-CippApiAuth {
"api://$ClientId"
}

if (!$AllowedAudiences) { $AllowedAudiences = @() }
if (!$ClientIds) { $ClientIds = @() }

# Set auth settings
$AuthSettings.properties.identityProviders.azureActiveDirectory = @{
registration = @{
clientId = $ClientIds[0] ?? $ClientIds
openIdIssuer = "https://sts.windows.net/$TenantID/v2.0"
}
validation = @{
allowedAudiences = @($AllowedAudiences)
defaultAuthorizationPolicy = @{
allowedApplications = @($ClientIds)

if (($ClientIds | Measure-Object).Count -gt 0) {
$AuthSettings.properties.identityProviders.azureActiveDirectory = @{
enabled = $true
registration = @{
clientId = $ClientIds[0] ?? $ClientIds
openIdIssuer = "https://sts.windows.net/$TenantID/v2.0"
}
validation = @{
allowedAudiences = @($AllowedAudiences)
defaultAuthorizationPolicy = @{
allowedApplications = @($ClientIds)
}
}
}
} else {
$AuthSettings.properties.identityProviders.azureActiveDirectory = @{
enabled = $false
registration = @{}
validation = @{}
}
}

$AuthSettings.properties.globalValidation = @{
unauthenticatedClientAction = 'Return401'
}
Expand All @@ -47,14 +61,12 @@ function Set-CippApiAuth {
}
}

Write-Information ($AuthSettings | ConvertTo-Json -Depth 10)

if ($PSCmdlet.ShouldProcess('Update auth settings')) {
# Update auth settings
Invoke-AzRestMethod -Uri "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$RGName/providers/Microsoft.Web/sites/$($FunctionAppName)/config/authsettingsV2?api-version=2020-06-01" -Method PUT -Payload ($AuthSettings | ConvertTo-Json -Depth 10)
$null = Invoke-AzRestMethod -Uri "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$RGName/providers/Microsoft.Web/sites/$($FunctionAppName)/config/authsettingsV2?api-version=2020-06-01" -Method PUT -Payload ($AuthSettings | ConvertTo-Json -Depth 10)
}

if ($PSCmdlet.ShouldProcess('Update allowed tenants')) {
Update-AzFunctionAppSetting -Name $FunctionAppName -ResourceGroupName $RGName -AppSetting @{ 'WEBSITE_AUTH_AAD_ALLOWED_TENANTS' = $TenantId }
$null = Update-AzFunctionAppSetting -Name $FunctionAppName -ResourceGroupName $RGName -AppSetting @{ 'WEBSITE_AUTH_AAD_ALLOWED_TENANTS' = $TenantId }
}
}

0 comments on commit 653b356

Please sign in to comment.