diff --git a/CommunityToolkit.Net.Authentication.Msal/CommunityToolkit.Net.Authentication.Msal.csproj b/CommunityToolkit.Net.Authentication.Msal/CommunityToolkit.Net.Authentication.Msal.csproj new file mode 100644 index 0000000..d9c0342 --- /dev/null +++ b/CommunityToolkit.Net.Authentication.Msal/CommunityToolkit.Net.Authentication.Msal.csproj @@ -0,0 +1,21 @@ + + + netstandard2.0 + + Windows Community Toolkit .NET Standard Auth Services + + This package includes .NET Standard authentication helpers such as: + - MsalProvider: + + Community Toolkit Provider Authentication Auth Msal + + + + + + + + + + + diff --git a/Microsoft.Toolkit.Graph/Providers/MsalProvider.cs b/CommunityToolkit.Net.Authentication.Msal/MsalProvider.cs similarity index 54% rename from Microsoft.Toolkit.Graph/Providers/MsalProvider.cs rename to CommunityToolkit.Net.Authentication.Msal/MsalProvider.cs index 42c00bc..5cd2de1 100644 --- a/Microsoft.Toolkit.Graph/Providers/MsalProvider.cs +++ b/CommunityToolkit.Net.Authentication.Msal/MsalProvider.cs @@ -6,102 +6,70 @@ using System.Collections.Generic; using System.Linq; using System.Net.Http; +using System.Net.Http.Headers; using System.Reflection; using System.Threading.Tasks; -using Microsoft.Graph; using Microsoft.Identity.Client; -using Microsoft.Toolkit.Graph.Extensions; -namespace Microsoft.Toolkit.Graph.Providers +namespace CommunityToolkit.Net.Authentication { - //// TODO: Move some of this to a simple base-class for non-MSAL parts related to Provider only and properties? - /// - /// MSAL.NET provider helper for tracking authentication state using an class. + /// MSAL.NET provider helper for tracking authentication state. /// - public class MsalProvider : IProvider + public class MsalProvider : BaseProvider { /// - /// Gets or sets the MSAL.NET Client used to authenticate the user. - /// - protected IPublicClientApplication Client { get; set; } - - /// - /// Gets or sets the provider used by the graph to manage requests. + /// Gets the MSAL.NET Client used to authenticate the user. /// - protected IAuthenticationProvider Provider { get; set; } - - private ProviderState _state = ProviderState.Loading; - - /// - public ProviderState State - { - get - { - return _state; - } - - private set - { - var current = _state; - _state = value; - - StateChanged?.Invoke(this, new StateChangedEventArgs(current, _state)); - } - } - - /// - public GraphServiceClient Graph { get; private set; } - - /// - public event EventHandler StateChanged; + protected IPublicClientApplication Client { get; private set; } /// - /// Initializes a new instance of the class. . + /// Gets an array of scopes to use for accessing Graph resources. /// - private MsalProvider() - { - } + protected string[] Scopes { get; private set; } /// /// Initializes a new instance of the class. /// - /// Existing instance. - /// Existing instance. - /// A returning a instance. - public static async Task CreateAsync(IPublicClientApplication client, IAuthenticationProvider provider) + /// Registered ClientId. + /// RedirectUri for auth response. + /// List of Scopes to initially request. + public MsalProvider(string clientId, string[] scopes = null, string redirectUri = "https://login.microsoftonline.com/common/oauth2/nativeclient") { - //// TODO: Check all config provided - - var msal = new MsalProvider - { - Client = client, - Provider = provider, - }; + var client = PublicClientApplicationBuilder.Create(clientId) + .WithAuthority(AzureCloudInstance.AzurePublic, AadAuthorityAudience.AzureAdAndPersonalMicrosoftAccount) + .WithRedirectUri(redirectUri) + .WithClientName(ProviderManager.ClientName) + .WithClientVersion(Assembly.GetExecutingAssembly().GetName().Version.ToString()) + .Build(); - msal.Graph = new GraphServiceClient(msal); + Scopes = scopes ?? new string[] { string.Empty }; - await msal.TrySilentSignInAsync(); + Client = client; - return msal; + _ = TrySilentSignInAsync(); } /// - public async Task AuthenticateRequestAsync(HttpRequestMessage request) + public override async Task AuthenticateRequestAsync(HttpRequestMessage request) { - request.AddSdkVersion(); + IEnumerable accounts = await Client.GetAccountsAsync(); + AuthenticationResult authResult; try { - await Provider.AuthenticateRequestAsync(request); + authResult = await Client.AcquireTokenSilent(Scopes, accounts.FirstOrDefault()).ExecuteAsync(); } - catch (Exception) + catch (MsalUiRequiredException) { - // TODO: Catch different types of errors and try and re-auth? Should be handled by Graph Auth Providers. - // Assume we're signed-out on error? - State = ProviderState.SignedOut; + authResult = await Client.AcquireTokenInteractive(Scopes).ExecuteAsync(); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken); + } - return; + if (authResult != null) + { + AddSdkVersion(request); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken); } // Check state after request to see if we're now signed-in. @@ -159,14 +127,14 @@ public async Task TrySilentSignInAsync() } /// - public async Task LoginAsync() + public override async Task LoginAsync() { // Force fake request to start auth process await AuthenticateRequestAsync(new System.Net.Http.HttpRequestMessage()); } /// - public async Task LogoutAsync() + public override async Task LogoutAsync() { // Forcibly remove each user. foreach (var user in await Client.GetAccountsAsync()) diff --git a/CommunityToolkit.Net.Authentication/BaseProvider.cs b/CommunityToolkit.Net.Authentication/BaseProvider.cs new file mode 100644 index 0000000..cbd5bd0 --- /dev/null +++ b/CommunityToolkit.Net.Authentication/BaseProvider.cs @@ -0,0 +1,68 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Net.Http; +using System.Threading.Tasks; +using CommunityToolkit.Net.Authentication.Extensions; + +namespace CommunityToolkit.Net.Authentication +{ + /// + /// A base construct for building Graph Providers on top of. + /// + public abstract class BaseProvider : IProvider + { + private ProviderState _state; + + /// + /// Gets or sets the current state of the provider. + /// + public ProviderState State + { + get => _state; + protected set + { + var oldState = _state; + var newState = value; + if (oldState != newState) + { + _state = newState; + StateChanged?.Invoke(this, new ProviderStateChangedEventArgs(oldState, newState)); + } + } + } + + /// + public event EventHandler StateChanged; + + /// + /// Initializes a new instance of the class. + /// + public BaseProvider() + { + _state = ProviderState.Loading; + } + + /// + public abstract Task LoginAsync(); + + /// + public abstract Task LogoutAsync(); + + /// + public abstract Task AuthenticateRequestAsync(HttpRequestMessage request); + + /// + /// Append the Sdk version to the request headers. + /// + /// + /// The request to append the header to. + /// + protected void AddSdkVersion(HttpRequestMessage request) + { + request.AddSdkVersion(); + } + } +} diff --git a/CommunityToolkit.Net.Authentication/CommunityToolkit.Net.Authentication.csproj b/CommunityToolkit.Net.Authentication/CommunityToolkit.Net.Authentication.csproj new file mode 100644 index 0000000..7bbf962 --- /dev/null +++ b/CommunityToolkit.Net.Authentication/CommunityToolkit.Net.Authentication.csproj @@ -0,0 +1,19 @@ + + + netstandard2.0 + + Windows Community Toolkit .NET Standard Auth Services + + This package includes .NET Standard authentication helpers such as: + - BaseProvider: + - IProvider: + - MockPRovider: + - ProviderManager: + - ProviderManagerChangedState: + - ProviderState: + - ProviderStateChangedEventArgs: + - ProviderUpdatedEventArgs: + + Community Toolkit Provider Authentication Auth + + diff --git a/Microsoft.Toolkit.Graph/Extensions/HttpRequestMessageExtensions.cs b/CommunityToolkit.Net.Authentication/Extensions/HttpRequestMessageExtensions.cs similarity index 59% rename from Microsoft.Toolkit.Graph/Extensions/HttpRequestMessageExtensions.cs rename to CommunityToolkit.Net.Authentication/Extensions/HttpRequestMessageExtensions.cs index 9d75abf..9a256c7 100644 --- a/Microsoft.Toolkit.Graph/Extensions/HttpRequestMessageExtensions.cs +++ b/CommunityToolkit.Net.Authentication/Extensions/HttpRequestMessageExtensions.cs @@ -4,26 +4,24 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Reflection; -using System.Text; using System.Threading.Tasks; -namespace Microsoft.Toolkit.Graph.Extensions +namespace CommunityToolkit.Net.Authentication.Extensions { /// /// Helpers for Graph related HTTP Headers. /// - internal static class HttpRequestMessageExtensions + public static class HttpRequestMessageExtensions { private const string SdkVersion = "SdkVersion"; private const string LibraryName = "wct"; private const string Bearer = "Bearer"; private const string MockGraphToken = "{token:https://graph.microsoft.com/}"; - public static void AddSdkVersion(this HttpRequestMessage request) + internal static void AddSdkVersion(this HttpRequestMessage request) { if (request == null || request.Headers == null) { @@ -39,11 +37,26 @@ public static void AddSdkVersion(this HttpRequestMessage request) } } - public static void AddMockProviderToken(this HttpRequestMessage request) + internal static void AddMockProviderToken(this HttpRequestMessage request) { request .Headers .Authorization = new AuthenticationHeaderValue(Bearer, MockGraphToken); } + + /// + /// Helper method for authenticating an http request using the GlobalProvider instance. + /// + /// The request to authenticate. + /// A task upon completion. + public static async Task AuthenticateAsync(this HttpRequestMessage request) + { + if (ProviderManager.Instance.GlobalProvider == null) + { + throw new InvalidOperationException("The request cannot be authenticated. The GlobalProvider is null."); + } + + await ProviderManager.Instance.GlobalProvider.AuthenticateRequestAsync(request); + } } } diff --git a/Microsoft.Toolkit.Graph/Providers/IProvider.cs b/CommunityToolkit.Net.Authentication/IProvider.cs similarity index 63% rename from Microsoft.Toolkit.Graph/Providers/IProvider.cs rename to CommunityToolkit.Net.Authentication/IProvider.cs index ea59b80..51d5584 100644 --- a/Microsoft.Toolkit.Graph/Providers/IProvider.cs +++ b/CommunityToolkit.Net.Authentication/IProvider.cs @@ -3,18 +3,15 @@ // See the LICENSE file in the project root for more information. using System; -using System.ComponentModel; using System.Net.Http; -using System.Runtime.CompilerServices; using System.Threading.Tasks; -using Microsoft.Graph; -namespace Microsoft.Toolkit.Graph.Providers +namespace CommunityToolkit.Net.Authentication { /// - /// helper wrapper to expose more states around the authentication process for graph controls. + /// Authentication provider to expose more states around the authentication process for graph controls. /// - public interface IProvider : IAuthenticationProvider + public interface IProvider { /// /// Gets the current login state of the provider. @@ -22,14 +19,16 @@ public interface IProvider : IAuthenticationProvider ProviderState State { get; } /// - /// Gets the object to access the Microsoft Graph APIs. + /// Event called when the login changes. /// - GraphServiceClient Graph { get; } + event EventHandler StateChanged; /// - /// Event called when the login changes. + /// Authenticate an outgoing request. /// - event EventHandler StateChanged; + /// The request to authenticate. + /// A task upon completion. + Task AuthenticateRequestAsync(HttpRequestMessage request); /// /// Login the user. diff --git a/CommunityToolkit.Net.Authentication/MockProvider.cs b/CommunityToolkit.Net.Authentication/MockProvider.cs new file mode 100644 index 0000000..a7dcc5f --- /dev/null +++ b/CommunityToolkit.Net.Authentication/MockProvider.cs @@ -0,0 +1,60 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Net.Http; +using System.Threading.Tasks; +using CommunityToolkit.Net.Authentication.Extensions; + +namespace CommunityToolkit.Net.Authentication +{ + /// + /// Provider to connect to the example data set for Microsoft Graph. Useful for prototyping and samples. + /// + public class MockProvider : BaseProvider + { + private const string GRAPH_PROXY_URL = "https://proxy.apisandbox.msdn.microsoft.com/svc?url="; + + /// + /// Initializes a new instance of the class. + /// + /// Configuration for the MockProvider. + public MockProvider(bool signedIn = true) + { + State = signedIn ? ProviderState.SignedIn : ProviderState.SignedOut; + } + + /// + public override Task AuthenticateRequestAsync(HttpRequestMessage request) + { + // Append the SDK version header + AddSdkVersion(request); + + // Append the token auth header + request.AddMockProviderToken(); + + // Prepend Proxy Service URI + var requestUri = request.RequestUri.ToString(); + request.RequestUri = new Uri(GRAPH_PROXY_URL + Uri.EscapeDataString(requestUri)); + + return Task.FromResult(0); + } + + /// + public override async Task LoginAsync() + { + State = ProviderState.Loading; + await Task.Delay(3000); + State = ProviderState.SignedIn; + } + + /// + public override async Task LogoutAsync() + { + State = ProviderState.Loading; + await Task.Delay(3000); + State = ProviderState.SignedOut; + } + } +} diff --git a/Microsoft.Toolkit.Graph/Providers/ProviderManager.cs b/CommunityToolkit.Net.Authentication/ProviderManager.cs similarity index 92% rename from Microsoft.Toolkit.Graph/Providers/ProviderManager.cs rename to CommunityToolkit.Net.Authentication/ProviderManager.cs index e417de0..d56deb6 100644 --- a/Microsoft.Toolkit.Graph/Providers/ProviderManager.cs +++ b/CommunityToolkit.Net.Authentication/ProviderManager.cs @@ -5,7 +5,7 @@ using System; using System.ComponentModel; -namespace Microsoft.Toolkit.Graph.Providers +namespace CommunityToolkit.Net.Authentication { /// /// Shared provider manager used by controls in Microsoft.Toolkit.Graph.Controls to authenticate and call the Microsoft Graph. @@ -15,7 +15,7 @@ namespace Microsoft.Toolkit.Graph.Providers /// ProviderManager.Instance.GlobalProvider = await MsalProvider.CreateAsync(...); /// /// - public class ProviderManager : INotifyPropertyChanged + public partial class ProviderManager : INotifyPropertyChanged { /// /// Gets the name of the toolkit client to identify self in Graph calls. @@ -72,7 +72,7 @@ private ProviderManager() // Use Instance } - private void ProviderStateChanged(object sender, StateChangedEventArgs e) + private void ProviderStateChanged(object sender, ProviderStateChangedEventArgs e) { ProviderUpdated?.Invoke(this, new ProviderUpdatedEventArgs(ProviderManagerChangedState.ProviderStateChanged)); } diff --git a/Microsoft.Toolkit.Graph/Providers/ProviderManagerChangedState.cs b/CommunityToolkit.Net.Authentication/ProviderManagerChangedState.cs similarity index 93% rename from Microsoft.Toolkit.Graph/Providers/ProviderManagerChangedState.cs rename to CommunityToolkit.Net.Authentication/ProviderManagerChangedState.cs index c7a5c2f..6350307 100644 --- a/Microsoft.Toolkit.Graph/Providers/ProviderManagerChangedState.cs +++ b/CommunityToolkit.Net.Authentication/ProviderManagerChangedState.cs @@ -2,7 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -namespace Microsoft.Toolkit.Graph.Providers +namespace CommunityToolkit.Net.Authentication { /// /// Enum representing reasons for provider state changing. diff --git a/Microsoft.Toolkit.Graph/Providers/ProviderState.cs b/CommunityToolkit.Net.Authentication/ProviderState.cs similarity index 94% rename from Microsoft.Toolkit.Graph/Providers/ProviderState.cs rename to CommunityToolkit.Net.Authentication/ProviderState.cs index 720f24a..373f3a7 100644 --- a/Microsoft.Toolkit.Graph/Providers/ProviderState.cs +++ b/CommunityToolkit.Net.Authentication/ProviderState.cs @@ -2,7 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -namespace Microsoft.Toolkit.Graph.Providers +namespace CommunityToolkit.Net.Authentication { /// /// represents the current authentication state of the session for a given . diff --git a/Microsoft.Toolkit.Graph/Providers/StateChangedEventArgs.cs b/CommunityToolkit.Net.Authentication/ProviderStateChangedEventArgs.cs similarity index 76% rename from Microsoft.Toolkit.Graph/Providers/StateChangedEventArgs.cs rename to CommunityToolkit.Net.Authentication/ProviderStateChangedEventArgs.cs index f2f65d0..9a4278e 100644 --- a/Microsoft.Toolkit.Graph/Providers/StateChangedEventArgs.cs +++ b/CommunityToolkit.Net.Authentication/ProviderStateChangedEventArgs.cs @@ -4,19 +4,19 @@ using System; -namespace Microsoft.Toolkit.Graph.Providers +namespace CommunityToolkit.Net.Authentication { /// /// event arguments. /// - public class StateChangedEventArgs : EventArgs + public class ProviderStateChangedEventArgs : EventArgs { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// Previous . /// Current . - public StateChangedEventArgs(ProviderState oldState, ProviderState newState) + public ProviderStateChangedEventArgs(ProviderState oldState, ProviderState newState) { OldState = oldState; NewState = newState; diff --git a/Microsoft.Toolkit.Graph/Providers/ProviderUpdatedEventArgs.cs b/CommunityToolkit.Net.Authentication/ProviderUpdatedEventArgs.cs similarity index 95% rename from Microsoft.Toolkit.Graph/Providers/ProviderUpdatedEventArgs.cs rename to CommunityToolkit.Net.Authentication/ProviderUpdatedEventArgs.cs index 4b652cb..a36fb3c 100644 --- a/Microsoft.Toolkit.Graph/Providers/ProviderUpdatedEventArgs.cs +++ b/CommunityToolkit.Net.Authentication/ProviderUpdatedEventArgs.cs @@ -4,7 +4,7 @@ using System; -namespace Microsoft.Toolkit.Graph.Providers +namespace CommunityToolkit.Net.Authentication { /// /// class for event. diff --git a/CommunityToolkit.Net.Graph/CommunityToolkit.Net.Graph.csproj b/CommunityToolkit.Net.Graph/CommunityToolkit.Net.Graph.csproj new file mode 100644 index 0000000..88f243b --- /dev/null +++ b/CommunityToolkit.Net.Graph/CommunityToolkit.Net.Graph.csproj @@ -0,0 +1,28 @@ + + + + netstandard2.0 + Windows Community Toolkit .NET Standard Graph Services + CommunityToolkit.Net.Graph + + This package includes .NET Standard code helpers such as: + - GraphExtensions: Helpers for common tasks related to the Microsoft Graph used by the Microsoft.Toolkit.Graph.Controls. + - ProviderExtensions: Extension on IProvider for accessing a pre-configured GraphServiceClient instance. + + Windows Community Toolkit Graph Provider Extensions + false + true + 8.0 + Debug;Release;CI + AnyCPU;ARM;ARM64;x64;x86 + + + + + + + + + + + diff --git a/Microsoft.Toolkit.Graph/Extensions/GraphExtensions.cs b/CommunityToolkit.Net.Graph/Extensions/GraphExtensions.cs similarity index 98% rename from Microsoft.Toolkit.Graph/Extensions/GraphExtensions.cs rename to CommunityToolkit.Net.Graph/Extensions/GraphExtensions.cs index 9d7668a..77d8637 100644 --- a/Microsoft.Toolkit.Graph/Extensions/GraphExtensions.cs +++ b/CommunityToolkit.Net.Graph/Extensions/GraphExtensions.cs @@ -2,13 +2,11 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using System.IO; using System.Threading.Tasks; using Microsoft.Graph; -using Microsoft.Graph.Auth; -namespace Microsoft.Toolkit.Graph.Extensions +namespace CommunityToolkit.Net.Graph.Extensions { /// /// Extension methods to the Graph SDK used by the Microsoft.Toolkit.Graph.Controls. diff --git a/CommunityToolkit.Net.Graph/Extensions/ProviderExtensions.cs b/CommunityToolkit.Net.Graph/Extensions/ProviderExtensions.cs new file mode 100644 index 0000000..5af1a2b --- /dev/null +++ b/CommunityToolkit.Net.Graph/Extensions/ProviderExtensions.cs @@ -0,0 +1,50 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using CommunityToolkit.Net.Authentication; +using Microsoft.Graph; + +namespace CommunityToolkit.Net.Graph.Extensions +{ + /// + /// Extension method for enabled Graph client access from an IProvider implementation. + /// + public static class ProviderExtensions + { + private static GraphServiceClient _client; + + static ProviderExtensions() + { + ProviderManager.Instance.ProviderUpdated += OnProviderUpdated; + } + + private static void OnProviderUpdated(object sender, ProviderUpdatedEventArgs e) + { + var providerManager = sender as ProviderManager; + if (e.Reason == ProviderManagerChangedState.ProviderChanged || !(providerManager.GlobalProvider?.State == ProviderState.SignedIn)) + { + _client = null; + } + } + + /// + /// Lazily gets a GraphServiceClient instance based on the current GlobalProvider. + /// The client instance is cleared whenever the GlobalProvider changes. + /// + /// The provider for authenticating Graph calls. + /// A GraphServiceClient instance. + public static GraphServiceClient Graph(this IProvider provider) + { + if (_client == null && provider?.State == ProviderState.SignedIn) + { + _client = new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) => + { + await provider.AuthenticateRequestAsync(requestMessage); + })); + } + + return _client; + } + } +} diff --git a/Microsoft.Toolkit.Graph.Controls/Assets/person.png b/CommunityToolkit.Uwp.Graph.Controls/Assets/person.png similarity index 100% rename from Microsoft.Toolkit.Graph.Controls/Assets/person.png rename to CommunityToolkit.Uwp.Graph.Controls/Assets/person.png diff --git a/Microsoft.Toolkit.Graph.Controls/Microsoft.Toolkit.Graph.Controls.csproj b/CommunityToolkit.Uwp.Graph.Controls/CommunityToolkit.Uwp.Graph.Controls.csproj similarity index 71% rename from Microsoft.Toolkit.Graph.Controls/Microsoft.Toolkit.Graph.Controls.csproj rename to CommunityToolkit.Uwp.Graph.Controls/CommunityToolkit.Uwp.Graph.Controls.csproj index bd63883..c4dcfae 100644 --- a/Microsoft.Toolkit.Graph.Controls/Microsoft.Toolkit.Graph.Controls.csproj +++ b/CommunityToolkit.Uwp.Graph.Controls/CommunityToolkit.Uwp.Graph.Controls.csproj @@ -2,12 +2,13 @@ uap10.0.17763 - Windows Community Toolkit Graph Controls - Microsoft.Toolkit.Graph.Controls + Windows Community Toolkit Graph Uwp Controls and Helpers + CommunityToolkit.Uwp.Graph.Controls - This library provides Microsoft Graph XAML controls. It is part of the Windows Community Toolkit. + This library provides Microsoft Graph UWP XAML controls. It is part of the Windows Community Toolkit. Classes: + - GraphPresenter: - LoginButton: The Login Control leverages MSAL libraries to support the sign-in processes for Microsoft Graph and beyond. - PersonView: The PersonView control displays a user photo and can display their name and e-mail. - PeoplePicker: The PeoplePicker Control is a simple control that allows for selection of one or more users. @@ -27,7 +28,6 @@ - @@ -35,8 +35,12 @@ - - + + + + + + @@ -45,7 +49,11 @@ - + + $(DefineConstants);WINRT + + + diff --git a/Microsoft.Toolkit.Graph.Controls/Controls/GraphPresenter/GraphPresenter.cs b/CommunityToolkit.Uwp.Graph.Controls/Controls/GraphPresenter/GraphPresenter.cs similarity index 99% rename from Microsoft.Toolkit.Graph.Controls/Controls/GraphPresenter/GraphPresenter.cs rename to CommunityToolkit.Uwp.Graph.Controls/Controls/GraphPresenter/GraphPresenter.cs index 91cb6a5..be33c2d 100644 --- a/Microsoft.Toolkit.Graph.Controls/Controls/GraphPresenter/GraphPresenter.cs +++ b/CommunityToolkit.Uwp.Graph.Controls/Controls/GraphPresenter/GraphPresenter.cs @@ -13,7 +13,7 @@ using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; -namespace Microsoft.Toolkit.Graph.Controls +namespace CommunityToolkit.Uwp.Graph.Controls { /// /// Specialized to fetch and display data from the Microsoft Graph. diff --git a/Microsoft.Toolkit.Graph.Controls/Controls/GraphPresenter/QueryOption.cs b/CommunityToolkit.Uwp.Graph.Controls/Controls/GraphPresenter/QueryOption.cs similarity index 96% rename from Microsoft.Toolkit.Graph.Controls/Controls/GraphPresenter/QueryOption.cs rename to CommunityToolkit.Uwp.Graph.Controls/Controls/GraphPresenter/QueryOption.cs index 8af7159..2bdb1c8 100644 --- a/Microsoft.Toolkit.Graph.Controls/Controls/GraphPresenter/QueryOption.cs +++ b/CommunityToolkit.Uwp.Graph.Controls/Controls/GraphPresenter/QueryOption.cs @@ -9,7 +9,7 @@ using System.Threading.Tasks; using Windows.UI.Xaml; -namespace Microsoft.Toolkit.Graph.Controls +namespace CommunityToolkit.Uwp.Graph.Controls { /// /// XAML Proxy for . diff --git a/Microsoft.Toolkit.Graph.Controls/Controls/LoginButton/LoginButton.Events.cs b/CommunityToolkit.Uwp.Graph.Controls/Controls/LoginButton/LoginButton.Events.cs similarity index 96% rename from Microsoft.Toolkit.Graph.Controls/Controls/LoginButton/LoginButton.Events.cs rename to CommunityToolkit.Uwp.Graph.Controls/Controls/LoginButton/LoginButton.Events.cs index 2cb11f8..7717fe1 100644 --- a/Microsoft.Toolkit.Graph.Controls/Controls/LoginButton/LoginButton.Events.cs +++ b/CommunityToolkit.Uwp.Graph.Controls/Controls/LoginButton/LoginButton.Events.cs @@ -5,7 +5,7 @@ using System; using System.ComponentModel; -namespace Microsoft.Toolkit.Graph.Controls +namespace CommunityToolkit.Uwp.Graph.Controls { /// /// The control is a button which can be used to sign the user in or show them profile details. diff --git a/Microsoft.Toolkit.Graph.Controls/Controls/LoginButton/LoginButton.Properties.cs b/CommunityToolkit.Uwp.Graph.Controls/Controls/LoginButton/LoginButton.Properties.cs similarity index 97% rename from Microsoft.Toolkit.Graph.Controls/Controls/LoginButton/LoginButton.Properties.cs rename to CommunityToolkit.Uwp.Graph.Controls/Controls/LoginButton/LoginButton.Properties.cs index d7a5e66..e7b36c7 100644 --- a/Microsoft.Toolkit.Graph.Controls/Controls/LoginButton/LoginButton.Properties.cs +++ b/CommunityToolkit.Uwp.Graph.Controls/Controls/LoginButton/LoginButton.Properties.cs @@ -5,7 +5,7 @@ using Microsoft.Graph; using Windows.UI.Xaml; -namespace Microsoft.Toolkit.Graph.Controls +namespace CommunityToolkit.Uwp.Graph.Controls { /// /// The control is a button which can be used to sign the user in or show them profile details. diff --git a/Microsoft.Toolkit.Graph.Controls/Controls/LoginButton/LoginButton.cs b/CommunityToolkit.Uwp.Graph.Controls/Controls/LoginButton/LoginButton.cs similarity index 96% rename from Microsoft.Toolkit.Graph.Controls/Controls/LoginButton/LoginButton.cs rename to CommunityToolkit.Uwp.Graph.Controls/Controls/LoginButton/LoginButton.cs index bb7c505..ebe5f79 100644 --- a/Microsoft.Toolkit.Graph.Controls/Controls/LoginButton/LoginButton.cs +++ b/CommunityToolkit.Uwp.Graph.Controls/Controls/LoginButton/LoginButton.cs @@ -6,12 +6,13 @@ using System.ComponentModel; using System.Diagnostics; using System.Threading.Tasks; -using Microsoft.Toolkit.Graph.Providers; +using CommunityToolkit.Net.Authentication; +using CommunityToolkit.Net.Graph.Extensions; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; -namespace Microsoft.Toolkit.Graph.Controls +namespace CommunityToolkit.Uwp.Graph.Controls { /// /// The control is a button which can be used to sign the user in or show them profile details. @@ -114,7 +115,7 @@ private async void LoadData() { // https://github.com/microsoftgraph/microsoft-graph-toolkit/blob/master/src/components/mgt-login/mgt-login.ts#L139 // TODO: Batch with photo request later? https://github.com/microsoftgraph/msgraph-sdk-dotnet-core/issues/29 - UserDetails = await provider.Graph.Me.Request().GetAsync(); + UserDetails = await provider.Graph().Me.Request().GetAsync(); } catch (Exception e) { diff --git a/Microsoft.Toolkit.Graph.Controls/Controls/LoginButton/LoginButton.xaml b/CommunityToolkit.Uwp.Graph.Controls/Controls/LoginButton/LoginButton.xaml similarity index 94% rename from Microsoft.Toolkit.Graph.Controls/Controls/LoginButton/LoginButton.xaml rename to CommunityToolkit.Uwp.Graph.Controls/Controls/LoginButton/LoginButton.xaml index db1ad64..4913494 100644 --- a/Microsoft.Toolkit.Graph.Controls/Controls/LoginButton/LoginButton.xaml +++ b/CommunityToolkit.Uwp.Graph.Controls/Controls/LoginButton/LoginButton.xaml @@ -1,8 +1,8 @@  + xmlns:graphconverters="using:CommunityToolkit.Uwp.Graph.Controls.Converters" + xmlns:local="using:CommunityToolkit.Uwp.Graph.Controls"> /// for event. diff --git a/Microsoft.Toolkit.Graph.Controls/Controls/PeoplePicker/PeoplePicker.cs b/CommunityToolkit.Uwp.Graph.Controls/Controls/PeoplePicker/PeoplePicker.cs similarity index 96% rename from Microsoft.Toolkit.Graph.Controls/Controls/PeoplePicker/PeoplePicker.cs rename to CommunityToolkit.Uwp.Graph.Controls/Controls/PeoplePicker/PeoplePicker.cs index c5850e2..24b8333 100644 --- a/Microsoft.Toolkit.Graph.Controls/Controls/PeoplePicker/PeoplePicker.cs +++ b/CommunityToolkit.Uwp.Graph.Controls/Controls/PeoplePicker/PeoplePicker.cs @@ -6,15 +6,15 @@ using System.Collections; using System.Collections.ObjectModel; using System.Linq; +using CommunityToolkit.Net.Authentication; +using CommunityToolkit.Net.Graph.Extensions; using Microsoft.Graph; -using Microsoft.Toolkit.Graph.Extensions; -using Microsoft.Toolkit.Graph.Providers; using Microsoft.Toolkit.Uwp.UI.Controls; using Microsoft.Toolkit.Uwp.UI.Extensions; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; -namespace Microsoft.Toolkit.Graph.Controls +namespace CommunityToolkit.Uwp.Graph.Controls { /// /// Control which allows user to search for a person or contact within Microsoft Graph. Built on top of . @@ -41,7 +41,7 @@ private async void TokenBox_TokenItemTokenItemAdding(TokenizingTextBox sender, T using (args.GetDeferral()) { // Try and convert typed text to people - var graph = ProviderManager.Instance.GlobalProvider.Graph; + var graph = ProviderManager.Instance.GlobalProvider.Graph(); if (graph != null) { args.Item = (await graph.FindPersonAsync(args.TokenText)).CurrentPage.FirstOrDefault(); @@ -74,7 +74,7 @@ private void TokenBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChang _typeTimer.Debounce( async () => { - var graph = ProviderManager.Instance.GlobalProvider.Graph; + var graph = ProviderManager.Instance.GlobalProvider.Graph(); if (graph != null) { // If empty, will clear out diff --git a/Microsoft.Toolkit.Graph.Controls/Controls/PeoplePicker/PeoplePicker.xaml b/CommunityToolkit.Uwp.Graph.Controls/Controls/PeoplePicker/PeoplePicker.xaml similarity index 95% rename from Microsoft.Toolkit.Graph.Controls/Controls/PeoplePicker/PeoplePicker.xaml rename to CommunityToolkit.Uwp.Graph.Controls/Controls/PeoplePicker/PeoplePicker.xaml index e3b1267..7164905 100644 --- a/Microsoft.Toolkit.Graph.Controls/Controls/PeoplePicker/PeoplePicker.xaml +++ b/CommunityToolkit.Uwp.Graph.Controls/Controls/PeoplePicker/PeoplePicker.xaml @@ -1,7 +1,7 @@  + xmlns:local="using:CommunityToolkit.Uwp.Graph.Controls"> diff --git a/Microsoft.Toolkit.Graph.Controls/Controls/PersonView/PersonView.Properties.cs b/CommunityToolkit.Uwp.Graph.Controls/Controls/PersonView/PersonView.Properties.cs similarity index 99% rename from Microsoft.Toolkit.Graph.Controls/Controls/PersonView/PersonView.Properties.cs rename to CommunityToolkit.Uwp.Graph.Controls/Controls/PersonView/PersonView.Properties.cs index 84fdbed..b2fa6e7 100644 --- a/Microsoft.Toolkit.Graph.Controls/Controls/PersonView/PersonView.Properties.cs +++ b/CommunityToolkit.Uwp.Graph.Controls/Controls/PersonView/PersonView.Properties.cs @@ -11,7 +11,7 @@ using Windows.UI.Xaml; using Windows.UI.Xaml.Media.Imaging; -namespace Microsoft.Toolkit.Graph.Controls +namespace CommunityToolkit.Uwp.Graph.Controls { /// /// The control displays a user photo and can display their name and e-mail. diff --git a/Microsoft.Toolkit.Graph.Controls/Controls/PersonView/PersonView.cs b/CommunityToolkit.Uwp.Graph.Controls/Controls/PersonView/PersonView.cs similarity index 93% rename from Microsoft.Toolkit.Graph.Controls/Controls/PersonView/PersonView.cs rename to CommunityToolkit.Uwp.Graph.Controls/Controls/PersonView/PersonView.cs index f3bbdc5..4e1199e 100644 --- a/Microsoft.Toolkit.Graph.Controls/Controls/PersonView/PersonView.cs +++ b/CommunityToolkit.Uwp.Graph.Controls/Controls/PersonView/PersonView.cs @@ -6,14 +6,14 @@ using System.IO; using System.Linq; using System.Threading.Tasks; +using CommunityToolkit.Net.Authentication; +using CommunityToolkit.Net.Graph.Extensions; using Microsoft.Graph; -using Microsoft.Toolkit.Graph.Extensions; -using Microsoft.Toolkit.Graph.Providers; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media.Imaging; -namespace Microsoft.Toolkit.Graph.Controls +namespace CommunityToolkit.Uwp.Graph.Controls { /// /// The control displays a user photo and can display their name and e-mail. @@ -138,7 +138,7 @@ private async void LoadData() // TODO: Batch when API easier https://github.com/microsoftgraph/msgraph-sdk-dotnet-core/issues/29 try { - user = await provider.Graph.Users[UserId].Request().GetAsync(); + user = await provider.Graph().Users[UserId].Request().GetAsync(); } catch { @@ -147,7 +147,7 @@ private async void LoadData() try { // TODO: Move to LoadImage based on previous call? - await DecodeStreamAsync(await provider.Graph.Users[UserId].Photo.Content.Request().GetAsync()); + await DecodeStreamAsync(await provider.Graph().Users[UserId].Photo.Content.Request().GetAsync()); _photoId = UserId; } catch @@ -158,7 +158,7 @@ private async void LoadData() { try { - user = await provider.Graph.Me.Request().GetAsync(); + user = await provider.Graph().Me.Request().GetAsync(); } catch { @@ -166,7 +166,7 @@ private async void LoadData() try { - await DecodeStreamAsync(await provider.Graph.Me.Photo.Content.Request().GetAsync()); + await DecodeStreamAsync(await provider.Graph().Me.Photo.Content.Request().GetAsync()); _photoId = user.Id; } catch @@ -181,7 +181,7 @@ private async void LoadData() } else if (PersonDetails == null && !string.IsNullOrWhiteSpace(PersonQuery)) { - var people = await provider.Graph.FindPersonAsync(PersonQuery); + var people = await provider.Graph().FindPersonAsync(PersonQuery); if (people != null && people.Count > 0) { var person = people.FirstOrDefault(); @@ -196,7 +196,7 @@ private async Task LoadImageAsync(Person person) try { // TODO: Better guarding - var graph = ProviderManager.Instance.GlobalProvider.Graph; + var graph = ProviderManager.Instance.GlobalProvider.Graph(); if (!string.IsNullOrWhiteSpace(person.UserPrincipalName)) { diff --git a/Microsoft.Toolkit.Graph.Controls/Controls/PersonView/PersonView.xaml b/CommunityToolkit.Uwp.Graph.Controls/Controls/PersonView/PersonView.xaml similarity index 98% rename from Microsoft.Toolkit.Graph.Controls/Controls/PersonView/PersonView.xaml rename to CommunityToolkit.Uwp.Graph.Controls/Controls/PersonView/PersonView.xaml index c1caa12..260d054 100644 --- a/Microsoft.Toolkit.Graph.Controls/Controls/PersonView/PersonView.xaml +++ b/CommunityToolkit.Uwp.Graph.Controls/Controls/PersonView/PersonView.xaml @@ -1,7 +1,7 @@  + xmlns:local="using:CommunityToolkit.Uwp.Graph.Controls"> /// Converts a to a . diff --git a/Microsoft.Toolkit.Graph.Controls/Properties/AssemblyInfo.cs b/CommunityToolkit.Uwp.Graph.Controls/Properties/AssemblyInfo.cs similarity index 100% rename from Microsoft.Toolkit.Graph.Controls/Properties/AssemblyInfo.cs rename to CommunityToolkit.Uwp.Graph.Controls/Properties/AssemblyInfo.cs diff --git a/Microsoft.Toolkit.Graph.Controls/Properties/Microsoft.Toolkit.Graph.Controls.rd.xml b/CommunityToolkit.Uwp.Graph.Controls/Properties/CommunityToolkit.Uwp.Graph.Controls.rd.xml similarity index 71% rename from Microsoft.Toolkit.Graph.Controls/Properties/Microsoft.Toolkit.Graph.Controls.rd.xml rename to CommunityToolkit.Uwp.Graph.Controls/Properties/CommunityToolkit.Uwp.Graph.Controls.rd.xml index 24ec172..f4a5a7b 100644 --- a/Microsoft.Toolkit.Graph.Controls/Properties/Microsoft.Toolkit.Graph.Controls.rd.xml +++ b/CommunityToolkit.Uwp.Graph.Controls/Properties/CommunityToolkit.Uwp.Graph.Controls.rd.xml @@ -1,5 +1,5 @@ - + diff --git a/CommunityToolkit.Uwp.Graph.Controls/Themes/Generic.xaml b/CommunityToolkit.Uwp.Graph.Controls/Themes/Generic.xaml new file mode 100644 index 0000000..f0b4e04 --- /dev/null +++ b/CommunityToolkit.Uwp.Graph.Controls/Themes/Generic.xaml @@ -0,0 +1,8 @@ + + + + + + + \ No newline at end of file diff --git a/CommunityToolkit.Uwp.Graph.Controls/VisualStudioToolsManifest.xml b/CommunityToolkit.Uwp.Graph.Controls/VisualStudioToolsManifest.xml new file mode 100644 index 0000000..cd0f0c9 --- /dev/null +++ b/CommunityToolkit.Uwp.Graph.Controls/VisualStudioToolsManifest.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/Microsoft.Toolkit.Graph.Controls/Providers/CommonProviderBehaviorBase.Properties.cs b/Microsoft.Toolkit.Graph.Controls/Providers/CommonProviderBehaviorBase.Properties.cs deleted file mode 100644 index e957366..0000000 --- a/Microsoft.Toolkit.Graph.Controls/Providers/CommonProviderBehaviorBase.Properties.cs +++ /dev/null @@ -1,74 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -#if DOTNET -using System.Windows; -#else -using Windows.UI.Xaml; -#endif - -#if DOTNET -namespace Microsoft.Toolkit.Wpf.Graph.Providers -#else -namespace Microsoft.Toolkit.Graph.Providers -#endif -{ - /// - /// Properties for . - /// - public partial class CommonProviderBehaviorBase - { - /// - /// Gets or sets the Client ID (the unique application (client) ID assigned to your app by Azure AD when the app was registered). - /// - /// - /// For details about how to register an app and get a client ID, - /// see the Register an app quick start. - /// - public string ClientId - { - get { return (string)GetValue(ClientIdProperty); } - set { SetValue(ClientIdProperty, value); } - } - - /// - /// Identifies the dependency property. - /// - /// - /// The identifier for the dependency property. - /// - public static readonly DependencyProperty ClientIdProperty = - DependencyProperty.Register(nameof(ClientId), typeof(string), typeof(CommonProviderBehaviorBase), new PropertyMetadata(string.Empty)); - - /// - /// Gets or sets the redirect URI (the URI the identity provider will send the security tokens back to). - /// - public string RedirectUri - { - get { return (string)GetValue(RedirectUriProperty); } - set { SetValue(RedirectUriProperty, value); } - } - - /// - /// Identifies the dependency property. - /// - /// - /// The identifier for the dependency property. - /// - public static readonly DependencyProperty RedirectUriProperty = -#if DOTNET - DependencyProperty.Register(nameof(RedirectUri), typeof(string), typeof(CommonProviderBehaviorBase), new PropertyMetadata("http://localhost")); //// https://aka.ms/msal-net-os-browser -#else - DependencyProperty.Register(nameof(RedirectUri), typeof(string), typeof(CommonProviderBehaviorBase), new PropertyMetadata("https://login.microsoftonline.com/common/oauth2/nativeclient")); -#endif - - /// - /// Gets or sets the list of Scopes (permissions) to request on initial login. - /// - /// - /// This list can be modified by controls which require specific scopes to function. This will aid in requesting all scopes required by controls used before login is initiated, if using the LoginButton. - /// - public ScopeSet Scopes { get; set; } = new ScopeSet { "User.Read", "User.ReadBasic.All" }; - } -} diff --git a/Microsoft.Toolkit.Graph.Controls/Providers/CommonProviderBehaviorBase.cs b/Microsoft.Toolkit.Graph.Controls/Providers/CommonProviderBehaviorBase.cs deleted file mode 100644 index bf8bd86..0000000 --- a/Microsoft.Toolkit.Graph.Controls/Providers/CommonProviderBehaviorBase.cs +++ /dev/null @@ -1,25 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -#if DOTNET -using System.Windows; -using Microsoft.Toolkit.Wpf.UI.Behaviors; -#else -using Microsoft.Toolkit.Uwp.UI.Behaviors; -using Windows.UI.Xaml; -#endif - -#if DOTNET -namespace Microsoft.Toolkit.Wpf.Graph.Providers -#else -namespace Microsoft.Toolkit.Graph.Providers -#endif -{ - /// - /// Provides a common base class for UWP XAML based provider wrappers to the Microsoft.Graph.Auth SDK. - /// - public abstract partial class CommonProviderBehaviorBase : BehaviorBase - { - } -} diff --git a/Microsoft.Toolkit.Graph.Controls/Providers/InteractiveProviderBehavior.cs b/Microsoft.Toolkit.Graph.Controls/Providers/InteractiveProviderBehavior.cs deleted file mode 100644 index 99de148..0000000 --- a/Microsoft.Toolkit.Graph.Controls/Providers/InteractiveProviderBehavior.cs +++ /dev/null @@ -1,63 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -#if DOTNET -using System; -using System.Linq; -using System.Windows; -using Microsoft.Toolkit.Graph.Providers; -#else -using System.Linq; -using Windows.System; -#endif - -#if DOTNET -namespace Microsoft.Toolkit.Wpf.Graph.Providers -#else -namespace Microsoft.Toolkit.Graph.Providers -#endif -{ - /// - /// Put in a xaml page with ClientId - /// https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/Acquiring-tokens-interactively. - /// - /// - /// - /// <Interactivity:Interaction.Behaviors> - /// <providers:InteractiveProviderBehavior ClientId = "MyClientIdGuid"/> - /// </Interactivity:Interaction.Behaviors> - /// - /// - public class InteractiveProviderBehavior : CommonProviderBehaviorBase - { - private object lock_sync = new object(); - private bool initialized = false; - - /// - protected override bool Initialize() - { - lock (lock_sync) - { - if (!initialized) - { -#if DOTNET - _ = Dispatcher.BeginInvoke(new Action(async () => - { - ProviderManager.Instance.GlobalProvider = - await QuickCreate.CreateMsalProviderAsync(ClientId, RedirectUri, Scopes.ToArray()); - }), System.Windows.Threading.DispatcherPriority.Normal); -#else - _ = DispatcherQueue.GetForCurrentThread().TryEnqueue(DispatcherQueuePriority.Normal, async () => - { - ProviderManager.Instance.GlobalProvider = - await QuickCreate.CreateMsalProviderAsync(ClientId, RedirectUri, Scopes.ToArray()); - }); -#endif - } - } - - return base.Initialize(); - } - } -} diff --git a/Microsoft.Toolkit.Graph.Controls/Providers/MockProviderBehavior.cs b/Microsoft.Toolkit.Graph.Controls/Providers/MockProviderBehavior.cs deleted file mode 100644 index 03a7ba7..0000000 --- a/Microsoft.Toolkit.Graph.Controls/Providers/MockProviderBehavior.cs +++ /dev/null @@ -1,69 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -#if DOTNET -using System; -using System.Linq; -using System.Windows; -using Microsoft.Toolkit.Graph.Providers; -#else -using System.Linq; -using Windows.UI.Xaml; -#endif - -#if DOTNET -namespace Microsoft.Toolkit.Wpf.Graph.Providers -#else -namespace Microsoft.Toolkit.Graph.Providers -#endif -{ - /// - /// Put in a xaml page with ClientId - /// https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/Acquiring-tokens-interactively. - /// - /// - /// - /// <Interactivity:Interaction.Behaviors> - /// <providers:InteractiveProviderBehavior ClientId = "MyClientIdGuid"/> - /// </Interactivity:Interaction.Behaviors> - /// - /// - public class MockProviderBehavior : CommonProviderBehaviorBase - { - private object lock_sync = new object(); - private bool initialized = false; - - /// - /// Gets or sets a value indicating whether the mock provider is signed-in upon initialization. - /// - public bool SignedIn - { - get { return (bool)GetValue(SignedInProperty); } - set { SetValue(SignedInProperty, value); } - } - - /// - /// Identifies the dependency property. - /// - /// - /// The identifier for the dependency property. - /// - public static readonly DependencyProperty SignedInProperty = - DependencyProperty.Register(nameof(SignedIn), typeof(bool), typeof(MockProviderBehavior), new PropertyMetadata(true)); - - /// - protected override bool Initialize() - { - lock (lock_sync) - { - if (!initialized) - { - ProviderManager.Instance.GlobalProvider = new MockProvider(SignedIn); - } - } - - return base.Initialize(); - } - } -} diff --git a/Microsoft.Toolkit.Graph.Controls/Providers/QuickCreate.cs b/Microsoft.Toolkit.Graph.Controls/Providers/QuickCreate.cs deleted file mode 100644 index b9fdfe7..0000000 --- a/Microsoft.Toolkit.Graph.Controls/Providers/QuickCreate.cs +++ /dev/null @@ -1,56 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Reflection; -using System.Threading.Tasks; -using Microsoft.Graph.Auth; -using Microsoft.Identity.Client; -using Microsoft.Toolkit.Graph.Providers; - -#if DOTNET -namespace Microsoft.Toolkit.Wpf.Graph.Providers -#else -namespace Microsoft.Toolkit.Graph.Providers -#endif -{ - //// TODO: This should probably live in .NET Standard lib; however, Uno one needs to be at UI layer for Parent Window? - //// TODO: Need to set up XAML Islands sample to test in the new repo and make sure this works from this context. - - /// - /// Helper class to easily initialize provider from just ClientId. - /// - public static class QuickCreate - { - /// - /// Easily creates a from a ClientId. - /// - /// - /// - /// ProviderManager.Instance.GlobalProvider = await QuickCreate.CreateMsalProviderAsync("MyClientId"); - /// - /// - /// Registered ClientId. - /// RedirectUri for auth response. - /// List of Scopes to initially request. - /// New reference. - public static async Task CreateMsalProviderAsync(string clientid, string redirectUri = "https://login.microsoftonline.com/common/oauth2/nativeclient", string[] scopes = null) - { - var client = PublicClientApplicationBuilder.Create(clientid) - .WithAuthority(AzureCloudInstance.AzurePublic, AadAuthorityAudience.AzureAdAndPersonalMicrosoftAccount) - .WithRedirectUri(redirectUri) - .WithClientName(ProviderManager.ClientName) - .WithClientVersion(Assembly.GetExecutingAssembly().GetName().Version.ToString()) - .Build(); - - if (scopes == null) - { - scopes = new string[] { string.Empty }; - } - - var provider = new InteractiveAuthenticationProvider(client, scopes); - - return await MsalProvider.CreateAsync(client, provider); - } - } -} diff --git a/Microsoft.Toolkit.Graph.Controls/Providers/ScopeSet.cs b/Microsoft.Toolkit.Graph.Controls/Providers/ScopeSet.cs deleted file mode 100644 index 28ba91d..0000000 --- a/Microsoft.Toolkit.Graph.Controls/Providers/ScopeSet.cs +++ /dev/null @@ -1,90 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -#if DOTNET -using Microsoft.Xaml.Behaviors; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.ComponentModel; -using System.Linq; -#else -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -#endif - -#if DOTNET -namespace Microsoft.Toolkit.Wpf.Graph.Providers -#else -namespace Microsoft.Toolkit.Graph.Providers -#endif -{ - /// - /// Helper Class for XAML string Scope conversion. - /// -#if DOTNET - [TypeConverter(typeof(ScopeSetTypeConverter))] -#else - [Windows.Foundation.Metadata.CreateFromString(MethodName = "Microsoft.Toolkit.Graph.Providers.ScopeSet.ConvertToScopeArray")] -#endif - public class ScopeSet : Collection - { - /// - /// Empty ScopeSet helper. - /// - public static readonly ScopeSet Empty = new ScopeSet(new string[] { }); - - /// - /// Helper to convert a string of scopes to a list of strings. - /// - /// Comma separated scope list. - /// New List of strings, i.e. ScopeSet. - public static ScopeSet ConvertToScopeArray(string rawString) - { - if (rawString != null) - { - return new ScopeSet(rawString.Split(",")); - } - - return ScopeSet.Empty; - } - - /// - /// Initializes a new instance of the class. - /// - /// Array to copy as ScopeSet. - public ScopeSet(string[] arr) - { - this.AddRange(arr); - } - - /// - /// Initializes a new instance of the class. - /// - /// List to copy as ScopeSet. - public ScopeSet(List list) - { - this.AddRange(list.ToArray()); - } - - /// - /// Initializes a new instance of the class. - /// - public ScopeSet() - { - } - - /// - /// Adds range of items to the scope set. - /// - /// Items to add. - public void AddRange(string[] items) - { - foreach (var item in items) - { - this.Add(item); - } - } - } -} diff --git a/Microsoft.Toolkit.Graph.Controls/Themes/Generic.xaml b/Microsoft.Toolkit.Graph.Controls/Themes/Generic.xaml deleted file mode 100644 index 6cf76ca..0000000 --- a/Microsoft.Toolkit.Graph.Controls/Themes/Generic.xaml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/Microsoft.Toolkit.Graph.Controls/VisualStudioToolsManifest.xml b/Microsoft.Toolkit.Graph.Controls/VisualStudioToolsManifest.xml deleted file mode 100644 index 71baf39..0000000 --- a/Microsoft.Toolkit.Graph.Controls/VisualStudioToolsManifest.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/Microsoft.Toolkit.Graph/Microsoft.Toolkit.Graph.csproj b/Microsoft.Toolkit.Graph/Microsoft.Toolkit.Graph.csproj deleted file mode 100644 index b29ab1d..0000000 --- a/Microsoft.Toolkit.Graph/Microsoft.Toolkit.Graph.csproj +++ /dev/null @@ -1,28 +0,0 @@ - - - - uap10.0.17763;netstandard2.0 - Windows Community Toolkit .NET Standard Graph Services - - This package includes .NET Standard code helpers such as: - - GraphExtensions: Helpers for common tasks related to the Microsoft Graph used by the Microsoft.Toolkit.Graph.Controls. - - GlobalProvider: A shared provider instance used by the Microsoft.Toolkit.Graph.Controls. - - UWP Graph Toolkit Windows Provider extensions helpers - - - Full - Debug;Release;CI - AnyCPU;ARM;ARM64;x64;x86 - - - - $(DefineConstants);WINRT - - - - - - - - diff --git a/Microsoft.Toolkit.Graph/Providers/MockProvider.cs b/Microsoft.Toolkit.Graph/Providers/MockProvider.cs deleted file mode 100644 index a0d54ec..0000000 --- a/Microsoft.Toolkit.Graph/Providers/MockProvider.cs +++ /dev/null @@ -1,102 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; -using System.Web; -using Microsoft.Graph; -using Microsoft.Toolkit.Graph.Extensions; - -namespace Microsoft.Toolkit.Graph.Providers -{ - /// - /// Provider to connect to the example data set for Microsoft Graph. Useful for prototyping and samples. - /// - public class MockProvider : IProvider - { - private const string GRAPH_PROXY_URL = "https://proxy.apisandbox.msdn.microsoft.com/svc?url="; - - private ProviderState _state = ProviderState.Loading; - - /// - public ProviderState State - { - get - { - return _state; - } - - private set - { - var current = _state; - _state = value; - - StateChanged?.Invoke(this, new StateChangedEventArgs(current, _state)); - } - } - - /// - public GraphServiceClient Graph => new GraphServiceClient( - new DelegateAuthenticationProvider((requestMessage) => - { - var requestUri = requestMessage.RequestUri.ToString(); - - // Prepend Proxy Service URI to our request - requestMessage.RequestUri = new Uri(GRAPH_PROXY_URL + Uri.EscapeDataString(requestUri)); - - return this.AuthenticateRequestAsync(requestMessage); - })); - - /// - public event EventHandler StateChanged; - - /// - /// Initializes a new instance of the class. - /// - /// Whether the default state should be signedIn, defaults to true. - public MockProvider(bool signedIn = true) - { - if (signedIn) - { - State = ProviderState.SignedIn; - } - else - { - State = ProviderState.SignedOut; - } - } - - /// - public Task AuthenticateRequestAsync(HttpRequestMessage request) - { - request.AddSdkVersion(); - - request.AddMockProviderToken(); - - return Task.FromResult(0); - } - - /// - public async Task LoginAsync() - { - State = ProviderState.Loading; - await Task.Delay(3000); - State = ProviderState.SignedIn; - } - - /// - public async Task LogoutAsync() - { - State = ProviderState.Loading; - await Task.Delay(3000); - State = ProviderState.SignedOut; - } - } -} diff --git a/Microsoft.Toolkit.Wpf.Graph.Providers/AssemblyInfo.cs b/Microsoft.Toolkit.Wpf.Graph.Providers/AssemblyInfo.cs deleted file mode 100644 index a06202d..0000000 --- a/Microsoft.Toolkit.Wpf.Graph.Providers/AssemblyInfo.cs +++ /dev/null @@ -1,14 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Windows; - -[assembly: ThemeInfo( - ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located - //(used if a resource is not found in the page, - // or application resource dictionaries) - ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located - //(used if a resource is not found in the page, - // app, or any theme specific resource dictionaries) -)] diff --git a/Microsoft.Toolkit.Wpf.Graph.Providers/Behaviors/BehaviorBase.cs b/Microsoft.Toolkit.Wpf.Graph.Providers/Behaviors/BehaviorBase.cs deleted file mode 100644 index 800df3c..0000000 --- a/Microsoft.Toolkit.Wpf.Graph.Providers/Behaviors/BehaviorBase.cs +++ /dev/null @@ -1,169 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Windows; -using Microsoft.Xaml.Behaviors; - -namespace Microsoft.Toolkit.Wpf.UI.Behaviors -{ - /// - /// INTERNAL DO NOT USE, please thumb-up this comment https://github.com/windows-toolkit/Microsoft.Toolkit.Win32/issues/93#issuecomment-545091683 if you think this would be useful to you. - /// Base class for behaviors that solves 2 problems: - /// 1. Prevent duplicate initialization that can happen (prevent multiple OnAttached calls); - /// 2. Whenever initially fails, this method will subscribe to to allow lazy initialization. - /// - /// The type of the associated object. - /// - /// - /// For more info, see https://github.com/windows-toolkit/WindowsCommunityToolkit/issues/1008. - /// - public abstract class BehaviorBase : Behavior - where T : UIElement - { - private bool _isAttaching; - private bool _isAttached; - - /// - /// Gets a value indicating whether this behavior is attached. - /// - /// - /// true if this behavior is attached; otherwise, false. - /// - protected bool IsAttached - { - get { return _isAttached; } - } - - /// - /// Called after the behavior is attached to the . - /// - /// - /// Override this to hook up functionality to the - /// - protected override void OnAttached() - { - base.OnAttached(); - - HandleAttach(); - - var frameworkElement = AssociatedObject as FrameworkElement; - if (frameworkElement != null) - { - frameworkElement.Loaded += OnAssociatedObjectLoaded; - frameworkElement.Unloaded += OnAssociatedObjectUnloaded; - frameworkElement.SizeChanged += OnAssociatedObjectSizeChanged; - } - } - - /// - /// Called when the behavior is being detached from its . - /// - /// - /// Override this to unhook functionality from the - /// - protected override void OnDetaching() - { - base.OnDetaching(); - - var frameworkElement = AssociatedObject as FrameworkElement; - if (frameworkElement != null) - { - frameworkElement.Loaded -= OnAssociatedObjectLoaded; - frameworkElement.Unloaded -= OnAssociatedObjectUnloaded; - frameworkElement.SizeChanged -= OnAssociatedObjectSizeChanged; - } - - HandleDetach(); - } - - /// - /// Called when the associated object has been loaded. - /// - protected virtual void OnAssociatedObjectLoaded() - { - } - - /// - /// Called when the associated object has been unloaded. - /// - protected virtual void OnAssociatedObjectUnloaded() - { - } - - /// - /// Initializes the behavior to the associated object. - /// - /// true if the initialization succeeded; otherwise false. - protected virtual bool Initialize() - { - return true; - } - - /// - /// Uninitializes the behavior from the associated object. - /// - /// true if uninitialization succeeded; otherwise false. - protected virtual bool Uninitialize() - { - return true; - } - - private void OnAssociatedObjectLoaded(object sender, RoutedEventArgs e) - { - if (!_isAttached) - { - HandleAttach(); - } - - OnAssociatedObjectLoaded(); - } - - private void OnAssociatedObjectUnloaded(object sender, RoutedEventArgs e) - { - OnAssociatedObjectUnloaded(); - - // Note: don't detach here, we'll let the behavior implementation take care of that - } - - private void OnAssociatedObjectSizeChanged(object sender, SizeChangedEventArgs e) - { - if (!_isAttached) - { - HandleAttach(); - } - } - - private void HandleAttach() - { - if (_isAttaching || _isAttached) - { - return; - } - - _isAttaching = true; - - var attached = Initialize(); - if (attached) - { - _isAttached = true; - } - - _isAttaching = false; - } - - private void HandleDetach() - { - if (!_isAttached) - { - return; - } - - var detached = Uninitialize(); - if (detached) - { - _isAttached = false; - } - } - } -} \ No newline at end of file diff --git a/Microsoft.Toolkit.Wpf.Graph.Providers/Microsoft.Toolkit.Wpf.Graph.Providers.csproj b/Microsoft.Toolkit.Wpf.Graph.Providers/Microsoft.Toolkit.Wpf.Graph.Providers.csproj deleted file mode 100644 index ae43cf0..0000000 --- a/Microsoft.Toolkit.Wpf.Graph.Providers/Microsoft.Toolkit.Wpf.Graph.Providers.csproj +++ /dev/null @@ -1,30 +0,0 @@ - - - - netcoreapp3.1 - true - AnyCPU;x86;x64 - Debug;Release;CI - - - - DOTNET - - - - - - - - - - - - - - - - - - - diff --git a/Microsoft.Toolkit.Wpf.Graph.Providers/Providers/ScopeSetTypeConverter.cs b/Microsoft.Toolkit.Wpf.Graph.Providers/Providers/ScopeSetTypeConverter.cs deleted file mode 100644 index 2ce2951..0000000 --- a/Microsoft.Toolkit.Wpf.Graph.Providers/Providers/ScopeSetTypeConverter.cs +++ /dev/null @@ -1,45 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System; -using System.ComponentModel; -using System.Globalization; - -namespace Microsoft.Toolkit.Wpf.Graph.Providers -{ - /// - /// - /// - public class ScopeSetTypeConverter : TypeConverter - { - /// - public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) - { - return sourceType == typeof(string); - } - - /// - public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) - { - return ScopeSet.ConvertToScopeArray(value as string); - } - - /// - public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) - { - return destinationType == typeof(string[]); - } - - /// - public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) - { - if (value is ScopeSet set) - { - return string.Join(",", set); - } - - return null; - } - } -} \ No newline at end of file diff --git a/SampleTest/App.xaml b/SampleTest/App.xaml index ea9689d..13f775b 100644 --- a/SampleTest/App.xaml +++ b/SampleTest/App.xaml @@ -1,4 +1,4 @@  + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" /> + diff --git a/SampleTest/App.xaml.cs b/SampleTest/App.xaml.cs index cf46cb5..17faf49 100644 --- a/SampleTest/App.xaml.cs +++ b/SampleTest/App.xaml.cs @@ -2,21 +2,13 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using CommunityToolkit.Net.Authentication; using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; +using System.Threading.Tasks; using Windows.ApplicationModel; using Windows.ApplicationModel.Activation; -using Windows.Foundation; -using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Controls.Primitives; -using Windows.UI.Xaml.Data; -using Windows.UI.Xaml.Input; -using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; namespace SampleTest @@ -36,6 +28,45 @@ public App() this.Suspending += OnSuspending; } + + // Which provider should be used for authentication? + private readonly ProviderType _providerType = ProviderType.Mock; + + // List of available authentication providers. + private enum ProviderType + { + Mock, + Msal + } + + /// + /// Initialize the global authentication provider. + /// + private void InitializeGlobalProvider() + { + if (ProviderManager.Instance.GlobalProvider != null) + { + return; + } + + // Provider config + string clientId = "YOUR_CLIENT_ID_HERE"; + string[] scopes = { "User.Read", "User.ReadBasic.All", "People.Read", "Calendars.Read", "Mail.Read", "Group.Read.All", "ChannelMessage.Read.All" }; + + switch(_providerType) + { + // Mock provider + case ProviderType.Mock: + ProviderManager.Instance.GlobalProvider = new MockProvider(signedIn: true); + break; + + //Msal provider + case ProviderType.Msal: + ProviderManager.Instance.GlobalProvider = new MsalProvider(clientId, scopes); + break; + } + } + /// /// Invoked when the application is launched normally by the end user. Other entry points /// will be used such as when the application is launched to open a specific file. @@ -43,6 +74,8 @@ public App() /// Details about the launch request and process. protected override void OnLaunched(LaunchActivatedEventArgs e) { + Task.Run(InitializeGlobalProvider); + Frame rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content, diff --git a/SampleTest/MainPage.xaml b/SampleTest/MainPage.xaml index 272dfa1..d7757de 100644 --- a/SampleTest/MainPage.xaml +++ b/SampleTest/MainPage.xaml @@ -1,31 +1,16 @@  - - - - - - +