API endpoint token auth works in app without pages - but not in pages
Hi there,
I found a strange behavior in apps when calling endpoints in PSU.
Summary
Token authentication works perfectly if the app does not have pages and just -Content {} which includes Invoke-WebRequest.
As soon as you move the working structure out of -Content {} from an app into Pages it doesn't work anymore:
Details
I have a working element asif (-not $UserSuggestions){ $RequestParameter = $RequestParameterDefault $RequestParameter["Method"] = "Get" $RequestParameter["Uri"] = "https://${hostName}:8443/ad/user" $Result = (Invoke-RestMethod @RequestParameter) $UserSuggestions = @() if ($Result.Data) { foreach ($user in $Result.data.GetEnumerator() ) { # using objectGUID as value as UPN can be empty $UserSuggestions += (New-UDAutocompleteOption -Name "$($user.name) `($($user.userPrincipalName)`)" -Value $($user.objectGUID)) } Show-UDToast -Message "User data loaded successfully" -MessageColor "green" -BackgroundColor "lightgreen" } else { Show-UDToast -Message "Failed to load user data" -MessageColor "darkred" -BackgroundColor "lightred" } } else { Show-UDToast -Message "User data already loaded" -MessageColor "darkblue" -BackgroundColor "lightblue" }
basically calling `/ad/user` endpoint on the PSU server.
This works if it is integrated in:New-UDApp -Title "User Management" -Content { ... }
I then moved it into a subpage which I declared viaNew-UDPage -Url "/user-offboarding" -Name "User Offboarding" -Content {
and integrated in the app via$Pages = @()$Pages += Get-UDPage -Name "User Offboarding"New-UDApp -Title "User Management" -Pages $Pages
Actual Behavior
Invoke-WebRequest creates 401 unauthorized error when opening the page via menu.
It works if no pages are created and just a single page app exists.
Expected Behavior
Token authentication should work as well on all subpages.
Is that a bug I found or really expected behavior?
How to resolve this issue?
Recommended Answer
Hi @Adam Driscoll
I could resolve the issue now after tinkering around...
The intermittend issue was caused by initializing $Headers variable, which erased the existing token in the existing headers...
The subpage issue was caused by a shared initialization of the variables for the RequestParameters outside of the page.
Each page needs a new initialization as it seems that only pages are called by the app.
Best,
Matthias
@blindzero Can you share how you are defining $RequestParameterDefault? I suspect it is a scoping issue with the variable. Can you try using cache scope?
$Cache:RequestParameterDefault = @{ Headers = @{ Authoriation = "Bearer xyx" } }
$RequestParameters = $Cache:RequestParametersDefaultAdam Driscoll
PowerShell Expert and Developer at Devolutions
Hi @Adam Driscoll
thanks for your quick response.
I am initializing the RequestParameterDefault just above (outside) of New-UDApp:# Preparing REST Requests for pages$Headers = @{}$Headers["Accept"] = "application/json"$RequestParameterDefault = @{ Headers = $Headers Method = "Get"}New-UDApp -Title "User Management" -Content {
Changing it to $Cache:RequestParameterDefault didn't change anything.
Also moving this whole init block into New-UDApp did not change anything.
Maybe my info or description was also not precise enough. I am not using any distinct / defined token, but expected the existing OIDC token to be used in the App.
So admins are logging in via OIDC. Access rights to the app are the same as to the endpoints.
I also observed that this issue is somewhat intermittent, because when I moved it back out of the pages into the app it is still not working anymore, although it definetly worked before.
Best & Thanks,
Matthias
Hi @Adam Driscoll
I could resolve the issue now after tinkering around...
The intermittend issue was caused by initializing $Headers variable, which erased the existing token in the existing headers...
The subpage issue was caused by a shared initialization of the variables for the RequestParameters outside of the page.
Each page needs a new initialization as it seems that only pages are called by the app.
Best,
Matthias