Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
22 changes: 22 additions & 0 deletions Mac.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MauiMacAppWithBroker", "tests\devapps\MauiMacAppWithBroker\MauiMacAppWithBroker.csproj", "{C0EBA279-7647-42B1-ACEA-B9CC95E2DE18}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C0EBA279-7647-42B1-ACEA-B9CC95E2DE18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C0EBA279-7647-42B1-ACEA-B9CC95E2DE18}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C0EBA279-7647-42B1-ACEA-B9CC95E2DE18}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C0EBA279-7647-42B1-ACEA-B9CC95E2DE18}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
11 changes: 10 additions & 1 deletion src/client/Microsoft.Identity.Client.Broker/BrokerExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,21 @@ private static void AddRuntimeSupport(PublicClientApplicationBuilder builder)
return new RuntimeBroker(uiParent, appConfig, logger);
};
}
else if (DesktopOsHelper.IsMac())
{
builder.Config.BrokerCreatorFunc =
(uiParent, appConfig, logger) =>
{
logger.Info("[Runtime] macOS supports broker.");
return new RuntimeBroker(uiParent, appConfig, logger);
};
}
else
{
builder.Config.BrokerCreatorFunc =
(uiParent, appConfig, logger) =>
{
logger.Info("[RuntimeBroker] Not a Windows 10 or Server equivalent machine. Runtime broker or SsoPolicy support is not available.");
logger.Info("[RuntimeBroker] not available in current platform.");
return new NullBroker(logger);
};
}
Expand Down
36 changes: 24 additions & 12 deletions src/client/Microsoft.Identity.Client.Broker/RuntimeBroker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class RuntimeBroker : IBroker
private readonly ILoggerAdapter _logger;
private readonly IntPtr _parentHandle = IntPtr.Zero;
internal const string ErrorMessageSuffix = " For more details see https://aka.ms/msal-net-wam";
private readonly BrokerOptions _wamOptions;
private readonly BrokerOptions _brokerOptions;
private static Exception s_initException;

private static Dictionary<NativeInterop.LogLevel, LogLevel> LogLevelMap = new Dictionary<NativeInterop.LogLevel, LogLevel>()
Expand Down Expand Up @@ -100,10 +100,22 @@ public RuntimeBroker(
s_lazyCore.Value.EnablePii(_logger.PiiLoggingEnabled);
}

_parentHandle = GetParentWindow(uiParent);
if (DesktopOsHelper.IsWindows())
{
_parentHandle = GetParentWindow(uiParent);
}
else
{
// TODO:ADO 3055958 Parent window handle support on mac
// Without setting parent window on macOS, the mac broker UI will show up in the middle
// of the screen, and keep in the foreground until UI dismissed.
// Technically, macOS broker only accept an objc pointer as window handle, currently we
// do not know how to get such kind of pointer in MAUI. The solution is still unclear.
_parentHandle = (IntPtr)1;
}

// Broker options cannot be null
_wamOptions = appConfig.BrokerOptions;
_brokerOptions = appConfig.BrokerOptions;
}

