Skip to content

Commit b579ef9

Browse files
committed
cleanup duplicate auth checks
1 parent e3c423d commit b579ef9

File tree

6 files changed

+134
-152
lines changed

6 files changed

+134
-152
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,27 @@ function Get-CIPPAccessRole {
1616
Internal
1717
#>
1818
[CmdletBinding()]
19-
Param()
19+
param($Request)
2020

21-
21+
$CacheAccessUserRoleTable = Get-CIPPTable -tablename 'cacheAccessUserRole'
22+
$CachedRoles = Get-CIPPAzDataTableEntity @CacheAccessUserRoleTable -Filter "PartitionKey eq 'AccessUser' and RowKey eq '$($Request.Headers.'x-ms-client-principal-name')'" | Select-Object -ExpandProperty Role | ConvertFrom-Json
23+
24+
$SwaCreds = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($request.headers.'x-ms-client-principal')) | ConvertFrom-Json)
25+
$SwaRoles = $SwaCreds.userRoles
26+
27+
# Combine SWA roles and cached roles into a single deduplicated list
28+
$AllRoles = [System.Collections.Generic.List[string]]::new()
29+
if ($null -ne $SwaRoles) {
30+
$AllRoles.AddRange($SwaRoles)
31+
}
32+
if ($null -ne $CachedRoles) {
33+
$AllRoles.AddRange($CachedRoles)
34+
}
35+
36+
# Remove duplicates and ensure we have a clean array
37+
$CombinedRoles = $AllRoles | Select-Object -Unique
38+
39+
# For debugging
40+
Write-Information "Combined Roles: $($CombinedRoles -join ', ')"
41+
return $CombinedRoles
2242
}
Lines changed: 52 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
Function Invoke-ExecOffloadFunctions {
2+
function Invoke-ExecOffloadFunctions {
33
<#
44
.FUNCTIONALITY
55
Entrypoint
@@ -9,78 +9,68 @@ Function Invoke-ExecOffloadFunctions {
99
[CmdletBinding()]
1010
param($Request, $TriggerMetadata)
1111

12-
$roles = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($request.headers.'x-ms-client-principal')) | ConvertFrom-Json).userRoles
13-
if ('superadmin' -notin $roles) {
14-
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
15-
StatusCode = [HttpStatusCode]::Forbidden
16-
Body = @{ error = 'You do not have permission to perform this action.' }
17-
})
18-
return
19-
} else {
20-
$Table = Get-CippTable -tablename 'Config'
12+
$Table = Get-CippTable -tablename 'Config'
2113

22-
if ($Request.Query.Action -eq 'ListCurrent') {
23-
$CurrentState = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'OffloadFunctions' and RowKey eq 'OffloadFunctions'"
24-
$VersionTable = Get-CippTable -tablename 'Version'
25-
$Version = Get-CIPPAzDataTableEntity @VersionTable -Filter "RowKey ne 'Version'"
26-
$MainVersion = $Version | Where-Object { $_.RowKey -eq $env:WEBSITE_SITE_NAME }
27-
$OffloadVersions = $Version | Where-Object { $_.RowKey -match '-' }
14+
if ($Request.Query.Action -eq 'ListCurrent') {
15+
$CurrentState = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'OffloadFunctions' and RowKey eq 'OffloadFunctions'"
16+
$VersionTable = Get-CippTable -tablename 'Version'
17+
$Version = Get-CIPPAzDataTableEntity @VersionTable -Filter "RowKey ne 'Version'"
18+
$MainVersion = $Version | Where-Object { $_.RowKey -eq $env:WEBSITE_SITE_NAME }
19+
$OffloadVersions = $Version | Where-Object { $_.RowKey -match '-' }
2820

29-
$Alerts = [System.Collections.Generic.List[string]]::new()
21+
$Alerts = [System.Collections.Generic.List[string]]::new()
3022

31-
$CanEnable = $false
32-
if (!$OffloadVersions.Version) {
33-
$Alerts.Add('No offloaded function apps have been registered. If you''ve just deployed one, this can take up to 15 minutes.')
34-
} else {
35-
$CanEnable = $true
36-
}
23+
$CanEnable = $false
24+
if (!$OffloadVersions.Version) {
25+
$Alerts.Add('No offloaded function apps have been registered. If you''ve just deployed one, this can take up to 15 minutes.')
26+
} else {
27+
$CanEnable = $true
28+
}
3729

38-
foreach ($Offload in $OffloadVersions) {
39-
$FunctionName = $Offload.RowKey
40-
if ([semver]$Offload.Version -ne [semver]$MainVersion.Version) {
41-
$CanEnable = $false
42-
$Alerts.Add("The version of $FunctionName ($($Offload.Version)) does not match the current version of $($MainVersion.Version).")
43-
}
30+
foreach ($Offload in $OffloadVersions) {
31+
$FunctionName = $Offload.RowKey
32+
if ([semver]$Offload.Version -ne [semver]$MainVersion.Version) {
33+
$CanEnable = $false
34+
$Alerts.Add("The version of $FunctionName ($($Offload.Version)) does not match the current version of $($MainVersion.Version).")
4435
}
36+
}
4537

46-
$VersionTable = $Version | Select-Object @{n = 'Name'; e = { $_.RowKey } }, @{n = 'Version'; e = { $_.Version } }, @{n = 'Default'; e = { $_.RowKey -notmatch '-' } }
38+
$VersionTable = $Version | Select-Object @{n = 'Name'; e = { $_.RowKey } }, @{n = 'Version'; e = { $_.Version } }, @{n = 'Default'; e = { $_.RowKey -notmatch '-' } }
4739

48-
$CurrentState = if (!$CurrentState) {
49-
[PSCustomObject]@{
50-
OffloadFunctions = $false
51-
Version = @($VersionTable)
52-
Alerts = $Alerts
53-
CanEnable = $CanEnable
54-
}
55-
} else {
56-
[PSCustomObject]@{
57-
OffloadFunctions = $CurrentState.state
58-
Version = @($VersionTable)
59-
Alerts = $Alerts
60-
CanEnable = $CanEnable
61-
}
40+
$CurrentState = if (!$CurrentState) {
41+
[PSCustomObject]@{
42+
OffloadFunctions = $false
43+
Version = @($VersionTable)
44+
Alerts = $Alerts
45+
CanEnable = $CanEnable
6246
}
63-
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
64-
StatusCode = [HttpStatusCode]::OK
65-
Body = $CurrentState
66-
})
6747
} else {
68-
Add-CIPPAzDataTableEntity @Table -Entity @{
69-
PartitionKey = 'OffloadFunctions'
70-
RowKey = 'OffloadFunctions'
71-
state = $request.Body.OffloadFunctions
72-
} -Force
73-
74-
if ($Request.Body.OffloadFunctions) {
75-
$Results = 'Enabled Offload Functions'
76-
} else {
77-
$Results = 'Disabled Offload Functions'
48+
[PSCustomObject]@{
49+
OffloadFunctions = $CurrentState.state
50+
Version = @($VersionTable)
51+
Alerts = $Alerts
52+
CanEnable = $CanEnable
7853
}
79-
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
80-
StatusCode = [HttpStatusCode]::OK
81-
Body = @{ results = $Results }
82-
})
8354
}
55+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
56+
StatusCode = [HttpStatusCode]::OK
57+
Body = $CurrentState
58+
})
59+
} else {
60+
Add-CIPPAzDataTableEntity @Table -Entity @{
61+
PartitionKey = 'OffloadFunctions'
62+
RowKey = 'OffloadFunctions'
63+
state = $request.Body.OffloadFunctions
64+
} -Force
8465

66+
if ($Request.Body.OffloadFunctions) {
67+
$Results = 'Enabled Offload Functions'
68+
} else {
69+
$Results = 'Disabled Offload Functions'
70+
}
71+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
72+
StatusCode = [HttpStatusCode]::OK
73+
Body = @{ results = $Results }
74+
})
8575
}
8676
}
Lines changed: 53 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using namespace System.Net
22

