From 9fc2a992a04596562178dce14e6d01e1b32b374b Mon Sep 17 00:00:00 2001 From: Richasy Date: Tue, 18 Apr 2023 05:52:38 +0800 Subject: [PATCH] Support for injecting WebTokenRequest.Properties (#207) * Add V2 model for Uwp authorization * Make v2 model only for MSA * Replace with new properties * Update comment * Remove null check --- .../WebAccountProviderConfig.cs | 14 ++++++++++++++ .../WindowsProvider.cs | 16 +++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CommunityToolkit.Authentication.Uwp/WebAccountProviderConfig.cs b/CommunityToolkit.Authentication.Uwp/WebAccountProviderConfig.cs index 9747d97..8acb939 100644 --- a/CommunityToolkit.Authentication.Uwp/WebAccountProviderConfig.cs +++ b/CommunityToolkit.Authentication.Uwp/WebAccountProviderConfig.cs @@ -2,6 +2,8 @@ // 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.Collections.Generic; + namespace CommunityToolkit.Authentication { /// @@ -19,6 +21,16 @@ namespace CommunityToolkit.Authentication /// public WebAccountProviderType WebAccountProviderType { get; set; } + /// + /// Gets or sets the properties that need to be added when constructing (for MSA). + /// + public IDictionary MSATokenRequestProperties { get; set; } + + /// + /// Gets or sets the properties that need to be added when constructing (for AAD). + /// + public IDictionary AADTokenRequestProperties { get; set; } + /// /// Initializes a new instance of the struct. /// @@ -28,6 +40,8 @@ namespace CommunityToolkit.Authentication { WebAccountProviderType = webAccountProviderType; ClientId = clientId; + MSATokenRequestProperties = new Dictionary(); + AADTokenRequestProperties = new Dictionary(); } } } diff --git a/CommunityToolkit.Authentication.Uwp/WindowsProvider.cs b/CommunityToolkit.Authentication.Uwp/WindowsProvider.cs index 234521f..ed2d89b 100644 --- a/CommunityToolkit.Authentication.Uwp/WindowsProvider.cs +++ b/CommunityToolkit.Authentication.Uwp/WindowsProvider.cs @@ -33,7 +33,7 @@ namespace CommunityToolkit.Authentication private const string SettingsKeyProviderId = "WindowsProvider_ProviderId"; private const string SettingsKeyProviderAuthority = "WindowsProvider_Authority"; - private static readonly SemaphoreSlim SemaphoreSlim = new (1); + private static readonly SemaphoreSlim SemaphoreSlim = new(1); // Default/minimal scopes for authentication, if none are provided. private static readonly string[] DefaultScopes = { "User.Read" }; @@ -550,6 +550,20 @@ namespace CommunityToolkit.Authentication : new WebTokenRequest(provider, scopesString, clientId); webTokenRequest.Properties.Add(GraphResourcePropertyKey, GraphResourcePropertyValue); + if (provider.Authority == MicrosoftAccountAuthority) + { + foreach (var property in _webAccountProviderConfig.MSATokenRequestProperties) + { + webTokenRequest.Properties.Add(property); + } + } + else if (provider.Authority == AadAuthority) + { + foreach (var property in _webAccountProviderConfig.AADTokenRequestProperties) + { + webTokenRequest.Properties.Add(property); + } + } return webTokenRequest; }