private void LogEventRaised(NativeInterop.Core sender, LogEventArgs args)
Expand All @@ -130,7 +142,7 @@ public async Task<MsalTokenResponse> AcquireTokenInteractiveAsync(
Debug.Assert(s_lazyCore.Value != null, "Should not call this API if MSAL runtime init failed");

//need to provide a handle
if (_parentHandle == IntPtr.Zero)
if (DesktopOsHelper.IsWindows() && _parentHandle == IntPtr.Zero)
{
throw new MsalClientException(
"window_handle_required",
Expand All @@ -153,7 +165,7 @@ public async Task<MsalTokenResponse> AcquireTokenInteractiveAsync(
{
using (var authParams = WamAdapters.GetCommonAuthParameters(
authenticationRequestParameters,
_wamOptions,
_brokerOptions,
_logger))
{
using (var readAccountResult = await s_lazyCore.Value.ReadAccountByIdAsync(
Expand Down Expand Up @@ -208,7 +220,7 @@ private async Task<MsalTokenResponse> SignInInteractivelyAsync(

using (var authParams = WamAdapters.GetCommonAuthParameters(
authenticationRequestParameters,
_wamOptions,
_brokerOptions,
_logger))
{
//Login Hint
Expand Down Expand Up @@ -242,7 +254,7 @@ private async Task<MsalTokenResponse> AcquireTokenInteractiveDefaultUserAsync(

using (var authParams = WamAdapters.GetCommonAuthParameters(
authenticationRequestParameters,
_wamOptions,
_brokerOptions,
_logger))
{
using (NativeInterop.AuthResult result = await s_lazyCore.Value.SignInAsync(
Expand Down Expand Up @@ -283,7 +295,7 @@ public async Task<MsalTokenResponse> AcquireTokenSilentAsync(

using (var authParams = WamAdapters.GetCommonAuthParameters(
authenticationRequestParameters,
_wamOptions,
_brokerOptions,
_logger))
{
using (var readAccountResult = await s_lazyCore.Value.ReadAccountByIdAsync(
Expand Down Expand Up @@ -346,7 +358,7 @@ public async Task<MsalTokenResponse> AcquireTokenSilentDefaultUserAsync(

using (var authParams = WamAdapters.GetCommonAuthParameters(
authenticationRequestParameters,
_wamOptions,
_brokerOptions,
_logger))
{
using (NativeInterop.AuthResult result = await s_lazyCore.Value.SignInSilentlyAsync(
Expand Down Expand Up @@ -391,7 +403,7 @@ public async Task<MsalTokenResponse> AcquireTokenByUsernamePasswordAsync(

using (AuthParameters authParams = WamAdapters.GetCommonAuthParameters(
authenticationRequestParameters,
_wamOptions,
_brokerOptions,
_logger))
{
authParams.Properties["MSALRuntime_Username"] = acquireTokenByUsernamePasswordParameters.Username;
Expand Down Expand Up @@ -477,7 +489,7 @@ public async Task<IReadOnlyList<IAccount>> GetAccountsAsync(
ICacheSessionManager cacheSessionManager,
IInstanceDiscoveryManager instanceDiscoveryManager)
{
if (!_wamOptions.ListOperatingSystemAccounts)
if (_brokerOptions.EnabledOn == BrokerOptions.OperatingSystems.Windows && !_brokerOptions.ListOperatingSystemAccounts)
{
_logger.Info("[RuntimeBroker] ListWindowsWorkAndSchoolAccounts option was not enabled.");
return Array.Empty<IAccount>();
Expand Down Expand Up @@ -586,7 +598,7 @@ public void HandleInstallUrl(string appLink)

public bool IsBrokerInstalledAndInvokable(AuthorityType authorityType)
{
if (!DesktopOsHelper.IsWin10OrServerEquivalent())
if (!DesktopOsHelper.IsWin10OrServerEquivalent() && !DesktopOsHelper.IsMac())
{
_logger?.Warning("[RuntimeBroker] Not a supported operating system. WAM broker is not available. ");
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public enum OperatingSystems
/// Use broker on Windows OS
/// </summary>
Windows = 0b_0000_0001, // 1
/// <summary>
/// Use broker on OSX
/// </summary>
OSX = 0b_0000_0010, // 10
}

/// <summary>
Expand Down Expand Up @@ -83,6 +87,11 @@ internal bool IsBrokerEnabledOnCurrentOs()
return true;
}

if (EnabledOn.HasFlag(OperatingSystems.OSX) && DesktopOsHelper.IsMac())
{
return true;
}

return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ public static bool IsLinux()

public static bool IsMac()
{
#if MAC
return true;
#if __MOBILE__
return false;
#elif NETFRAMEWORK
return Environment.OSVersion.Platform == PlatformID.MacOSX;
#elif !__MOBILE__
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
#elif NET8_0_OR_GREATER
string OSDescription = RuntimeInformation.OSDescription;
return OSDescription.Contains("Darwin", StringComparison.OrdinalIgnoreCase);
#else
return false;
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
#endif
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Microsoft.Identity.Client.BrokerOptions.OperatingSystems.OSX = 2 -> Microsoft.Identity.Client.BrokerOptions.OperatingSystems
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Microsoft.Identity.Client.BrokerOptions.OperatingSystems.OSX = 2 -> Microsoft.Identity.Client.BrokerOptions.OperatingSystems
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Microsoft.Identity.Client.BrokerOptions.OperatingSystems.OSX = 2 -> Microsoft.Identity.Client.BrokerOptions.OperatingSystems
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Microsoft.Identity.Client.BrokerOptions.OperatingSystems.OSX = 2 -> Microsoft.Identity.Client.BrokerOptions.OperatingSystems
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Microsoft.Identity.Client.BrokerOptions.OperatingSystems.OSX = 2 -> Microsoft.Identity.Client.BrokerOptions.OperatingSystems
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Microsoft.Identity.Client.BrokerOptions.OperatingSystems.OSX = 2 -> Microsoft.Identity.Client.BrokerOptions.OperatingSystems
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public static bool IsWindowsPlatform()
/// <returns>A value indicating if we are running on mac or not</returns>
public static bool IsMacPlatform()
{
#if NET45_OR_GREATER
#if NET8_0_OR_GREATER
return DesktopOsHelper.IsMac();
#elif NET45_OR_GREATER
// we have to also check for PlatformID.Unix because Mono can sometimes return Unix as the platform on a Mac machine.
// see http://www.mono-project.com/docs/faq/technical/
return Environment.OSVersion.Platform == PlatformID.MacOSX || Environment.OSVersion.Platform == PlatformID.Unix;
Expand Down
14 changes: 14 additions & 0 deletions tests/devapps/MauiMacAppWithBroker/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiMacAppWithBroker"
x:Class="MauiMacAppWithBroker.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
11 changes: 11 additions & 0 deletions tests/devapps/MauiMacAppWithBroker/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace MauiMacAppWithBroker;

public partial class App : Application
{
public App()
{
InitializeComponent();

MainPage = new AppShell();
}
}
15 changes: 15 additions & 0 deletions tests/devapps/MauiMacAppWithBroker/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="MauiMacAppWithBroker.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiMacAppWithBroker"
Shell.FlyoutBehavior="Disabled"
Title="MauiMacAppWithBroker">

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

</Shell>
9 changes: 9 additions & 0 deletions tests/devapps/MauiMacAppWithBroker/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace MauiMacAppWithBroker;

public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
43 changes: 43 additions & 0 deletions tests/devapps/MauiMacAppWithBroker/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiMacAppWithBroker.MainPage">

<ScrollView>
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<Image
Source="dotnet_bot.png"
HeightRequest="185"
Aspect="AspectFit"
SemanticProperties.Description="dot net bot in a race car number eight" />

<Label
Text="Hello, World!"
Style="{StaticResource Headline}"
SemanticProperties.HeadingLevel="Level1" />

<Label
Text="Welcome to &#10;.NET Multi-platform App UI"
Style="{StaticResource SubHeadline}"
SemanticProperties.HeadingLevel="Level2"
SemanticProperties.Description="Welcome to dot net Multi platform App U I" />

<Button
x:Name="CACIACSBtn"
Text="ACI and ACS"
SemanticProperties.Hint="Acquire credential interactively and then silently via mac broker"
Clicked="OnACIACSClicked"
HorizontalOptions="Fill" />

<Button
x:Name="GetAllAccountsBtn"
Text="Get all accounts"
SemanticProperties.Hint="Get all accounts via mac broker"
Clicked="OnGetAllAccountsClicked"
HorizontalOptions="Fill" />
</VerticalStackLayout>
</ScrollView>

</ContentPage>
Loading
Loading