-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove non-functioning code and instead throw a more helpful platform…
…-not-supported error (#23617) * Remove non-functioning code and instead throw a more helpful platform-not-supported error Also mark the APIs not supported on Windows. * Fix build failures * Update tests for new webauth behavior
- Loading branch information
Showing
3 changed files
with
26 additions
and
60 deletions.
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."); | ||
} | ||
} | ||
} |
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