Skip to content

Commit

Permalink
feat: Updating WebAuthbrowser to latest form winuiex
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrandolph committed Nov 14, 2023
1 parent 79444f7 commit 816d27c
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 122 deletions.
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
Expand Up @@ -6,7 +6,8 @@ namespace Uno.Extensions.Authentication.Oidc;

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

Check failure on line 24 in src/Uno.Extensions.Authentication.Oidc/OidcAuthenticationProvider.cs

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Cannot assign to 'RedirectUri' because it is a 'method group'

Check failure on line 24 in src/Uno.Extensions.Authentication.Oidc/OidcAuthenticationProvider.cs

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Cannot assign to 'RedirectUri' because it is a 'method group'
config.PostLogoutRedirectUri = WebAuthenticationBroker.GetCurrentApplicationCallbackUri().OriginalString;

Check failure on line 25 in src/Uno.Extensions.Authentication.Oidc/OidcAuthenticationProvider.cs

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Cannot assign to 'PostLogoutRedirectUri' because it is a 'method group'

Check failure on line 25 in src/Uno.Extensions.Authentication.Oidc/OidcAuthenticationProvider.cs

View workflow job for this annotation

GitHub Actions / Build and Deploy Job

Cannot assign to 'PostLogoutRedirectUri' because it is a 'method group'
}
config.Browser = new WebAuthenticatorBrowser();
config.Browser = Browser;
_client = new OidcClient(config);
}

Expand Down Expand Up @@ -87,47 +88,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

0 comments on commit 816d27c

Please sign in to comment.