Skip to content

Commit 1177ffd

Browse files
committed
Add dynamic redirect URI update for application
This update retrieves the application's web redirect URIs and adds a new redirect URI based on the current request if it is not already present. The change ensures the application's redirect URIs are kept up to date automatically during execution.
1 parent a16affb commit 1177ffd

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Modules/CIPPCore/Public/Entrypoints/Invoke-ExecListAppId.ps1

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,45 @@ function Invoke-ExecListAppId {
4545
url = '/me?$select=displayName,userPrincipalName'
4646
method = 'GET'
4747
}
48+
@{
49+
id = 'application'
50+
url = "/applications(appId='$($env:ApplicationID)')?`$select=id,web"
51+
method = 'GET'
52+
}
4853
)
4954

5055
$BulkResponse = New-GraphBulkRequest -Requests $BulkRequests -tenantid $env:TenantID -NoAuthCheck $true
5156
$OrgResponse = $BulkResponse | Where-Object { $_.id -eq 'organization' }
5257
$MeResponse = $BulkResponse | Where-Object { $_.id -eq 'me' }
58+
$AppResponse = $BulkResponse | Where-Object { $_.id -eq 'application' }
5359
if ($MeResponse.body) {
5460
$AuthenticatedUserDisplayName = $MeResponse.body.displayName
5561
$AuthenticatedUserPrincipalName = $MeResponse.body.userPrincipalName
5662
}
5763
if ($OrgResponse.body.value -and $OrgResponse.body.value.Count -gt 0) {
5864
$OrgInfo = $OrgResponse.body.value[0]
5965
}
66+
67+
if ($AppResponse.body) {
68+
$AppWeb = $AppResponse.body.web
69+
if ($AppWeb.redirectUris -and $AppWeb.redirectUris.Count -gt 0) {
70+
# construct new redirect uri with current
71+
$URL = ($Request.headers.'x-ms-original-url').split('/api') | Select-Object -First 1
72+
$NewRedirectUri = "$($URL)/authredirect"
73+
if ($AppWeb.redirectUris -notcontains $NewRedirectUri) {
74+
$RedirectUris = [system.collections.generic.list[string]]::new()
75+
$AppWeb.redirectUris | ForEach-Object { $RedirectUris.Add($_) }
76+
$RedirectUris.Add($NewRedirectUri)
77+
$AppUpdateBody = @{
78+
web = @{
79+
redirectUris = $RedirectUris
80+
}
81+
} | ConvertTo-Json -Depth 10
82+
Invoke-GraphRequest -Method PATCH -Url "https://graph.microsoft.com/v1.0/applications/$($AppResponse.body.id)" -Body $AppUpdateBody -tenantid $env:TenantID -NoAuthCheck $true
83+
Write-LogMessage -message "Updated redirect URIs for application $($env:ApplicationID) to include $NewRedirectUri" -Sev 'Info'
84+
}
85+
}
86+
}
6087
} catch {
6188
Write-LogMessage -message 'Failed to retrieve organization info and authenticated user' -LogData (Get-CippException -Exception $_) -Sev 'Warning'
6289
}

0 commit comments

Comments
 (0)