Skip to content

Commit

Permalink
Add directory creation for MSAL token cache if it doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Gautam Sheth committed Nov 28, 2024
1 parent 2151841 commit f2c74e4
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/Commands/Base/PnPConnection.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Microsoft.Identity.Client;
using Microsoft.Identity.Client.Extensions.Msal;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.TenantCdn;
using Microsoft.VisualBasic;
using PnP.Core.Services;
using PnP.Framework;
using PnP.Framework.Utilities.Context;
Expand All @@ -11,21 +9,16 @@
using PnP.PowerShell.Commands.Model;
using PnP.PowerShell.Commands.Utilities;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Language;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -1066,12 +1059,19 @@ private static List<string> GetCheckUrls(string url)

private static void EnableCaching(string url, string clientid)
{
bool folderExists = Directory.Exists(Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell"));
if (!folderExists)
{
Directory.CreateDirectory(Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell"));
}

var configFile = Path.Combine(MsalCacheHelper.UserRootDirectory, ".m365pnppowershell", "cachesettings.json");
var configs = new List<TokenCacheConfiguration>();
if (System.IO.File.Exists(configFile))
{
configs = JsonSerializer.Deserialize<List<TokenCacheConfiguration>>(System.IO.File.ReadAllText(configFile));
}

var urls = GetCheckUrls(url);
var entry = configs.FirstOrDefault(c => urls.Contains(c.Url) && c.ClientId == clientid);
if (entry != null)
Expand All @@ -1090,7 +1090,6 @@ private static void EnableCaching(string url, string clientid)
private static void WriteCacheEnabledMessage(PSHost host)
{
host.UI.WriteLine(ConsoleColor.Yellow, ConsoleColor.Black, "Secure token cache used for authentication. Clear the cache entry with Disconnect-PnPOnline -ClearPersistedLogin.");

}

internal static void ClearCache(PnPConnection connection)
Expand Down

0 comments on commit f2c74e4

Please sign in to comment.