Skip to content

Commit

Permalink
Enhance device login functionality by adding persist login and host p…
Browse files Browse the repository at this point in the history
…arameters, and implement caching logic for improved connection management. (pnp#4589)

Co-authored-by: Gautam Sheth <gautam.sheth@staffbase.com>
  • Loading branch information
gautamdsheth and Gautam Sheth authored Nov 28, 2024
1 parent faadb88 commit 0c352a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/Commands/Base/ConnectOnline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -725,6 +724,8 @@ private PnPConnection ConnectCredentials(PSCredential credentials, Initializatio
credentials,
CurrentCredentials,
TenantAdminUrl,
PersistLogin,
Host,
AzureEnvironment,
ClientId,
RedirectUri, TransformationOnPrem, initializationType);
Expand Down Expand Up @@ -864,6 +865,8 @@ private PnPConnection ConnectEnvironmentVariable(InitializationType initializati
credentials,
CurrentCredentials,
TenantAdminUrl,
PersistLogin,
Host,
AzureEnvironment,
azureClientId,
RedirectUri, TransformationOnPrem, initializationType);
Expand Down
24 changes: 20 additions & 4 deletions src/Commands/Base/PnPConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1059,10 +1075,10 @@ private static List<string> 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");
Expand Down

0 comments on commit 0c352a3

Please sign in to comment.