diff --git a/src/lib/PnP.Framework/AuthenticationManager.cs b/src/lib/PnP.Framework/AuthenticationManager.cs index 4790c7021..b24ac14c2 100644 --- a/src/lib/PnP.Framework/AuthenticationManager.cs +++ b/src/lib/PnP.Framework/AuthenticationManager.cs @@ -187,6 +187,22 @@ public static AuthenticationManager CreateWithDeviceLogin(string clientId, strin public static AuthenticationManager CreateWithInteractiveLogin(string clientId, Action openBrowserCallback, string tenantId = null, string successMessageHtml = null, string failureMessageHtml = null, AzureEnvironment azureEnvironment = AzureEnvironment.Production, Action tokenCacheCallback = null, bool useWAM = false) { return new AuthenticationManager(clientId, Utilities.OAuth.DefaultBrowserUi.FindFreeLocalhostRedirectUri(), tenantId, azureEnvironment, tokenCacheCallback, new Utilities.OAuth.DefaultBrowserUi(openBrowserCallback, successMessageHtml, failureMessageHtml), useWAM); + } + + /// + /// Creates a new instance of the Authentication Manager to acquire access tokens and client contexts using the Azure AD Interactive flow. + /// + /// The client id of the Azure AD application to use for authentication + /// This callback will be called providing the URL and port to open during the authentication flow + /// Optional tenant id or tenant url + /// Allows you to override the success message. You will have to provide the full HTML document. + /// llows you to override the failure message. You will have to provide the full HTML document. + /// The azure environment to use. Defaults to AzureEnvironment.Production + /// If present, after setting up the base flow for authentication this callback will be called to register a custom tokencache. See https://aka.ms/msal-net-token-cache-serialization. + /// If true, uses WAM for authentication. Works only on Windows OS. Default is false + public static AuthenticationManager CreateWithInteractiveWebBrowserLogin(string clientId, Action openBrowserCallback, string tenantId = null, string successFullMessageHtml = null, string failureFullMessageHtml = null, AzureEnvironment azureEnvironment = AzureEnvironment.Production, Action tokenCacheCallback = null, bool useWAM = false) + { + return new AuthenticationManager(clientId, Utilities.OAuth.DefaultBrowserUi.FindFreeLocalhostRedirectUri(), tenantId, azureEnvironment, tokenCacheCallback, new Utilities.OAuth.DefaultBrowserUi(openBrowserCallback, successFullMessageHtml, failureFullMessageHtml, true), useWAM); } /// @@ -771,8 +787,8 @@ public string GetAccessToken(string siteUrl, CancellationToken cancellationToken /// /// Optional cancellation token to cancel the request /// The prompt style to use. Notice that this only works with the Interactive Login flow, for all other flows this parameter is ignored. - /// Optional app name to show when using on MacOS - /// Optional url of app to show when using on MacOS + /// Optional app name to show when using on MacOS + /// Optional url of app to show when using on MacOS /// public string GetAccessToken(string siteUrl, CancellationToken cancellationToken, Prompt prompt = default, string appName = "PnP", string appUrl = "https://pnp.github.io") { @@ -818,8 +834,8 @@ public async Task GetAccessTokenAsync(string[] scopes, Prompt prompt = d /// Optional cancellation token to cancel the request /// The prompt style to use. Notice that this only works with the Interactive Login flow, for all other flows this parameter is ignored. /// for ClientContextType.PnPCoreSdk case as by interface definition needed for GetAccessTokenAsync - /// Optional app name to show when using on MacOS - /// Optional url of app to show when using on MacOS + /// Optional app name to show when using on MacOS + /// Optional url of app to show when using on MacOS /// public async Task GetAccessTokenAsync(string[] scopes, CancellationToken cancellationToken, Prompt prompt = default, Uri uri = null, string appName = "PnP", string appUrl = "https://pnp.github.io") { @@ -856,7 +872,9 @@ public async Task GetAccessTokenAsync(string[] scopes, CancellationToken catch { var builder = publicClientApplication.AcquireTokenInteractive(scopes); - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + + // On MacOS we always use the browser login + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { var options = new SystemWebViewOptions() { @@ -979,8 +997,8 @@ public ClientContext GetContext(string siteUrl) /// /// /// Optional cancellation token to cancel the request - /// Optional app name to show when using on MacOS - /// Optional url of app to show when using on MacOS + /// Optional app name to show when using on MacOS + /// Optional url of app to show when using on MacOS /// public ClientContext GetContext(string siteUrl, CancellationToken cancellationToken, string appName = "PnP", string appUrl = "https://pnp.github.io") { @@ -1002,8 +1020,8 @@ public async Task GetContextAsync(string siteUrl) /// /// /// Optional cancellation token to cancel the request - /// Optional app name to show when using on MacOS or Linux - /// Optional url of app to show when using on MacOS or Linux + /// Optional app name to show when using on MacOS + /// Optional url of app to show when using on MacOS /// public async Task GetContextAsync(string siteUrl, CancellationToken cancellationToken, string appName = "PnP", string appUrl = "https://pnp.github.io") { diff --git a/src/lib/PnP.Framework/Utilities/OAuth/DefaultBrowserUi.cs b/src/lib/PnP.Framework/Utilities/OAuth/DefaultBrowserUi.cs index 84077b678..0feca7bce 100644 --- a/src/lib/PnP.Framework/Utilities/OAuth/DefaultBrowserUi.cs +++ b/src/lib/PnP.Framework/Utilities/OAuth/DefaultBrowserUi.cs @@ -18,11 +18,13 @@ internal class DefaultBrowserUi : ICustomWebUi private Action _openBrowserAction = null; private string _successMessageHtml = string.Empty; private string _failureMessageHtml = string.Empty; - public DefaultBrowserUi(Action openBrowserAction, string successMessageHtml, string failureMessageHtml) + private bool _fullHtml = false; + public DefaultBrowserUi(Action openBrowserAction, string successMessageHtml, string failureMessageHtml, bool fullHtml = false) { _openBrowserAction = openBrowserAction; _successMessageHtml = successMessageHtml; _failureMessageHtml = failureMessageHtml; + _fullHtml = fullHtml; } private const string SuccessMessageHtml = "You successfully authenticated. Feel free to close this browser/tab."; @@ -107,20 +109,34 @@ private string GetMessageToShowInBrowserAfterAuth(Uri uri) #endif if (!string.IsNullOrEmpty(errorString)) { + if (!_fullHtml) + { #if !NETFRAMEWORK - string errorDescription = authCodeQueryKeyValue.Get("error_description"); + string errorDescription = authCodeQueryKeyValue.Get("error_description"); #else string errorDescription = dicQueryString.ContainsKey("error_description") ? dicQueryString["error_description"] : null; #endif - return string.Format( - CultureInfo.InvariantCulture, - CloseWindowFailureHtml, - errorString, - errorDescription, - string.IsNullOrEmpty(_failureMessageHtml) ? FailureMessageHtml : _failureMessageHtml); + return string.Format( + CultureInfo.InvariantCulture, + CloseWindowFailureHtml, + errorString, + errorDescription, + string.IsNullOrEmpty(_failureMessageHtml) ? FailureMessageHtml : _failureMessageHtml); + } + else + { + return string.Format(_failureMessageHtml, errorString); + } } - return string.Format(CloseWindowSuccessHtml, string.IsNullOrEmpty(_successMessageHtml) ? SuccessMessageHtml : _successMessageHtml); + if (!_fullHtml) + { + return string.Format(CloseWindowSuccessHtml, string.IsNullOrEmpty(_successMessageHtml) ? SuccessMessageHtml : _successMessageHtml); + } + else + { + return _successMessageHtml; + } } } }