From 0c352a313ab0d2912a23171c9dca20fa1e884a44 Mon Sep 17 00:00:00 2001 From: Gautam Sheth Date: Thu, 28 Nov 2024 20:49:57 +0200 Subject: [PATCH] Enhance device login functionality by adding persist login and host parameters, and implement caching logic for improved connection management. (#4589) Co-authored-by: Gautam Sheth --- src/Commands/Base/ConnectOnline.cs | 7 +++++-- src/Commands/Base/PnPConnection.cs | 24 ++++++++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/Commands/Base/ConnectOnline.cs b/src/Commands/Base/ConnectOnline.cs index a06b22ccb..c70e1d958 100644 --- a/src/Commands/Base/ConnectOnline.cs +++ b/src/Commands/Base/ConnectOnline.cs @@ -267,7 +267,6 @@ public class ConnectOnline : BasePSCmdlet [Parameter(Mandatory = true, ParameterSetName = ParameterSet_OSLOGIN)] public SwitchParameter OSLogin; - [Parameter(Mandatory = false, ParameterSetName = ParameterSet_INTERACTIVE)] [Parameter(Mandatory = false, ParameterSetName = ParameterSet_DEVICELOGIN)] [Parameter(Mandatory = false, ParameterSetName = ParameterSet_OSLOGIN)] @@ -556,7 +555,7 @@ private PnPConnection ConnectDeviceLogin() } } - var returnedConnection = PnPConnection.CreateWithDeviceLogin(clientId, Url, Tenant, messageWriter, AzureEnvironment, cancellationTokenSource); + var returnedConnection = PnPConnection.CreateWithDeviceLogin(clientId, Url, Tenant, messageWriter, AzureEnvironment, cancellationTokenSource, PersistLogin, Host); connection = returnedConnection; messageWriter.Finished = true; } @@ -725,6 +724,8 @@ private PnPConnection ConnectCredentials(PSCredential credentials, Initializatio credentials, CurrentCredentials, TenantAdminUrl, + PersistLogin, + Host, AzureEnvironment, ClientId, RedirectUri, TransformationOnPrem, initializationType); @@ -864,6 +865,8 @@ private PnPConnection ConnectEnvironmentVariable(InitializationType initializati credentials, CurrentCredentials, TenantAdminUrl, + PersistLogin, + Host, AzureEnvironment, azureClientId, RedirectUri, TransformationOnPrem, initializationType); diff --git a/src/Commands/Base/PnPConnection.cs b/src/Commands/Base/PnPConnection.cs index 943c6d0d4..5bd5b7209 100644 --- a/src/Commands/Base/PnPConnection.cs +++ b/src/Commands/Base/PnPConnection.cs @@ -258,8 +258,16 @@ internal static PnPConnection CreateWithACSAppOnly(Uri url, string realm, string return spoConnection; } - internal static PnPConnection CreateWithDeviceLogin(string clientId, string url, string tenantId, CmdletMessageWriter messageWriter, AzureEnvironment azureEnvironment, CancellationTokenSource cancellationTokenSource) + internal static PnPConnection CreateWithDeviceLogin(string clientId, string url, string tenantId, CmdletMessageWriter messageWriter, AzureEnvironment azureEnvironment, CancellationTokenSource cancellationTokenSource, bool persistLogin, System.Management.Automation.Host.PSHost host) { + if (persistLogin) + { + EnableCaching(url, clientId); + } + if (CacheEnabled(url, clientId)) + { + WriteCacheEnabledMessage(host); + } var connectionUri = new Uri(url); var scopes = new[] { $"{connectionUri.Scheme}://{connectionUri.Authority}//.default" }; // the second double slash is not a typo. Framework.AuthenticationManager authManager = null; @@ -444,8 +452,16 @@ internal static PnPConnection CreateWithManagedIdentity(Cmdlet cmdlet, string ur return connection; } - internal static PnPConnection CreateWithCredentials(Cmdlet cmdlet, Uri url, PSCredential credentials, bool currentCredentials, string tenantAdminUrl, AzureEnvironment azureEnvironment = AzureEnvironment.Production, string clientId = null, string redirectUrl = null, bool onPrem = false, InitializationType initializationType = InitializationType.Credentials) + internal static PnPConnection CreateWithCredentials(Cmdlet cmdlet, Uri url, PSCredential credentials, bool currentCredentials, string tenantAdminUrl, bool persistLogin, System.Management.Automation.Host.PSHost host, AzureEnvironment azureEnvironment = AzureEnvironment.Production, string clientId = null, string redirectUrl = null, bool onPrem = false, InitializationType initializationType = InitializationType.Credentials) { + if (persistLogin) + { + EnableCaching(url.ToString(), clientId); + } + if (CacheEnabled(url.ToString(), clientId)) + { + WriteCacheEnabledMessage(host); + } var context = new PnPClientContext(url.AbsoluteUri) { ApplicationName = Resources.ApplicationName, @@ -1059,10 +1075,10 @@ private static List GetCheckUrls(string url) private static void EnableCaching(string url, string clientid) { - bool folderExists = Directory.Exists(Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell")); + bool folderExists = System.IO.Directory.Exists(Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell")); if (!folderExists) { - Directory.CreateDirectory(Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell")); + System.IO.Directory.CreateDirectory(Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell")); } var configFile = Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell", "cachesettings.json");