3-
Function Invoke-ExecPartnerMode {
3+
function Invoke-ExecPartnerMode {
44
<#
55
.FUNCTIONALITY
66
Entrypoint
@@ -10,74 +10,67 @@ Function Invoke-ExecPartnerMode {
1010
[CmdletBinding()]
1111
param($Request, $TriggerMetadata)
1212

13-
$roles = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($request.headers.'x-ms-client-principal')) | ConvertFrom-Json).userRoles
14-
if ('superadmin' -notin $roles) {
15-
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
16-
StatusCode = [HttpStatusCode]::Forbidden
17-
Body = @{ error = 'You do not have permission to perform this action.' }
18-
})
19-
return
20-
} else {
21-
$Table = Get-CippTable -tablename 'tenantMode'
22-
if ($request.body.TenantMode) {
23-
Add-CIPPAzDataTableEntity @Table -Entity @{
24-
PartitionKey = 'Setting'
25-
RowKey = 'PartnerModeSetting'
26-
state = $request.body.TenantMode
27-
} -Force
2813

29-
if ($Request.Body.TenantMode -eq 'default') {
30-
$Table = Get-CippTable -tablename 'Tenants'
31-
$Tenant = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'Tenants' and RowKey eq '$($env:TenantID)'" -Property RowKey, PartitionKey, customerId, displayName
32-
if ($Tenant) {
33-
try {
34-
Remove-AzDataTableEntity -Force @Table -Entity $Tenant
35-
} catch {
36-
}
14+
$Table = Get-CippTable -tablename 'tenantMode'
15+
if ($request.body.TenantMode) {
16+
Add-CIPPAzDataTableEntity @Table -Entity @{
17+
PartitionKey = 'Setting'
18+
RowKey = 'PartnerModeSetting'
19+
state = $request.body.TenantMode
20+
} -Force
21+
22+
if ($Request.Body.TenantMode -eq 'default') {
23+
$Table = Get-CippTable -tablename 'Tenants'
24+
$Tenant = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'Tenants' and RowKey eq '$($env:TenantID)'" -Property RowKey, PartitionKey, customerId, displayName
25+
if ($Tenant) {
26+
try {
27+
Remove-AzDataTableEntity -Force @Table -Entity $Tenant
28+
} catch {
3729
}
38-
} elseif ($Request.Body.TenantMode -eq 'PartnerTenantAvailable') {
39-
$InputObject = [PSCustomObject]@{
40-
Batch = @(
30+
}
31+
} elseif ($Request.Body.TenantMode -eq 'PartnerTenantAvailable') {
32+
$InputObject = [PSCustomObject]@{
33+
Batch = @(
34+
@{
35+
FunctionName = 'UpdateTenants'
36+
}
37+
)
38+
OrchestratorName = 'UpdateTenants'
39+
SkipLog = $true
40+
}
41+
Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($InputObject | ConvertTo-Json -Compress -Depth 5)
42+
}
43+
44+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
45+
StatusCode = [HttpStatusCode]::OK
46+
Body = @{
47+
results = @(
4148
@{
42-
FunctionName = 'UpdateTenants'
49+
resultText = "Set Tenant mode to $($Request.body.TenantMode)"
50+
state = 'success'
4351
}
4452
)
45-
OrchestratorName = 'UpdateTenants'
46-
SkipLog = $true
4753
}
48-
Start-NewOrchestration -FunctionName 'CIPPOrchestrator' -InputObject ($InputObject | ConvertTo-Json -Compress -Depth 5)
49-
}
50-
51-
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
52-
StatusCode = [HttpStatusCode]::OK
53-
Body = @{
54-
results = @(
55-
@{
56-
resultText = "Set Tenant mode to $($Request.body.TenantMode)"
57-
state = 'success'
58-
}
59-
)
60-
}
61-
})
54+
})
6255

63-
}
56+
}
6457

