-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove non-functioning code and instead throw a more helpful platform-not-supported error #23617
Merged
mattleibow
merged 4 commits into
dotnet:main
from
dotMorten:dotMorten/win_auth_notsupported
Aug 22, 2024
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d29f0de
Remove non-functioning code and instead throw a more helpful platform…
dotMorten 4d027b1
Fix build failures
dotMorten b358ef6
Merge remote-tracking branch 'origin/main' into dotMorten/win_auth_no…
dotMorten 131fc3c
Update tests for new webauth behavior
dotMorten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
55 changes: 2 additions & 53 deletions
55
src/Essentials/src/WebAuthenticator/WebAuthenticator.uwp.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,13 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using System.Xml; | ||
using System.Xml.Linq; | ||
using System.Xml.XPath; | ||
using Microsoft.Maui.ApplicationModel; | ||
using Microsoft.Maui.Storage; | ||
using Windows.Security.Authentication.Web; | ||
|
||
namespace Microsoft.Maui.Authentication | ||
{ | ||
partial class WebAuthenticatorImplementation : IWebAuthenticator | ||
{ | ||
public async Task<WebAuthenticatorResult> AuthenticateAsync(WebAuthenticatorOptions webAuthenticatorOptions) | ||
public Task<WebAuthenticatorResult> AuthenticateAsync(WebAuthenticatorOptions webAuthenticatorOptions) | ||
{ | ||
var url = webAuthenticatorOptions?.Url; | ||
var callbackUrl = webAuthenticatorOptions?.CallbackUrl; | ||
|
||
if (!IsUriProtocolDeclared(callbackUrl.Scheme)) | ||
throw new InvalidOperationException($"You need to declare the windows.protocol usage of the protocol/scheme `{callbackUrl.Scheme}` in your AppxManifest.xml file"); | ||
|
||
try | ||
{ | ||
var r = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, url, callbackUrl); | ||
|
||
switch (r.ResponseStatus) | ||
{ | ||
case WebAuthenticationStatus.Success: | ||
// For GET requests this is a URI: | ||
var resultUri = new Uri(r.ResponseData.ToString()); | ||
return new WebAuthenticatorResult(resultUri, webAuthenticatorOptions?.ResponseDecoder); | ||
case WebAuthenticationStatus.UserCancel: | ||
throw new TaskCanceledException(); | ||
case WebAuthenticationStatus.ErrorHttp: | ||
throw new HttpRequestException("Error: " + r.ResponseErrorDetail); | ||
default: | ||
throw new Exception("Response: " + r.ResponseData.ToString() + "\nStatus: " + r.ResponseStatus); | ||
} | ||
} | ||
catch (FileNotFoundException) | ||
{ | ||
throw new TaskCanceledException(); | ||
} | ||
} | ||
|
||
static bool IsUriProtocolDeclared(string scheme) | ||
{ | ||
var docPath = FileSystemUtils.PlatformGetFullAppPackageFilePath(PlatformUtils.AppManifestFilename); | ||
var doc = XDocument.Load(docPath, LoadOptions.None); | ||
var reader = doc.CreateReader(); | ||
var namespaceManager = new XmlNamespaceManager(reader.NameTable); | ||
namespaceManager.AddNamespace("x", PlatformUtils.AppManifestXmlns); | ||
namespaceManager.AddNamespace("uap", "http://schemas.microsoft.com/appx/manifest/uap/windows10"); | ||
|
||
// Check if the protocol was declared | ||
var decl = doc.Root.XPathSelectElements($"//uap:Extension[@Category='windows.protocol']/uap:Protocol[@Name='{scheme}']", namespaceManager); | ||
|
||
return decl != null && decl.Any(); | ||
throw new PlatformNotSupportedException("This implementation of WebAuthenticator does not support Windows. See https://github.com/microsoft/WindowsAppSDK/issues/441 for more details."); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if you have a better location to direct people to?
You could direct to https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/communication/authentication?view=net-maui-8.0&tabs=windows#get-started, but that just directs to a closed maui issue which is just as confusing, and in turn that just directs to the above referenced issue, which is where the problem really needs to be addressed first.
The doc could be the better place to direct people to, but in that case the doc needs improvements.