Skip to content

Commit fe39335

Browse files
1 parent 6fc2c42 commit fe39335

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

Modules/CIPPCore/Public/Standards/Invoke-CIPPStandardOauthConsent.ps1

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,35 @@ function Invoke-CIPPStandardOauthConsent {
3737
$StateIsCorrect = if ($State.permissionGrantPolicyIdsAssignedToDefaultUserRole -eq 'managePermissionGrantsForSelf.cipp-consent-policy') { $true } else { $false }
3838

3939
if ($Settings.remediate -eq $true) {
40-
$AllowedAppIdsForTenant = $settings.AllowedApps -split ','
40+
$AllowedAppIdsForTenant = $settings.AllowedApps -split ',' | ForEach-Object { $_.Trim() }
4141
try {
42-
if ($State.permissionGrantPolicyIdsAssignedToDefaultUserRole -notin @('managePermissionGrantsForSelf.cipp-consent-policy')) {
43-
$Existing = (New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/policies/permissionGrantPolicies/' -tenantid $tenant) | Where-Object -Property id -EQ 'cipp-consent-policy'
44-
if (!$Existing) {
45-
New-GraphPostRequest -tenantid $tenant -Uri 'https://graph.microsoft.com/beta/policies/permissionGrantPolicies' -Type POST -Body '{ "id":"cipp-consent-policy", "displayName":"Application Consent Policy", "description":"This policy controls the current application consent policies."}' -ContentType 'application/json'
46-
#Replaced static web app appid with Office 365 Management by Microsoft's recommendation; this application is always consented, cannot be removed nor elevated as the portals run on this app id.
47-
New-GraphPostRequest -tenantid $tenant -Uri 'https://graph.microsoft.com/beta/policies/permissionGrantPolicies/cipp-consent-policy/includes' -Type POST -Body '{"permissionClassification":"all","permissionType":"delegated","clientApplicationIds":["00b41c95-dab0-4487-9791-b9d2c32c80f2"]}' -ContentType 'application/json'
42+
$Existing = (New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/policies/permissionGrantPolicies/' -tenantid $tenant) | Where-Object -Property id -EQ 'cipp-consent-policy'
43+
if (!$Existing) {
44+
New-GraphPostRequest -tenantid $tenant -Uri 'https://graph.microsoft.com/beta/policies/permissionGrantPolicies' -Type POST -Body '{ "id":"cipp-consent-policy", "displayName":"Application Consent Policy", "description":"This policy controls the current application consent policies."}' -ContentType 'application/json'
45+
# Replaced static web app appid with Office 365 Management by Microsoft's recommendation
46+
New-GraphPostRequest -tenantid $tenant -Uri 'https://graph.microsoft.com/beta/policies/permissionGrantPolicies/cipp-consent-policy/includes' -Type POST -Body '{"permissionClassification":"all","permissionType":"delegated","clientApplicationIds":["00b41c95-dab0-4487-9791-b9d2c32c80f2"]}' -ContentType 'application/json'
47+
}
48+
49+
try {
50+
$ExistingIncludes = New-GraphGetRequest -tenantid $tenant -Uri 'https://graph.microsoft.com/beta/policies/permissionGrantPolicies/cipp-consent-policy/includes'
51+
52+
$ExistingAppIds = foreach ($entry in $ExistingIncludes.value) {
53+
$entry.clientApplicationIds
4854
}
49-
try {
50-
foreach ($AllowedApp in $AllowedAppIdsForTenant) {
51-
Write-Host "$AllowedApp"
55+
$ExistingAppIds = $ExistingAppIds | Sort-Object -Unique
56+
57+
foreach ($AllowedApp in $AllowedAppIdsForTenant) {
58+
if ($AllowedApp -and ($AllowedApp -notin $ExistingAppIds)) {
59+
Write-Host "Adding missing approved app: $AllowedApp"
5260
New-GraphPostRequest -tenantid $tenant -Uri 'https://graph.microsoft.com/beta/policies/permissionGrantPolicies/cipp-consent-policy/includes' -Type POST -Body ('{"permissionType": "delegated","clientApplicationIds": ["' + $AllowedApp + '"]}') -ContentType 'application/json'
5361
New-GraphPostRequest -tenantid $tenant -Uri 'https://graph.microsoft.com/beta/policies/permissionGrantPolicies/cipp-consent-policy/includes' -Type POST -Body ('{ "permissionType": "Application", "clientApplicationIds": ["' + $AllowedApp + '"] }') -ContentType 'application/json'
5462
}
55-
} catch {
56-
"Could not add exclusions, probably already exist: $($_)"
5763
}
64+
} catch {
65+
"Could not add exclusions, probably already exist: $($_)"
66+
}
67+
68+
if ($State.permissionGrantPolicyIdsAssignedToDefaultUserRole -notin @('managePermissionGrantsForSelf.cipp-consent-policy')) {
5869
New-GraphPostRequest -tenantid $tenant -Uri 'https://graph.microsoft.com/beta/policies/authorizationPolicy/authorizationPolicy' -Type PATCH -Body '{"permissionGrantPolicyIdsAssignedToDefaultUserRole":["managePermissionGrantsForSelf.cipp-consent-policy"]}' -ContentType 'application/json'
5970
}
6071

@@ -64,15 +75,16 @@ function Invoke-CIPPStandardOauthConsent {
6475
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to apply Application Consent Mode Error: $ErrorMessage" -sev Error
6576
}
6677
}
67-
if ($Settings.alert -eq $true) {
6878

79+
if ($Settings.alert -eq $true) {
6980
if ($StateIsCorrect -eq $true) {
7081
Write-LogMessage -API 'Standards' -tenant $tenant -message 'Application Consent Mode is enabled.' -sev Info
7182
} else {
7283
Write-StandardsAlert -message 'Application Consent Mode is not enabled.' -object ($State.defaultUserRolePermissions) -tenant $tenant -standardName 'OauthConsent' -standardId $Settings.standardId
7384
Write-LogMessage -API 'Standards' -tenant $tenant -message 'Application Consent Mode is not enabled.' -sev Info
7485
}
7586
}
87+
7688
if ($Settings.report -eq $true) {
7789
Add-CIPPBPAField -FieldName 'OauthConsent' -FieldValue $StateIsCorrect -StoreAs bool -Tenant $tenant
7890
if ($StateIsCorrect) {

0 commit comments

Comments
 (0)