65-
if ($request.query.action -eq 'ListCurrent') {
66-
$CurrentState = Get-CIPPAzDataTableEntity @Table
67-
$CurrentState = if (!$CurrentState) {
68-
[PSCustomObject]@{
69-
TenantMode = 'default'
70-
}
71-
} else {
72-
[PSCustomObject]@{
73-
TenantMode = $CurrentState.state
74-
}
58+
if ($request.query.action -eq 'ListCurrent') {
59+
$CurrentState = Get-CIPPAzDataTableEntity @Table
60+
$CurrentState = if (!$CurrentState) {
61+
[PSCustomObject]@{
62+
TenantMode = 'default'
63+
}
64+
} else {
65+
[PSCustomObject]@{
66+
TenantMode = $CurrentState.state
7567
}
76-
77-
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
78-
StatusCode = [HttpStatusCode]::OK
79-
Body = $CurrentState
80-
})
8168
}
69+
70+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
71+
StatusCode = [HttpStatusCode]::OK
72+
Body = $CurrentState
73+
})
8274
}
75+
8376
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Setup/Invoke-ExecDeviceCodeLogon.ps1

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using namespace System.Net
22

3-
Function Invoke-ExecDeviceCodeLogon {
3+
function Invoke-ExecDeviceCodeLogon {
44
<#
55
.FUNCTIONALITY
66
Entrypoint,AnyTenant
@@ -10,19 +10,6 @@ Function Invoke-ExecDeviceCodeLogon {
1010
[CmdletBinding()]
1111
param($Request, $TriggerMetadata)
1212

13-
$UserCreds = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($request.headers.'x-ms-client-principal')) | ConvertFrom-Json)
14-
if ('admin' -notin $UserCreds.userRoles) {
15-
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
16-
ContentType = 'application/json'
17-
StatusCode = [HttpStatusCode]::Forbidden
18-
Body = @{
19-
error = 'Forbidden'
20-
errorMessage = 'You do not have permission to perform this action'
21-
} | ConvertTo-Json
22-
})
23-
exit
24-
}
25-
2613
$APIName = $Request.Params.CIPPEndpoint
2714
$Headers = $Request.Headers
2815
Write-LogMessage -headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug'

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Setup/Invoke-ExecSAMSetup.ps1

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using namespace System.Net
22

