Skip to content
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

feat: Updating WebAuthbrowser to latest form winuiex #1891

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Uno.Extensions.Authentication.Oidc/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Uno.Extensions;
using IdentityModel.OidcClient.Browser;
using Microsoft.Extensions.DependencyInjection;

namespace Uno.Extensions;

/// <summary>
/// Provides extension methods for OIDC authentication to use with <see cref="IAuthenticationBuilder"/>.
Expand Down Expand Up @@ -26,7 +29,7 @@ public static IAuthenticationBuilder AddOidc(
string name = OidcAuthenticationProvider.DefaultName)
{
#if WINDOWS
WinUIEx.WebAuthenticator.Init();
WinUIEx.WebAuthenticator.CheckOAuthRedirectionActivation();
#endif

var hostBuilder = (builder as IBuilder)?.HostBuilder;
Expand All @@ -35,6 +38,19 @@ public static IAuthenticationBuilder AddOidc(
return builder;
}

hostBuilder = hostBuilder
.ConfigureServices((ctx, services) =>
{
if (ctx.IsRegistered(nameof(AddOidc)))
{
return;
}

services
.AddTransient<IBrowser, WebAuthenticatorBrowser>();
});


hostBuilder
.UseConfiguration(configure: configBuilder =>
configBuilder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

using IdentityModel.OidcClient.Browser;
using System.Diagnostics;
using IdentityModel.OidcClient.Browser;

namespace Uno.Extensions.Authentication.Oidc;

internal record OidcAuthenticationProvider(
ILogger<OidcAuthenticationProvider> ProviderLogger,
IBrowser Browser,
IOptionsSnapshot<OidcClientOptions> Configuration,
ITokenCache Tokens,
OidcAuthenticationSettings? Settings = null) : BaseAuthenticationProvider(ProviderLogger, DefaultName, Tokens)
Expand All @@ -23,7 +22,7 @@ public void Build()
config.RedirectUri = WebAuthenticationBroker.GetCurrentApplicationCallbackUri().OriginalString;
config.PostLogoutRedirectUri = WebAuthenticationBroker.GetCurrentApplicationCallbackUri().OriginalString;
}
config.Browser = new WebAuthenticatorBrowser();
config.Browser = Browser;
_client = new OidcClient(config);
}

Expand Down Expand Up @@ -87,47 +86,4 @@ protected async override ValueTask<bool> InternalLogoutAsync(IDispatcher? dispat
}
return default;
}


}




public class WebAuthenticatorBrowser : IBrowser
{
public async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = default)
{
try
{
#if WINDOWS
var userResult = await WinUIEx.WebAuthenticator.AuthenticateAsync(new Uri(options.StartUrl), new Uri(options.EndUrl));
var callbackurl = $"{options.EndUrl}/?{string.Join("&", userResult.Properties.Select(x => $"{x.Key}={x.Value}"))}";
return new BrowserResult
{
Response = callbackurl
};
#else
var userResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, new Uri(options.StartUrl), new Uri(options.EndUrl));

return new BrowserResult
{
Response = userResult.ResponseData
};
#endif
}
catch (Exception ex)
{
Debug.WriteLine(ex);
return new BrowserResult()
{
ResultType = BrowserResultType.UnknownError,
Error = ex.ToString()
};
}
}


}


42 changes: 42 additions & 0 deletions src/Uno.Extensions.Authentication.Oidc/WebAuthenticatorBrowser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

using IdentityModel.OidcClient.Browser;
using System.Diagnostics;

namespace Uno.Extensions.Authentication.Oidc;

public class WebAuthenticatorBrowser : IBrowser
{
public async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = default)
{
try
{
#if WINDOWS
var userResult = await WinUIEx.WebAuthenticator.AuthenticateAsync(new Uri(options.StartUrl), new Uri(options.EndUrl));
var callbackurl = $"{options.EndUrl}/?{string.Join("&", userResult.Properties.Select(x => $"{x.Key}={x.Value}"))}";
return new BrowserResult
{
Response = callbackurl
};
#else
var userResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, new Uri(options.StartUrl), new Uri(options.EndUrl));

return new BrowserResult
{
Response = userResult.ResponseData
};
#endif
}
catch (Exception ex)
{
Debug.WriteLine(ex);
return new BrowserResult()
{
ResultType = BrowserResultType.UnknownError,
Error = ex.ToString()
};
}
}
}



4 changes: 2 additions & 2 deletions src/Uno.Extensions.Authentication.UI/HostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static IAuthenticationBuilder AddWeb(
string name = WebAuthenticationProvider.DefaultName)
{
#if WINDOWS
WinUIEx.WebAuthenticator.Init();
WinUIEx.WebAuthenticator.CheckOAuthRedirectionActivation();
#endif
var hostBuilder = (builder as IBuilder)?.HostBuilder;
if (hostBuilder is null)
Expand Down Expand Up @@ -78,7 +78,7 @@ public static IAuthenticationBuilder AddWeb<TService>(

{
#if WINDOWS
WinUIEx.WebAuthenticator.Init();
WinUIEx.WebAuthenticator.CheckOAuthRedirectionActivation();
#endif
var hostBuilder = (builder as IBuilder)?.HostBuilder;
if (hostBuilder is null)
Expand Down
Loading
Loading