Fix: handle 302 redirects to redirect_uri in WebView2 authorization flow - #15
Open
yfridelance wants to merge 8 commits into
Open
Fix: handle 302 redirects to redirect_uri in WebView2 authorization flow#15yfridelance wants to merge 8 commits into
yfridelance wants to merge 8 commits into
Conversation
Also add to New-Oauth2JwtAssertion so we can pass a privatekey as string Primarily to make Googel Service Account keys easier
Add JWT Bearer grant support to Invoke-OAuth2TokenEndpoint
The SourceChanged event is not triggered when the redirect_uri is reached through a 302 redirect, so the WebView2 form never closed and the authorization code could not be captured. Add a NavigationStarting event handler that matches the close-condition regex, cancels navigation to the redirect_uri, stores the URL in Form.Tag and closes the form. The captured URL is then used as the response when present. Also escape and anchor the redirect_uri in UrlCloseConditionRegex with [regex]::Escape() to avoid regex metacharacters in the URI breaking the match, and apply consistent code formatting.
Owner
|
Thanks for this, @yfridelance. Nice catch. I reworked it a bit before landing it on
One heads-up, the dev branch this targets had gone stale, and I'm still finding my feet with branching, so I did my best to keep the fix clean and credit you as co-author, so apologies if anything came across differently than intended. Before I merge and cut 1.4.1, could you confirm that fix/auth-window-hang resolves the hang on your setup? I've attached a build of the module from that branch if that is more convenient for you. I'll leave this open until you've had a chance to test it. No rush. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When the
redirect_uriis reached through an HTTP 302 redirect, the WebView2SourceChangedevent does not fire. As a result, the authorization windownever closes and the authorization
code(or error) cannot be captured,leaving the flow hanging.
Changes
Invoke-WebView2.ps1: Add aNavigationStartingevent handler thatmatches the close-condition regex. When the
redirect_uriis hit, it cancelsthe navigation, stores the URL in
Form.Tagand closes the form. The capturedURL is then used as the response when present (falling back to
$web.Sourceotherwise).
Invoke-OAuth2AuthorizationEndpoint.ps1: Escape and anchor theredirect_uriinUrlCloseConditionRegexusing[regex]::Escape(), so thatregex metacharacters in the URI (e.g.
?,.) no longer break the match.Testing
Tested authorization code flow against private IdP with a redirect_uri
that returns a 302; window now closes and the code is captured correctly.