3-
Function Invoke-ExecSAMSetup {
3+
function Invoke-ExecSAMSetup {
44
<#
55
.FUNCTIONALITY
66
Entrypoint,AnyTenant
@@ -13,7 +13,7 @@ Function Invoke-ExecSAMSetup {
1313
[CmdletBinding()]
1414
param($Request, $TriggerMetadata)
1515

16-
$UserCreds = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($request.headers.'x-ms-client-principal')) | ConvertFrom-Json)
16+
1717
if ($Request.Query.error) {
1818
Add-Type -AssemblyName System.Web
1919
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
@@ -23,14 +23,6 @@ Function Invoke-ExecSAMSetup {
2323
})
2424
exit
2525
}
26-
if ('admin' -notin $UserCreds.userRoles) {
27-
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
28-
ContentType = 'text/html'
29-
StatusCode = [HttpStatusCode]::Forbidden
30-
Body = 'Could not find an admin cookie in your browser, please confirm that you have the admin role in CIPP. Make sure you do not have an adblocker active, use a Chromium browser, and allow cookies. If our automatic refresh does not work, try pressing the URL bar and hitting enter. We will try to refresh ourselves in 3 seconds.<meta http-equiv="refresh" content="3" />'
31-
})
32-
exit
33-
}
3426

3527
$APIName = $Request.Params.CIPPEndpoint
3628
$Headers = $Request.Headers

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Standards/Invoke-ListDomainHealth.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using namespace System.Net
22

3-
Function Invoke-ListDomainHealth {
3+
function Invoke-ListDomainHealth {
44
<#
55
.FUNCTIONALITY
66
Entrypoint,AnyTenant
@@ -38,8 +38,8 @@ Function Invoke-ListDomainHealth {
3838
}
3939

4040
Set-DnsResolver -Resolver $Resolver
41-
#UNDOREPLACE
42-
$UserCreds = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($request.headers.'x-ms-client-principal')) | ConvertFrom-Json)
41+
42+
$UserRoles = Get-CIPPAccessPermissions -Request $Request
4343

4444
$APIName = $Request.Params.CIPPEndpoint
4545
$Headers = $Request.Headers
@@ -87,7 +87,7 @@ Function Invoke-ListDomainHealth {
8787
if ($Request.Query.Selector) {
8888
$DkimQuery.Selectors = ($Request.Query.Selector).trim() -split '\s*,\s*'
8989

90-
if ('admin' -in $UserCreds.userRoles -or 'editor' -in $UserCreds.userRoles) {
90+
if ('admin' -in $UserRoles -or 'editor' -in $UserRoles) {
9191
$DkimSelectors = [string]($DkimQuery.Selectors | ConvertTo-Json -Compress)
9292
if ($DomainInfo) {
9393
$DomainInfo.DkimSelectors = $DkimSelectors

0 commit comments

Comments
 (0)